xorp

win_dispatcher.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/win_dispatcher.hh,v 1.23 2008/10/02 21:57:36 bms Exp $
00023 
00024 #ifndef __LIBXORP_WIN_DISPATCHER_HH__
00025 #define __LIBXORP_WIN_DISPATCHER_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 #include "libxorp/xorpfd.hh"
00029 #include "libxorp/timeval.hh"
00030 #include "libxorp/ioevents.hh"
00031 #include "libxorp/task.hh"
00032 
00033 #include <vector>
00034 #include <map>
00035 
00036 
00037 #ifdef HOST_OS_WINDOWS
00038 
00039 class ClockBase;
00040 
00044 struct IoEventTuple {
00045 private:
00046     XorpFd  _fd;
00047     IoEventType _type;
00048 public:
00049     IoEventTuple(XorpFd fd, IoEventType type) : _fd(fd), _type(type) {}
00050 
00051     XorpFd fd()     { return (_fd); };
00052     IoEventType type()  { return (_type); };
00053 
00054     bool operator ==(const IoEventTuple& rhand) const {
00055     return ((_fd == rhand._fd) && (_type == rhand._type));
00056     }
00057 
00058     bool operator !=(const IoEventTuple& rhand) const {
00059     return (!((_fd == rhand._fd) && (_type == rhand._type)));
00060     }
00061 
00062     bool operator >(const IoEventTuple& rhand) const {
00063     if (_fd != rhand._fd)
00064         return ((int)_type > (int)rhand._type);
00065     else
00066         return (_fd > rhand._fd);
00067     }
00068 
00069     bool operator <(const IoEventTuple& rhand) const {
00070     if (_fd != rhand._fd)
00071         return (_fd < rhand._fd);
00072     else
00073         return ((int)_type < (int)rhand._type);
00074     }
00075 };
00076 
00081 typedef map<IoEventTuple, IoEventCb>    IoEventMap;
00082 
00088 typedef map<HANDLE, XorpFd>     EventSocketMap;
00089 
00093 typedef std::pair<HANDLE, long>     MaskEventPair;
00094 
00098 typedef map<SOCKET, MaskEventPair>  SocketEventMap;
00099 
00104 #define WSAEVENT_SUPPORTED  \
00105     (FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE)
00106 
00141 class WinDispatcher {
00142 public:
00146     WinDispatcher(ClockBase *clock);
00147 
00151     virtual ~WinDispatcher();
00152 
00181     bool add_ioevent_cb(XorpFd      fd, 
00182             IoEventType type, 
00183             const IoEventCb& cb,
00184             int     priority = XorpTask::PRIORITY_DEFAULT);
00185 
00194     bool remove_ioevent_cb(XorpFd fd, IoEventType type);
00195 
00201     bool ready();
00202 
00208     int get_ready_priority();
00209 
00216     void wait_and_dispatch(TimeVal& timeout) {
00217     if (timeout == TimeVal::MAXIMUM())
00218         wait_and_dispatch(INFINITE);
00219     else
00220         wait_and_dispatch(timeout.to_ms());
00221     }
00222 
00229     void wait_and_dispatch(int ms);
00230 
00236     size_t descriptor_count() const { return _descriptor_count; }
00237 
00238 protected:
00242     void dispatch_sockevent(HANDLE hevent, XorpFd fd);
00243 
00244 private:
00245 
00247     bool do_wait_and_dispatch(vector<HANDLE>& handles, int ms, bool first);
00248 
00249     bool add_socket_cb(XorpFd& fd, IoEventType type, const IoEventCb& cb,
00250                int priority);
00251     bool add_handle_cb(XorpFd& fd, IoEventType type, const IoEventCb& cb,
00252                int priority);
00253     void callback_bad_handle();
00254     void callback_bad_socket(XorpFd& fd);
00255     bool remove_socket_cb(XorpFd& fd, IoEventType type);
00256     bool remove_handle_cb(XorpFd& fd, IoEventType type);
00257     void dispatch_pipe_reads();
00258 
00259 private:
00260     static const int POLLED_INTERVAL_MS = 250;
00261 
00262 private:
00263     ClockBase*      _clock;         // System clock
00264 
00265     IoEventMap      _callback_map;      // XorpFd + IoEventType -> Callback
00266     SocketEventMap  _socket_event_map;  // Socket -> Event + Mask
00267     EventSocketMap  _event_socket_map;  // Event -> Socket
00268     vector<HANDLE>  _handles;       // All Win32 handles pending wait
00269     vector<HANDLE>  _polled_pipes;      // Pipe handles pending poll
00270     size_t      _descriptor_count;  // Count for socket/event handlers
00271 };
00272 
00273 #endif // HOST_OS_WINDOWS
00274 
00275 #endif // __LIBXORP_WIN_DISPATCHER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations