blob: 0b94de865312aa75c85ed54ea595fdd1e50edf57 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_SELECT_H__
20#define __PJ_SELECT_H__
21
22/**
23 * @file sock_select.h
24 * @brief Socket select().
25 */
26
27#include <pj/types.h>
28
29PJ_BEGIN_DECL
30
31/**
32 * @defgroup PJ_SOCK_SELECT Socket select() API.
33 * @ingroup PJ_IO
34 * @{
35 * This module provides portable abstraction for \a select() like API.
36 * The abstraction is needed so that it can utilize various event
37 * dispatching mechanisms that are available across platforms.
38 *
39 * The API is very similar to normal \a select() usage.
40 *
41 * \section pj_sock_select_examples_sec Examples
42 *
43 * For some examples on how to use the select API, please see:
44 *
45 * - \ref page_pjlib_select_test
46 */
47
48/**
49 * Portable structure declarations for pj_fd_set.
50 * The implementation of pj_sock_select() does not use this structure
51 * per-se, but instead it will use the native fd_set structure. However,
52 * we must make sure that the size of pj_fd_set_t can accomodate the
53 * native fd_set structure.
54 */
55typedef struct pj_fd_set_t
56{
57 pj_sock_t data[FD_SETSIZE + 4]; /**< Opaque buffer for fd_set */
58} pj_fd_set_t;
59
60
61/**
62 * Initialize the descriptor set pointed to by fdsetp to the null set.
63 *
64 * @param fdsetp The descriptor set.
65 */
66PJ_DECL(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp);
67
68
69/**
70 * Add the file descriptor fd to the set pointed to by fdsetp.
71 * If the file descriptor fd is already in this set, there shall be no effect
72 * on the set, nor will an error be returned.
73 *
74 * @param fd The socket descriptor.
75 * @param fdsetp The descriptor set.
76 */
77PJ_DECL(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp);
78
79/**
80 * Remove the file descriptor fd from the set pointed to by fdsetp.
81 * If fd is not a member of this set, there shall be no effect on the set,
82 * nor will an error be returned.
83 *
84 * @param fd The socket descriptor.
85 * @param fdsetp The descriptor set.
86 */
87PJ_DECL(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp);
88
89
90/**
91 * Evaluate to non-zero if the file descriptor fd is a member of the set
92 * pointed to by fdsetp, and shall evaluate to zero otherwise.
93 *
94 * @param fd The socket descriptor.
95 * @param fdsetp The descriptor set.
96 *
97 * @return Nonzero if fd is member of the descriptor set.
98 */
99PJ_DECL(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp);
100
101
102/**
103 * This function wait for a number of file descriptors to change status.
104 * The behaviour is the same as select() function call which appear in
105 * standard BSD socket libraries.
106 *
107 * @param n On Unices, this specifies the highest-numbered
108 * descriptor in any of the three set, plus 1. On Windows,
109 * the value is ignored.
110 * @param readfds Optional pointer to a set of sockets to be checked for
111 * readability.
112 * @param writefds Optional pointer to a set of sockets to be checked for
113 * writability.
114 * @param exceptfds Optional pointer to a set of sockets to be checked for
115 * errors.
116 * @param timeout Maximum time for select to wait, or null for blocking
117 * operations.
118 *
119 * @return The total number of socket handles that are ready, or
120 * zero if the time limit expired, or -1 if an error occurred.
121 */
122PJ_DECL(int) pj_sock_select( int n,
123 pj_fd_set_t *readfds,
124 pj_fd_set_t *writefds,
125 pj_fd_set_t *exceptfds,
126 const pj_time_val *timeout);
127
128
129/**
130 * @}
131 */
132
133
134PJ_END_DECL
135
136#endif /* __PJ_SELECT_H__ */