1
/* $Id: activesock.h 4359 2013-02-21 11:18:36Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJ_ASYNCSOCK_H__
21
#define __PJ_ASYNCSOCK_H__
25
* @brief Active socket
28
#include <pj/ioqueue.h>
35
* @defgroup PJ_ACTIVESOCK Active socket I/O
36
* @brief Active socket performs active operations on socket.
40
* Active socket is a higher level abstraction to the ioqueue. It provides
41
* automation to socket operations which otherwise would have to be done
42
* manually by the applications. For example with socket recv(), recvfrom(),
43
* and accept() operations, application only needs to invoke these
44
* operation once, and it will be notified whenever data or incoming TCP
45
* connection (in the case of accept()) arrives.
49
* This opaque structure describes the active socket.
51
typedef struct pj_activesock_t pj_activesock_t;
54
* This structure contains the callbacks to be called by the active socket.
56
typedef struct pj_activesock_cb
59
* This callback is called when a data arrives as the result of
60
* pj_activesock_start_read().
62
* @param asock The active socket.
63
* @param data The buffer containing the new data, if any. If
64
* the status argument is non-PJ_SUCCESS, this
65
* argument may be NULL.
66
* @param size The length of data in the buffer.
67
* @param status The status of the read operation. This may contain
68
* non-PJ_SUCCESS for example when the TCP connection
69
* has been closed. In this case, the buffer may
70
* contain left over data from previous callback which
71
* the application may want to process.
72
* @param remainder If application wishes to leave some data in the
73
* buffer (common for TCP applications), it should
74
* move the remainder data to the front part of the
75
* buffer and set the remainder length here. The value
76
* of this parameter will be ignored for datagram
79
* @return PJ_TRUE if further read is desired, and PJ_FALSE
80
* when application no longer wants to receive data.
81
* Application may destroy the active socket in the
82
* callback and return PJ_FALSE here.
84
pj_bool_t (*on_data_read)(pj_activesock_t *asock,
88
pj_size_t *remainder);
90
* This callback is called when a packet arrives as the result of
91
* pj_activesock_start_recvfrom().
93
* @param asock The active socket.
94
* @param data The buffer containing the packet, if any. If
95
* the status argument is non-PJ_SUCCESS, this
96
* argument will be set to NULL.
97
* @param size The length of packet in the buffer. If
98
* the status argument is non-PJ_SUCCESS, this
99
* argument will be set to zero.
100
* @param src_addr Source address of the packet.
101
* @param addr_len Length of the source address.
102
* @param status This contains
104
* @return PJ_TRUE if further read is desired, and PJ_FALSE
105
* when application no longer wants to receive data.
106
* Application may destroy the active socket in the
107
* callback and return PJ_FALSE here.
109
pj_bool_t (*on_data_recvfrom)(pj_activesock_t *asock,
112
const pj_sockaddr_t *src_addr,
117
* This callback is called when data has been sent.
119
* @param asock The active socket.
120
* @param send_key Key associated with the send operation.
121
* @param sent If value is positive non-zero it indicates the
122
* number of data sent. When the value is negative,
123
* it contains the error code which can be retrieved
124
* by negating the value (i.e. status=-sent).
126
* @return Application may destroy the active socket in the
127
* callback and return PJ_FALSE here.
129
pj_bool_t (*on_data_sent)(pj_activesock_t *asock,
130
pj_ioqueue_op_key_t *send_key,
134
* This callback is called when new connection arrives as the result
135
* of pj_activesock_start_accept().
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.
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.
147
pj_bool_t (*on_accept_complete)(pj_activesock_t *asock,
149
const pj_sockaddr_t *src_addr,
153
* This callback is called when pending connect operation has been
156
* @param asock The active socket.
157
* @param status The connection result. If connection has been
158
* successfully established, the status will contain
161
* @return Application may destroy the active socket in the
162
* callback and return PJ_FALSE here.
164
pj_bool_t (*on_connect_complete)(pj_activesock_t *asock,
171
* Settings that can be given during active socket creation. Application
172
* must initialize this structure with #pj_activesock_cfg_default().
174
typedef struct pj_activesock_cfg
177
* Optional group lock to be assigned to the ioqueue key.
179
pj_grp_lock_t *grp_lock;
182
* Number of concurrent asynchronous operations that is to be supported
183
* by the active socket. This value only affects socket receive and
184
* accept operations -- the active socket will issue one or more
185
* asynchronous read and accept operations based on the value of this
186
* field. Setting this field to more than one will allow more than one
187
* incoming data or incoming connections to be processed simultaneously
188
* on multiprocessor systems, when the ioqueue is polled by more than
191
* The default value is 1.
196
* The ioqueue concurrency to be forced on the socket when it is
197
* registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
198
* info about ioqueue concurrency.
200
* When this value is -1, the concurrency setting will not be forced for
201
* this socket, and the socket will inherit the concurrency setting of
202
* the ioqueue. When this value is zero, the active socket will disable
203
* concurrency for the socket. When this value is +1, the active socket
204
* will enable concurrency for the socket.
206
* The default value is -1.
211
* If this option is specified, the active socket will make sure that
212
* asynchronous send operation with stream oriented socket will only
213
* call the callback after all data has been sent. This means that the
214
* active socket will automatically resend the remaining data until
215
* all data has been sent.
217
* Please note that when this option is specified, it is possible that
218
* error is reported after partial data has been sent. Also setting
219
* this will disable the ioqueue concurrency for the socket.
221
* Default value is 1.
223
pj_bool_t whole_data;
229
* Initialize the active socket configuration with the default values.
231
* @param cfg The configuration to be initialized.
233
PJ_DECL(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg);
237
* Create the active socket for the specified socket. This will register
238
* the socket to the specified ioqueue.
240
* @param pool Pool to allocate memory from.
241
* @param sock The socket handle.
242
* @param sock_type Specify socket type, either pj_SOCK_DGRAM() or
243
* pj_SOCK_STREAM(). The active socket needs this
244
* information to handle connection closure for
245
* connection oriented sockets.
246
* @param ioqueue The ioqueue to use.
247
* @param opt Optional settings. When this setting is not specifed,
248
* the default values will be used.
249
* @param cb Pointer to structure containing application
251
* @param user_data Arbitrary user data to be associated with this
253
* @param p_asock Pointer to receive the active socket instance.
255
* @return PJ_SUCCESS if the operation has been successful,
256
* or the appropriate error code on failure.
258
PJ_DECL(pj_status_t) pj_activesock_create(pj_pool_t *pool,
261
const pj_activesock_cfg *opt,
262
pj_ioqueue_t *ioqueue,
263
const pj_activesock_cb *cb,
265
pj_activesock_t **p_asock);
268
* Create UDP socket descriptor, bind it to the specified address, and
269
* create the active socket for the socket descriptor.
271
* @param pool Pool to allocate memory from.
272
* @param addr Specifies the address family of the socket and the
273
* address where the socket should be bound to. If
274
* this argument is NULL, then AF_INET is assumed and
275
* the socket will be bound to any addresses and port.
276
* @param opt Optional settings. When this setting is not specifed,
277
* the default values will be used.
278
* @param cb Pointer to structure containing application
280
* @param user_data Arbitrary user data to be associated with this
282
* @param p_asock Pointer to receive the active socket instance.
283
* @param bound_addr If this argument is specified, it will be filled with
284
* the bound address on return.
286
* @return PJ_SUCCESS if the operation has been successful,
287
* or the appropriate error code on failure.
289
PJ_DECL(pj_status_t) pj_activesock_create_udp(pj_pool_t *pool,
290
const pj_sockaddr *addr,
291
const pj_activesock_cfg *opt,
292
pj_ioqueue_t *ioqueue,
293
const pj_activesock_cb *cb,
295
pj_activesock_t **p_asock,
296
pj_sockaddr *bound_addr);
299
* Close the active socket. This will unregister the socket from the
300
* ioqueue and ultimately close the socket.
302
* @param asock The active socket.
304
* @return PJ_SUCCESS if the operation has been successful,
305
* or the appropriate error code on failure.
307
PJ_DECL(pj_status_t) pj_activesock_close(pj_activesock_t *asock);
309
#if (defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
310
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0) || \
313
* Set iPhone OS background mode setting. Setting to 1 will enable TCP
314
* active socket to receive incoming data when application is in the
315
* background. Setting to 0 will disable it. Default value of this
316
* setting is PJ_ACTIVESOCK_TCP_IPHONE_OS_BG.
318
* This API is only available if PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT
319
* is set to non-zero.
321
* @param asock The active socket.
322
* @param val The value of background mode setting.
325
PJ_DECL(void) pj_activesock_set_iphone_os_bg(pj_activesock_t *asock,
329
* Enable/disable support for iPhone OS background mode. This setting
330
* will apply globally and will affect any active sockets created
331
* afterwards, if you want to change the setting for a particular
332
* active socket, use #pj_activesock_set_iphone_os_bg() instead.
333
* By default, this setting is enabled.
335
* This API is only available if PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT
336
* is set to non-zero.
338
* @param val The value of global background mode setting.
341
PJ_DECL(void) pj_activesock_enable_iphone_os_bg(pj_bool_t val);
345
* Associate arbitrary data with the active socket. Application may
346
* inspect this data in the callbacks and associate it with higher
349
* @param asock The active socket.
350
* @param user_data The user data to be associated with the active
353
* @return PJ_SUCCESS if the operation has been successful,
354
* or the appropriate error code on failure.
356
PJ_DECL(pj_status_t) pj_activesock_set_user_data(pj_activesock_t *asock,
360
* Retrieve the user data previously associated with this active
363
* @param asock The active socket.
365
* @return The user data.
367
PJ_DECL(void*) pj_activesock_get_user_data(pj_activesock_t *asock);
371
* Starts read operation on this active socket. This function will create
372
* \a async_cnt number of buffers (the \a async_cnt parameter was given
373
* in \a pj_activesock_create() function) where each buffer is \a buff_size
374
* long. The buffers are allocated from the specified \a pool. Once the
375
* buffers are created, it then issues \a async_cnt number of asynchronous
376
* \a recv() operations to the socket and returns back to caller. Incoming
377
* data on the socket will be reported back to application via the
378
* \a on_data_read() callback.
380
* Application only needs to call this function once to initiate read
381
* operations. Further read operations will be done automatically by the
382
* active socket when \a on_data_read() callback returns non-zero.
384
* @param asock The active socket.
385
* @param pool Pool used to allocate buffers for incoming data.
386
* @param buff_size The size of each buffer, in bytes.
387
* @param flags Flags to be given to pj_ioqueue_recv().
389
* @return PJ_SUCCESS if the operation has been successful,
390
* or the appropriate error code on failure.
392
PJ_DECL(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock,
398
* Same as #pj_activesock_start_read(), except that the application
399
* supplies the buffers for the read operation so that the acive socket
400
* does not have to allocate the buffers.
402
* @param asock The active socket.
403
* @param pool Pool used to allocate buffers for incoming data.
404
* @param buff_size The size of each buffer, in bytes.
405
* @param readbuf Array of packet buffers, each has buff_size size.
406
* @param flags Flags to be given to pj_ioqueue_recv().
408
* @return PJ_SUCCESS if the operation has been successful,
409
* or the appropriate error code on failure.
411
PJ_DECL(pj_status_t) pj_activesock_start_read2(pj_activesock_t *asock,
418
* Same as pj_activesock_start_read(), except that this function is used
419
* only for datagram sockets, and it will trigger \a on_data_recvfrom()
422
* @param asock The active socket.
423
* @param pool Pool used to allocate buffers for incoming data.
424
* @param buff_size The size of each buffer, in bytes.
425
* @param flags Flags to be given to pj_ioqueue_recvfrom().
427
* @return PJ_SUCCESS if the operation has been successful,
428
* or the appropriate error code on failure.
430
PJ_DECL(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock,
436
* Same as #pj_activesock_start_recvfrom() except that the recvfrom()
437
* operation takes the buffer from the argument rather than creating
440
* @param asock The active socket.
441
* @param pool Pool used to allocate buffers for incoming data.
442
* @param buff_size The size of each buffer, in bytes.
443
* @param readbuf Array of packet buffers, each has buff_size size.
444
* @param flags Flags to be given to pj_ioqueue_recvfrom().
446
* @return PJ_SUCCESS if the operation has been successful,
447
* or the appropriate error code on failure.
449
PJ_DECL(pj_status_t) pj_activesock_start_recvfrom2(pj_activesock_t *asock,
456
* Send data using the socket.
458
* @param asock The active socket.
459
* @param send_key The operation key to send the data, which is useful
460
* if application wants to submit multiple pending
461
* send operations and want to track which exact data
462
* has been sent in the \a on_data_sent() callback.
463
* @param data The data to be sent. This data must remain valid
464
* until the data has been sent.
465
* @param size The size of the data.
466
* @param flags Flags to be given to pj_ioqueue_send().
469
* @return PJ_SUCCESS if data has been sent immediately, or
470
* PJ_EPENDING if data cannot be sent immediately. In
471
* this case the \a on_data_sent() callback will be
472
* called when data is actually sent. Any other return
473
* value indicates error condition.
475
PJ_DECL(pj_status_t) pj_activesock_send(pj_activesock_t *asock,
476
pj_ioqueue_op_key_t *send_key,
482
* Send datagram using the socket.
484
* @param asock The active socket.
485
* @param send_key The operation key to send the data, which is useful
486
* if application wants to submit multiple pending
487
* send operations and want to track which exact data
488
* has been sent in the \a on_data_sent() callback.
489
* @param data The data to be sent. This data must remain valid
490
* until the data has been sent.
491
* @param size The size of the data.
492
* @param flags Flags to be given to pj_ioqueue_send().
493
* @param addr The destination address.
494
* @param addr_len The length of the address.
496
* @return PJ_SUCCESS if data has been sent immediately, or
497
* PJ_EPENDING if data cannot be sent immediately. In
498
* this case the \a on_data_sent() callback will be
499
* called when data is actually sent. Any other return
500
* value indicates error condition.
502
PJ_DECL(pj_status_t) pj_activesock_sendto(pj_activesock_t *asock,
503
pj_ioqueue_op_key_t *send_key,
507
const pj_sockaddr_t *addr,
512
* Starts asynchronous socket accept() operations on this active socket.
513
* Application must bind the socket before calling this function. This
514
* function will issue \a async_cnt number of asynchronous \a accept()
515
* operations to the socket and returns back to caller. Incoming
516
* connection on the socket will be reported back to application via the
517
* \a on_accept_complete() callback.
519
* Application only needs to call this function once to initiate accept()
520
* operations. Further accept() operations will be done automatically by
521
* the active socket when \a on_accept_complete() callback returns non-zero.
523
* @param asock The active socket.
524
* @param pool Pool used to allocate some internal data for the
527
* @return PJ_SUCCESS if the operation has been successful,
528
* or the appropriate error code on failure.
530
PJ_DECL(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock,
534
* Starts asynchronous socket connect() operation for this socket. Once
535
* the connection is done (either successfully or not), the
536
* \a on_connect_complete() callback will be called.
538
* @param asock The active socket.
539
* @param pool The pool to allocate some internal data for the
541
* @param remaddr Remote address.
542
* @param addr_len Length of the remote address.
544
* @return PJ_SUCCESS if connection can be established immediately,
545
* or PJ_EPENDING if connection cannot be established
546
* immediately. In this case the \a on_connect_complete()
547
* callback will be called when connection is complete.
548
* Any other return value indicates error condition.
550
PJ_DECL(pj_status_t) pj_activesock_start_connect(pj_activesock_t *asock,
552
const pj_sockaddr_t *remaddr,
556
#endif /* PJ_HAS_TCP */
564
#endif /* __PJ_ASYNCSOCK_H__ */