activesock.h 25 KB

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