libutil++  1.9.3
 All Classes Functions Variables
SocketMux.h
1 /*
2 ** libutil++
3 ** $Id: SocketMux.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__net__SocketMux_H__
10 #define __libutilxx__sella__net__SocketMux_H__
11 
12 #include "../../common.h"
13 #include "Socket.h"
14 #include "SocketException.h"
15 
16 #include <sys/select.h>
17 #include <sys/epoll.h>
18 
19 #include <map>
20 #include <forward_list>
21 
22 /* NOTE: Current implementation limited to FD_SETSIZE(1024) for select and epoll. */
23 
24 /* NOTE: EPOLL_MAXEVENTS_DEF
25  Max events that will be returned per epoll_wait() call.
26  This does not limit the total number of sockets epoll will monitor.
27 */
28 #define EPOLL_MAXEVENTS_DEF 128
29 
30 namespace sella {
31  namespace net {
32  class SocketMux;
33  }
34 }
35 
36 /* NOTE: When using epoll(), it is recommended to set socket->setBlock(false) to
37  handle false positives. This may also happen with select().
38 */
39 
41  public:
42  typedef std::shared_ptr<SocketMux> shared;
43  typedef SocketMux *ptr;
44  typedef std::map<int, Socket::shared> int_shared_map;
45  typedef std::forward_list<Socket::shared> shared_flist;
46 
47  public:
48  SocketMux(ssize_t epollMaxEvents = EPOLL_MAXEVENTS_DEF, bool pthread_cancel = true) throw (SocketException);
49  virtual ~SocketMux();
50 
51  int epoll(struct timeval *tv = NULL) throw (SocketException);
52  int epoll(uint64_t usec) throw (SocketException);
53 
54  int select(struct timeval *tv = NULL) throw (SocketException);
55  int select(uint64_t usec) throw (SocketException);
56  int rselect(struct timeval *tv = NULL) throw (SocketException);
57  int rselect(uint64_t usec) throw (SocketException);
58  int wselect(struct timeval *tv = NULL) throw (SocketException);
59  int wselect(uint64_t usec) throw (SocketException);
60  int eselect(struct timeval *tv = NULL) throw (SocketException);
61  int eselect(uint64_t usec) throw (SocketException);
62  int rwselect(struct timeval *tv = NULL) throw (SocketException);
63  int rwselect(uint64_t usec) throw (SocketException);
64 
65  void clear(void) throw (SocketException);
66  size_t size(void) const;
67 
68  const shared_flist& getReadList(void);
69  const shared_flist& getWriteList(void);
70  const shared_flist& getExceptList(void);
71 
72  bool insertRead(Socket::shared socket) throw (SocketException);
73  bool removeRead(Socket::shared socket) throw (SocketException);
74 
75  bool insertWrite(Socket::shared socket) throw (SocketException);
76  bool removeWrite(Socket::shared socket) throw (SocketException);
77 
78  bool insertExcept(Socket::shared socket) throw (SocketException);
79  bool removeExcept(Socket::shared socket) throw (SocketException);
80 
81  static const SocketMux::shared shared_ptr(ssize_t epollMaxEvents = EPOLL_MAXEVENTS_DEF, bool pthread_cancel = true) throw (SocketException);
82 
83  protected:
84 
85  private:
86  bool insert(Socket::shared socket, fd_set *set, int_shared_map *map, uint32_t events) throw (SocketException);
87  bool remove(Socket::shared socket, fd_set *set, int_shared_map *map, uint32_t events) throw (SocketException);
88 
89  void clearLists(void);
90 
91  int e;
92  int fdmax;
93  bool cancel;
94 
95  fd_set rfdset;
96  fd_set wfdset;
97  fd_set efdset;
98 
99  int_shared_map rMap;
100  int_shared_map wMap;
101  int_shared_map eMap;
102 
103  shared_flist rList;
104  shared_flist wList;
105  shared_flist eList;
106 
107  ssize_t epollMaxEvents;
108  struct epoll_event *events;
109 };
110 
111 #endif
112 
113 /*
114 ** vim: noet ts=3 sw=3
115 */