bio
 
Loading...
Searching...
No Matches
buffering.h
1#ifndef BIO_BUFFERING_H
2#define BIO_BUFFERING_H
3
4#include "bio.h"
5#include "file.h"
6#include "net.h"
7
23typedef union {
25 void* ptr;
27
29typedef size_t (*bio_read_fn_t)(
31 void* buf,
32 size_t size,
33 bio_error_t* error
34);
35
37typedef size_t (*bio_write_fn_t)(
39 const void* buf,
40 size_t size,
41 bio_error_t* error
42);
43
45typedef bool (*bio_flush_fn_t)(
47 bio_error_t* error
48);
49
63
65typedef struct {
66 bio_handle_t handle;
68
80
86void
88
92
102bio_make_file_write_buffer(bio_file_t file, size_t size, bool fflush);
103
107
111
113size_t
115 bio_io_buffer_t buffer,
116 void* in,
117 size_t size,
118 bio_error_t* error
119);
120
122size_t
124 bio_io_buffer_t buffer,
125 const void* out,
126 size_t size,
127 bio_error_t* error
128);
129
131bool
133
135static inline size_t
137 bio_io_buffer_t buffer,
138 void* in,
139 size_t size,
140 bio_error_t* error
141) {
142 size_t total_bytes_read = 0;
143 while (total_bytes_read < size) {
144 size_t bytes_read = bio_buffered_read(
145 buffer,
146 (char*)in + total_bytes_read,
147 size - total_bytes_read,
148 error
149 );
150 if (bytes_read == 0) { break; }
151 total_bytes_read += bytes_read;
152 }
153
154 return total_bytes_read;
155}
156
158static inline size_t
160 bio_io_buffer_t buffer,
161 const void* out,
162 size_t size,
163 bio_error_t* error
164) {
165 size_t total_bytes_written = 0;
166 while (total_bytes_written < size) {
167 size_t bytes_written = bio_buffered_write(
168 buffer,
169 (const char*)out + total_bytes_written,
170 size - total_bytes_written,
171 error
172 );
173 if (bytes_written == 0) { break; }
174 total_bytes_written += bytes_written;
175 }
176
177 return total_bytes_written;
178}
179
182#endif
bool(* bio_flush_fn_t)(bio_io_buffer_ctx_t ctx, bio_error_t *error)
Flush callback.
Definition buffering.h:45
size_t bio_buffered_read(bio_io_buffer_t buffer, void *in, size_t size, bio_error_t *error)
Read from a buffer.
bio_io_buffer_t bio_make_buffer(const bio_buffer_options_t *options)
Create a new I/O buffer.
static size_t bio_buffered_write_exactly(bio_io_buffer_t buffer, const void *out, size_t size, bio_error_t *error)
Helper to deal with short write.
Definition buffering.h:159
bio_io_buffer_t bio_make_socket_write_buffer(bio_socket_t socket, size_t size)
Create a write buffer for a socket.
bio_io_buffer_t bio_make_file_read_buffer(bio_file_t file, size_t size)
Create a read buffer for a file.
size_t(* bio_read_fn_t)(bio_io_buffer_ctx_t ctx, void *buf, size_t size, bio_error_t *error)
Read callback.
Definition buffering.h:29
static size_t bio_buffered_read_exactly(bio_io_buffer_t buffer, void *in, size_t size, bio_error_t *error)
Helper to deal with short read.
Definition buffering.h:136
void bio_destroy_buffer(bio_io_buffer_t buffer)
Destroy a buffer.
size_t(* bio_write_fn_t)(bio_io_buffer_ctx_t ctx, const void *buf, size_t size, bio_error_t *error)
Write callback.
Definition buffering.h:37
size_t bio_buffered_write(bio_io_buffer_t buffer, const void *out, size_t size, bio_error_t *error)
Write to a buffer.
bool bio_flush_buffer(bio_io_buffer_t buffer, bio_error_t *error)
Flush all bufferred content.
bio_io_buffer_t bio_make_socket_read_buffer(bio_socket_t socket, size_t size)
Create a read buffer for a socket.
bio_io_buffer_t bio_make_file_write_buffer(bio_file_t file, size_t size, bool fflush)
Create a write buffer for a file.
Options for a buffer.
Definition buffering.h:51
size_t size
Buffer size in bytes.
Definition buffering.h:61
bio_io_buffer_ctx_t ctx
Context for functions.
Definition buffering.h:53
bio_flush_fn_t flush_fn
Flush callback.
Definition buffering.h:59
bio_write_fn_t write_fn
Write callback.
Definition buffering.h:57
bio_read_fn_t read_fn
Read callback.
Definition buffering.h:55
An error returned from a function.
Definition bio.h:468
Handle to a file.
Definition file.h:15
Definition bio.h:126
Handle to a buffer.
Definition buffering.h:65
Handle to a socket.
Definition net.h:15
Context for a buffer function.
Definition buffering.h:23
void * ptr
A pointer.
Definition buffering.h:25
bio_handle_t handle
A Handle.
Definition buffering.h:24