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