Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2007 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 | /** |
| 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(). |
| 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 | */ |
| 173 | typedef 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 | |
| 204 | } pj_activesock_cfg; |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Initialize the active socket configuration with the default values. |
| 209 | * |
| 210 | * @param cfg The configuration to be initialized. |
| 211 | */ |
| 212 | PJ_DECL(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg); |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * Create the active socket for the specified socket. This will register |
| 217 | * the socket to the specified ioqueue. |
| 218 | * |
| 219 | * @param pool Pool to allocate memory from. |
| 220 | * @param sock The socket handle. |
| 221 | * @param sock_type Specify socket type, either pj_SOCK_DGRAM() or |
| 222 | * pj_SOCK_STREAM(). The active socket needs this |
| 223 | * information to handle connection closure for |
| 224 | * connection oriented sockets. |
| 225 | * @param ioqueue The ioqueue to use. |
| 226 | * @param opt Optional settings. When this setting is not specifed, |
| 227 | * the default values will be used. |
| 228 | * @param cb Pointer to structure containing application |
| 229 | * callbacks. |
| 230 | * @param p_asock Pointer to receive the active socket instance. |
| 231 | * |
| 232 | * @return PJ_SUCCESS if the operation has been successful, |
| 233 | * or the appropriate error code on failure. |
| 234 | */ |
| 235 | PJ_DECL(pj_status_t) pj_activesock_create(pj_pool_t *pool, |
| 236 | pj_sock_t sock, |
| 237 | int sock_type, |
| 238 | const pj_activesock_cfg *opt, |
| 239 | pj_ioqueue_t *ioqueue, |
| 240 | const pj_activesock_cb *cb, |
| 241 | pj_activesock_t **p_asock); |
| 242 | |
| 243 | /** |
| 244 | * Create UDP socket descriptor, bind it to the specified address, and |
| 245 | * create the active socket for the socket descriptor. |
| 246 | * |
| 247 | * @param pool Pool to allocate memory from. |
| 248 | * @param addr Specifies the address family of the socket and the |
| 249 | * address where the socket should be bound to. If |
| 250 | * this argument is NULL, then AF_INET is assumed and |
| 251 | * the socket will be bound to any addresses and port. |
| 252 | * @param opt Optional settings. When this setting is not specifed, |
| 253 | * the default values will be used. |
| 254 | * @param cb Pointer to structure containing application |
| 255 | * callbacks. |
| 256 | * @param p_asock Pointer to receive the active socket instance. |
| 257 | * @param bound_addr If this argument is specified, it will be filled with |
| 258 | * the bound address on return. |
| 259 | * |
| 260 | * @return PJ_SUCCESS if the operation has been successful, |
| 261 | * or the appropriate error code on failure. |
| 262 | */ |
| 263 | PJ_DECL(pj_status_t) pj_activesock_create_udp(pj_pool_t *pool, |
| 264 | const pj_sockaddr *addr, |
| 265 | const pj_activesock_cfg *opt, |
| 266 | pj_ioqueue_t *ioqueue, |
| 267 | const pj_activesock_cb *cb, |
| 268 | pj_activesock_t **p_asock, |
| 269 | pj_sockaddr *bound_addr); |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Close the active socket. This will unregister the socket from the |
| 274 | * ioqueue and ultimately close the socket. |
| 275 | * |
| 276 | * @param asock The active socket. |
| 277 | * |
| 278 | * @return PJ_SUCCESS if the operation has been successful, |
| 279 | * or the appropriate error code on failure. |
| 280 | */ |
| 281 | PJ_DECL(pj_status_t) pj_activesock_close(pj_activesock_t *asock); |
| 282 | |
| 283 | |
| 284 | /** |
| 285 | * Associate arbitrary data with the active socket. Application may |
| 286 | * inspect this data in the callbacks and associate it with higher |
| 287 | * level processing. |
| 288 | * |
| 289 | * @param asock The active socket. |
| 290 | * @param user_data The user data to be associated with the active |
| 291 | * socket. |
| 292 | * |
| 293 | * @return PJ_SUCCESS if the operation has been successful, |
| 294 | * or the appropriate error code on failure. |
| 295 | */ |
| 296 | PJ_DECL(pj_status_t) pj_activesock_set_user_data(pj_activesock_t *asock, |
| 297 | void *user_data); |
| 298 | |
| 299 | /** |
| 300 | * Retrieve the user data previously associated with this active |
| 301 | * socket. |
| 302 | * |
| 303 | * @param asock The active socket. |
| 304 | * |
| 305 | * @return The user data. |
| 306 | */ |
| 307 | PJ_DECL(void*) pj_activesock_get_user_data(pj_activesock_t *asock); |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * Starts read operation on this active socket. This function will create |
| 312 | * \a async_cnt number of buffers (the \a async_cnt parameter was given |
| 313 | * in \a pj_activesock_create() function) where each buffer is \a buff_size |
| 314 | * long. The buffers are allocated from the specified \a pool. Once the |
| 315 | * buffers are created, it then issues \a async_cnt number of asynchronous |
| 316 | * \a recv() operations to the socket and returns back to caller. Incoming |
| 317 | * data on the socket will be reported back to application via the |
| 318 | * \a on_data_read() callback. |
| 319 | * |
| 320 | * Application only needs to call this function once to initiate read |
| 321 | * operations. Further read operations will be done automatically by the |
| 322 | * active socket when \a on_data_read() callback returns non-zero. |
| 323 | * |
| 324 | * @param asock The active socket. |
| 325 | * @param pool Pool used to allocate buffers for incoming data. |
| 326 | * @param buff_size The size of each buffer, in bytes. |
| 327 | * @param flags Flags to be given to pj_ioqueue_recv(). |
| 328 | * |
| 329 | * @return PJ_SUCCESS if the operation has been successful, |
| 330 | * or the appropriate error code on failure. |
| 331 | */ |
| 332 | PJ_DECL(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock, |
| 333 | pj_pool_t *pool, |
| 334 | unsigned buff_size, |
| 335 | pj_uint32_t flags); |
| 336 | |
| 337 | /** |
| 338 | * Same as pj_activesock_start_read(), except that this function is used |
| 339 | * only for datagram sockets, and it will trigger \a on_data_recvfrom() |
| 340 | * callback instead. |
| 341 | * |
| 342 | * @param asock The active socket. |
| 343 | * @param pool Pool used to allocate buffers for incoming data. |
| 344 | * @param buff_size The size of each buffer, in bytes. |
| 345 | * @param flags Flags to be given to pj_ioqueue_recvfrom(). |
| 346 | * |
| 347 | * @return PJ_SUCCESS if the operation has been successful, |
| 348 | * or the appropriate error code on failure. |
| 349 | */ |
| 350 | PJ_DECL(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock, |
| 351 | pj_pool_t *pool, |
| 352 | unsigned buff_size, |
| 353 | pj_uint32_t flags); |
| 354 | |
| 355 | /** |
| 356 | * Send data using the socket. |
| 357 | * |
| 358 | * @param asock The active socket. |
| 359 | * @param send_key The operation key to send the data, which is useful |
| 360 | * if application wants to submit multiple pending |
| 361 | * send operations and want to track which exact data |
| 362 | * has been sent in the \a on_data_sent() callback. |
| 363 | * @param data The data to be sent. This data must remain valid |
| 364 | * until the data has been sent. |
| 365 | * @param size The size of the data. |
| 366 | * @param flags Flags to be given to pj_ioqueue_send(). |
| 367 | * |
| 368 | * |
| 369 | * @return PJ_SUCCESS if data has been sent immediately, or |
| 370 | * PJ_EPENDING if data cannot be sent immediately. In |
| 371 | * this case the \a on_data_sent() callback will be |
| 372 | * called when data is actually sent. Any other return |
| 373 | * value indicates error condition. |
| 374 | */ |
| 375 | PJ_DECL(pj_status_t) pj_activesock_send(pj_activesock_t *asock, |
| 376 | pj_ioqueue_op_key_t *send_key, |
| 377 | const void *data, |
| 378 | pj_ssize_t *size, |
| 379 | unsigned flags); |
| 380 | |
| 381 | /** |
| 382 | * Send datagram using the socket. |
| 383 | * |
| 384 | * @param asock The active socket. |
| 385 | * @param send_key The operation key to send the data, which is useful |
| 386 | * if application wants to submit multiple pending |
| 387 | * send operations and want to track which exact data |
| 388 | * has been sent in the \a on_data_sent() callback. |
| 389 | * @param data The data to be sent. This data must remain valid |
| 390 | * until the data has been sent. |
| 391 | * @param size The size of the data. |
| 392 | * @param flags Flags to be given to pj_ioqueue_send(). |
| 393 | * @param addr The destination address. |
| 394 | * @param addr_len The length of the address. |
| 395 | * |
| 396 | * @return PJ_SUCCESS if data has been sent immediately, or |
| 397 | * PJ_EPENDING if data cannot be sent immediately. In |
| 398 | * this case the \a on_data_sent() callback will be |
| 399 | * called when data is actually sent. Any other return |
| 400 | * value indicates error condition. |
| 401 | */ |
| 402 | PJ_DECL(pj_status_t) pj_activesock_sendto(pj_activesock_t *asock, |
| 403 | pj_ioqueue_op_key_t *send_key, |
| 404 | const void *data, |
| 405 | pj_ssize_t *size, |
| 406 | unsigned flags, |
| 407 | const pj_sockaddr_t *addr, |
| 408 | int addr_len); |
| 409 | |
| 410 | /** |
| 411 | * Starts asynchronous socket accept() operations on this active socket. |
| 412 | * Application must bind the socket before calling this function. This |
| 413 | * function will issue \a async_cnt number of asynchronous \a accept() |
| 414 | * operations to the socket and returns back to caller. Incoming |
| 415 | * connection on the socket will be reported back to application via the |
| 416 | * \a on_accept_complete() callback. |
| 417 | * |
| 418 | * Application only needs to call this function once to initiate accept() |
| 419 | * operations. Further accept() operations will be done automatically by |
| 420 | * the active socket when \a on_accept_complete() callback returns non-zero. |
| 421 | * |
| 422 | * @param asock The active socket. |
| 423 | * @param pool Pool used to allocate some internal data for the |
| 424 | * operation. |
| 425 | * |
| 426 | * @return PJ_SUCCESS if the operation has been successful, |
| 427 | * or the appropriate error code on failure. |
| 428 | */ |
| 429 | PJ_DECL(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock, |
| 430 | pj_pool_t *pool); |
| 431 | |
| 432 | /** |
| 433 | * Starts asynchronous socket connect() operation for this socket. Once |
| 434 | * the connection is done (either successfully or not), the |
| 435 | * \a on_connect_complete() callback will be called. |
| 436 | * |
| 437 | * @param asock The active socket. |
| 438 | * @param pool The pool to allocate some internal data for the |
| 439 | * operation. |
| 440 | * @param remaddr Remote address. |
| 441 | * @param addr_len Length of the remote address. |
| 442 | * |
| 443 | * @return PJ_SUCCESS if connection can be established immediately, |
| 444 | * or PJ_EPENDING if connection cannot be established |
| 445 | * immediately. In this case the \a on_connect_complete() |
| 446 | * callback will be called when connection is complete. |
| 447 | * Any other return value indicates error condition. |
| 448 | */ |
| 449 | PJ_DECL(pj_status_t) pj_activesock_start_connect(pj_activesock_t *asock, |
| 450 | pj_pool_t *pool, |
| 451 | const pj_sockaddr_t *remaddr, |
| 452 | int addr_len); |
| 453 | |
| 454 | |
| 455 | /** |
| 456 | * @} |
| 457 | */ |
| 458 | |
| 459 | PJ_END_DECL |
| 460 | |
| 461 | #endif /* __PJ_ASYNCSOCK_H__ */ |
| 462 | |