Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame^] | 1 | /* $Header: /pjproject-0.3/pjlib/include/pj/ioqueue.h 10 10/29/05 11:29a Bennylp $ */ |
| 2 | |
| 3 | #ifndef __PJ_IOQUEUE_H__ |
| 4 | #define __PJ_IOQUEUE_H__ |
| 5 | |
| 6 | /** |
| 7 | * @file ioqueue.h |
| 8 | * @brief I/O Dispatching Mechanism |
| 9 | */ |
| 10 | |
| 11 | #include <pj/types.h> |
| 12 | |
| 13 | PJ_BEGIN_DECL |
| 14 | |
| 15 | /** |
| 16 | * @defgroup PJ_IO Network I/O |
| 17 | * @brief Network I/O |
| 18 | * @ingroup PJ_OS |
| 19 | * |
| 20 | * This section contains API building blocks to perform network I/O and |
| 21 | * communications. If provides: |
| 22 | * - @ref PJ_SOCK |
| 23 | *\n |
| 24 | * A highly portable socket abstraction, runs on all kind of |
| 25 | * network APIs such as standard BSD socket, Windows socket, Linux |
| 26 | * \b kernel socket, PalmOS networking API, etc. |
| 27 | * |
| 28 | * - @ref pj_addr_resolve |
| 29 | *\n |
| 30 | * Portable address resolution, which implements #pj_gethostbyname(). |
| 31 | * |
| 32 | * - @ref PJ_SOCK_SELECT |
| 33 | *\n |
| 34 | * A portable \a select() like API (#pj_sock_select()) which can be |
| 35 | * implemented with various back-ends. |
| 36 | * |
| 37 | * - @ref PJ_IOQUEUE |
| 38 | *\n |
| 39 | * Framework for dispatching network events. |
| 40 | * |
| 41 | * For more information see the modules below. |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * @defgroup PJ_IOQUEUE I/O Event Dispatching Queue |
| 46 | * @ingroup PJ_IO |
| 47 | * @{ |
| 48 | * |
| 49 | * This file provides abstraction for various event dispatching mechanisms. |
| 50 | * The interfaces for event dispatching vary alot, even in a single |
| 51 | * operating system. The abstraction here hopefully is suitable for most of |
| 52 | * the event dispatching available. |
| 53 | * |
| 54 | * Currently, the I/O Queue supports: |
| 55 | * - select(), as the common denominator, but the least efficient. |
| 56 | * - I/O Completion ports in Windows NT/2000/XP, which is the most efficient |
| 57 | * way to dispatch events in Windows NT based OSes, and most importantly, |
| 58 | * it doesn't have the limit on how many handles to monitor. And it works |
| 59 | * with files (not only sockets) as well. |
| 60 | * |
| 61 | * \section pj_ioqeuue_examples_sec Examples |
| 62 | * |
| 63 | * For some examples on how to use the I/O Queue, please see: |
| 64 | * |
| 65 | * - \ref page_pjlib_ioqueue_tcp_test |
| 66 | * - \ref page_pjlib_ioqueue_udp_test |
| 67 | * - \ref page_pjlib_ioqueue_perf_test |
| 68 | */ |
| 69 | |
| 70 | /** |
| 71 | * This structure describes the callbacks to be called when I/O operation |
| 72 | * completes. |
| 73 | */ |
| 74 | typedef struct pj_ioqueue_callback |
| 75 | { |
| 76 | /** |
| 77 | * This callback is called when #pj_ioqueue_read or #pj_ioqueue_recvfrom |
| 78 | * completes. |
| 79 | * |
| 80 | * @param key The key. |
| 81 | * @param bytes_read The size of data that has just been read. |
| 82 | */ |
| 83 | void (*on_read_complete)(pj_ioqueue_key_t *key, pj_ssize_t bytes_read); |
| 84 | |
| 85 | /** |
| 86 | * This callback is called when #pj_ioqueue_write or #pj_ioqueue_sendto |
| 87 | * completes. |
| 88 | * |
| 89 | * @param key The key. |
| 90 | * @param bytes_read The size of data that has just been read. |
| 91 | */ |
| 92 | void (*on_write_complete)(pj_ioqueue_key_t *key, pj_ssize_t bytes_sent); |
| 93 | |
| 94 | /** |
| 95 | * This callback is called when #pj_ioqueue_accept completes. |
| 96 | * |
| 97 | * @param key The key. |
| 98 | * @param sock Newly connected socket. |
| 99 | * @param status Zero if the operation completes successfully. |
| 100 | */ |
| 101 | void (*on_accept_complete)(pj_ioqueue_key_t *key, pj_sock_t sock, |
| 102 | int status); |
| 103 | |
| 104 | /** |
| 105 | * This callback is called when #pj_ioqueue_connect completes. |
| 106 | * |
| 107 | * @param key The key. |
| 108 | * @param status Zero if the operation completes successfully. |
| 109 | */ |
| 110 | void (*on_connect_complete)(pj_ioqueue_key_t *key, int status); |
| 111 | } pj_ioqueue_callback; |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Types of I/O Queue operation. |
| 116 | */ |
| 117 | typedef enum pj_ioqueue_operation_e |
| 118 | { |
| 119 | PJ_IOQUEUE_OP_NONE = 0, /**< No operation. */ |
| 120 | PJ_IOQUEUE_OP_READ = 1, /**< read() operation. */ |
| 121 | PJ_IOQUEUE_OP_RECV = 2, /**< recv() operation. */ |
| 122 | PJ_IOQUEUE_OP_RECV_FROM = 4, /**< recvfrom() operation. */ |
| 123 | PJ_IOQUEUE_OP_WRITE = 8, /**< write() operation. */ |
| 124 | PJ_IOQUEUE_OP_SEND = 16, /**< send() operation. */ |
| 125 | PJ_IOQUEUE_OP_SEND_TO = 32, /**< sendto() operation. */ |
| 126 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 127 | PJ_IOQUEUE_OP_ACCEPT = 64, /**< accept() operation. */ |
| 128 | PJ_IOQUEUE_OP_CONNECT = 128, /**< connect() operation. */ |
| 129 | #endif /* PJ_HAS_TCP */ |
| 130 | } pj_ioqueue_operation_e; |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Indicates that the I/O Queue should be created to handle reasonable |
| 135 | * number of threads. |
| 136 | */ |
| 137 | #define PJ_IOQUEUE_DEFAULT_THREADS 0 |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Create a new I/O Queue framework. |
| 142 | * |
| 143 | * @param pool The pool to allocate the I/O queue structure. |
| 144 | * @param max_fd The maximum number of handles to be supported, which |
| 145 | * should not exceed PJ_IOQUEUE_MAX_HANDLES. |
| 146 | * @param max_threads The maximum number of threads that are allowed to |
| 147 | * operate on a single descriptor simultaneously. If |
| 148 | * the value is zero, the framework will set it |
| 149 | * to a reasonable value. |
| 150 | * @param ioqueue Pointer to hold the newly created I/O Queue. |
| 151 | * |
| 152 | * @return PJ_SUCCESS on success. |
| 153 | */ |
| 154 | PJ_DECL(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 155 | pj_size_t max_fd, |
| 156 | int max_threads, |
| 157 | pj_ioqueue_t **ioqueue); |
| 158 | |
| 159 | /** |
| 160 | * Destroy the I/O queue. |
| 161 | * |
| 162 | * @param ioque The I/O Queue to be destroyed. |
| 163 | * |
| 164 | * @return PJ_SUCCESS if success. |
| 165 | */ |
| 166 | PJ_DECL(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioque ); |
| 167 | |
| 168 | /** |
| 169 | * Set the lock object to be used by the I/O Queue. This function can only |
| 170 | * be called right after the I/O queue is created, before any handle is |
| 171 | * registered to the I/O queue. |
| 172 | * |
| 173 | * Initially the I/O queue is created with non-recursive mutex protection. |
| 174 | * Applications can supply alternative lock to be used by calling this |
| 175 | * function. |
| 176 | * |
| 177 | * @param ioque The ioqueue instance. |
| 178 | * @param lock The lock to be used by the ioqueue. |
| 179 | * @param auto_delete In non-zero, the lock will be deleted by the ioqueue. |
| 180 | * |
| 181 | * @return PJ_SUCCESS or the appropriate error code. |
| 182 | */ |
| 183 | PJ_DECL(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioque, |
| 184 | pj_lock_t *lock, |
| 185 | pj_bool_t auto_delete ); |
| 186 | |
| 187 | /** |
| 188 | * Register a socket to the I/O queue framework. |
| 189 | * When a socket is registered to the IOQueue, it may be modified to use |
| 190 | * non-blocking IO. If it is modified, there is no guarantee that this |
| 191 | * modification will be restored after the socket is unregistered. |
| 192 | * |
| 193 | * @param pool To allocate the resource for the specified handle, |
| 194 | * which must be valid until the handle/key is unregistered |
| 195 | * from I/O Queue. |
| 196 | * @param ioque The I/O Queue. |
| 197 | * @param sock The socket. |
| 198 | * @param user_data User data to be associated with the key, which can be |
| 199 | * retrieved later. |
| 200 | * @param cb Callback to be called when I/O operation completes. |
| 201 | * @param key Pointer to receive the returned key. |
| 202 | * |
| 203 | * @return PJ_SUCCESS on success, or the error code. |
| 204 | */ |
| 205 | PJ_DECL(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool, |
| 206 | pj_ioqueue_t *ioque, |
| 207 | pj_sock_t sock, |
| 208 | void *user_data, |
| 209 | const pj_ioqueue_callback *cb, |
| 210 | pj_ioqueue_key_t **key); |
| 211 | |
| 212 | /** |
| 213 | * Unregister a handle from the I/O Queue framework. |
| 214 | * |
| 215 | * @param ioque The I/O Queue. |
| 216 | * @param key The key that uniquely identifies the handle, which is |
| 217 | * returned from the function #pj_ioqueue_register_sock() |
| 218 | * or other registration functions. |
| 219 | * |
| 220 | * @return PJ_SUCCESS on success or the error code. |
| 221 | */ |
| 222 | PJ_DECL(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_t *ioque, |
| 223 | pj_ioqueue_key_t *key ); |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Get user data associated with the I/O Queue key. |
| 228 | * |
| 229 | * @param key The key previously associated with the socket/handle with |
| 230 | * #pj_ioqueue_register_sock() (or other registration |
| 231 | * functions). |
| 232 | * |
| 233 | * @return The user data associated with the key, or NULL on error |
| 234 | * of if no data is associated with the key during |
| 235 | * registration. |
| 236 | */ |
| 237 | PJ_DECL(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key ); |
| 238 | |
| 239 | |
| 240 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 241 | /** |
| 242 | * Instruct I/O Queue to wait for incoming connections on the specified |
| 243 | * listening socket. This function will return |
| 244 | * immediately (i.e. non-blocking) regardless whether some data has been |
| 245 | * transfered. If the function can't complete immediately, and the caller will |
| 246 | * be notified about the completion when it calls pj_ioqueue_poll(). |
| 247 | * |
| 248 | * @param ioqueue The I/O Queue |
| 249 | * @param key The key which registered to the server socket. |
| 250 | * @param sock Argument which contain pointer to receive |
| 251 | * the socket for the incoming connection. |
| 252 | * @param local Optional argument which contain pointer to variable to |
| 253 | * receive local address. |
| 254 | * @param remote Optional argument which contain pointer to variable to |
| 255 | * receive the remote address. |
| 256 | * @param addrlen On input, contains the length of the buffer for the |
| 257 | * address, and on output, contains the actual length of the |
| 258 | * address. This argument is optional. |
| 259 | * @return |
| 260 | * - PJ_SUCCESS If there's a connection available immediately, which |
| 261 | * in this case the callback should have been called before |
| 262 | * the function returns. |
| 263 | * - PJ_EPENDING If accept is queued, or |
| 264 | * - non-zero which indicates the appropriate error code. |
| 265 | */ |
| 266 | PJ_DECL(pj_status_t) pj_ioqueue_accept( pj_ioqueue_t *ioqueue, |
| 267 | pj_ioqueue_key_t *key, |
| 268 | pj_sock_t *sock, |
| 269 | pj_sockaddr_t *local, |
| 270 | pj_sockaddr_t *remote, |
| 271 | int *addrlen ); |
| 272 | |
| 273 | /** |
| 274 | * Initiate non-blocking socket connect. If the socket can NOT be connected |
| 275 | * immediately, the result will be reported during poll. |
| 276 | * |
| 277 | * @param ioqueue The ioqueue |
| 278 | * @param key The key associated with TCP socket |
| 279 | * @param addr The remote address. |
| 280 | * @param addrlen The remote address length. |
| 281 | * |
| 282 | * @return |
| 283 | * - PJ_SUCCESS If socket is connected immediately, which in this case |
| 284 | * the callback should have been called. |
| 285 | * - PJ_EPENDING If operation is queued, or |
| 286 | * - non-zero Indicates the error code. |
| 287 | */ |
| 288 | PJ_DECL(pj_status_t) pj_ioqueue_connect( pj_ioqueue_t *ioqueue, |
| 289 | pj_ioqueue_key_t *key, |
| 290 | const pj_sockaddr_t *addr, |
| 291 | int addrlen ); |
| 292 | |
| 293 | #endif /* PJ_HAS_TCP */ |
| 294 | |
| 295 | /** |
| 296 | * Poll the I/O Queue for completed events. |
| 297 | * |
| 298 | * @param ioque the I/O Queue. |
| 299 | * @param timeout polling timeout, or NULL if the thread wishes to wait |
| 300 | * indefinetely for the event. |
| 301 | * |
| 302 | * @return |
| 303 | * - zero if timed out (no event). |
| 304 | * - (<0) if error occured during polling. Callback will NOT be called. |
| 305 | * - (>1) to indicate numbers of events. Callbacks have been called. |
| 306 | */ |
| 307 | PJ_DECL(int) pj_ioqueue_poll( pj_ioqueue_t *ioque, |
| 308 | const pj_time_val *timeout); |
| 309 | |
| 310 | /** |
| 311 | * Instruct the I/O Queue to read from the specified handle. This function |
| 312 | * returns immediately (i.e. non-blocking) regardless whether some data has |
| 313 | * been transfered. If the operation can't complete immediately, caller will |
| 314 | * be notified about the completion when it calls pj_ioqueue_poll(). |
| 315 | * |
| 316 | * @param ioque The I/O Queue. |
| 317 | * @param key The key that uniquely identifies the handle. |
| 318 | * @param buffer The buffer to hold the read data. The caller MUST make sure |
| 319 | * that this buffer remain valid until the framework completes |
| 320 | * reading the handle. |
| 321 | * @param buflen The maximum size to be read. |
| 322 | * |
| 323 | * @return |
| 324 | * - PJ_SUCCESS If immediate data has been received. In this case, the |
| 325 | * callback must have been called before this function |
| 326 | * returns, and no pending operation is scheduled. |
| 327 | * - PJ_EPENDING If the operation has been queued. |
| 328 | * - non-zero The return value indicates the error code. |
| 329 | */ |
| 330 | PJ_DECL(pj_status_t) pj_ioqueue_read( pj_ioqueue_t *ioque, |
| 331 | pj_ioqueue_key_t *key, |
| 332 | void *buffer, |
| 333 | pj_size_t buflen); |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * This function behaves similarly as #pj_ioqueue_read(), except that it is |
| 338 | * normally called for socket. |
| 339 | * |
| 340 | * @param ioque The I/O Queue. |
| 341 | * @param key The key that uniquely identifies the handle. |
| 342 | * @param buffer The buffer to hold the read data. The caller MUST make sure |
| 343 | * that this buffer remain valid until the framework completes |
| 344 | * reading the handle. |
| 345 | * @param buflen The maximum size to be read. |
| 346 | * @param flags Recv flag. |
| 347 | * |
| 348 | * @return |
| 349 | * - PJ_SUCCESS If immediate data has been received. In this case, the |
| 350 | * callback must have been called before this function |
| 351 | * returns, and no pending operation is scheduled. |
| 352 | * - PJ_EPENDING If the operation has been queued. |
| 353 | * - non-zero The return value indicates the error code. |
| 354 | */ |
| 355 | PJ_DECL(pj_status_t) pj_ioqueue_recv( pj_ioqueue_t *ioque, |
| 356 | pj_ioqueue_key_t *key, |
| 357 | void *buffer, |
| 358 | pj_size_t buflen, |
| 359 | unsigned flags ); |
| 360 | |
| 361 | /** |
| 362 | * This function behaves similarly as #pj_ioqueue_read(), except that it is |
| 363 | * normally called for socket, and the remote address will also be returned |
| 364 | * along with the data. Caller MUST make sure that both buffer and addr |
| 365 | * remain valid until the framework completes reading the data. |
| 366 | * |
| 367 | * @param ioque The I/O Queue. |
| 368 | * @param key The key that uniquely identifies the handle. |
| 369 | * @param buffer The buffer to hold the read data. The caller MUST make sure |
| 370 | * that this buffer remain valid until the framework completes |
| 371 | * reading the handle. |
| 372 | * @param buflen The maximum size to be read. |
| 373 | * @param flags Recv flag. |
| 374 | * @param addr Pointer to buffer to receive the address, or NULL. |
| 375 | * @param addrlen On input, specifies the length of the address buffer. |
| 376 | * On output, it will be filled with the actual length of |
| 377 | * the address. |
| 378 | * |
| 379 | * @return |
| 380 | * - PJ_SUCCESS If immediate data has been received. In this case, the |
| 381 | * callback must have been called before this function |
| 382 | * returns, and no pending operation is scheduled. |
| 383 | * - PJ_EPENDING If the operation has been queued. |
| 384 | * - non-zero The return value indicates the error code. |
| 385 | */ |
| 386 | PJ_DECL(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_t *ioque, |
| 387 | pj_ioqueue_key_t *key, |
| 388 | void *buffer, |
| 389 | pj_size_t buflen, |
| 390 | unsigned flags, |
| 391 | pj_sockaddr_t *addr, |
| 392 | int *addrlen); |
| 393 | |
| 394 | /** |
| 395 | * Instruct the I/O Queue to write to the handle. This function will return |
| 396 | * immediately (i.e. non-blocking) regardless whether some data has been |
| 397 | * transfered. If the function can't complete immediately, and the caller will |
| 398 | * be notified about the completion when it calls pj_ioqueue_poll(). |
| 399 | * |
| 400 | * @param ioque the I/O Queue. |
| 401 | * @param key the key that identifies the handle. |
| 402 | * @param data the data to send. Caller MUST make sure that this buffer |
| 403 | * remains valid until the write operation completes. |
| 404 | * @param datalen the length of the data. |
| 405 | * |
| 406 | * @return |
| 407 | * - PJ_SUCCESS If data was immediately written. |
| 408 | * - PJ_EPENDING If the operation has been queued. |
| 409 | * - non-zero The return value indicates the error code. |
| 410 | */ |
| 411 | PJ_DECL(pj_status_t) pj_ioqueue_write( pj_ioqueue_t *ioque, |
| 412 | pj_ioqueue_key_t *key, |
| 413 | const void *data, |
| 414 | pj_size_t datalen); |
| 415 | |
| 416 | /** |
| 417 | * This function behaves similarly as #pj_ioqueue_write(), except that |
| 418 | * pj_sock_send() (or equivalent) will be called to send the data. |
| 419 | * |
| 420 | * @param ioque the I/O Queue. |
| 421 | * @param key the key that identifies the handle. |
| 422 | * @param data the data to send. Caller MUST make sure that this buffer |
| 423 | * remains valid until the write operation completes. |
| 424 | * @param datalen the length of the data. |
| 425 | * @param flags send flags. |
| 426 | * |
| 427 | * @return |
| 428 | * - PJ_SUCCESS If data was immediately written. |
| 429 | * - PJ_EPENDING If the operation has been queued. |
| 430 | * - non-zero The return value indicates the error code. |
| 431 | */ |
| 432 | PJ_DECL(pj_status_t) pj_ioqueue_send( pj_ioqueue_t *ioque, |
| 433 | pj_ioqueue_key_t *key, |
| 434 | const void *data, |
| 435 | pj_size_t datalen, |
| 436 | unsigned flags ); |
| 437 | |
| 438 | |
| 439 | /** |
| 440 | * This function behaves similarly as #pj_ioqueue_write(), except that |
| 441 | * pj_sock_sendto() (or equivalent) will be called to send the data. |
| 442 | * |
| 443 | * @param ioque the I/O Queue. |
| 444 | * @param key the key that identifies the handle. |
| 445 | * @param data the data to send. Caller MUST make sure that this buffer |
| 446 | * remains valid until the write operation completes. |
| 447 | * @param datalen the length of the data. |
| 448 | * @param flags send flags. |
| 449 | * @param addr remote address. |
| 450 | * @param addrlen remote address length. |
| 451 | * |
| 452 | * @return |
| 453 | * - PJ_SUCCESS If data was immediately written. |
| 454 | * - PJ_EPENDING If the operation has been queued. |
| 455 | * - non-zero The return value indicates the error code. |
| 456 | */ |
| 457 | PJ_DECL(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_t *ioque, |
| 458 | pj_ioqueue_key_t *key, |
| 459 | const void *data, |
| 460 | pj_size_t datalen, |
| 461 | unsigned flags, |
| 462 | const pj_sockaddr_t *addr, |
| 463 | int addrlen); |
| 464 | |
| 465 | |
| 466 | /** |
| 467 | * !} |
| 468 | */ |
| 469 | |
| 470 | PJ_END_DECL |
| 471 | |
| 472 | #endif /* __PJ_IOQUEUE_H__ */ |
| 473 | |