Undo test commit.
[seven-1.x.git] / libseven / ports.c
blob3b9a9f1593732d11a176938ab481c2338f775592
1 /*
2 * charybdis: A slightly useful ircd.
3 * ports.c: Solaris ports compatible network routines.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
8 * Copyright (C) 2002-2004 ircd-ratbox development team
9 * Copyright (C) 2005 Edward Brocklesby.
10 * Copyright (C) 2005 William Pitcock and Jilles Tjoelker
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * USA
27 * $Id: ports.c 181 2006-12-18 02:56:07Z beu $
30 #include "stdinc.h"
31 #include <port.h>
32 #include <time.h>
34 #include "libseven.h"
36 #define PE_LENGTH 128
38 static void pe_update_events(fde_t *, short, PF *);
39 static int pe;
40 static struct timespec zero_timespec;
42 static port_event_t *pelst; /* port buffer */
43 static int pemax; /* max structs to buffer */
45 int
46 ircd_setup_fd(int fd)
48 return 0;
52 void
53 pe_update_events(fde_t * F, short filter, PF * handler)
55 PF *cur_handler = NULL;
57 if (filter == POLLRDNORM)
58 cur_handler = F->read_handler;
59 else if (filter == POLLWRNORM)
60 cur_handler = F->write_handler;
62 if (!cur_handler && handler)
63 port_associate(pe, PORT_SOURCE_FD, F->fd, filter, F);
64 else if (cur_handler && !handler)
65 port_dissociate(pe, PORT_SOURCE_FD, F->fd);
68 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
69 /* Public functions */
73 * init_netio
75 * This is a needed exported function which will be called to initialise
76 * the network loop code.
78 void
79 init_netio(void)
81 if((pe = port_create()) < 0) {
82 libseven_log("init_netio: Couldn't open port fd!\n");
83 exit(115); /* Whee! */
85 pemax = getdtablesize();
86 pelst = MyMalloc(sizeof(port_event_t) * pemax);
87 zero_timespec.tv_sec = 0;
88 zero_timespec.tv_nsec = 0;
92 * ircd_setselect
94 * This is a needed exported function which will be called to register
95 * and deregister interest in a pending IO state for a given FD.
97 void
98 ircd_setselect(int fd, fdlist_t list, unsigned int type, PF * handler,
99 void *client_data)
101 fde_t *F = &fd_table[fd];
102 s_assert(fd >= 0);
103 s_assert(F->flags.open);
105 /* Update the list, even though we're not using it .. */
106 F->list = list;
108 if(type & IRCD_SELECT_READ) {
109 pe_update_events(F, POLLRDNORM, handler);
110 F->read_handler = handler;
111 F->read_data = client_data;
113 if(type & IRCD_SELECT_WRITE) {
114 pe_update_events(F, POLLWRNORM, handler);
115 F->write_handler = handler;
116 F->write_data = client_data;
121 * ircd_select
123 * Called to do the new-style IO, courtesy of squid (like most of this
124 * new IO code). This routine handles the stuff we've hidden in
125 * ircd_setselect and fd_table[] and calls callbacks for IO ready
126 * events.
130 ircd_select(unsigned long delay)
132 int i, fd;
133 uint nget = 1;
134 struct timespec poll_time;
135 struct timer_data *tdata;
137 poll_time.tv_sec = delay / 1000;
138 poll_time.tv_nsec = (delay % 1000) * 1000000;
140 i = port_getn(pe, pelst, pemax, &nget, &poll_time);
141 ircd_set_time();
143 if (i == -1)
144 return COMM_ERROR;
146 for (i = 0; i < nget; i++) {
147 switch(pelst[i].portev_source) {
148 case PORT_SOURCE_FD:
149 fd = pelst[i].portev_object;
150 PF *hdl = NULL;
151 fde_t *F = &fd_table[fd];
153 if ((pelst[i].portev_events & POLLRDNORM) && (hdl = F->read_handler)) {
154 F->read_handler = NULL;
155 hdl(fd, F->read_data);
157 if ((pelst[i].portev_events & POLLWRNORM) && (hdl = F->write_handler)) {
158 F->write_handler = NULL;
159 hdl(fd, F->write_data);
161 break;
163 case PORT_SOURCE_TIMER:
164 tdata = pelst[i].portev_user;
165 tdata->td_cb(tdata->td_udata);
167 if (!tdata->td_repeat)
168 free(tdata);
170 break;
173 return COMM_OK;