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