Debian Codegraph @

Xpjproject/pjlib/include/pj/activesock.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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
31 PJ_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 */
50 typedef struct pj_activesock_t pj_activesock_t;
51
52 /**
53 * This structure contains the callbacks to be called by the active socket.
54 */
55 typedef 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(). If the status of accept operation is
135 * needed use on_accept_complete2 instead of this callback.
136 *
137 * @param asock The active socket.
138 * @param newsock The new incoming socket.
139 * @param src_addr The source address of the connection.
140 * @param addr_len Length of the source address.
141 *
142 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
143 * when application no longer wants to accept incoming
144 * connection. Application may destroy the active socket
145 * in the callback and return PJ_FALSE here.
146 */
147 pj_bool_t (*on_accept_complete)(pj_activesock_t *asock,
148 pj_sock_t newsock,
149 const pj_sockaddr_t *src_addr,
150 int src_addr_len);
151
152 /**
153 * This callback is called when new connection arrives as the result
154 * of pj_activesock_start_accept().
155 *
156 * @param asock The active socket.
157 * @param newsock The new incoming socket.
158 * @param src_addr The source address of the connection.
159 * @param addr_len Length of the source address.
160 * @param status The status of the accept operation. This may contain
161 * non-PJ_SUCCESS for example when the TCP listener is in
162 * bad state for example on iOS platform after the
163 * application waking up from background.
164 *
165 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
166 * when application no longer wants to accept incoming
167 * connection. Application may destroy the active socket
168 * in the callback and return PJ_FALSE here.
169 */
170 pj_bool_t (*on_accept_complete2)(pj_activesock_t *asock,
171 pj_sock_t newsock,
172 const pj_sockaddr_t *src_addr,
173 int src_addr_len,
174 pj_status_t status);
175
176 /**
177 * This callback is called when pending connect operation has been
178 * completed.
179 *
180 * @param asock The active socket.
181 * @param status The connection result. If connection has been
182 * successfully established, the status will contain
183 * PJ_SUCCESS.
184 *
185 * @return Application may destroy the active socket in the
186 * callback and return PJ_FALSE here.
187 */
188 pj_bool_t (*on_connect_complete)(pj_activesock_t *asock,
189 pj_status_t status);
190
191 } pj_activesock_cb;
192
193
194 /**
195 * Settings that can be given during active socket creation. Application
196 * must initialize this structure with #pj_activesock_cfg_default().
197 */
198 typedef struct pj_activesock_cfg
199 {
200 /**
201 * Optional group lock to be assigned to the ioqueue key.
202 */
203 pj_grp_lock_t *grp_lock;
204
205 /**
206 * Number of concurrent asynchronous operations that is to be supported
207 * by the active socket. This value only affects socket receive and
208 * accept operations -- the active socket will issue one or more
209 * asynchronous read and accept operations based on the value of this
210 * field. Setting this field to more than one will allow more than one
211 * incoming data or incoming connections to be processed simultaneously
212 * on multiprocessor systems, when the ioqueue is polled by more than
213 * one threads.
214 *
215 * The default value is 1.
216 */
217 unsigned async_cnt;
218
219 /**
220 * The ioqueue concurrency to be forced on the socket when it is
221 * registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
222 * info about ioqueue concurrency.
223 *
224 * When this value is -1, the concurrency setting will not be forced for
225 * this socket, and the socket will inherit the concurrency setting of
226 * the ioqueue. When this value is zero, the active socket will disable
227 * concurrency for the socket. When this value is +1, the active socket
228 * will enable concurrency for the socket.
229 *
230 * The default value is -1.
231 */
232 int concurrency;
233
234 /**
235 * If this option is specified, the active socket will make sure that
236 * asynchronous send operation with stream oriented socket will only
237 * call the callback after all data has been sent. This means that the
238 * active socket will automatically resend the remaining data until
239 * all data has been sent.
240 *
241 * Please note that when this option is specified, it is possible that
242 * error is reported after partial data has been sent. Also setting
243 * this will disable the ioqueue concurrency for the socket.
244 *
245 * Default value is PJ_TRUE.
246 */
247 pj_bool_t whole_data;
248
249 /**
250 * If this option is specified, set close-on-exec flag for socket.
251 * This option is only used by #pj_activesock_create_udp()
252 *
253 * Default value is PJ_TRUE.
254 */
255 pj_bool_t sock_cloexec;
256
257 } pj_activesock_cfg;
258
259
260 /**
261 * Initialize the active socket configuration with the default values.
262 *
263 * @param cfg The configuration to be initialized.
264 */
265 PJ_DECL(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg);
266
267
268 /**
269 * Create the active socket for the specified socket. This will register
270 * the socket to the specified ioqueue.
271 *
272 * @param pool Pool to allocate memory from.
273 * @param sock The socket handle.
274 * @param sock_type Specify socket type, either pj_SOCK_DGRAM() or
275 * pj_SOCK_STREAM(). The active socket needs this
276 * information to handle connection closure for
277 * connection oriented sockets.
278 * @param ioqueue The ioqueue to use.
279 * @param opt Optional settings. When this setting is not specifed,
280 * the default values will be used.
281 * @param cb Pointer to structure containing application
282 * callbacks.
283 * @param user_data Arbitrary user data to be associated with this
284 * active socket.
285 * @param p_asock Pointer to receive the active socket instance.
286 *
287 * @return PJ_SUCCESS if the operation has been successful,
288 * or the appropriate error code on failure.
289 */
290 PJ_DECL(pj_status_t) pj_activesock_create(pj_pool_t *pool,
291 pj_sock_t sock,
292 int sock_type,
293 const pj_activesock_cfg *opt,
294 pj_ioqueue_t *ioqueue,
295 const pj_activesock_cb *cb,
296 void *user_data,
297 pj_activesock_t **p_asock);
298
299 /**
300 * Create UDP socket descriptor, bind it to the specified address, and
301 * create the active socket for the socket descriptor.
302 *
303 * @param pool Pool to allocate memory from.
304 * @param addr Specifies the address family of the socket and the
305 * address where the socket should be bound to. If
306 * this argument is NULL, then AF_INET is assumed and
307 * the socket will be bound to any addresses and port.
308 * @param ioqueue The ioqueue.
309 * @param opt Optional settings. When this setting is not specifed,
310 * the default values will be used.
311 * @param cb Pointer to structure containing application
312 * callbacks.
313 * @param user_data Arbitrary user data to be associated with this
314 * active socket.
315 * @param p_asock Pointer to receive the active socket instance.
316 * @param bound_addr If this argument is specified, it will be filled with
317 * the bound address on return.
318 *
319 * @return PJ_SUCCESS if the operation has been successful,
320 * or the appropriate error code on failure.
321 */
322 PJ_DECL(pj_status_t) pj_activesock_create_udp(pj_pool_t *pool,
323 const pj_sockaddr *addr,
324 const pj_activesock_cfg *opt,
325 pj_ioqueue_t *ioqueue,
326 const pj_activesock_cb *cb,
327 void *user_data,
328 pj_activesock_t **p_asock,
329 pj_sockaddr *bound_addr);
330
331 /**
332 * Close the active socket. This will unregister the socket from the
333 * ioqueue and ultimately close the socket.
334 *
335 * @param asock The active socket.
336 *
337 * @return PJ_SUCCESS if the operation has been successful,
338 * or the appropriate error code on failure.
339 */
340 PJ_DECL(pj_status_t) pj_activesock_close(pj_activesock_t *asock);
341
342 #if (defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
343 PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0) || \
344 defined(DOXYGEN)
345 /**
346 * Set iPhone OS background mode setting. Setting to 1 will enable TCP
347 * active socket to receive incoming data when application is in the
348 * background. Setting to 0 will disable it. Default value of this
349 * setting is PJ_ACTIVESOCK_TCP_IPHONE_OS_BG.
350 *
351 * This API is only available if PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT
352 * is set to non-zero.
353 *
354 * @param asock The active socket.
355 * @param val The value of background mode setting.
356 *
357 */
358 PJ_DECL(void) pj_activesock_set_iphone_os_bg(pj_activesock_t *asock,
359 int val);
360
361 /**
362 * Enable/disable support for iPhone OS background mode. This setting
363 * will apply globally and will affect any active sockets created
364 * afterwards, if you want to change the setting for a particular
365 * active socket, use #pj_activesock_set_iphone_os_bg() instead.
366 * By default, this setting is enabled.
367 *
368 * This API is only available if PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT
369 * is set to non-zero.
370 *
371 * @param val The value of global background mode setting.
372 *
373 */
374 PJ_DECL(void) pj_activesock_enable_iphone_os_bg(pj_bool_t val);
375 #endif
376
377 /**
378 * Associate arbitrary data with the active socket. Application may
379 * inspect this data in the callbacks and associate it with higher
380 * level processing.
381 *
382 * @param asock The active socket.
383 * @param user_data The user data to be associated with the active
384 * socket.
385 *
386 * @return PJ_SUCCESS if the operation has been successful,
387 * or the appropriate error code on failure.
388 */
389 PJ_DECL(pj_status_t) pj_activesock_set_user_data(pj_activesock_t *asock,
390 void *user_data);
391
392 /**
393 * Retrieve the user data previously associated with this active
394 * socket.
395 *
396 * @param asock The active socket.
397 *
398 * @return The user data.
399 */
400 PJ_DECL(void*) pj_activesock_get_user_data(pj_activesock_t *asock);
401
402
403 /**
404 * Starts read operation on this active socket. This function will create
405 * \a async_cnt number of buffers (the \a async_cnt parameter was given
406 * in \a pj_activesock_create() function) where each buffer is \a buff_size
407 * long. The buffers are allocated from the specified \a pool. Once the
408 * buffers are created, it then issues \a async_cnt number of asynchronous
409 * \a recv() operations to the socket and returns back to caller. Incoming
410 * data on the socket will be reported back to application via the
411 * \a on_data_read() callback.
412 *
413 * Application only needs to call this function once to initiate read
414 * operations. Further read operations will be done automatically by the
415 * active socket when \a on_data_read() callback returns non-zero.
416 *
417 * @param asock The active socket.
418 * @param pool Pool used to allocate buffers for incoming data.
419 * @param buff_size The size of each buffer, in bytes.
420 * @param flags Flags to be given to pj_ioqueue_recv().
421 *
422 * @return PJ_SUCCESS if the operation has been successful,
423 * or the appropriate error code on failure.
424 */
425 PJ_DECL(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock,
426 pj_pool_t *pool,
427 unsigned buff_size,
428 pj_uint32_t flags);
429
430 /**
431 * Same as #pj_activesock_start_read(), except that the application
432 * supplies the buffers for the read operation so that the acive socket
433 * does not have to allocate the buffers.
434 *
435 * @param asock The active socket.
436 * @param pool Pool used to allocate buffers for incoming data.
437 * @param buff_size The size of each buffer, in bytes.
438 * @param readbuf Array of packet buffers, each has buff_size size.
439 * @param flags Flags to be given to pj_ioqueue_recv().
440 *
441 * @return PJ_SUCCESS if the operation has been successful,
442 * or the appropriate error code on failure.
443 */
444 PJ_DECL(pj_status_t) pj_activesock_start_read2(pj_activesock_t *asock,
445 pj_pool_t *pool,
446 unsigned buff_size,
447 void *readbuf[],
448 pj_uint32_t flags);
449
450 /**
451 * Same as pj_activesock_start_read(), except that this function is used
452 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
453 * callback instead.
454 *
455 * @param asock The active socket.
456 * @param pool Pool used to allocate buffers for incoming data.
457 * @param buff_size The size of each buffer, in bytes.
458 * @param flags Flags to be given to pj_ioqueue_recvfrom().
459 *
460 * @return PJ_SUCCESS if the operation has been successful,
461 * or the appropriate error code on failure.
462 */
463 PJ_DECL(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock,
464 pj_pool_t *pool,
465 unsigned buff_size,
466 pj_uint32_t flags);
467
468 /**
469 * Same as #pj_activesock_start_recvfrom() except that the recvfrom()
470 * operation takes the buffer from the argument rather than creating
471 * new ones.
472 *
473 * @param asock The active socket.
474 * @param pool Pool used to allocate buffers for incoming data.
475 * @param buff_size The size of each buffer, in bytes.
476 * @param readbuf Array of packet buffers, each has buff_size size.
477 * @param flags Flags to be given to pj_ioqueue_recvfrom().
478 *
479 * @return PJ_SUCCESS if the operation has been successful,
480 * or the appropriate error code on failure.
481 */
482 PJ_DECL(pj_status_t) pj_activesock_start_recvfrom2(pj_activesock_t *asock,
483 pj_pool_t *pool,
484 unsigned buff_size,
485 void *readbuf[],
486 pj_uint32_t flags);
487
488 /**
489 * Send data using the socket.
490 *
491 * @param asock The active socket.
492 * @param send_key The operation key to send the data, which is useful
493 * if application wants to submit multiple pending
494 * send operations and want to track which exact data
495 * has been sent in the \a on_data_sent() callback.
496 * @param data The data to be sent. This data must remain valid
497 * until the data has been sent.
498 * @param size The size of the data.
499 * @param flags Flags to be given to pj_ioqueue_send().
500 *
501 *
502 * @return PJ_SUCCESS if data has been sent immediately, or
503 * PJ_EPENDING if data cannot be sent immediately. In
504 * this case the \a on_data_sent() callback will be
505 * called when data is actually sent. Any other return
506 * value indicates error condition.
507 */
508 PJ_DECL(pj_status_t) pj_activesock_send(pj_activesock_t *asock,
509 pj_ioqueue_op_key_t *send_key,
510 const void *data,
511 pj_ssize_t *size,
512 unsigned flags);
513
514 /**
515 * Send datagram using the socket.
516 *
517 * @param asock The active socket.
518 * @param send_key The operation key to send the data, which is useful
519 * if application wants to submit multiple pending
520 * send operations and want to track which exact data
521 * has been sent in the \a on_data_sent() callback.
522 * @param data The data to be sent. This data must remain valid
523 * until the data has been sent.
524 * @param size The size of the data.
525 * @param flags Flags to be given to pj_ioqueue_send().
526 * @param addr The destination address.
527 * @param addr_len The length of the address.
528 *
529 * @return PJ_SUCCESS if data has been sent immediately, or
530 * PJ_EPENDING if data cannot be sent immediately. In
531 * this case the \a on_data_sent() callback will be
532 * called when data is actually sent. Any other return
533 * value indicates error condition.
534 */
535 PJ_DECL(pj_status_t) pj_activesock_sendto(pj_activesock_t *asock,
536 pj_ioqueue_op_key_t *send_key,
537 const void *data,
538 pj_ssize_t *size,
539 unsigned flags,
540 const pj_sockaddr_t *addr,
541 int addr_len);
542
543 #if PJ_HAS_TCP
544 /**
545 * Starts asynchronous socket accept() operations on this active socket.
546 * Application must bind the socket before calling this function. This
547 * function will issue \a async_cnt number of asynchronous \a accept()
548 * operations to the socket and returns back to caller. Incoming
549 * connection on the socket will be reported back to application via the
550 * \a on_accept_complete() callback.
551 *
552 * Application only needs to call this function once to initiate accept()
553 * operations. Further accept() operations will be done automatically by
554 * the active socket when \a on_accept_complete() callback returns non-zero.
555 *
556 * @param asock The active socket.
557 * @param pool Pool used to allocate some internal data for the
558 * operation.
559 *
560 * @return PJ_SUCCESS if the operation has been successful,
561 * or the appropriate error code on failure.
562 */
563 PJ_DECL(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock,
564 pj_pool_t *pool);
565
566 /**
567 * Starts asynchronous socket connect() operation for this socket. Once
568 * the connection is done (either successfully or not), the
569 * \a on_connect_complete() callback will be called.
570 *
571 * @param asock The active socket.
572 * @param pool The pool to allocate some internal data for the
573 * operation.
574 * @param remaddr Remote address.
575 * @param addr_len Length of the remote address.
576 *
577 * @return PJ_SUCCESS if connection can be established immediately,
578 * or PJ_EPENDING if connection cannot be established
579 * immediately. In this case the \a on_connect_complete()
580 * callback will be called when connection is complete.
581 * Any other return value indicates error condition.
582 */
583 PJ_DECL(pj_status_t) pj_activesock_start_connect(pj_activesock_t *asock,
584 pj_pool_t *pool,
585 const pj_sockaddr_t *remaddr,
586 int addr_len);
587
588
589 #endif /* PJ_HAS_TCP */
590
591 /**
592 * @}
593 */
594
595 PJ_END_DECL
596
597 #endif /* __PJ_ASYNCSOCK_H__ */
598