The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / liboi / oi.pod @ 90fc8d36

History | View | Annotate | Download (9.12 KB)

1 40c0f755 Ryan
=head1 NAME
2
3
liboi - a C library for doing evented I/O.
4
5
=head1 SYNOPSIS
6
7
    #include <oi.h>
8
9
=head1 DESCRIPTION
10
11
liboi is an object oriented library for doing evented socket and file I/O.
12
The API is mostly about registering callbacks to be executed on certain
13
events.
14
15
Because most systems do not support asynchornous file I/O, the behavior is
16
emulated with an internal thread pool. The thread pool is accessed with the
17
C<oi_async> and C<oi_task> objects. Typically one will not need to use these
18
directly as C<oi_file> wraps that functionality. 
19
20
=head2 CONVENTIONS
21
22
liboi's goal is to be very simple layer above the POSIX API. To that end it
23
avoids internal allocations as much as possible. Unless otherwise noted you
24
should assume all pointers passed into liboi will remain your responsibility
25
to maintain.  That means you should not free the data passed into liboi
26
until the object in question has completed.
27
28
C<oi_socket> and C<oi_file> objects must be attached to an event loop. This
29
is completed with the C<*_attach> and C<*_detach>  methods. When an object
30
is detached, other methods can be called - just the loop will not churn out
31
callbacks.
32
33
Both C<oi_socket> and C<oi_file> contain a number of callback pointers.
34
These are to be set manually after calling their initalization functions.
35
All classes include a C<void *data> member which is left for you to use.
36
37
=head1 ERROR HANDLING
38
39
40
=head1 Sockets
41
42
The C<oi_socket> structure represents a socket.
43
The callbacks inside C<oi_socket> are 
44
    void (*on_connect)   (oi_socket *);
45
    void (*on_read)      (oi_socket *, const void *buf, size_t count);
46
    void (*on_drain)     (oi_socket *);
47
    void (*on_error)     (oi_socket *, struct oi_error e);
48
    void (*on_close)     (oi_socket *);
49
    void (*on_timeout)   (oi_socket *);
50
51
A the memory for a socket is released when the C<on_close()> callback is
52
made. That is, the user may free the memory for the socket with-in the
53
C<on_close()> callback.
54
55
=over 4
56
57
=item void oi_socket_init (oi_socket *, float timeout);
58
59
Initialize a socket. C<timeout> is the number of seconds of inactivity that
60
is allowed before the socket closes. The timeout only starts once the
61
socket is attached to a loop and open.
62
63
A C<timeout> argument of 0.0 signals that no timeout should be used.
64
65
After calling this function, register your callbacks manually. Thus your
66
code will probably look like this 
67
    oi_socket socket;
68
    oi_socket_init(&socket, 60.0);
69
    socket.on_connect = my_on_connect;
70
    socket.on_read = my_on_read;
71
    /* etc */
72
73
74
=item int oi_socket_connect (oi_socket *, struct addrinfo *addrinfo);
75
76
Open a client connect to the specified address. When the connection is made
77
C<socket.on_connect> will be called. 
78
79
Here is an example of filling in C<addrinfo> for a local TCP connection on
80
port 5555:
81
    struct addrinfo *servinfo;
82
    struct addrinfo hints;
83
    memset(&hints, 0, sizeof hints);
84
    hints.ai_family = AF_UNSPEC;
85
    hints.ai_socktype = SOCK_STREAM;
86
    hints.ai_flags = AI_PASSIVE;
87
    r = getaddrinfo(NULL, "5555", &hints, &servinfo);
88
    assert(r == 0);
89
    oi_socket_connect(socket, servinfo);
90
91
=item void oi_socket_attach (oi_socket *, struct ev_loop *loop);
92
93
A socket must be attached to a loop in order before any callbacks will be
94
made. 
95
96
=item void oi_socket_detach (oi_socket *);
97
98
Detaching a socket will not close the connection.
99
100
=item void oi_socket_read_start (oi_socket *);
101
102
This will make the socket start receiving data. When data is received the
103
C<socket.on_read> callback is made. The maximum amount of data that can be
104
receive at a time is controlled by C<socket.chunksize>. 
105
106
The buffer returned by C<socket.on_read> is statically allocated exists only
107
for the length of the callback. That means if you need to save any of the
108
data coming down the line, you must copy it to a new buffer. 
109
110
Ideally you will have a parser attached to the C<on_read> callback which can
111
be interrupted at any time. 
112
113
C<socket.chunksize> can be changed at any time.
114
115
=item void oi_socket_read_stop (oi_socket *);
116
117
Stops receiving data. You may receive spurious C<on_read> attempts even
118
though the socket reading is stopped - be prepared to handle them. 
119
120
=item void oi_socket_reset_timeout (oi_socket *);
121
122
Reset the timeout to allow the socket to exist for another few seconds
123
(however long you specified in the initialization function).  
124
125
=item void oi_socket_write (oi_socket *socket, oi_buf *buf);
126
127
Write the I<buf> to the socket. Each socket has a queue of C<oi_buf> objects
128
to be written - this appends the specified buffer to the end of that queue.
129
You will be notified when the queue is empty with the C<socket.on_drain()>
130
callback. When the socket has written the buffer C<buf.release()> will be
131
called. The release callback does not imply that the buffer was successfully
132
written.
133
134
=item void oi_socket_write_simple (oi_socket *, const char *str, size_t len);
135
136
Sometimes you are just hacking around and need to quickly write a string to
137
the socket. This convenience function allocates an C<oi_buf> object, and
138
C<strdup>s  the given string. The allocated buffer will be freed by liboi
139
internally. 
140
141
Most production most applications will use their own memory pool and will
142
not need this function. 
143
144
=item void oi_socket_write_eof (oi_socket *);
145
146
This closes the write end of the socket. Further writes are not allowed
147
after this. 
148
149
=item void oi_socket_close (oi_socket *);
150
151
Attempts to close the socket. 
152
153
If the socket is secure, an SSL bye message will be sent. 
154
SSL recommends that you wait for a bye response from the peer however this
155
tends to be overkill for most people. By default liboi will not wait for
156
peer to send a matching bye message. If you require this then set
157
C<socket.wait_for_secure_hangup> to 1.
158
159
When the close is complete C<on_close()> is made. The C<on_close()>
160
callback is not made until the program returns to the event loop. This is
161
because C<on_close()> may free the socket memory and if C<on_close()> was
162
called from C<oi_socket_close()>, then the socket object might unexpectedly
163
be gone. To summarize: C<oi_socket_close()> does not call C<on_close()> and
164
the socket memory is still accessable immediately after making calling
165
C<oi_socket_close()>. 
166
167
=item void oi_socket_set_secure_session (oi_socket *, gnutls_session_t);
168
169
This make a socket use SSL. You must create the GnuTLS session yourself and
170
assign its credentials.
171
172
=back
173
174
=head1 Servers
175
176
A server simply listens on an address for new connections. The connections
177
come in the form of C<oi_socket> objects. The key is to give a
178
C<server.on_connection()> callback which returns an initialized C<oi_socket>.
179
The callback looks like this 
180
181
    oi_socket* (*on_connection) (oi_server *, struct sockaddr *remote_addr, socklen_t remove_addr_len);
182
183
Returning NULL from C<on_connection()> will reject the connection.
184
185
=over 4
186
187
=item void oi_server_init (oi_server *, int backlog);
188
189
Initializes a server object. C<backlog> is the argument given
190
internally to C<listen()>. Set the C<server.on_connection()> callback
191
after calling this.
192
193
=item int oi_server_listen (oi_server *, struct addrinfo *addrinfo);
194
195
Listens on the specified address. The server will not accept connections
196
until it is attached to a loop, however.
197
198
=item void oi_server_attach (oi_server *, struct ev_loop *loop);
199
200
Attaches a server to a loop. 
201
202
=item void oi_server_detach (oi_server *);
203
204
Detaches a server to a loop. Does not close the server.
205
206
=item void oi_server_close (oi_server *); 
207
208
Stops the server from listening. 
209
210
=back
211
212
=head1 Files
213
214
Files internally use a thread pool to operate without blocking.
215
The thread pool is started once a file is attached and it continues until
216
program termination. 
217
218
The following callbacks are used inside of the file object
219
    void (*on_open)  (oi_file *);
220
    void (*on_read)  (oi_file *, size_t count);
221
    void (*on_drain) (oi_file *);
222
    void (*on_error) (oi_file *, struct oi_error);
223
    void (*on_close) (oi_file *);
224
225
=over 4
226
227
=item int oi_file_init (oi_file *);
228
229
Initializes a file object.
230
231
=item void oi_file_attach (oi_file *, struct ev_loop *);
232
233
Attaches a file object to a loop. If the thread pool has not been started,
234
then it is started at this call. 
235
236
=item void oi_file_detach (oi_file *);
237
238
Detaches a file object from the loop.
239
240
=item int  oi_file_open_path (oi_file *, const char *path, int flags, mode_t mode);
241
242
Opens a file specified by the path. The C<flag> and C<mode> arguments are
243
the same as used by L<open.2>. The C<file.on_open> callback is triggered
244
when the file is opened. Returns 0 on success. Returns -1 if the given file
245
is already open.
246
247
WARNING: path argument must be valid until C<oi_file> object is closed and
248
the C<file.on_close()> callback is made. I.E., liboi does not strdup the path
249
pointer. 
250
251
=item int oi_file_open_stdin (oi_file *);
252
253
=item int oi_file_open_stdout (oi_file *);
254
255
=item int oi_file_open_stderr (oi_file *);
256
257
=item void oi_file_read_start (oi_file *, void *buffer, size_t bufsize);
258
259
260
261
=item void oi_file_read_stop (oi_file *);
262
263
=item int oi_file_write (oi_file *, oi_buf *to_write);
264
265
=item int oi_file_write_simple (oi_file *, const char *, size_t);
266
267
=item int oi_file_send (oi_file *source, oi_socket *destination, off_t offset, size_t length);
268
269
=item void oi_file_close (oi_file *);
270
271
=over 4
272
273
=back
274
275
=head1 AUTHOR
276
277
Ryan Dahl <ry@tinyclouds.org>