forked from joyent/libuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oio.h
269 lines (227 loc) · 8 KB
/
oio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef OIO_H
#define OIO_H
#define OIO_VERSION_MAJOR 0
#define OIO_VERSION_MINOR 1
#include <stdint.h> /* int64_t */
#include <sys/types.h> /* size_t */
typedef struct oio_err_s oio_err_t;
typedef struct oio_handle_s oio_handle_t;
typedef struct oio_req_s oio_req_t;
#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__)
# include "oio-unix.h"
#else
# include "oio-win.h"
#endif
/* The status parameter is 0 if the request completed successfully,
* and should be -1 if the request was cancelled or failed.
* For oio_close_cb, -1 means that the handle was closed due to an error.
* Error details can be obtained by calling oio_last_error().
*
* In the case of oio_read_cb the oio_buf returned should be freed by the
* user.
*/
typedef oio_buf (*oio_alloc_cb)(oio_handle_t* handle, size_t suggested_size);
typedef void (*oio_read_cb)(oio_handle_t *handle, int nread, oio_buf buf);
typedef void (*oio_write_cb)(oio_req_t* req, int status);
typedef void (*oio_connect_cb)(oio_req_t* req, int status);
typedef void (*oio_shutdown_cb)(oio_req_t* req, int status);
typedef void (*oio_accept_cb)(oio_handle_t* handle);
typedef void (*oio_close_cb)(oio_handle_t* handle, int status);
typedef void (*oio_timer_cb)(oio_req_t* req, int64_t skew, int status);
/* TODO: do loop_cb and async_cb really need a status argument? */
typedef void (*oio_loop_cb)(oio_handle_t* handle, int status);
typedef void (*oio_async_cb)(oio_handle_t* handle, int stats);
/* Expand this list if necessary. */
typedef enum {
OIO_UNKNOWN = -1,
OIO_OK = 0,
OIO_EOF,
OIO_EACCESS,
OIO_EAGAIN,
OIO_EADDRINUSE,
OIO_EADDRNOTAVAIL,
OIO_EAFNOSUPPORT,
OIO_EALREADY,
OIO_EBADF,
OIO_EBUSY,
OIO_ECONNABORTED,
OIO_ECONNREFUSED,
OIO_ECONNRESET,
OIO_EDESTADDRREQ,
OIO_EFAULT,
OIO_EHOSTUNREACH,
OIO_EINTR,
OIO_EINVAL,
OIO_EISCONN,
OIO_EMFILE,
OIO_ENETDOWN,
OIO_ENETUNREACH,
OIO_ENFILE,
OIO_ENOBUFS,
OIO_ENOMEM,
OIO_ENONET,
OIO_ENOPROTOOPT,
OIO_ENOTCONN,
OIO_ENOTSOCK,
OIO_ENOTSUP,
OIO_EPROTO,
OIO_EPROTONOSUPPORT,
OIO_EPROTOTYPE,
OIO_ETIMEDOUT
} oio_err_code;
typedef enum {
OIO_UNKNOWN_HANDLE = 0,
OIO_TCP,
OIO_NAMED_PIPE,
OIO_TTY,
OIO_FILE,
OIO_PREPARE,
OIO_CHECK,
OIO_IDLE,
OIO_ASYNC
} oio_handle_type;
typedef enum {
OIO_UNKNOWN_REQ = 0,
OIO_CONNECT,
OIO_ACCEPT,
OIO_READ,
OIO_WRITE,
OIO_SHUTDOWN,
OIO_TIMEOUT,
OIO_WAKEUP
} oio_req_type;
struct oio_err_s {
/* read-only */
oio_err_code code;
/* private */
int sys_errno_;
};
struct oio_req_s {
/* read-only */
oio_req_type type;
/* public */
oio_handle_t* handle;
void* cb;
void* data;
/* private */
oio_req_private_fields
};
struct oio_handle_s {
/* read-only */
oio_handle_type type;
/* public */
oio_close_cb close_cb;
void* data;
/* private */
oio_handle_private_fields
};
/* Most functions return boolean: 0 for success and -1 for failure.
* On error the user should then call oio_last_error() to determine
* the error code.
*/
oio_err_t oio_last_error();
char* oio_strerror(oio_err_t err);
void oio_init(oio_alloc_cb alloc);
int oio_run();
/* Manually modify the event loop's reference count. Useful if the user wants
* to have a handle or timeout that doesn't keep the loop alive.
*/
void oio_ref();
void oio_unref();
void oio_update_time();
int64_t oio_now();
void oio_req_init(oio_req_t* req, oio_handle_t* handle, void* cb);
/*
* TODO:
* - oio_(pipe|pipe_tty)_handle_init
* - oio_bind_pipe(char* name)
* - oio_continuous_read(oio_handle_t* handle, oio_continuous_read_cb* cb)
* - A way to list cancelled oio_reqs after before/on oio_close_cb
*/
/* TCP socket methods.
* Handle and callback bust be set by calling oio_req_init.
*/
int oio_tcp_init(oio_handle_t* handle, oio_close_cb close_cb, void* data);
int oio_bind(oio_handle_t* handle, struct sockaddr* addr);
int oio_connect(oio_req_t* req, struct sockaddr* addr);
int oio_shutdown(oio_req_t* req);
/* TCP server methods. */
int oio_listen(oio_handle_t* handle, int backlog, oio_accept_cb cb);
/* Call this after accept_cb. client does not need to be initialized. */
int oio_accept(oio_handle_t* server, oio_handle_t* client,
oio_close_cb close_cb, void* data);
/* Read data from an incoming stream. The callback will be made several
* several times until there is no more data to read or oio_read_stop is
* called. When we've reached EOF nread will be set to -1 and the error is
* set to OIO_EOF. When nread == -1 the buf parameter might not point to a
* valid buffer; in that case buf.len and buf.base are both set to 0.
* Note that nread might also be 0, which does *not* indicate an error or
* eof; it happens when liboio requested a buffer through the alloc callback
* but then decided that it didn't need that buffer.
*/
int oio_read_start(oio_handle_t* handle, oio_read_cb cb);
int oio_read_stop(oio_handle_t* handle);
int oio_write(oio_req_t* req, oio_buf bufs[], int bufcnt);
/* Timer methods */
int oio_timeout(oio_req_t* req, int64_t timeout);
/* libev wrapper. Every active prepare handle gets its callback called
* exactly once per loop iteration, just before the system blocks to wait
* for completed i/o.
*/
int oio_prepare_init(oio_handle_t* handle, oio_close_cb close_cb, void* data);
int oio_prepare_start(oio_handle_t* handle, oio_loop_cb cb);
int oio_prepare_stop(oio_handle_t* handle);
/* libev wrapper. Every active check handle gets its callback called exactly
* once per loop iteration, just after the system returns from blocking.
*/
int oio_check_init(oio_handle_t* handle, oio_close_cb close_cb, void* data);
int oio_check_start(oio_handle_t* handle, oio_loop_cb cb);
int oio_check_stop(oio_handle_t* handle);
/* libev wrapper. Every active idle handle gets its callback called repeatedly until it is
* stopped. This happens after all other types of callbacks are processed.
* When there are multiple "idle" handles active, their callbacks are called
* in turn.
*/
int oio_idle_init(oio_handle_t* handle, oio_close_cb close_cb, void* data);
int oio_idle_start(oio_handle_t* handle, oio_loop_cb cb);
int oio_idle_stop(oio_handle_t* handle);
/* libev wrapper. oio_async_send wakes up the event loop and calls the async
* handle's callback There is no guarantee that every oio_async_send call
* leads to exactly one invocation of the callback; The only guarantee is
* that the callback function is called at least once after the call to
* async_send. Unlike everything else, oio_async_send can be called from
* another thread.
*
* QUESTION(ryan) Can OIO_ASYNC just use oio_loop_cb? Same signature on my
* side.
*/
int oio_async_init(oio_handle_t* handle, oio_async_cb async_cb,
oio_close_cb close_cb, void* data);
int oio_async_send(oio_handle_t* handle);
/* Request handle to be closed. close_cb will be called
* asynchronously after this call.
*/
int oio_close(oio_handle_t* handle);
/* Utility */
struct sockaddr_in oio_ip4_addr(char* ip, int port);
#endif /* OIO_H */