ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_select.c
Revision: 2650
Committed: Tue Dec 10 19:46:48 2013 UTC (11 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 4599 byte(s)
Log Message:
- s_bsd_select.c, s_bsd_devpoll.c, s_bsd_epoll.c,
  s_bsd_kqueue.c, s_bsd_poll.c: style corrections

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * s_bsd_select.c: select() compatible network routines.
4     *
5     * Originally by Adrian Chadd <adrian@creative.net.au>
6     * Copyright (C) 2002 Hybrid Development Team
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21     * USA
22     *
23 knight 31 * $Id$
24 adx 30 */
25    
26     #include "stdinc.h"
27 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_SELECT
28 michael 1021 #include "list.h"
29 adx 30 #include "fdlist.h"
30     #include "hook.h"
31     #include "ircd.h"
32     #include "s_bsd.h"
33 michael 1309 #include "log.h"
34 adx 30
35     /*
36     * Note that this is only a single list - multiple lists is kinda pointless
37     * under select because the list size is a function of the highest FD :-)
38     * -- adrian
39     */
40    
41     static fd_set select_readfds, tmpreadfds;
42     static fd_set select_writefds, tmpwritefds;
43     static int highest_fd = -1;
44    
45    
46     /*
47     * init_netio
48     *
49     * This is a needed exported function which will be called to initialise
50     * the network loop code.
51     */
52     void
53     init_netio(void)
54     {
55     FD_ZERO(&select_readfds);
56     FD_ZERO(&select_writefds);
57     }
58    
59     /*
60     * comm_setselect
61     *
62     * This is a needed exported function which will be called to register
63     * and deregister interest in a pending IO state for a given FD.
64     */
65     void
66     comm_setselect(fde_t *F, unsigned int type, PF *handler,
67     void *client_data, time_t timeout)
68     {
69     int new_events;
70    
71     if ((type & COMM_SELECT_READ))
72     {
73     F->read_handler = handler;
74     F->read_data = client_data;
75     }
76    
77     if ((type & COMM_SELECT_WRITE))
78     {
79     F->write_handler = handler;
80     F->write_data = client_data;
81     }
82    
83     new_events = (F->read_handler ? COMM_SELECT_READ : 0) |
84 michael 2650 (F->write_handler ? COMM_SELECT_WRITE : 0);
85 adx 30
86     if (timeout != 0)
87     F->timeout = CurrentTime + (timeout / 1000);
88    
89     if (new_events != F->evcache)
90     {
91     if ((new_events & COMM_SELECT_READ))
92     FD_SET(F->fd, &select_readfds);
93     else
94     {
95     FD_CLR(F->fd, &select_readfds);
96     FD_CLR(F->fd, &tmpreadfds);
97     }
98    
99     if ((new_events & COMM_SELECT_WRITE))
100     FD_SET(F->fd, &select_writefds);
101     else
102     {
103     FD_CLR(F->fd, &select_writefds);
104     FD_CLR(F->fd, &tmpwritefds);
105     }
106    
107     if (new_events == 0)
108     {
109     if (highest_fd == F->fd)
110     while (highest_fd >= 0 && (FD_ISSET(highest_fd, &select_readfds) ||
111 michael 2650 FD_ISSET(highest_fd, &select_writefds)))
112 adx 30 highest_fd--;
113     }
114     else if (F->evcache == 0)
115     if (F->fd > highest_fd)
116     highest_fd = F->fd;
117    
118     F->evcache = new_events;
119     }
120     }
121    
122     /*
123     * comm_select
124     *
125     * Called to do the new-style IO, courtesy of squid (like most of this
126     * new IO code). This routine handles the stuff we've hidden in
127     * comm_setselect and fd_table[] and calls callbacks for IO ready
128     * events.
129     */
130     void
131     comm_select(void)
132     {
133     struct timeval to;
134     int num, fd;
135     fde_t *F;
136     PF *hdl;
137    
138     /* Copy over the read/write sets so we don't have to rebuild em */
139     memcpy(&tmpreadfds, &select_readfds, sizeof(fd_set));
140     memcpy(&tmpwritefds, &select_writefds, sizeof(fd_set));
141    
142     to.tv_sec = 0;
143     to.tv_usec = SELECT_DELAY * 1000;
144     num = select(highest_fd + 1, &tmpreadfds, &tmpwritefds, NULL, &to);
145    
146     set_time();
147    
148     if (num < 0)
149     {
150     #ifdef HAVE_USLEEP
151     usleep(50000);
152     #endif
153     return;
154     }
155    
156     for (fd = 0; fd <= highest_fd && num > 0; fd++)
157 michael 2650 {
158 adx 30 if (FD_ISSET(fd, &tmpreadfds) || FD_ISSET(fd, &tmpwritefds))
159     {
160     num--;
161    
162     F = lookup_fd(fd);
163     if (F == NULL || !F->flags.open)
164     continue;
165    
166     if (FD_ISSET(fd, &tmpreadfds))
167 michael 2650 {
168 adx 30 if ((hdl = F->read_handler) != NULL)
169     {
170     F->read_handler = NULL;
171     hdl(F, F->read_data);
172     if (!F->flags.open)
173     continue;
174     }
175 michael 2650 }
176 adx 30
177     if (FD_ISSET(fd, &tmpwritefds))
178 michael 2650 {
179 adx 30 if ((hdl = F->write_handler) != NULL)
180     {
181     F->write_handler = NULL;
182     hdl(F, F->write_data);
183     if (!F->flags.open)
184     continue;
185     }
186 michael 2650 }
187 adx 30
188     comm_setselect(F, 0, NULL, NULL, 0);
189     }
190 michael 2650 }
191 adx 30 }
192 stu 908 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision