1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2012 Index Data and Mike Taylor
3 * See the file LICENSE for details.
13 #include <sys/types.h>
27 #include <yazpp/socket-manager.h>
30 using namespace yazpp_1;
32 struct SocketManager::SocketEntry {
33 ISocketObserver *observer;
42 struct SocketManager::SocketEvent {
43 ISocketObserver *observer;
45 SocketEvent *next; // front in queue
46 SocketEvent *prev; // back in queue
49 struct SocketManager::Rep {
50 SocketEntry *observers; // all registered observers
51 SocketEvent *queue_front;
52 SocketEvent *queue_back;
56 SocketManager::SocketEntry **SocketManager::lookupObserver(
57 ISocketObserver *observer)
61 for (se = &m_p->observers; *se; se = &(*se)->next)
62 if ((*se)->observer == observer)
67 int SocketManager::getNumberOfObservers()
71 for (se = m_p->observers; se; se = se->next, i++)
76 void SocketManager::addObserver(int fd, ISocketObserver *observer)
80 se = *lookupObserver(observer);
84 se->next= m_p->observers;
86 se->observer = observer;
90 se->last_activity = 0;
94 void SocketManager::deleteObserver(ISocketObserver *observer)
96 SocketEntry **se = lookupObserver(observer);
99 removeEvent(observer);
100 SocketEntry *se_tmp = *se;
106 void SocketManager::deleteObservers()
108 SocketEntry *se = m_p->observers;
112 SocketEntry *se_next = se->next;
119 void SocketManager::maskObserver(ISocketObserver *observer, int mask)
123 yaz_log(m_p->log, "obs=%p read=%d write=%d except=%d", observer,
124 mask & SOCKET_OBSERVE_READ,
125 mask & SOCKET_OBSERVE_WRITE,
126 mask & SOCKET_OBSERVE_EXCEPT);
128 se = *lookupObserver(observer);
133 void SocketManager::timeoutObserver(ISocketObserver *observer,
138 se = *lookupObserver(observer);
140 se->timeout = timeout;
143 void SocketManager::inspect_poll_result(int res, struct yaz_poll_fd *fds,
144 int no_fds, int timeout)
147 yaz_log(m_p->log, "yaz_poll returned res=%d", res);
148 time_t now = time(0);
150 int no_put_events = 0;
151 int no_lost_observers = 0;
153 for (i = 0; i < no_fds; i++)
156 for (p = m_p->observers; p; p = p->next)
157 if (p->fd == fds[i].fd)
161 // m_p->observers list changed since poll started
166 enum yaz_poll_mask output_mask = fds[i].output_mask;
169 if (output_mask & yaz_poll_read)
170 mask |= SOCKET_OBSERVE_READ;
172 if (output_mask & yaz_poll_write)
173 mask |= SOCKET_OBSERVE_WRITE;
175 if (output_mask & yaz_poll_except)
176 mask |= SOCKET_OBSERVE_EXCEPT;
180 SocketEvent *event = new SocketEvent;
181 p->last_activity = now;
182 event->observer = p->observer;
186 yaz_log(m_p->log, "putEvent I/O mask=%d", mask);
188 else if (res == 0 && p->timeout_this == timeout)
190 SocketEvent *event = new SocketEvent;
191 assert(p->last_activity);
192 yaz_log(m_p->log, "putEvent timeout fd=%d, now = %ld "
193 "last_activity=%ld timeout=%d",
194 p->fd, now, p->last_activity, p->timeout);
195 p->last_activity = now;
196 event->observer = p->observer;
197 event->event = SOCKET_OBSERVE_TIMEOUT;
203 SocketEvent *event = getEvent();
206 event->observer->socketNotify(event->event);
211 if (no_lost_observers == 0)
214 yaz_log(YLOG_WARN, "unhandled socket event. yaz_poll returned %d",
216 yaz_log(YLOG_WARN, "no_put_events=%d no_fds=%d i=%d timeout=%d",
217 no_put_events, no_fds, i, timeout);
222 int SocketManager::processEvent()
225 SocketEvent *event = getEvent();
227 yaz_log(m_p->log, "SocketManager::processEvent manager=%p", this);
230 event->observer->socketNotify(event->event);
236 time_t now = time(0);
239 for (p = m_p->observers; p; p = p->next)
244 struct yaz_poll_fd *fds = new yaz_poll_fd [no_fds];
245 for (i = 0, p = m_p->observers; p; p = p->next, i++)
249 if (p->mask & SOCKET_OBSERVE_READ)
250 input_mask += yaz_poll_read;
251 if (p->mask & SOCKET_OBSERVE_WRITE)
252 input_mask += yaz_poll_write;
253 if (p->mask & SOCKET_OBSERVE_EXCEPT)
254 input_mask += yaz_poll_except;
255 if (p->timeout > 0 ||
256 (p->timeout == 0 && (p->mask & SOCKET_OBSERVE_WRITE) == 0))
259 timeout_this = p->timeout;
260 if (p->last_activity)
261 timeout_this -= now - p->last_activity;
263 p->last_activity = now;
264 if (timeout_this < 0 || timeout_this > 2147483646)
266 if (timeout == -1 || timeout_this < timeout)
267 timeout = timeout_this;
268 p->timeout_this = timeout_this;
269 yaz_log(m_p->log, "SocketManager::select timeout_this=%d",
273 p->timeout_this = -1;
274 fds[i].input_mask = (enum yaz_poll_mask) input_mask;
278 while ((res = yaz_poll(fds, no_fds, timeout, 0)) < 0 && pass < 10)
285 yaz_log(YLOG_ERRNO|YLOG_WARN, "yaz_poll");
286 yaz_log(YLOG_WARN, "errno=%d timeout=%d", errno, timeout);
290 inspect_poll_result(res, fds, no_fds, timeout);
293 return res >= 0 ? 1 : -1;
296 // n p n p ...... n p n p
299 void SocketManager::putEvent(SocketEvent *event)
301 // put in back of queue
304 m_p->queue_back->prev = event;
305 assert(m_p->queue_front);
309 assert(!m_p->queue_front);
310 m_p->queue_front = event;
312 event->next = m_p->queue_back;
314 m_p->queue_back = event;
317 SocketManager::SocketEvent *SocketManager::getEvent()
319 // get from front of queue
320 SocketEvent *event = m_p->queue_front;
323 assert(m_p->queue_back);
324 m_p->queue_front = event->prev;
325 if (m_p->queue_front)
327 assert(m_p->queue_back);
328 m_p->queue_front->next = 0;
335 void SocketManager::removeEvent(ISocketObserver *observer)
337 SocketEvent *ev = m_p->queue_back;
340 SocketEvent *ev_next = ev->next;
341 if (observer == ev->observer)
344 ev->prev->next = ev->next;
346 m_p->queue_back = ev->next;
348 ev->next->prev = ev->prev;
350 m_p->queue_front = ev->prev;
357 SocketManager::SocketManager()
361 m_p->queue_front = 0;
363 m_p->log = YLOG_DEBUG;
366 SocketManager::~SocketManager()
374 * c-file-style: "Stroustrup"
375 * indent-tabs-mode: nil
377 * vim: shiftwidth=4 tabstop=8 expandtab