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