00001 #ifndef N2NC_NETSOCKETEVENTSHANDLER_H
00002 #define N2NC_NETSOCKETEVENTSHANDLER_H
00003
00004 #include "nixsys.h"
00005 #include "thread.h"
00006 #include "socket.h"
00007 #include "mutex.h"
00008 #include "condition.h"
00009
00010
00011
00012 namespace n2nc {
00013 namespace net {
00014
00015 class SocketEvents;
00016
00026 class SocketEventsHandler : n2nc::sync::Thread{
00027 public:
00029 SocketEventsHandler();
00030 ~SocketEventsHandler();
00031
00032 enum check_for_t{
00033 READ = 1,
00034 WRITE = 2,
00035 EXCEPT = 4,
00036 NOTHING = 8
00037 };
00041 int add(Socket &sock,check_for_t checkfor, SocketEvents& se);
00043 int del(Socket &sock);
00044
00045 bool enableSocket(const Socket& sock);
00046 bool disableSocket(const Socket& sock);
00047
00049 int waitForEvents();
00050
00051 struct socket_extra_t{
00052 check_for_t check;
00053 SocketEvents* se ;
00054 Socket* sock ;
00055 n2nc::sync::Condition* wait ;
00056 int last_errno ;
00057 };
00058
00059 virtual void* entry_point();
00060
00061 private:
00062
00063 int _main_loop();
00064
00065 int computeMaxFD();
00066 int tryAddMaxFD(Socket::fd_t fd);
00067 int tryAddMaxFD(const std::pair<Socket::fd_t,socket_extra_t*>& mypair);
00068
00069 std::map<Socket::fd_t,socket_extra_t*> m_map_new ;
00070 int m_max_fd;
00071
00072 std::vector<Socket*> m_dispatched ;
00073 n2nc::sync::Mutex m_lock ;
00074 int m_wakeup[2];
00075 pthread_t m_this_thread ;
00076
00077
00078 };
00079
00080
00081
00082
00083 }
00084 }
00085
00086 #endif
00087