diff options
Diffstat (limited to 'src/ot_batch.h')
| -rw-r--r-- | src/ot_batch.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/ot_batch.h b/src/ot_batch.h new file mode 100644 index 0000000..910bea7 --- /dev/null +++ b/src/ot_batch.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | #ifndef OT_BATCH_H__ | ||
| 5 | #define OT_BATCH_H__ | ||
| 6 | |||
| 7 | #include <stddef.h> | ||
| 8 | #include <sys/types.h> | ||
| 9 | #include <sys/uio.h> | ||
| 10 | |||
| 11 | /* A queue of buffers on their way to one socket. The batch owns every buffer | ||
| 12 | handed to it and frees each one as soon as it has left, so a client that | ||
| 13 | reads slowly costs memory only for what it has not received yet. | ||
| 14 | |||
| 15 | A zeroed ot_batch is ready to use. */ | ||
| 16 | typedef struct { | ||
| 17 | struct iovec *entries; | ||
| 18 | size_t space; | ||
| 19 | size_t count; | ||
| 20 | size_t first; /* entries before this one are sent and freed */ | ||
| 21 | size_t offset; /* bytes of entries[first] already sent */ | ||
| 22 | } ot_batch; | ||
| 23 | |||
| 24 | static inline int batch_pending(const ot_batch *batch) { return batch->first != batch->count; } | ||
| 25 | |||
| 26 | /* What batch_send reports instead of a byte count */ | ||
| 27 | enum { BATCH_DONE = 0, BATCH_AGAIN = -1, BATCH_FAILED = -2 }; | ||
| 28 | |||
| 29 | /* Queue buf and take ownership of it. Returns 0, or -1 having freed buf. */ | ||
| 30 | int batch_add(ot_batch *batch, void *buf, size_t len); | ||
| 31 | |||
| 32 | /* Write as much as the socket takes. Returns the number of bytes written, or | ||
| 33 | BATCH_DONE, BATCH_AGAIN or BATCH_FAILED. */ | ||
| 34 | ssize_t batch_send(int s, ot_batch *batch); | ||
| 35 | |||
| 36 | /* Queue every entry of a vector, taking ownership of each buffer but not of | ||
| 37 | the vector itself. Returns 0, or -1 having freed all of them. */ | ||
| 38 | int batch_add_vector(ot_batch *batch, const struct iovec *entries, size_t count); | ||
| 39 | |||
| 40 | /* Queue a copy of buf, which the batch then owns. Returns 0, or -1. */ | ||
| 41 | int batch_add_copy(ot_batch *batch, const void *buf, size_t len); | ||
| 42 | |||
| 43 | /* Free whatever has not left yet, along with the batch's own array */ | ||
| 44 | void batch_reset(ot_batch *batch); | ||
| 45 | |||
| 46 | #endif | ||
