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