blob: 59950e62a0fc5c253498c898d5ca071efc07296e [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#include <pj/sock_select.h>
20#include <pj/compat/socket.h>
21#include <pj/os.h>
22#include <pj/assert.h>
23#include <pj/errno.h>
24
25#if defined(PJ_HAS_STRING_H) && PJ_HAS_STRING_H!=0
26# include <string.h>
27#endif
28
Benny Prijonoe67d99a2006-03-20 12:39:24 +000029#if defined(PJ_HAS_SYS_TIME_H) && PJ_HAS_SYS_TIME_H!=0
30# include <sys/time.h>
31#endif
32
Benny Prijono9033e312005-11-21 02:08:39 +000033#ifdef _MSC_VER
34# pragma warning(disable: 4018) // Signed/unsigned mismatch in FD_*
35# pragma warning(disable: 4389) // Signed/unsigned mismatch in FD_*
36#endif
37
38#define PART_FDSET(ps) ((fd_set*)&ps->data[1])
39#define PART_FDSET_OR_NULL(ps) (ps ? PART_FDSET(ps) : NULL)
40#define PART_COUNT(ps) (ps->data[0])
41
42PJ_DEF(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp)
43{
44 PJ_CHECK_STACK();
45 pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
46
47 FD_ZERO(PART_FDSET(fdsetp));
48 PART_COUNT(fdsetp) = 0;
49}
50
51
52PJ_DEF(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp)
53{
54 PJ_CHECK_STACK();
55 pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
56
57 if (!PJ_FD_ISSET(fd, fdsetp))
58 ++PART_COUNT(fdsetp);
59 FD_SET(fd, PART_FDSET(fdsetp));
60}
61
62
63PJ_DEF(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp)
64{
65 PJ_CHECK_STACK();
66 pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
67
68 if (PJ_FD_ISSET(fd, fdsetp))
69 --PART_COUNT(fdsetp);
70 FD_CLR(fd, PART_FDSET(fdsetp));
71}
72
73
74PJ_DEF(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp)
75{
76 PJ_CHECK_STACK();
77 PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
78 0);
79
80 return FD_ISSET(fd, PART_FDSET(fdsetp));
81}
82
83PJ_DEF(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp)
84{
85 return PART_COUNT(fdsetp);
86}
87
88PJ_DEF(int) pj_sock_select( int n,
89 pj_fd_set_t *readfds,
90 pj_fd_set_t *writefds,
91 pj_fd_set_t *exceptfds,
92 const pj_time_val *timeout)
93{
94 struct timeval os_timeout, *p_os_timeout;
95
96 PJ_CHECK_STACK();
97
98 PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
99 PJ_EBUG);
100
101 if (timeout) {
102 os_timeout.tv_sec = timeout->sec;
103 os_timeout.tv_usec = timeout->msec * 1000;
104 p_os_timeout = &os_timeout;
105 } else {
106 p_os_timeout = NULL;
107 }
108
109 return select(n, PART_FDSET_OR_NULL(readfds), PART_FDSET_OR_NULL(writefds),
110 PART_FDSET_OR_NULL(exceptfds), p_os_timeout);
111}
112