blob: 8ce7a65b939175405a59c2517f41b51d451834cc [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +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_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/**
Benny Prijono8ab968f2007-07-20 08:08:30 +000070 * This is an internal function, application shouldn't use this.
71 *
72 * Get the number of descriptors in the set. This is defined in sock_select.c
73 * This function will only return the number of sockets set from PJ_FD_SET
74 * operation. When the set is modified by other means (such as by select()),
75 * the count will not be reflected here.
76 *
77 * @param fdsetp The descriptor set.
78 *
79 * @return Number of descriptors in the set.
80 */
81PJ_DECL(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp);
82
83
84/**
Benny Prijono9033e312005-11-21 02:08:39 +000085 * Add the file descriptor fd to the set pointed to by fdsetp.
86 * If the file descriptor fd is already in this set, there shall be no effect
87 * on the set, nor will an error be returned.
88 *
89 * @param fd The socket descriptor.
90 * @param fdsetp The descriptor set.
91 */
92PJ_DECL(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp);
93
94/**
95 * Remove the file descriptor fd from the set pointed to by fdsetp.
96 * If fd is not a member of this set, there shall be no effect on the set,
97 * nor will an error be returned.
98 *
99 * @param fd The socket descriptor.
100 * @param fdsetp The descriptor set.
101 */
102PJ_DECL(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp);
103
104
105/**
106 * Evaluate to non-zero if the file descriptor fd is a member of the set
107 * pointed to by fdsetp, and shall evaluate to zero otherwise.
108 *
109 * @param fd The socket descriptor.
110 * @param fdsetp The descriptor set.
111 *
112 * @return Nonzero if fd is member of the descriptor set.
113 */
114PJ_DECL(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp);
115
116
117/**
118 * This function wait for a number of file descriptors to change status.
119 * The behaviour is the same as select() function call which appear in
120 * standard BSD socket libraries.
121 *
122 * @param n On Unices, this specifies the highest-numbered
123 * descriptor in any of the three set, plus 1. On Windows,
124 * the value is ignored.
125 * @param readfds Optional pointer to a set of sockets to be checked for
126 * readability.
127 * @param writefds Optional pointer to a set of sockets to be checked for
128 * writability.
129 * @param exceptfds Optional pointer to a set of sockets to be checked for
130 * errors.
131 * @param timeout Maximum time for select to wait, or null for blocking
132 * operations.
133 *
134 * @return The total number of socket handles that are ready, or
135 * zero if the time limit expired, or -1 if an error occurred.
136 */
137PJ_DECL(int) pj_sock_select( int n,
138 pj_fd_set_t *readfds,
139 pj_fd_set_t *writefds,
140 pj_fd_set_t *exceptfds,
141 const pj_time_val *timeout);
142
143
144/**
145 * @}
146 */
147
148
149PJ_END_DECL
150
151#endif /* __PJ_SELECT_H__ */