1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/* select.c, liboop, copyright 1999 Dan Egnor
This is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License, version 2.1 or later.
See the file COPYING for details. */
#include "oop.h"
#include <assert.h>
#ifdef HAVE_STRING_H
# include <string.h> /* Needed on NetBSD1.1/SPARC due to bzero/FD_ZERO. */
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h> /* Needed on AIX 4.2 due to bzero/FD_ZERO. */
#endif
struct select_set {
fd_set rfd,wfd,xfd;
};
struct oop_adapter_select {
oop_source *source;
struct select_set watch,active;
struct timeval timeout;
int num_fd,do_timeout,is_active,num_fd_active;
oop_call_select *call;
void *data;
};
static oop_call_fd on_fd;
static oop_call_time on_timeout,on_collect;
oop_adapter_select *oop_select_new(
oop_source *source,
oop_call_select *call,void *data)
{
oop_adapter_select *s = oop_malloc(sizeof(*s));
if (NULL == s) return s;
s->source = source;
FD_ZERO(&s->watch.rfd);
FD_ZERO(&s->watch.wfd);
FD_ZERO(&s->watch.xfd);
FD_ZERO(&s->active.rfd);
FD_ZERO(&s->active.wfd);
FD_ZERO(&s->active.xfd);
s->num_fd = 0;
s->num_fd_active = 0;
s->do_timeout = 0;
s->is_active = 0;
s->call = call;
s->data = data;
return s;
}
static void *activate(oop_adapter_select *s) {
if (!s->is_active) {
s->is_active = 1;
s->source->on_time(s->source,OOP_TIME_NOW,on_collect,s);
}
return OOP_CONTINUE;
}
static void deactivate(oop_adapter_select *s) {
if (s->is_active) {
s->source->cancel_time(s->source,OOP_TIME_NOW,on_collect,s);
s->is_active = 0;
s->num_fd_active = 0;
FD_ZERO(&s->active.rfd);
FD_ZERO(&s->active.wfd);
FD_ZERO(&s->active.xfd);
}
}
void oop_select_set(
oop_adapter_select *s,int num_fd,
fd_set *rfd,fd_set *wfd,fd_set *xfd,struct timeval *timeout)
{
int fd;
for (fd = 0; fd < num_fd || fd < s->num_fd; ++fd) {
int rfd_set = fd < num_fd && FD_ISSET(fd,rfd);
int wfd_set = fd < num_fd && FD_ISSET(fd,wfd);
int xfd_set = fd < num_fd && FD_ISSET(fd,xfd);
int w_rfd_set = fd < s->num_fd && FD_ISSET(fd,&s->watch.rfd);
int w_wfd_set = fd < s->num_fd && FD_ISSET(fd,&s->watch.wfd);
int w_xfd_set = fd < s->num_fd && FD_ISSET(fd,&s->watch.xfd);
if (rfd_set && !w_rfd_set) {
s->source->on_fd(s->source,fd,OOP_READ,on_fd,s);
FD_SET(fd,&s->watch.rfd);
}
if (!rfd_set && w_rfd_set) {
s->source->cancel_fd(s->source,fd,OOP_READ);
FD_CLR(fd,&s->watch.rfd);
}
if (wfd_set && !w_wfd_set) {
s->source->on_fd(s->source,fd,OOP_WRITE,on_fd,s);
FD_SET(fd,&s->watch.wfd);
}
if (!wfd_set && w_wfd_set) {
s->source->cancel_fd(s->source,fd,OOP_WRITE);
FD_CLR(fd,&s->watch.wfd);
}
if (xfd_set && !w_xfd_set) {
s->source->on_fd(s->source,fd,OOP_EXCEPTION,on_fd,s);
FD_SET(fd,&s->watch.xfd);
}
if (!xfd_set && w_xfd_set) {
s->source->cancel_fd(s->source,fd,OOP_EXCEPTION);
FD_CLR(fd,&s->watch.xfd);
}
}
s->num_fd = num_fd;
if (s->do_timeout) {
s->source->cancel_time(s->source,s->timeout,on_timeout,s);
s->do_timeout = 0;
}
if (NULL != timeout) {
gettimeofday(&s->timeout,NULL);
s->timeout.tv_sec += timeout->tv_sec;
s->timeout.tv_usec += timeout->tv_usec;
while (s->timeout.tv_usec >= 1000000) {
++s->timeout.tv_sec;
s->timeout.tv_usec -= 1000000;
}
s->do_timeout = 1;
s->source->on_time(s->source,s->timeout,on_timeout,s);
}
deactivate(s);
}
void oop_select_delete(oop_adapter_select *s) {
fd_set fd;
FD_ZERO(&fd);
oop_select_set(s,0,&fd,&fd,&fd,NULL);
oop_free(s);
}
static void set_fd(int fd,fd_set *fds,int *num) {
if (!FD_ISSET(fd,fds)) {
FD_SET(fd,fds);
if (fd >= *num) *num = fd + 1;
}
}
static void *on_fd(oop_source *source,int fd,oop_event event,void *data) {
oop_adapter_select *s = (oop_adapter_select *) data;
switch (event) {
case OOP_READ:
assert(FD_ISSET(fd,&s->watch.rfd));
set_fd(fd,&s->active.rfd,&s->num_fd_active);
break;
case OOP_WRITE:
assert(FD_ISSET(fd,&s->watch.wfd));
set_fd(fd,&s->active.wfd,&s->num_fd_active);
break;
case OOP_EXCEPTION:
assert(FD_ISSET(fd,&s->watch.xfd));
set_fd(fd,&s->active.xfd,&s->num_fd_active);
break;
default:
assert(0);
break;
}
return activate(s);
}
static void *on_timeout(oop_source *source,struct timeval when,void *data) {
oop_adapter_select *s = (oop_adapter_select *) data;
assert(s->do_timeout);
return activate(s);
}
static void *on_collect(oop_source *source,struct timeval when,void *data) {
oop_adapter_select *s = (oop_adapter_select *) data;
struct select_set set = s->active;
int num = s->num_fd_active;
struct timeval now;
gettimeofday(&now,NULL);
deactivate(s);
return s->call(s,num,&set.rfd,&set.wfd,&set.xfd,now,s->data);
}
|