also link statically
[libjh.git] / bufio.h
1 #ifndef _BUFIO_HEADER
2 #define _BUFIO_HEADER
3
4 /* BUFFER CHAIN */
5 typedef struct bufio_chain_entry bufio_chain_entry;
6
7 struct bufio_chain_entry {
8   bufio_chain_entry *next;
9   void *buf;
10   size_t len;
11   off_t used;
12 };
13
14 typedef struct {
15   bufio_chain_entry *head, *tail;
16 } bufio_chain;
17
18 /**
19  * Enqueues the given buffer for writing. After writing, it will be freed.
20  * Returns -1 if calloc failed.
21  */
22 int bufio_chain_append(bufio_chain *bc, void *buf, size_t len);
23
24 /**
25  * Flush as much data as possible into the given FD.
26  * Returns -1 for errors, 0 for ok. errno is left
27  * intact on errors, so you might want to check for
28  * EAGAIN or so.
29  */
30 int bufio_chain_flush(bufio_chain *bc, int fd);
31
32 /**
33  * Clear the buffer chain without sending any data.
34  */
35 void bufio_chain_clear(bufio_chain *bc);
36
37
38
39 /* CONNECTION */
40 typedef struct bufio_connection bufio_connection;
41
42 typedef void (*bufio_connection_error_cb_t)(bufio_connection *con);
43 typedef void (*bufio_connection_data_cb_t)(bufio_connection *con);
44
45 struct bufio_connection {
46   ev_io rw; /* readonly */
47   ev_io ww; /* readonly */
48   struct ev_loop *loop; /* readonly */
49   void *inbuf; /* readonly */
50   size_t inbuf_size; /* readonly */
51   size_t inbuf_used; /* readonly */
52   bufio_chain outbuf; /* readonly */
53
54   /**
55    * Called when an error has occured. You should react to it by destroying
56    * the connection. This property can be changed manually at any time.
57    */
58   bufio_connection_error_cb_t err_cb;
59
60   /**
61    * Called when your read buffer has been filled completely. When this is
62    * called, nothing will be read anymore until you set a new read buffer.
63    * This property can be changed manually at any time.
64    */
65   bufio_connection_data_cb_t data_cb;
66
67   /**
68    * Not touched by this library.
69    */
70   void *data;
71 };
72
73 /**
74  * Creates a bufio connection around an existing file descriptor.
75  * This call already activates the watcher without initializing some
76  * important stuff, so set it before returning to the eventloop/entering
77  * it:
78  *
79  * - the input buffer – set it using `bufio_connection_set_read_buffer`
80  * - the callbacks (`err_cb` and `data_cb`) – set them directly
81  */
82 bufio_connection *bufio_connection_create(struct ev_loop *loop, int fd);
83
84 void bufio_connection_set_read_buffer(bufio_connection *con, void *buf, size_t size);
85
86 /**
87  * This stops our watcher and clears and frees the output buffers and the
88  * connection struct, but it doesn't touch the input buffer or the fd.
89  */
90 void bufio_connection_destroy(bufio_connection *con);
91
92 /**
93  * This enqueues a chunk of data to be written. You are not allowed to
94  * touch the buffer anymore after calling this function!
95  */
96 int bufio_connection_write(bufio_connection *con, void *buf, size_t len);
97
98 #endif