00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__SocketMux_H__
00010 #define __libutilxx__sella__net__SocketMux_H__
00011
00012 #include "../../common.h"
00013 #include "Socket.h"
00014 #include "SocketException.h"
00015
00016 #include <sys/select.h>
00017 #include <sys/epoll.h>
00018
00019 #include <map>
00020 #include <forward_list>
00021
00022
00023
00024
00025
00026
00027
00028 #define EPOLL_MAXEVENTS_DEF 128
00029
00030 namespace sella {
00031 namespace net {
00032 class SocketMux;
00033 }
00034 }
00035
00036
00037
00038
00039
00040 class sella::net::SocketMux {
00041 public:
00042 typedef std::shared_ptr<SocketMux> shared;
00043 typedef SocketMux *ptr;
00044 typedef std::map<int, Socket::shared> int_shared_map;
00045 typedef std::forward_list<Socket::shared> shared_flist;
00046
00047 public:
00048 SocketMux(ssize_t epollMaxEvents = EPOLL_MAXEVENTS_DEF, bool pthread_cancel = true) throw (SocketException);
00049 virtual ~SocketMux();
00050
00051 int epoll(struct timeval *tv = NULL) throw (SocketException);
00052
00053 int select(struct timeval *tv = NULL) throw (SocketException);
00054 int rselect(struct timeval *tv = NULL) throw (SocketException);
00055 int wselect(struct timeval *tv = NULL) throw (SocketException);
00056 int eselect(struct timeval *tv = NULL) throw (SocketException);
00057 int rwselect(struct timeval *tv = NULL) throw (SocketException);
00058
00059 void clear(void) throw (SocketException);
00060 size_t size(void) const;
00061
00062 const shared_flist& getReadList(void);
00063 const shared_flist& getWriteList(void);
00064 const shared_flist& getExceptList(void);
00065
00066 bool insertRead(Socket::shared socket) throw (SocketException);
00067 bool removeRead(Socket::shared socket) throw (SocketException);
00068
00069 bool insertWrite(Socket::shared socket) throw (SocketException);
00070 bool removeWrite(Socket::shared socket) throw (SocketException);
00071
00072 bool insertExcept(Socket::shared socket) throw (SocketException);
00073 bool removeExcept(Socket::shared socket) throw (SocketException);
00074
00075 static const SocketMux::shared shared_ptr(ssize_t epollMaxEvents = EPOLL_MAXEVENTS_DEF, bool pthread_cancel = true) throw (SocketException);
00076
00077 protected:
00078
00079 private:
00080 bool insert(Socket::shared socket, fd_set *set, int_shared_map *map, uint32_t events) throw (SocketException);
00081 bool remove(Socket::shared socket, fd_set *set, int_shared_map *map, uint32_t events) throw (SocketException);
00082
00083 void clearLists(void);
00084
00085 int e;
00086 int fdmax;
00087 bool cancel;
00088
00089 fd_set rfdset;
00090 fd_set wfdset;
00091 fd_set efdset;
00092
00093 int_shared_map rMap;
00094 int_shared_map wMap;
00095 int_shared_map eMap;
00096
00097 shared_flist rList;
00098 shared_flist wList;
00099 shared_flist eList;
00100
00101 ssize_t epollMaxEvents;
00102 struct epoll_event *events;
00103 };
00104
00105 #endif
00106
00107
00108
00109