blob: 7166219043cc9e982a366e9178eb48f0e6cbb193 [file] [log] [blame]
Benny Prijono4bac2c12008-05-11 18:12:16 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono4bac2c12008-05-11 18:12:16 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_ASYNCSOCK_H__
20#define __PJ_ASYNCSOCK_H__
21
22/**
23 * @file activesock.h
24 * @brief Active socket
25 */
26
27#include <pj/ioqueue.h>
28#include <pj/sock.h>
29
30
31PJ_BEGIN_DECL
32
33/**
34 * @defgroup PJ_ACTIVESOCK Active socket I/O
35 * @brief Active socket performs active operations on socket.
36 * @ingroup PJ_IO
37 * @{
38 *
39 * Active socket is a higher level abstraction to the ioqueue. It provides
40 * automation to socket operations which otherwise would have to be done
41 * manually by the applications. For example with socket recv(), recvfrom(),
42 * and accept() operations, application only needs to invoke these
43 * operation once, and it will be notified whenever data or incoming TCP
44 * connection (in the case of accept()) arrives.
45 */
46
47/**
48 * This opaque structure describes the active socket.
49 */
50typedef struct pj_activesock_t pj_activesock_t;
51
52/**
53 * This structure contains the callbacks to be called by the active socket.
54 */
55typedef struct pj_activesock_cb
56{
57 /**
58 * This callback is called when a data arrives as the result of
59 * pj_activesock_start_read().
60 *
61 * @param asock The active socket.
62 * @param data The buffer containing the new data, if any. If
63 * the status argument is non-PJ_SUCCESS, this
64 * argument may be NULL.
65 * @param size The length of data in the buffer.
66 * @param status The status of the read operation. This may contain
67 * non-PJ_SUCCESS for example when the TCP connection
68 * has been closed. In this case, the buffer may
69 * contain left over data from previous callback which
70 * the application may want to process.
71 * @param remainder If application wishes to leave some data in the
72 * buffer (common for TCP applications), it should
73 * move the remainder data to the front part of the
74 * buffer and set the remainder length here. The value
75 * of this parameter will be ignored for datagram
76 * sockets.
77 *
78 * @return PJ_TRUE if further read is desired, and PJ_FALSE
79 * when application no longer wants to receive data.
80 * Application may destroy the active socket in the
81 * callback and return PJ_FALSE here.
82 */
83 pj_bool_t (*on_data_read)(pj_activesock_t *asock,
84 void *data,
85 pj_size_t size,
86 pj_status_t status,
87 pj_size_t *remainder);
88 /**
89 * This callback is called when a packet arrives as the result of
90 * pj_activesock_start_recvfrom().
91 *
92 * @param asock The active socket.
93 * @param data The buffer containing the packet, if any. If
94 * the status argument is non-PJ_SUCCESS, this
95 * argument will be set to NULL.
96 * @param size The length of packet in the buffer. If
97 * the status argument is non-PJ_SUCCESS, this
98 * argument will be set to zero.
99 * @param src_addr Source address of the packet.
100 * @param addr_len Length of the source address.
101 * @param status This contains
102 *
103 * @return PJ_TRUE if further read is desired, and PJ_FALSE
104 * when application no longer wants to receive data.
105 * Application may destroy the active socket in the
106 * callback and return PJ_FALSE here.
107 */
108 pj_bool_t (*on_data_recvfrom)(pj_activesock_t *asock,
109 void *data,
110 pj_size_t size,
111 const pj_sockaddr_t *src_addr,
112 int addr_len,
113 pj_status_t status);
114
115 /**
116 * This callback is called when data has been sent.
117 *
118 * @param asock The active socket.
119 * @param send_key Key associated with the send operation.
120 * @param sent If value is positive non-zero it indicates the
121 * number of data sent. When the value is negative,
122 * it contains the error code which can be retrieved
123 * by negating the value (i.e. status=-sent).
124 *
125 * @return Application may destroy the active socket in the
126 * callback and return PJ_FALSE here.
127 */
128 pj_bool_t (*on_data_sent)(pj_activesock_t *asock,
129 pj_ioqueue_op_key_t *send_key,
130 pj_ssize_t sent);
131
132 /**
133 * This callback is called when new connection arrives as the result
134 * of pj_activesock_start_accept().
135 *
136 * @param asock The active socket.
137 * @param newsock The new incoming socket.
138 * @param src_addr The source address of the connection.
139 * @param addr_len Length of the source address.
140 *
141 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
142 * when application no longer wants to accept incoming
143 * connection. Application may destroy the active socket
144 * in the callback and return PJ_FALSE here.
145 */
146 pj_bool_t (*on_accept_complete)(pj_activesock_t *asock,
147 pj_sock_t newsock,
148 const pj_sockaddr_t *src_addr,
149 int src_addr_len);
150
151 /**
152 * This callback is called when pending connect operation has been
153 * completed.
154 *
155 * @param asock The active socket.
156 * @param status The connection result. If connection has been
157 * successfully established, the status will contain
158 * PJ_SUCCESS.
159 *
160 * @return Application may destroy the active socket in the
161 * callback and return PJ_FALSE here.
162 */
163 pj_bool_t (*on_connect_complete)(pj_activesock_t *asock,
164 pj_status_t status);
165
166} pj_activesock_cb;
167
168
169/**
170 * Settings that can be given during active socket creation. Application
171 * must initialize this structure with #pj_activesock_cfg_default().
172 */
173typedef struct pj_activesock_cfg
174{
175 /**
176 * Number of concurrent asynchronous operations that is to be supported
177 * by the active socket. This value only affects socket receive and
178 * accept operations -- the active socket will issue one or more
179 * asynchronous read and accept operations based on the value of this
180 * field. Setting this field to more than one will allow more than one
181 * incoming data or incoming connections to be processed simultaneously
182 * on multiprocessor systems, when the ioqueue is polled by more than
183 * one threads.
184 *
185 * The default value is 1.
186 */
187 unsigned async_cnt;
188
189 /**
190 * The ioqueue concurrency to be forced on the socket when it is
191 * registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
192 * info about ioqueue concurrency.
193 *
194 * When this value is -1, the concurrency setting will not be forced for
195 * this socket, and the socket will inherit the concurrency setting of
196 * the ioqueue. When this value is zero, the active socket will disable
197 * concurrency for the socket. When this value is +1, the active socket
198 * will enable concurrency for the socket.
199 *
200 * The default value is -1.
201 */
202 int concurrency;
203
Benny Prijono417d6052008-07-29 20:15:15 +0000204 /**
205 * If this option is specified, the active socket will make sure that
206 * asynchronous send operation with stream oriented socket will only
207 * call the callback after all data has been sent. This means that the
208 * active socket will automatically resend the remaining data until
209 * all data has been sent.
210 *
211 * Please note that when this option is specified, it is possible that
212 * error is reported after partial data has been sent. Also setting
213 * this will disable the ioqueue concurrency for the socket.
214 *
215 * Default value is 1.
216 */
217 pj_bool_t whole_data;
218
Benny Prijono4bac2c12008-05-11 18:12:16 +0000219} pj_activesock_cfg;
220
221
222/**
223 * Initialize the active socket configuration with the default values.
224 *
225 * @param cfg The configuration to be initialized.
226 */
227PJ_DECL(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg);
228
229
230/**
231 * Create the active socket for the specified socket. This will register
232 * the socket to the specified ioqueue.
233 *
234 * @param pool Pool to allocate memory from.
235 * @param sock The socket handle.
236 * @param sock_type Specify socket type, either pj_SOCK_DGRAM() or
237 * pj_SOCK_STREAM(). The active socket needs this
238 * information to handle connection closure for
239 * connection oriented sockets.
240 * @param ioqueue The ioqueue to use.
241 * @param opt Optional settings. When this setting is not specifed,
242 * the default values will be used.
243 * @param cb Pointer to structure containing application
244 * callbacks.
Benny Prijonoea8e4362008-06-06 14:12:23 +0000245 * @param user_data Arbitrary user data to be associated with this
246 * active socket.
Benny Prijono4bac2c12008-05-11 18:12:16 +0000247 * @param p_asock Pointer to receive the active socket instance.
248 *
249 * @return PJ_SUCCESS if the operation has been successful,
250 * or the appropriate error code on failure.
251 */
252PJ_DECL(pj_status_t) pj_activesock_create(pj_pool_t *pool,
253 pj_sock_t sock,
254 int sock_type,
255 const pj_activesock_cfg *opt,
256 pj_ioqueue_t *ioqueue,
257 const pj_activesock_cb *cb,
Benny Prijonoea8e4362008-06-06 14:12:23 +0000258 void *user_data,
Benny Prijono4bac2c12008-05-11 18:12:16 +0000259 pj_activesock_t **p_asock);
260
261/**
262 * Create UDP socket descriptor, bind it to the specified address, and
263 * create the active socket for the socket descriptor.
264 *
265 * @param pool Pool to allocate memory from.
266 * @param addr Specifies the address family of the socket and the
267 * address where the socket should be bound to. If
268 * this argument is NULL, then AF_INET is assumed and
269 * the socket will be bound to any addresses and port.
270 * @param opt Optional settings. When this setting is not specifed,
271 * the default values will be used.
272 * @param cb Pointer to structure containing application
273 * callbacks.
Benny Prijonoea8e4362008-06-06 14:12:23 +0000274 * @param user_data Arbitrary user data to be associated with this
275 * active socket.
Benny Prijono4bac2c12008-05-11 18:12:16 +0000276 * @param p_asock Pointer to receive the active socket instance.
277 * @param bound_addr If this argument is specified, it will be filled with
278 * the bound address on return.
279 *
280 * @return PJ_SUCCESS if the operation has been successful,
281 * or the appropriate error code on failure.
282 */
283PJ_DECL(pj_status_t) pj_activesock_create_udp(pj_pool_t *pool,
284 const pj_sockaddr *addr,
285 const pj_activesock_cfg *opt,
286 pj_ioqueue_t *ioqueue,
287 const pj_activesock_cb *cb,
Benny Prijonoea8e4362008-06-06 14:12:23 +0000288 void *user_data,
Benny Prijono4bac2c12008-05-11 18:12:16 +0000289 pj_activesock_t **p_asock,
290 pj_sockaddr *bound_addr);
291
292
293/**
294 * Close the active socket. This will unregister the socket from the
295 * ioqueue and ultimately close the socket.
296 *
297 * @param asock The active socket.
298 *
299 * @return PJ_SUCCESS if the operation has been successful,
300 * or the appropriate error code on failure.
301 */
302PJ_DECL(pj_status_t) pj_activesock_close(pj_activesock_t *asock);
303
304
305/**
306 * Associate arbitrary data with the active socket. Application may
307 * inspect this data in the callbacks and associate it with higher
308 * level processing.
309 *
310 * @param asock The active socket.
311 * @param user_data The user data to be associated with the active
312 * socket.
313 *
314 * @return PJ_SUCCESS if the operation has been successful,
315 * or the appropriate error code on failure.
316 */
317PJ_DECL(pj_status_t) pj_activesock_set_user_data(pj_activesock_t *asock,
318 void *user_data);
319
320/**
321 * Retrieve the user data previously associated with this active
322 * socket.
323 *
324 * @param asock The active socket.
325 *
326 * @return The user data.
327 */
328PJ_DECL(void*) pj_activesock_get_user_data(pj_activesock_t *asock);
329
330
331/**
332 * Starts read operation on this active socket. This function will create
333 * \a async_cnt number of buffers (the \a async_cnt parameter was given
334 * in \a pj_activesock_create() function) where each buffer is \a buff_size
335 * long. The buffers are allocated from the specified \a pool. Once the
336 * buffers are created, it then issues \a async_cnt number of asynchronous
337 * \a recv() operations to the socket and returns back to caller. Incoming
338 * data on the socket will be reported back to application via the
339 * \a on_data_read() callback.
340 *
341 * Application only needs to call this function once to initiate read
342 * operations. Further read operations will be done automatically by the
343 * active socket when \a on_data_read() callback returns non-zero.
344 *
345 * @param asock The active socket.
346 * @param pool Pool used to allocate buffers for incoming data.
347 * @param buff_size The size of each buffer, in bytes.
348 * @param flags Flags to be given to pj_ioqueue_recv().
349 *
350 * @return PJ_SUCCESS if the operation has been successful,
351 * or the appropriate error code on failure.
352 */
353PJ_DECL(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock,
354 pj_pool_t *pool,
355 unsigned buff_size,
356 pj_uint32_t flags);
357
358/**
Benny Prijonobd344ff2008-08-04 09:59:02 +0000359 * Same as #pj_activesock_start_read(), except that the application
360 * supplies the buffers for the read operation so that the acive socket
361 * does not have to allocate the buffers.
362 *
363 * @param asock The active socket.
364 * @param pool Pool used to allocate buffers for incoming data.
365 * @param buff_size The size of each buffer, in bytes.
366 * @param readbuf Array of packet buffers, each has buff_size size.
367 * @param flags Flags to be given to pj_ioqueue_recv().
368 *
369 * @return PJ_SUCCESS if the operation has been successful,
370 * or the appropriate error code on failure.
371 */
372PJ_DECL(pj_status_t) pj_activesock_start_read2(pj_activesock_t *asock,
373 pj_pool_t *pool,
374 unsigned buff_size,
375 void *readbuf[],
376 pj_uint32_t flags);
377
378/**
Benny Prijono4bac2c12008-05-11 18:12:16 +0000379 * Same as pj_activesock_start_read(), except that this function is used
380 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
381 * callback instead.
382 *
383 * @param asock The active socket.
384 * @param pool Pool used to allocate buffers for incoming data.
385 * @param buff_size The size of each buffer, in bytes.
386 * @param flags Flags to be given to pj_ioqueue_recvfrom().
387 *
388 * @return PJ_SUCCESS if the operation has been successful,
389 * or the appropriate error code on failure.
390 */
391PJ_DECL(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock,
392 pj_pool_t *pool,
393 unsigned buff_size,
394 pj_uint32_t flags);
395
396/**
Benny Prijonobd344ff2008-08-04 09:59:02 +0000397 * Same as #pj_activesock_start_recvfrom() except that the recvfrom()
398 * operation takes the buffer from the argument rather than creating
399 * new ones.
400 *
401 * @param asock The active socket.
402 * @param pool Pool used to allocate buffers for incoming data.
403 * @param buff_size The size of each buffer, in bytes.
404 * @param readbuf Array of packet buffers, each has buff_size size.
405 * @param flags Flags to be given to pj_ioqueue_recvfrom().
406 *
407 * @return PJ_SUCCESS if the operation has been successful,
408 * or the appropriate error code on failure.
409 */
410PJ_DECL(pj_status_t) pj_activesock_start_recvfrom2(pj_activesock_t *asock,
411 pj_pool_t *pool,
412 unsigned buff_size,
413 void *readbuf[],
414 pj_uint32_t flags);
415
416/**
Benny Prijono4bac2c12008-05-11 18:12:16 +0000417 * Send data using the socket.
418 *
419 * @param asock The active socket.
420 * @param send_key The operation key to send the data, which is useful
421 * if application wants to submit multiple pending
422 * send operations and want to track which exact data
423 * has been sent in the \a on_data_sent() callback.
424 * @param data The data to be sent. This data must remain valid
425 * until the data has been sent.
426 * @param size The size of the data.
427 * @param flags Flags to be given to pj_ioqueue_send().
428 *
429 *
430 * @return PJ_SUCCESS if data has been sent immediately, or
431 * PJ_EPENDING if data cannot be sent immediately. In
432 * this case the \a on_data_sent() callback will be
433 * called when data is actually sent. Any other return
434 * value indicates error condition.
435 */
436PJ_DECL(pj_status_t) pj_activesock_send(pj_activesock_t *asock,
437 pj_ioqueue_op_key_t *send_key,
438 const void *data,
439 pj_ssize_t *size,
440 unsigned flags);
441
442/**
443 * Send datagram using the socket.
444 *
445 * @param asock The active socket.
446 * @param send_key The operation key to send the data, which is useful
447 * if application wants to submit multiple pending
448 * send operations and want to track which exact data
449 * has been sent in the \a on_data_sent() callback.
450 * @param data The data to be sent. This data must remain valid
451 * until the data has been sent.
452 * @param size The size of the data.
453 * @param flags Flags to be given to pj_ioqueue_send().
454 * @param addr The destination address.
455 * @param addr_len The length of the address.
456 *
457 * @return PJ_SUCCESS if data has been sent immediately, or
458 * PJ_EPENDING if data cannot be sent immediately. In
459 * this case the \a on_data_sent() callback will be
460 * called when data is actually sent. Any other return
461 * value indicates error condition.
462 */
463PJ_DECL(pj_status_t) pj_activesock_sendto(pj_activesock_t *asock,
464 pj_ioqueue_op_key_t *send_key,
465 const void *data,
466 pj_ssize_t *size,
467 unsigned flags,
468 const pj_sockaddr_t *addr,
469 int addr_len);
470
Benny Prijono1dd54202008-07-25 10:45:34 +0000471#if PJ_HAS_TCP
Benny Prijono4bac2c12008-05-11 18:12:16 +0000472/**
473 * Starts asynchronous socket accept() operations on this active socket.
474 * Application must bind the socket before calling this function. This
475 * function will issue \a async_cnt number of asynchronous \a accept()
476 * operations to the socket and returns back to caller. Incoming
477 * connection on the socket will be reported back to application via the
478 * \a on_accept_complete() callback.
479 *
480 * Application only needs to call this function once to initiate accept()
481 * operations. Further accept() operations will be done automatically by
482 * the active socket when \a on_accept_complete() callback returns non-zero.
483 *
484 * @param asock The active socket.
485 * @param pool Pool used to allocate some internal data for the
486 * operation.
487 *
488 * @return PJ_SUCCESS if the operation has been successful,
489 * or the appropriate error code on failure.
490 */
491PJ_DECL(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock,
492 pj_pool_t *pool);
493
494/**
495 * Starts asynchronous socket connect() operation for this socket. Once
496 * the connection is done (either successfully or not), the
497 * \a on_connect_complete() callback will be called.
498 *
499 * @param asock The active socket.
500 * @param pool The pool to allocate some internal data for the
501 * operation.
502 * @param remaddr Remote address.
503 * @param addr_len Length of the remote address.
504 *
505 * @return PJ_SUCCESS if connection can be established immediately,
506 * or PJ_EPENDING if connection cannot be established
507 * immediately. In this case the \a on_connect_complete()
508 * callback will be called when connection is complete.
509 * Any other return value indicates error condition.
510 */
511PJ_DECL(pj_status_t) pj_activesock_start_connect(pj_activesock_t *asock,
512 pj_pool_t *pool,
513 const pj_sockaddr_t *remaddr,
514 int addr_len);
515
Benny Prijono1dd54202008-07-25 10:45:34 +0000516#endif /* PJ_HAS_TCP */
Benny Prijono4bac2c12008-05-11 18:12:16 +0000517
518/**
519 * @}
520 */
521
522PJ_END_DECL
523
524#endif /* __PJ_ASYNCSOCK_H__ */
525