blob: d8df170eda1ad574adb16336a61a0c0a700fbee3 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$
2 */
3/*
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00005 *
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.
10 *
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.
15 *
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
19 */
20#ifndef __PJ_IOQUEUE_H__
21#define __PJ_IOQUEUE_H__
22
23/**
24 * @file ioqueue.h
25 * @brief I/O Dispatching Mechanism
26 */
27
28#include <pj/types.h>
29
30PJ_BEGIN_DECL
31
32/**
33 * @defgroup PJ_IO Input/Output
34 * @brief Input/Output
35 * @ingroup PJ_OS
36 *
37 * This section contains API building blocks to perform network I/O and
38 * communications. If provides:
39 * - @ref PJ_SOCK
40 *\n
41 * A highly portable socket abstraction, runs on all kind of
42 * network APIs such as standard BSD socket, Windows socket, Linux
43 * \b kernel socket, PalmOS networking API, etc.
44 *
45 * - @ref pj_addr_resolve
46 *\n
47 * Portable address resolution, which implements #pj_gethostbyname().
48 *
49 * - @ref PJ_SOCK_SELECT
50 *\n
51 * A portable \a select() like API (#pj_sock_select()) which can be
52 * implemented with various back-ends.
53 *
54 * - @ref PJ_IOQUEUE
55 *\n
56 * Framework for dispatching network events.
57 *
58 * For more information see the modules below.
59 */
60
61/**
Benny Prijonoa3cbb1c2006-08-25 12:41:05 +000062 * @defgroup PJ_IOQUEUE IOQueue: I/O Event Dispatching with Proactor Pattern
Benny Prijono9033e312005-11-21 02:08:39 +000063 * @ingroup PJ_IO
64 * @{
65 *
66 * I/O Queue provides API for performing asynchronous I/O operations. It
67 * conforms to proactor pattern, which allows application to submit an
68 * asynchronous operation and to be notified later when the operation has
69 * completed.
70 *
71 * The I/O Queue can work on both socket and file descriptors. For
72 * asynchronous file operations however, one must make sure that the correct
73 * file I/O back-end is used, because not all file I/O back-end can be
74 * used with the ioqueue. Please see \ref PJ_FILE_IO for more details.
75 *
76 * The framework works natively in platforms where asynchronous operation API
77 * exists, such as in Windows NT with IoCompletionPort/IOCP. In other
78 * platforms, the I/O queue abstracts the operating system's event poll API
79 * to provide semantics similar to IoCompletionPort with minimal penalties
80 * (i.e. per ioqueue and per handle mutex protection).
81 *
82 * The I/O queue provides more than just unified abstraction. It also:
83 * - makes sure that the operation uses the most effective way to utilize
84 * the underlying mechanism, to achieve the maximum theoritical
85 * throughput possible on a given platform.
86 * - choose the most efficient mechanism for event polling on a given
87 * platform.
88 *
89 * Currently, the I/O Queue is implemented using:
90 * - <tt><b>select()</b></tt>, as the common denominator, but the least
91 * efficient. Also the number of descriptor is limited to
92 * \c PJ_IOQUEUE_MAX_HANDLES (which by default is 64).
93 * - <tt><b>/dev/epoll</b></tt> on Linux (user mode and kernel mode),
94 * a much faster replacement for select() on Linux (and more importantly
95 * doesn't have limitation on number of descriptors).
96 * - <b>I/O Completion ports</b> on Windows NT/2000/XP, which is the most
97 * efficient way to dispatch events in Windows NT based OSes, and most
98 * importantly, it doesn't have the limit on how many handles to monitor.
99 * And it works with files (not only sockets) as well.
100 *
101 *
102 * \section pj_ioqueue_concurrency_sec Concurrency Rules
103 *
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000104 * The ioqueue has been fine tuned to allow multiple threads to poll the
105 * handles simultaneously, to maximize scalability when the application is
106 * running on multiprocessor systems. When more than one threads are polling
107 * the ioqueue and there are more than one handles are signaled, more than
108 * one threads will execute the callback simultaneously to serve the events.
109 * These parallel executions are completely safe when the events happen for
110 * two different handles.
111 *
112 * However, with multithreading, care must be taken when multiple events
113 * happen on the same handle, or when event is happening on a handle (and
114 * the callback is being executed) and application is performing
115 * unregistration to the handle at the same time.
116 *
117 * The treatments of above scenario differ according to the concurrency
118 * setting that are applied to the handle.
119 *
120 * \subsection pj_ioq_concur_set Concurrency Settings for Handles
121 *
122 * Concurrency can be set on per handle (key) basis, by using
123 * #pj_ioqueue_set_concurrency() function. The default key concurrency value
124 * for the handle is inherited from the key concurrency setting of the ioqueue,
125 * and the key concurrency setting for the ioqueue can be changed by using
126 * #pj_ioqueue_set_default_concurrency(). The default key concurrency setting
127 * for ioqueue itself is controlled by compile time setting
128 * PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY.
129 *
130 * Note that this key concurrency setting only controls whether multiple
131 * threads are allowed to operate <b>on the same key</b> at the same time.
132 * The ioqueue itself always allows multiple threads to enter the ioqeuue at
133 * the same time, and also simultaneous callback calls to <b>differrent
134 * keys</b> is always allowed regardless to the key concurrency setting.
135 *
136 * \subsection pj_ioq_parallel Parallel Callback Executions for the Same Handle
137 *
138 * Note that when key concurrency is enabled (i.e. parallel callback calls on
139 * the same key is allowed; this is the default setting), the ioqueue will only
140 * perform simultaneous callback executions on the same key when the key has
141 * invoked multiple pending operations. This could be done for example by
142 * calling #pj_ioqueue_recvfrom() more than once on the same key, each with
143 * the same key but different operation key (pj_ioqueue_op_key_t). With this
144 * scenario, when multiple packets arrive on the key at the same time, more
145 * than one threads may execute the callback simultaneously, each with the
146 * same key but different operation key.
147 *
148 * When there is only one pending operation on the key (e.g. there is only one
149 * #pj_ioqueue_recvfrom() invoked on the key), then events occuring to the
150 * same key will be queued by the ioqueue, thus no simultaneous callback calls
151 * will be performed.
152 *
153 * \subsection pj_ioq_allow_concur Concurrency is Enabled (Default Value)
154 *
155 * The default setting for the ioqueue is to allow multiple threads to
156 * execute callbacks for the same handle/key. This setting is selected to
157 * promote good performance and scalability for application.
158 *
159 * However this setting has a major drawback with regard to synchronization,
160 * and application MUST carefully follow the following guidelines to ensure
161 * that parallel access to the key does not cause problems:
162 *
163 * - Always note that callback may be called simultaneously for the same
164 * key.
165 * - <b>Care must be taken when unregistering a key</b> from the
Benny Prijono9033e312005-11-21 02:08:39 +0000166 * ioqueue. Application must take care that when one thread is issuing
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000167 * an unregistration, other thread is not simultaneously invoking the
168 * callback <b>to the same key</b>.
Benny Prijono9033e312005-11-21 02:08:39 +0000169 *\n
170 * This happens because the ioqueue functions are working with a pointer
171 * to the key, and there is a possible race condition where the pointer
172 * has been rendered invalid by other threads before the ioqueue has a
173 * chance to acquire mutex on it.
174 *
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000175 * \subsection pj_ioq_disallow_concur Concurrency is Disabled
176 *
177 * Alternatively, application may disable key concurrency to make
178 * synchronization easier. As noted above, there are three ways to control
179 * key concurrency setting:
180 * - by controlling on per handle/key basis, with #pj_ioqueue_set_concurrency().
181 * - by changing default key concurrency setting on the ioqueue, with
182 * #pj_ioqueue_set_default_concurrency().
183 * - by changing the default concurrency on compile time, by declaring
184 * PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY macro to zero in your config_site.h
185 *
Benny Prijono9033e312005-11-21 02:08:39 +0000186 * \section pj_ioqeuue_examples_sec Examples
187 *
188 * For some examples on how to use the I/O Queue, please see:
189 *
190 * - \ref page_pjlib_ioqueue_tcp_test
191 * - \ref page_pjlib_ioqueue_udp_test
192 * - \ref page_pjlib_ioqueue_perf_test
193 */
194
195
196/**
197 * This structure describes operation specific key to be submitted to
198 * I/O Queue when performing the asynchronous operation. This key will
199 * be returned to the application when completion callback is called.
200 *
201 * Application normally wants to attach it's specific data in the
202 * \c user_data field so that it can keep track of which operation has
203 * completed when the callback is called. Alternatively, application can
204 * also extend this struct to include its data, because the pointer that
205 * is returned in the completion callback will be exactly the same as
206 * the pointer supplied when the asynchronous function is called.
207 */
208typedef struct pj_ioqueue_op_key_t
209{
210 void *internal__[32]; /**< Internal I/O Queue data. */
211 void *user_data; /**< Application data. */
212} pj_ioqueue_op_key_t;
213
214/**
215 * This structure describes the callbacks to be called when I/O operation
216 * completes.
217 */
218typedef struct pj_ioqueue_callback
219{
220 /**
221 * This callback is called when #pj_ioqueue_recv or #pj_ioqueue_recvfrom
222 * completes.
223 *
224 * @param key The key.
225 * @param op_key Operation key.
226 * @param bytes_read >= 0 to indicate the amount of data read,
227 * otherwise negative value containing the error
228 * code. To obtain the pj_status_t error code, use
229 * (pj_status_t code = -bytes_read).
230 */
231 void (*on_read_complete)(pj_ioqueue_key_t *key,
232 pj_ioqueue_op_key_t *op_key,
233 pj_ssize_t bytes_read);
234
235 /**
Benny Prijonoa7b376b2008-01-25 16:06:33 +0000236 * This callback is called when #pj_ioqueue_send or #pj_ioqueue_sendto
Benny Prijono9033e312005-11-21 02:08:39 +0000237 * completes.
238 *
239 * @param key The key.
240 * @param op_key Operation key.
241 * @param bytes_sent >= 0 to indicate the amount of data written,
242 * otherwise negative value containing the error
243 * code. To obtain the pj_status_t error code, use
244 * (pj_status_t code = -bytes_sent).
245 */
246 void (*on_write_complete)(pj_ioqueue_key_t *key,
247 pj_ioqueue_op_key_t *op_key,
248 pj_ssize_t bytes_sent);
249
250 /**
251 * This callback is called when #pj_ioqueue_accept completes.
252 *
253 * @param key The key.
254 * @param op_key Operation key.
255 * @param sock Newly connected socket.
256 * @param status Zero if the operation completes successfully.
257 */
258 void (*on_accept_complete)(pj_ioqueue_key_t *key,
259 pj_ioqueue_op_key_t *op_key,
260 pj_sock_t sock,
261 pj_status_t status);
262
263 /**
264 * This callback is called when #pj_ioqueue_connect completes.
265 *
266 * @param key The key.
267 * @param status PJ_SUCCESS if the operation completes successfully.
268 */
269 void (*on_connect_complete)(pj_ioqueue_key_t *key,
270 pj_status_t status);
271} pj_ioqueue_callback;
272
273
274/**
275 * Types of pending I/O Queue operation. This enumeration is only used
276 * internally within the ioqueue.
277 */
278typedef enum pj_ioqueue_operation_e
279{
280 PJ_IOQUEUE_OP_NONE = 0, /**< No operation. */
281 PJ_IOQUEUE_OP_READ = 1, /**< read() operation. */
282 PJ_IOQUEUE_OP_RECV = 2, /**< recv() operation. */
283 PJ_IOQUEUE_OP_RECV_FROM = 4, /**< recvfrom() operation. */
284 PJ_IOQUEUE_OP_WRITE = 8, /**< write() operation. */
285 PJ_IOQUEUE_OP_SEND = 16, /**< send() operation. */
286 PJ_IOQUEUE_OP_SEND_TO = 32, /**< sendto() operation. */
287#if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
288 PJ_IOQUEUE_OP_ACCEPT = 64, /**< accept() operation. */
Benny Prijono92ac4472006-07-22 13:42:56 +0000289 PJ_IOQUEUE_OP_CONNECT = 128 /**< connect() operation. */
Benny Prijono9033e312005-11-21 02:08:39 +0000290#endif /* PJ_HAS_TCP */
291} pj_ioqueue_operation_e;
292
293
294/**
295 * This macro specifies the maximum number of events that can be
296 * processed by the ioqueue on a single poll cycle, on implementation
297 * that supports it. The value is only meaningfull when specified
298 * during PJLIB build.
299 */
300#ifndef PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL
301# define PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL (16)
302#endif
303
304/**
305 * When this flag is specified in ioqueue's recv() or send() operations,
306 * the ioqueue will always mark the operation as asynchronous.
307 */
308#define PJ_IOQUEUE_ALWAYS_ASYNC ((pj_uint32_t)1 << (pj_uint32_t)31)
309
310/**
311 * Return the name of the ioqueue implementation.
312 *
313 * @return Implementation name.
314 */
315PJ_DECL(const char*) pj_ioqueue_name(void);
316
317
318/**
319 * Create a new I/O Queue framework.
320 *
321 * @param pool The pool to allocate the I/O queue structure.
322 * @param max_fd The maximum number of handles to be supported, which
323 * should not exceed PJ_IOQUEUE_MAX_HANDLES.
324 * @param ioqueue Pointer to hold the newly created I/O Queue.
325 *
326 * @return PJ_SUCCESS on success.
327 */
328PJ_DECL(pj_status_t) pj_ioqueue_create( pj_pool_t *pool,
329 pj_size_t max_fd,
330 pj_ioqueue_t **ioqueue);
331
332/**
333 * Destroy the I/O queue.
334 *
335 * @param ioque The I/O Queue to be destroyed.
336 *
337 * @return PJ_SUCCESS if success.
338 */
339PJ_DECL(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioque );
340
341/**
342 * Set the lock object to be used by the I/O Queue. This function can only
343 * be called right after the I/O queue is created, before any handle is
344 * registered to the I/O queue.
345 *
346 * Initially the I/O queue is created with non-recursive mutex protection.
347 * Applications can supply alternative lock to be used by calling this
348 * function.
349 *
350 * @param ioque The ioqueue instance.
351 * @param lock The lock to be used by the ioqueue.
352 * @param auto_delete In non-zero, the lock will be deleted by the ioqueue.
353 *
354 * @return PJ_SUCCESS or the appropriate error code.
355 */
356PJ_DECL(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioque,
357 pj_lock_t *lock,
358 pj_bool_t auto_delete );
359
360/**
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000361 * Set default concurrency policy for this ioqueue. If this function is not
362 * called, the default concurrency policy for the ioqueue is controlled by
363 * compile time setting PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY.
364 *
365 * Note that changing the concurrency setting to the ioqueue will only affect
366 * subsequent key registrations. To modify the concurrency setting for
367 * individual key, use #pj_ioqueue_set_concurrency().
368 *
369 * @param ioqueue The ioqueue instance.
370 * @param allow Non-zero to allow concurrent callback calls, or
371 * PJ_FALSE to disallow it.
372 *
373 * @return PJ_SUCCESS on success or the appropriate error code.
374 */
375PJ_DECL(pj_status_t) pj_ioqueue_set_default_concurrency(pj_ioqueue_t *ioqueue,
376 pj_bool_t allow);
377
378/**
Benny Prijono9033e312005-11-21 02:08:39 +0000379 * Register a socket to the I/O queue framework.
380 * When a socket is registered to the IOQueue, it may be modified to use
381 * non-blocking IO. If it is modified, there is no guarantee that this
382 * modification will be restored after the socket is unregistered.
383 *
384 * @param pool To allocate the resource for the specified handle,
385 * which must be valid until the handle/key is unregistered
386 * from I/O Queue.
387 * @param ioque The I/O Queue.
388 * @param sock The socket.
389 * @param user_data User data to be associated with the key, which can be
390 * retrieved later.
391 * @param cb Callback to be called when I/O operation completes.
392 * @param key Pointer to receive the key to be associated with this
393 * socket. Subsequent I/O queue operation will need this
394 * key.
395 *
396 * @return PJ_SUCCESS on success, or the error code.
397 */
398PJ_DECL(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool,
399 pj_ioqueue_t *ioque,
400 pj_sock_t sock,
401 void *user_data,
402 const pj_ioqueue_callback *cb,
403 pj_ioqueue_key_t **key );
404
405/**
406 * Unregister from the I/O Queue framework. Caller must make sure that
407 * the key doesn't have any pending operations before calling this function,
408 * by calling #pj_ioqueue_is_pending() for all previously submitted
409 * operations except asynchronous connect, and if necessary call
410 * #pj_ioqueue_post_completion() to cancel the pending operations.
411 *
412 * Note that asynchronous connect operation will automatically be
413 * cancelled during the unregistration.
414 *
Benny Prijono8d317a02006-03-22 11:49:19 +0000415 * Also note that when I/O Completion Port backend is used, application
416 * MUST close the handle immediately after unregistering the key. This is
417 * because there is no unregistering API for IOCP. The only way to
418 * unregister the handle from IOCP is to close the handle.
419 *
Benny Prijono9033e312005-11-21 02:08:39 +0000420 * @param key The key that was previously obtained from registration.
421 *
422 * @return PJ_SUCCESS on success or the error code.
423 *
424 * @see pj_ioqueue_is_pending
425 */
426PJ_DECL(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key );
427
428
429/**
430 * Get user data associated with an ioqueue key.
431 *
432 * @param key The key that was previously obtained from registration.
433 *
434 * @return The user data associated with the descriptor, or NULL
435 * on error or if no data is associated with the key during
436 * registration.
437 */
438PJ_DECL(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key );
439
440/**
441 * Set or change the user data to be associated with the file descriptor or
442 * handle or socket descriptor.
443 *
444 * @param key The key that was previously obtained from registration.
445 * @param user_data User data to be associated with the descriptor.
446 * @param old_data Optional parameter to retrieve the old user data.
447 *
448 * @return PJ_SUCCESS on success or the error code.
449 */
450PJ_DECL(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key,
451 void *user_data,
452 void **old_data);
453
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000454/**
455 * Configure whether the ioqueue is allowed to call the key's callback
456 * concurrently/in parallel. The default concurrency setting for the key
457 * is controlled by ioqueue's default concurrency value, which can be
458 * changed by calling #pj_ioqueue_set_default_concurrency().
459 *
460 * If concurrency is allowed for the key, it means that if there are more
461 * than one pending operations complete simultaneously, more than one
462 * threads may call the key's callback at the same time. This generally
463 * would promote good scalability for application, at the expense of more
464 * complexity to manage the concurrent accesses in application's code.
465 *
466 * Alternatively application may disable the concurrent access by
467 * setting the \a allow flag to false. With concurrency disabled, only
468 * one thread can call the key's callback at one time.
469 *
470 * @param key The key that was previously obtained from registration.
471 * @param allow Set this to non-zero to allow concurrent callback calls
472 * and zero (PJ_FALSE) to disallow it.
473 *
474 * @return PJ_SUCCESS on success or the appropriate error code.
475 */
476PJ_DECL(pj_status_t) pj_ioqueue_set_concurrency(pj_ioqueue_key_t *key,
477 pj_bool_t allow);
478
479/**
480 * Acquire the key's mutex. When the key's concurrency is disabled,
481 * application may call this function to synchronize its operation
482 * with the key's callback (i.e. this function will block until the
483 * key's callback returns).
484 *
485 * @param key The key that was previously obtained from registration.
486 *
487 * @return PJ_SUCCESS on success or the appropriate error code.
488 */
489PJ_DECL(pj_status_t) pj_ioqueue_lock_key(pj_ioqueue_key_t *key);
490
491/**
492 * Release the lock previously acquired with pj_ioqueue_lock_key().
493 *
494 * @param key The key that was previously obtained from registration.
495 *
496 * @return PJ_SUCCESS on success or the appropriate error code.
497 */
498PJ_DECL(pj_status_t) pj_ioqueue_unlock_key(pj_ioqueue_key_t *key);
Benny Prijono9033e312005-11-21 02:08:39 +0000499
500/**
501 * Initialize operation key.
502 *
503 * @param op_key The operation key to be initialied.
504 * @param size The size of the operation key.
505 */
506PJ_DECL(void) pj_ioqueue_op_key_init( pj_ioqueue_op_key_t *op_key,
507 pj_size_t size );
508
509/**
510 * Check if operation is pending on the specified operation key.
511 * The \c op_key must have been initialized with #pj_ioqueue_op_key_init()
512 * or submitted as pending operation before, or otherwise the result
513 * is undefined.
514 *
515 * @param key The key.
516 * @param op_key The operation key, previously submitted to any of
517 * the I/O functions and has returned PJ_EPENDING.
518 *
519 * @return Non-zero if operation is still pending.
520 */
521PJ_DECL(pj_bool_t) pj_ioqueue_is_pending( pj_ioqueue_key_t *key,
522 pj_ioqueue_op_key_t *op_key );
523
524
525/**
526 * Post completion status to the specified operation key and call the
527 * appropriate callback. When the callback is called, the number of bytes
528 * received in read/write callback or the status in accept/connect callback
529 * will be set from the \c bytes_status parameter.
530 *
531 * @param key The key.
532 * @param op_key Pending operation key.
533 * @param bytes_status Number of bytes or status to be set. A good value
534 * to put here is -PJ_ECANCELLED.
535 *
536 * @return PJ_SUCCESS if completion status has been successfully
537 * sent.
538 */
539PJ_DECL(pj_status_t) pj_ioqueue_post_completion( pj_ioqueue_key_t *key,
540 pj_ioqueue_op_key_t *op_key,
541 pj_ssize_t bytes_status );
542
543
544
545#if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
546/**
547 * Instruct I/O Queue to accept incoming connection on the specified
548 * listening socket. This function will return immediately (i.e. non-blocking)
549 * regardless whether a connection is immediately available. If the function
550 * can't complete immediately, the caller will be notified about the incoming
551 * connection when it calls pj_ioqueue_poll(). If a new connection is
552 * immediately available, the function returns PJ_SUCCESS with the new
553 * connection; in this case, the callback WILL NOT be called.
554 *
555 * @param key The key which registered to the server socket.
556 * @param op_key An operation specific key to be associated with the
557 * pending operation, so that application can keep track of
558 * which operation has been completed when the callback is
559 * called.
560 * @param new_sock Argument which contain pointer to receive the new socket
561 * for the incoming connection.
562 * @param local Optional argument which contain pointer to variable to
563 * receive local address.
564 * @param remote Optional argument which contain pointer to variable to
565 * receive the remote address.
566 * @param addrlen On input, contains the length of the buffer for the
567 * address, and on output, contains the actual length of the
568 * address. This argument is optional.
569 * @return
570 * - PJ_SUCCESS When connection is available immediately, and the
571 * parameters will be updated to contain information about
572 * the new connection. In this case, a completion callback
573 * WILL NOT be called.
574 * - PJ_EPENDING If no connection is available immediately. When a new
575 * connection arrives, the callback will be called.
576 * - non-zero which indicates the appropriate error code.
577 */
578PJ_DECL(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key,
579 pj_ioqueue_op_key_t *op_key,
Benny Prijono67b0d622006-06-23 15:03:04 +0000580 pj_sock_t *new_sock,
Benny Prijono9033e312005-11-21 02:08:39 +0000581 pj_sockaddr_t *local,
582 pj_sockaddr_t *remote,
583 int *addrlen );
584
585/**
586 * Initiate non-blocking socket connect. If the socket can NOT be connected
587 * immediately, asynchronous connect() will be scheduled and caller will be
588 * notified via completion callback when it calls pj_ioqueue_poll(). If
589 * socket is connected immediately, the function returns PJ_SUCCESS and
590 * completion callback WILL NOT be called.
591 *
592 * @param key The key associated with TCP socket
593 * @param addr The remote address.
594 * @param addrlen The remote address length.
595 *
596 * @return
597 * - PJ_SUCCESS If socket is connected immediately. In this case, the
598 * completion callback WILL NOT be called.
599 * - PJ_EPENDING If operation is queued, or
600 * - non-zero Indicates the error code.
601 */
602PJ_DECL(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key,
603 const pj_sockaddr_t *addr,
604 int addrlen );
605
606#endif /* PJ_HAS_TCP */
607
608/**
609 * Poll the I/O Queue for completed events.
610 *
Benny Prijono8ab968f2007-07-20 08:08:30 +0000611 * Note: polling the ioqueue is not necessary in Symbian. Please see
612 * @ref PJ_SYMBIAN_OS for more info.
613 *
Benny Prijono9033e312005-11-21 02:08:39 +0000614 * @param ioque the I/O Queue.
615 * @param timeout polling timeout, or NULL if the thread wishes to wait
616 * indefinetely for the event.
617 *
618 * @return
619 * - zero if timed out (no event).
620 * - (<0) if error occured during polling. Callback will NOT be called.
621 * - (>1) to indicate numbers of events. Callbacks have been called.
622 */
623PJ_DECL(int) pj_ioqueue_poll( pj_ioqueue_t *ioque,
624 const pj_time_val *timeout);
625
626
627/**
628 * Instruct the I/O Queue to read from the specified handle. This function
629 * returns immediately (i.e. non-blocking) regardless whether some data has
630 * been transfered. If the operation can't complete immediately, caller will
631 * be notified about the completion when it calls pj_ioqueue_poll(). If data
632 * is immediately available, the function will return PJ_SUCCESS and the
633 * callback WILL NOT be called.
634 *
635 * @param key The key that uniquely identifies the handle.
636 * @param op_key An operation specific key to be associated with the
637 * pending operation, so that application can keep track of
638 * which operation has been completed when the callback is
639 * called. Caller must make sure that this key remains
640 * valid until the function completes.
641 * @param buffer The buffer to hold the read data. The caller MUST make sure
642 * that this buffer remain valid until the framework completes
643 * reading the handle.
644 * @param length On input, it specifies the size of the buffer. If data is
645 * available to be read immediately, the function returns
646 * PJ_SUCCESS and this argument will be filled with the
647 * amount of data read. If the function is pending, caller
648 * will be notified about the amount of data read in the
649 * callback. This parameter can point to local variable in
650 * caller's stack and doesn't have to remain valid for the
651 * duration of pending operation.
652 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
653 * the function will never return PJ_SUCCESS.
654 *
655 * @return
656 * - PJ_SUCCESS If immediate data has been received in the buffer. In this
657 * case, the callback WILL NOT be called.
658 * - PJ_EPENDING If the operation has been queued, and the callback will be
659 * called when data has been received.
660 * - non-zero The return value indicates the error code.
661 */
662PJ_DECL(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key,
663 pj_ioqueue_op_key_t *op_key,
664 void *buffer,
665 pj_ssize_t *length,
666 pj_uint32_t flags );
667
668/**
669 * This function behaves similarly as #pj_ioqueue_recv(), except that it is
670 * normally called for socket, and the remote address will also be returned
671 * along with the data. Caller MUST make sure that both buffer and addr
672 * remain valid until the framework completes reading the data.
673 *
674 * @param key The key that uniquely identifies the handle.
675 * @param op_key An operation specific key to be associated with the
676 * pending operation, so that application can keep track of
677 * which operation has been completed when the callback is
678 * called.
679 * @param buffer The buffer to hold the read data. The caller MUST make sure
680 * that this buffer remain valid until the framework completes
681 * reading the handle.
682 * @param length On input, it specifies the size of the buffer. If data is
683 * available to be read immediately, the function returns
684 * PJ_SUCCESS and this argument will be filled with the
685 * amount of data read. If the function is pending, caller
686 * will be notified about the amount of data read in the
687 * callback. This parameter can point to local variable in
688 * caller's stack and doesn't have to remain valid for the
689 * duration of pending operation.
690 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
691 * the function will never return PJ_SUCCESS.
692 * @param addr Optional Pointer to buffer to receive the address.
693 * @param addrlen On input, specifies the length of the address buffer.
694 * On output, it will be filled with the actual length of
695 * the address. This argument can be NULL if \c addr is not
696 * specified.
697 *
698 * @return
699 * - PJ_SUCCESS If immediate data has been received. In this case, the
700 * callback must have been called before this function
701 * returns, and no pending operation is scheduled.
702 * - PJ_EPENDING If the operation has been queued.
703 * - non-zero The return value indicates the error code.
704 */
705PJ_DECL(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key,
706 pj_ioqueue_op_key_t *op_key,
707 void *buffer,
708 pj_ssize_t *length,
709 pj_uint32_t flags,
710 pj_sockaddr_t *addr,
711 int *addrlen);
712
713/**
714 * Instruct the I/O Queue to write to the handle. This function will return
715 * immediately (i.e. non-blocking) regardless whether some data has been
716 * transfered. If the function can't complete immediately, the caller will
717 * be notified about the completion when it calls pj_ioqueue_poll(). If
718 * operation completes immediately and data has been transfered, the function
719 * returns PJ_SUCCESS and the callback will NOT be called.
720 *
721 * @param key The key that identifies the handle.
722 * @param op_key An operation specific key to be associated with the
723 * pending operation, so that application can keep track of
724 * which operation has been completed when the callback is
725 * called.
726 * @param data The data to send. Caller MUST make sure that this buffer
727 * remains valid until the write operation completes.
728 * @param length On input, it specifies the length of data to send. When
729 * data was sent immediately, this function returns PJ_SUCCESS
730 * and this parameter contains the length of data sent. If
731 * data can not be sent immediately, an asynchronous operation
732 * is scheduled and caller will be notified via callback the
733 * number of bytes sent. This parameter can point to local
734 * variable on caller's stack and doesn't have to remain
735 * valid until the operation has completed.
736 * @param flags Send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
737 * the function will never return PJ_SUCCESS.
738 *
739 * @return
740 * - PJ_SUCCESS If data was immediately transfered. In this case, no
741 * pending operation has been scheduled and the callback
742 * WILL NOT be called.
743 * - PJ_EPENDING If the operation has been queued. Once data base been
744 * transfered, the callback will be called.
745 * - non-zero The return value indicates the error code.
746 */
747PJ_DECL(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key,
748 pj_ioqueue_op_key_t *op_key,
749 const void *data,
750 pj_ssize_t *length,
751 pj_uint32_t flags );
752
753
754/**
755 * Instruct the I/O Queue to write to the handle. This function will return
756 * immediately (i.e. non-blocking) regardless whether some data has been
757 * transfered. If the function can't complete immediately, the caller will
758 * be notified about the completion when it calls pj_ioqueue_poll(). If
759 * operation completes immediately and data has been transfered, the function
760 * returns PJ_SUCCESS and the callback will NOT be called.
761 *
762 * @param key the key that identifies the handle.
763 * @param op_key An operation specific key to be associated with the
764 * pending operation, so that application can keep track of
765 * which operation has been completed when the callback is
766 * called.
767 * @param data the data to send. Caller MUST make sure that this buffer
768 * remains valid until the write operation completes.
769 * @param length On input, it specifies the length of data to send. When
770 * data was sent immediately, this function returns PJ_SUCCESS
771 * and this parameter contains the length of data sent. If
772 * data can not be sent immediately, an asynchronous operation
773 * is scheduled and caller will be notified via callback the
774 * number of bytes sent. This parameter can point to local
775 * variable on caller's stack and doesn't have to remain
776 * valid until the operation has completed.
777 * @param flags send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
778 * the function will never return PJ_SUCCESS.
779 * @param addr Optional remote address.
780 * @param addrlen Remote address length, \c addr is specified.
781 *
782 * @return
783 * - PJ_SUCCESS If data was immediately written.
784 * - PJ_EPENDING If the operation has been queued.
785 * - non-zero The return value indicates the error code.
786 */
787PJ_DECL(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key,
788 pj_ioqueue_op_key_t *op_key,
789 const void *data,
790 pj_ssize_t *length,
791 pj_uint32_t flags,
792 const pj_sockaddr_t *addr,
793 int addrlen);
794
795
796/**
797 * !}
798 */
799
800PJ_END_DECL
801
802#endif /* __PJ_IOQUEUE_H__ */
803