blob: 616ccc156550260a346eb0a51c23f3e658bb4da0 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$
2 */
3/*
Benny Prijonoa771a512007-02-19 01:13:53 +00004 * Copyright (C)2003-2007 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 *
104 * The items below describe rules that must be obeyed when using the I/O
105 * queue, with regard to concurrency:
106 * - simultaneous operations (by different threads) to different key is safe.
107 * - simultaneous operations to the same key is also safe, except
108 * <b>unregistration</b>, which is described below.
109 * - <b>care must be taken when unregistering a key</b> from the
110 * ioqueue. Application must take care that when one thread is issuing
111 * an unregistration, other thread is not simultaneously invoking an
112 * operation <b>to the same key</b>.
113 *\n
114 * This happens because the ioqueue functions are working with a pointer
115 * to the key, and there is a possible race condition where the pointer
116 * has been rendered invalid by other threads before the ioqueue has a
117 * chance to acquire mutex on it.
118 *
119 * \section pj_ioqeuue_examples_sec Examples
120 *
121 * For some examples on how to use the I/O Queue, please see:
122 *
123 * - \ref page_pjlib_ioqueue_tcp_test
124 * - \ref page_pjlib_ioqueue_udp_test
125 * - \ref page_pjlib_ioqueue_perf_test
126 */
127
128
129/**
130 * This structure describes operation specific key to be submitted to
131 * I/O Queue when performing the asynchronous operation. This key will
132 * be returned to the application when completion callback is called.
133 *
134 * Application normally wants to attach it's specific data in the
135 * \c user_data field so that it can keep track of which operation has
136 * completed when the callback is called. Alternatively, application can
137 * also extend this struct to include its data, because the pointer that
138 * is returned in the completion callback will be exactly the same as
139 * the pointer supplied when the asynchronous function is called.
140 */
141typedef struct pj_ioqueue_op_key_t
142{
143 void *internal__[32]; /**< Internal I/O Queue data. */
144 void *user_data; /**< Application data. */
145} pj_ioqueue_op_key_t;
146
147/**
148 * This structure describes the callbacks to be called when I/O operation
149 * completes.
150 */
151typedef struct pj_ioqueue_callback
152{
153 /**
154 * This callback is called when #pj_ioqueue_recv or #pj_ioqueue_recvfrom
155 * completes.
156 *
157 * @param key The key.
158 * @param op_key Operation key.
159 * @param bytes_read >= 0 to indicate the amount of data read,
160 * otherwise negative value containing the error
161 * code. To obtain the pj_status_t error code, use
162 * (pj_status_t code = -bytes_read).
163 */
164 void (*on_read_complete)(pj_ioqueue_key_t *key,
165 pj_ioqueue_op_key_t *op_key,
166 pj_ssize_t bytes_read);
167
168 /**
169 * This callback is called when #pj_ioqueue_write or #pj_ioqueue_sendto
170 * completes.
171 *
172 * @param key The key.
173 * @param op_key Operation key.
174 * @param bytes_sent >= 0 to indicate the amount of data written,
175 * otherwise negative value containing the error
176 * code. To obtain the pj_status_t error code, use
177 * (pj_status_t code = -bytes_sent).
178 */
179 void (*on_write_complete)(pj_ioqueue_key_t *key,
180 pj_ioqueue_op_key_t *op_key,
181 pj_ssize_t bytes_sent);
182
183 /**
184 * This callback is called when #pj_ioqueue_accept completes.
185 *
186 * @param key The key.
187 * @param op_key Operation key.
188 * @param sock Newly connected socket.
189 * @param status Zero if the operation completes successfully.
190 */
191 void (*on_accept_complete)(pj_ioqueue_key_t *key,
192 pj_ioqueue_op_key_t *op_key,
193 pj_sock_t sock,
194 pj_status_t status);
195
196 /**
197 * This callback is called when #pj_ioqueue_connect completes.
198 *
199 * @param key The key.
200 * @param status PJ_SUCCESS if the operation completes successfully.
201 */
202 void (*on_connect_complete)(pj_ioqueue_key_t *key,
203 pj_status_t status);
204} pj_ioqueue_callback;
205
206
207/**
208 * Types of pending I/O Queue operation. This enumeration is only used
209 * internally within the ioqueue.
210 */
211typedef enum pj_ioqueue_operation_e
212{
213 PJ_IOQUEUE_OP_NONE = 0, /**< No operation. */
214 PJ_IOQUEUE_OP_READ = 1, /**< read() operation. */
215 PJ_IOQUEUE_OP_RECV = 2, /**< recv() operation. */
216 PJ_IOQUEUE_OP_RECV_FROM = 4, /**< recvfrom() operation. */
217 PJ_IOQUEUE_OP_WRITE = 8, /**< write() operation. */
218 PJ_IOQUEUE_OP_SEND = 16, /**< send() operation. */
219 PJ_IOQUEUE_OP_SEND_TO = 32, /**< sendto() operation. */
220#if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
221 PJ_IOQUEUE_OP_ACCEPT = 64, /**< accept() operation. */
Benny Prijono92ac4472006-07-22 13:42:56 +0000222 PJ_IOQUEUE_OP_CONNECT = 128 /**< connect() operation. */
Benny Prijono9033e312005-11-21 02:08:39 +0000223#endif /* PJ_HAS_TCP */
224} pj_ioqueue_operation_e;
225
226
227/**
228 * This macro specifies the maximum number of events that can be
229 * processed by the ioqueue on a single poll cycle, on implementation
230 * that supports it. The value is only meaningfull when specified
231 * during PJLIB build.
232 */
233#ifndef PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL
234# define PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL (16)
235#endif
236
237/**
238 * When this flag is specified in ioqueue's recv() or send() operations,
239 * the ioqueue will always mark the operation as asynchronous.
240 */
241#define PJ_IOQUEUE_ALWAYS_ASYNC ((pj_uint32_t)1 << (pj_uint32_t)31)
242
243/**
244 * Return the name of the ioqueue implementation.
245 *
246 * @return Implementation name.
247 */
248PJ_DECL(const char*) pj_ioqueue_name(void);
249
250
251/**
252 * Create a new I/O Queue framework.
253 *
254 * @param pool The pool to allocate the I/O queue structure.
255 * @param max_fd The maximum number of handles to be supported, which
256 * should not exceed PJ_IOQUEUE_MAX_HANDLES.
257 * @param ioqueue Pointer to hold the newly created I/O Queue.
258 *
259 * @return PJ_SUCCESS on success.
260 */
261PJ_DECL(pj_status_t) pj_ioqueue_create( pj_pool_t *pool,
262 pj_size_t max_fd,
263 pj_ioqueue_t **ioqueue);
264
265/**
266 * Destroy the I/O queue.
267 *
268 * @param ioque The I/O Queue to be destroyed.
269 *
270 * @return PJ_SUCCESS if success.
271 */
272PJ_DECL(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioque );
273
274/**
275 * Set the lock object to be used by the I/O Queue. This function can only
276 * be called right after the I/O queue is created, before any handle is
277 * registered to the I/O queue.
278 *
279 * Initially the I/O queue is created with non-recursive mutex protection.
280 * Applications can supply alternative lock to be used by calling this
281 * function.
282 *
283 * @param ioque The ioqueue instance.
284 * @param lock The lock to be used by the ioqueue.
285 * @param auto_delete In non-zero, the lock will be deleted by the ioqueue.
286 *
287 * @return PJ_SUCCESS or the appropriate error code.
288 */
289PJ_DECL(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioque,
290 pj_lock_t *lock,
291 pj_bool_t auto_delete );
292
293/**
294 * Register a socket to the I/O queue framework.
295 * When a socket is registered to the IOQueue, it may be modified to use
296 * non-blocking IO. If it is modified, there is no guarantee that this
297 * modification will be restored after the socket is unregistered.
298 *
299 * @param pool To allocate the resource for the specified handle,
300 * which must be valid until the handle/key is unregistered
301 * from I/O Queue.
302 * @param ioque The I/O Queue.
303 * @param sock The socket.
304 * @param user_data User data to be associated with the key, which can be
305 * retrieved later.
306 * @param cb Callback to be called when I/O operation completes.
307 * @param key Pointer to receive the key to be associated with this
308 * socket. Subsequent I/O queue operation will need this
309 * key.
310 *
311 * @return PJ_SUCCESS on success, or the error code.
312 */
313PJ_DECL(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool,
314 pj_ioqueue_t *ioque,
315 pj_sock_t sock,
316 void *user_data,
317 const pj_ioqueue_callback *cb,
318 pj_ioqueue_key_t **key );
319
320/**
321 * Unregister from the I/O Queue framework. Caller must make sure that
322 * the key doesn't have any pending operations before calling this function,
323 * by calling #pj_ioqueue_is_pending() for all previously submitted
324 * operations except asynchronous connect, and if necessary call
325 * #pj_ioqueue_post_completion() to cancel the pending operations.
326 *
327 * Note that asynchronous connect operation will automatically be
328 * cancelled during the unregistration.
329 *
Benny Prijono8d317a02006-03-22 11:49:19 +0000330 * Also note that when I/O Completion Port backend is used, application
331 * MUST close the handle immediately after unregistering the key. This is
332 * because there is no unregistering API for IOCP. The only way to
333 * unregister the handle from IOCP is to close the handle.
334 *
Benny Prijono9033e312005-11-21 02:08:39 +0000335 * @param key The key that was previously obtained from registration.
336 *
337 * @return PJ_SUCCESS on success or the error code.
338 *
339 * @see pj_ioqueue_is_pending
340 */
341PJ_DECL(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key );
342
343
344/**
345 * Get user data associated with an ioqueue key.
346 *
347 * @param key The key that was previously obtained from registration.
348 *
349 * @return The user data associated with the descriptor, or NULL
350 * on error or if no data is associated with the key during
351 * registration.
352 */
353PJ_DECL(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key );
354
355/**
356 * Set or change the user data to be associated with the file descriptor or
357 * handle or socket descriptor.
358 *
359 * @param key The key that was previously obtained from registration.
360 * @param user_data User data to be associated with the descriptor.
361 * @param old_data Optional parameter to retrieve the old user data.
362 *
363 * @return PJ_SUCCESS on success or the error code.
364 */
365PJ_DECL(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key,
366 void *user_data,
367 void **old_data);
368
369
370/**
371 * Initialize operation key.
372 *
373 * @param op_key The operation key to be initialied.
374 * @param size The size of the operation key.
375 */
376PJ_DECL(void) pj_ioqueue_op_key_init( pj_ioqueue_op_key_t *op_key,
377 pj_size_t size );
378
379/**
380 * Check if operation is pending on the specified operation key.
381 * The \c op_key must have been initialized with #pj_ioqueue_op_key_init()
382 * or submitted as pending operation before, or otherwise the result
383 * is undefined.
384 *
385 * @param key The key.
386 * @param op_key The operation key, previously submitted to any of
387 * the I/O functions and has returned PJ_EPENDING.
388 *
389 * @return Non-zero if operation is still pending.
390 */
391PJ_DECL(pj_bool_t) pj_ioqueue_is_pending( pj_ioqueue_key_t *key,
392 pj_ioqueue_op_key_t *op_key );
393
394
395/**
396 * Post completion status to the specified operation key and call the
397 * appropriate callback. When the callback is called, the number of bytes
398 * received in read/write callback or the status in accept/connect callback
399 * will be set from the \c bytes_status parameter.
400 *
401 * @param key The key.
402 * @param op_key Pending operation key.
403 * @param bytes_status Number of bytes or status to be set. A good value
404 * to put here is -PJ_ECANCELLED.
405 *
406 * @return PJ_SUCCESS if completion status has been successfully
407 * sent.
408 */
409PJ_DECL(pj_status_t) pj_ioqueue_post_completion( pj_ioqueue_key_t *key,
410 pj_ioqueue_op_key_t *op_key,
411 pj_ssize_t bytes_status );
412
413
414
415#if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
416/**
417 * Instruct I/O Queue to accept incoming connection on the specified
418 * listening socket. This function will return immediately (i.e. non-blocking)
419 * regardless whether a connection is immediately available. If the function
420 * can't complete immediately, the caller will be notified about the incoming
421 * connection when it calls pj_ioqueue_poll(). If a new connection is
422 * immediately available, the function returns PJ_SUCCESS with the new
423 * connection; in this case, the callback WILL NOT be called.
424 *
425 * @param key The key which registered to the server socket.
426 * @param op_key An operation specific key to be associated with the
427 * pending operation, so that application can keep track of
428 * which operation has been completed when the callback is
429 * called.
430 * @param new_sock Argument which contain pointer to receive the new socket
431 * for the incoming connection.
432 * @param local Optional argument which contain pointer to variable to
433 * receive local address.
434 * @param remote Optional argument which contain pointer to variable to
435 * receive the remote address.
436 * @param addrlen On input, contains the length of the buffer for the
437 * address, and on output, contains the actual length of the
438 * address. This argument is optional.
439 * @return
440 * - PJ_SUCCESS When connection is available immediately, and the
441 * parameters will be updated to contain information about
442 * the new connection. In this case, a completion callback
443 * WILL NOT be called.
444 * - PJ_EPENDING If no connection is available immediately. When a new
445 * connection arrives, the callback will be called.
446 * - non-zero which indicates the appropriate error code.
447 */
448PJ_DECL(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key,
449 pj_ioqueue_op_key_t *op_key,
Benny Prijono67b0d622006-06-23 15:03:04 +0000450 pj_sock_t *new_sock,
Benny Prijono9033e312005-11-21 02:08:39 +0000451 pj_sockaddr_t *local,
452 pj_sockaddr_t *remote,
453 int *addrlen );
454
455/**
456 * Initiate non-blocking socket connect. If the socket can NOT be connected
457 * immediately, asynchronous connect() will be scheduled and caller will be
458 * notified via completion callback when it calls pj_ioqueue_poll(). If
459 * socket is connected immediately, the function returns PJ_SUCCESS and
460 * completion callback WILL NOT be called.
461 *
462 * @param key The key associated with TCP socket
463 * @param addr The remote address.
464 * @param addrlen The remote address length.
465 *
466 * @return
467 * - PJ_SUCCESS If socket is connected immediately. In this case, the
468 * completion callback WILL NOT be called.
469 * - PJ_EPENDING If operation is queued, or
470 * - non-zero Indicates the error code.
471 */
472PJ_DECL(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key,
473 const pj_sockaddr_t *addr,
474 int addrlen );
475
476#endif /* PJ_HAS_TCP */
477
478/**
479 * Poll the I/O Queue for completed events.
480 *
Benny Prijono8ab968f2007-07-20 08:08:30 +0000481 * Note: polling the ioqueue is not necessary in Symbian. Please see
482 * @ref PJ_SYMBIAN_OS for more info.
483 *
Benny Prijono9033e312005-11-21 02:08:39 +0000484 * @param ioque the I/O Queue.
485 * @param timeout polling timeout, or NULL if the thread wishes to wait
486 * indefinetely for the event.
487 *
488 * @return
489 * - zero if timed out (no event).
490 * - (<0) if error occured during polling. Callback will NOT be called.
491 * - (>1) to indicate numbers of events. Callbacks have been called.
492 */
493PJ_DECL(int) pj_ioqueue_poll( pj_ioqueue_t *ioque,
494 const pj_time_val *timeout);
495
496
497/**
498 * Instruct the I/O Queue to read from the specified handle. This function
499 * returns immediately (i.e. non-blocking) regardless whether some data has
500 * been transfered. If the operation can't complete immediately, caller will
501 * be notified about the completion when it calls pj_ioqueue_poll(). If data
502 * is immediately available, the function will return PJ_SUCCESS and the
503 * callback WILL NOT be called.
504 *
505 * @param key The key that uniquely identifies the handle.
506 * @param op_key An operation specific key to be associated with the
507 * pending operation, so that application can keep track of
508 * which operation has been completed when the callback is
509 * called. Caller must make sure that this key remains
510 * valid until the function completes.
511 * @param buffer The buffer to hold the read data. The caller MUST make sure
512 * that this buffer remain valid until the framework completes
513 * reading the handle.
514 * @param length On input, it specifies the size of the buffer. If data is
515 * available to be read immediately, the function returns
516 * PJ_SUCCESS and this argument will be filled with the
517 * amount of data read. If the function is pending, caller
518 * will be notified about the amount of data read in the
519 * callback. This parameter can point to local variable in
520 * caller's stack and doesn't have to remain valid for the
521 * duration of pending operation.
522 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
523 * the function will never return PJ_SUCCESS.
524 *
525 * @return
526 * - PJ_SUCCESS If immediate data has been received in the buffer. In this
527 * case, the callback WILL NOT be called.
528 * - PJ_EPENDING If the operation has been queued, and the callback will be
529 * called when data has been received.
530 * - non-zero The return value indicates the error code.
531 */
532PJ_DECL(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key,
533 pj_ioqueue_op_key_t *op_key,
534 void *buffer,
535 pj_ssize_t *length,
536 pj_uint32_t flags );
537
538/**
539 * This function behaves similarly as #pj_ioqueue_recv(), except that it is
540 * normally called for socket, and the remote address will also be returned
541 * along with the data. Caller MUST make sure that both buffer and addr
542 * remain valid until the framework completes reading the data.
543 *
544 * @param key The key that uniquely identifies the handle.
545 * @param op_key An operation specific key to be associated with the
546 * pending operation, so that application can keep track of
547 * which operation has been completed when the callback is
548 * called.
549 * @param buffer The buffer to hold the read data. The caller MUST make sure
550 * that this buffer remain valid until the framework completes
551 * reading the handle.
552 * @param length On input, it specifies the size of the buffer. If data is
553 * available to be read immediately, the function returns
554 * PJ_SUCCESS and this argument will be filled with the
555 * amount of data read. If the function is pending, caller
556 * will be notified about the amount of data read in the
557 * callback. This parameter can point to local variable in
558 * caller's stack and doesn't have to remain valid for the
559 * duration of pending operation.
560 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
561 * the function will never return PJ_SUCCESS.
562 * @param addr Optional Pointer to buffer to receive the address.
563 * @param addrlen On input, specifies the length of the address buffer.
564 * On output, it will be filled with the actual length of
565 * the address. This argument can be NULL if \c addr is not
566 * specified.
567 *
568 * @return
569 * - PJ_SUCCESS If immediate data has been received. In this case, the
570 * callback must have been called before this function
571 * returns, and no pending operation is scheduled.
572 * - PJ_EPENDING If the operation has been queued.
573 * - non-zero The return value indicates the error code.
574 */
575PJ_DECL(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key,
576 pj_ioqueue_op_key_t *op_key,
577 void *buffer,
578 pj_ssize_t *length,
579 pj_uint32_t flags,
580 pj_sockaddr_t *addr,
581 int *addrlen);
582
583/**
584 * Instruct the I/O Queue to write to the handle. This function will return
585 * immediately (i.e. non-blocking) regardless whether some data has been
586 * transfered. If the function can't complete immediately, the caller will
587 * be notified about the completion when it calls pj_ioqueue_poll(). If
588 * operation completes immediately and data has been transfered, the function
589 * returns PJ_SUCCESS and the callback will NOT be called.
590 *
591 * @param key The key that identifies the handle.
592 * @param op_key An operation specific key to be associated with the
593 * pending operation, so that application can keep track of
594 * which operation has been completed when the callback is
595 * called.
596 * @param data The data to send. Caller MUST make sure that this buffer
597 * remains valid until the write operation completes.
598 * @param length On input, it specifies the length of data to send. When
599 * data was sent immediately, this function returns PJ_SUCCESS
600 * and this parameter contains the length of data sent. If
601 * data can not be sent immediately, an asynchronous operation
602 * is scheduled and caller will be notified via callback the
603 * number of bytes sent. This parameter can point to local
604 * variable on caller's stack and doesn't have to remain
605 * valid until the operation has completed.
606 * @param flags Send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
607 * the function will never return PJ_SUCCESS.
608 *
609 * @return
610 * - PJ_SUCCESS If data was immediately transfered. In this case, no
611 * pending operation has been scheduled and the callback
612 * WILL NOT be called.
613 * - PJ_EPENDING If the operation has been queued. Once data base been
614 * transfered, the callback will be called.
615 * - non-zero The return value indicates the error code.
616 */
617PJ_DECL(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key,
618 pj_ioqueue_op_key_t *op_key,
619 const void *data,
620 pj_ssize_t *length,
621 pj_uint32_t flags );
622
623
624/**
625 * Instruct the I/O Queue to write to the handle. This function will return
626 * immediately (i.e. non-blocking) regardless whether some data has been
627 * transfered. If the function can't complete immediately, the caller will
628 * be notified about the completion when it calls pj_ioqueue_poll(). If
629 * operation completes immediately and data has been transfered, the function
630 * returns PJ_SUCCESS and the callback will NOT be called.
631 *
632 * @param key the key that identifies the handle.
633 * @param op_key An operation specific key to be associated with the
634 * pending operation, so that application can keep track of
635 * which operation has been completed when the callback is
636 * called.
637 * @param data the data to send. Caller MUST make sure that this buffer
638 * remains valid until the write operation completes.
639 * @param length On input, it specifies the length of data to send. When
640 * data was sent immediately, this function returns PJ_SUCCESS
641 * and this parameter contains the length of data sent. If
642 * data can not be sent immediately, an asynchronous operation
643 * is scheduled and caller will be notified via callback the
644 * number of bytes sent. This parameter can point to local
645 * variable on caller's stack and doesn't have to remain
646 * valid until the operation has completed.
647 * @param flags send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
648 * the function will never return PJ_SUCCESS.
649 * @param addr Optional remote address.
650 * @param addrlen Remote address length, \c addr is specified.
651 *
652 * @return
653 * - PJ_SUCCESS If data was immediately written.
654 * - PJ_EPENDING If the operation has been queued.
655 * - non-zero The return value indicates the error code.
656 */
657PJ_DECL(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key,
658 pj_ioqueue_op_key_t *op_key,
659 const void *data,
660 pj_ssize_t *length,
661 pj_uint32_t flags,
662 const pj_sockaddr_t *addr,
663 int addrlen);
664
665
666/**
667 * !}
668 */
669
670PJ_END_DECL
671
672#endif /* __PJ_IOQUEUE_H__ */
673