diff options
author | Attila Molnar <attilamolnar@hush.com> | 2014-02-08 23:01:44 +0100 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2014-02-08 23:01:44 +0100 |
commit | efe77ba63b1e519fc4d563bd9b599277c4bd96e5 (patch) | |
tree | 85eff52bd7735d19f3bac254ad9cce1fb863de43 /src/socketengines | |
parent | 689996cb8856af43a033eb5ca1cb9df7475c0854 (diff) |
Change all socketengine methods to be static
Diffstat (limited to 'src/socketengines')
-rw-r--r-- | src/socketengines/socketengine_epoll.cpp | 48 | ||||
-rw-r--r-- | src/socketengines/socketengine_kqueue.cpp | 54 | ||||
-rw-r--r-- | src/socketengines/socketengine_poll.cpp | 48 | ||||
-rw-r--r-- | src/socketengines/socketengine_ports.cpp | 51 | ||||
-rw-r--r-- | src/socketengines/socketengine_select.cpp | 49 |
5 files changed, 101 insertions, 149 deletions
diff --git a/src/socketengines/socketengine_epoll.cpp b/src/socketengines/socketengine_epoll.cpp index 39cc60606..961dc8b49 100644 --- a/src/socketengines/socketengine_epoll.cpp +++ b/src/socketengines/socketengine_epoll.cpp @@ -31,27 +31,16 @@ /** A specialisation of the SocketEngine class, designed to use linux 2.6 epoll(). */ -class EPollEngine : public SocketEngine +namespace { -private: - /** These are used by epoll() to hold socket events - */ - std::vector<struct epoll_event> events; int EngineHandle; -public: - /** Create a new EPollEngine - */ - EPollEngine(); - /** Delete an EPollEngine + + /** These are used by epoll() to hold socket events */ - virtual ~EPollEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual void DelFd(EventHandler* eh); - virtual int DispatchEvents(); -}; + std::vector<struct epoll_event> events(1); +} -EPollEngine::EPollEngine() : events(1) +void SocketEngine::Init() { int max = ulimit(4, 0); if (max > 0) @@ -78,9 +67,13 @@ EPollEngine::EPollEngine() : events(1) } } -EPollEngine::~EPollEngine() +void SocketEngine::RecoverFromFork() { - this->Close(EngineHandle); +} + +void SocketEngine::Deinit() +{ + Close(EngineHandle); } static unsigned mask_to_epoll(int event_mask) @@ -106,7 +99,7 @@ static unsigned mask_to_epoll(int event_mask) return rv; } -bool EPollEngine::AddFd(EventHandler* eh, int event_mask) +bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -134,13 +127,13 @@ bool EPollEngine::AddFd(EventHandler* eh, int event_mask) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd); - SocketEngine::SetEventMask(eh, event_mask); + eh->SetEventMask(event_mask); ResizeDouble(events); return true; } -void EPollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) +void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { unsigned old_events = mask_to_epoll(old_mask); unsigned new_events = mask_to_epoll(new_mask); @@ -155,7 +148,7 @@ void EPollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) } } -void EPollEngine::DelFd(EventHandler* eh) +void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -180,7 +173,7 @@ void EPollEngine::DelFd(EventHandler* eh) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd); } -int EPollEngine::DispatchEvents() +int SocketEngine::DispatchEvents() { int i = epoll_wait(EngineHandle, &events[0], events.size(), 1000); ServerInstance->UpdateTime(); @@ -232,7 +225,7 @@ int EPollEngine::DispatchEvents() mask = nm; } } - SetEventMask(eh, mask); + eh->SetEventMask(mask); if (ev.events & EPOLLIN) { stats.ReadEvents++; @@ -250,8 +243,3 @@ int EPollEngine::DispatchEvents() return i; } - -SocketEngine* CreateSocketEngine() -{ - return new EPollEngine; -} diff --git a/src/socketengines/socketengine_kqueue.cpp b/src/socketengines/socketengine_kqueue.cpp index 0a9a0a399..61f5cd922 100644 --- a/src/socketengines/socketengine_kqueue.cpp +++ b/src/socketengines/socketengine_kqueue.cpp @@ -30,28 +30,17 @@ /** A specialisation of the SocketEngine class, designed to use BSD kqueue(). */ -class KQueueEngine : public SocketEngine +namespace { -private: int EngineHandle; /** These are used by kqueue() to hold socket events */ - std::vector<struct kevent> ke_list; -public: - /** Create a new KQueueEngine - */ - KQueueEngine(); - /** Delete a KQueueEngine - */ - virtual ~KQueueEngine(); - bool AddFd(EventHandler* eh, int event_mask); - void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual void DelFd(EventHandler* eh); - virtual int DispatchEvents(); - virtual void RecoverFromFork(); -}; - -KQueueEngine::KQueueEngine() : ke_list(1) + std::vector<struct kevent> ke_list(16); +} + +/** Initialize the kqueue engine + */ +void SocketEngine::Init() { MAX_DESCRIPTORS = 0; int mib[2]; @@ -72,10 +61,10 @@ KQueueEngine::KQueueEngine() : ke_list(1) ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE); } - this->RecoverFromFork(); + RecoverFromFork(); } -void KQueueEngine::RecoverFromFork() +void SocketEngine::RecoverFromFork() { /* * The only bad thing about kqueue is that its fd cant survive a fork and is not inherited. @@ -93,12 +82,14 @@ void KQueueEngine::RecoverFromFork() } } -KQueueEngine::~KQueueEngine() +/** Shutdown the kqueue engine + */ +void SocketEngine::Deinit() { - this->Close(EngineHandle); + Close(EngineHandle); } -bool KQueueEngine::AddFd(EventHandler* eh, int event_mask) +bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); @@ -122,14 +113,14 @@ bool KQueueEngine::AddFd(EventHandler* eh, int event_mask) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd); - SocketEngine::SetEventMask(eh, event_mask); + eh->SetEventMask(event_mask); OnSetEvent(eh, 0, event_mask); ResizeDouble(ke_list); return true; } -void KQueueEngine::DelFd(EventHandler* eh) +void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); @@ -161,7 +152,7 @@ void KQueueEngine::DelFd(EventHandler* eh) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd); } -void KQueueEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) +void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { if ((new_mask & FD_WANT_POLL_WRITE) && !(old_mask & FD_WANT_POLL_WRITE)) { @@ -195,7 +186,7 @@ void KQueueEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) } } -int KQueueEngine::DispatchEvents() +int SocketEngine::DispatchEvents() { struct timespec ts; ts.tv_nsec = 0; @@ -235,21 +226,16 @@ int KQueueEngine::DispatchEvents() * to detect when it set again. */ const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK; - SetEventMask(eh, eh->GetEventMask() & ~bits_to_clr); + eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr); eh->HandleEvent(EVENT_WRITE); } else if (filter == EVFILT_READ) { stats.ReadEvents++; - SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK); + eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK); eh->HandleEvent(EVENT_READ); } } return i; } - -SocketEngine* CreateSocketEngine() -{ - return new KQueueEngine; -} diff --git a/src/socketengines/socketengine_poll.cpp b/src/socketengines/socketengine_poll.cpp index 46a517c51..ac5e02cfd 100644 --- a/src/socketengines/socketengine_poll.cpp +++ b/src/socketengines/socketengine_poll.cpp @@ -43,26 +43,17 @@ /** A specialisation of the SocketEngine class, designed to use poll(). */ -class PollEngine : public SocketEngine +namespace { -private: /** These are used by poll() to hold socket events */ - std::vector<struct pollfd> events; + std::vector<struct pollfd> events(16); /** This vector maps fds to an index in the events array. */ - std::vector<int> fd_mappings; -public: - /** Create a new PollEngine - */ - PollEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual void DelFd(EventHandler* eh); - virtual int DispatchEvents(); -}; - -PollEngine::PollEngine() : events(1), fd_mappings(1) + std::vector<int> fd_mappings(16); +} + +void SocketEngine::Init() { struct rlimit limits; if (!getrlimit(RLIMIT_NOFILE, &limits)) @@ -77,6 +68,14 @@ PollEngine::PollEngine() : events(1), fd_mappings(1) } } +void SocketEngine::Deinit() +{ +} + +void SocketEngine::RecoverFromFork() +{ +} + static int mask_to_poll(int event_mask) { int rv = 0; @@ -87,7 +86,7 @@ static int mask_to_poll(int event_mask) return rv; } -bool PollEngine::AddFd(EventHandler* eh, int event_mask) +bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -119,11 +118,11 @@ bool PollEngine::AddFd(EventHandler* eh, int event_mask) events[index].events = mask_to_poll(event_mask); ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d (%d; index %d)", fd, events[index].events, index); - SocketEngine::SetEventMask(eh, event_mask); + eh->SetEventMask(event_mask); return true; } -void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) +void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { int fd = eh->GetFd(); if (fd < 0 || static_cast<unsigned int>(fd) >= fd_mappings.size() || fd_mappings[fd] == -1) @@ -135,7 +134,7 @@ void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) events[fd_mappings[fd]].events = mask_to_poll(new_mask); } -void PollEngine::DelFd(EventHandler* eh) +void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); if ((fd < 0) || (fd > MAX_DESCRIPTORS)) @@ -178,7 +177,7 @@ void PollEngine::DelFd(EventHandler* eh) "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index); } -int PollEngine::DispatchEvents() +int SocketEngine::DispatchEvents() { int i = poll(&events[0], CurrentSetSize, 1000); int processed = 0; @@ -218,7 +217,7 @@ int PollEngine::DispatchEvents() if (revents & POLLIN) { - SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK); + eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK); eh->HandleEvent(EVENT_READ); if (eh != GetRef(fd)) // whoops, deleted out from under us @@ -229,7 +228,7 @@ int PollEngine::DispatchEvents() { int mask = eh->GetEventMask(); mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE); - SetEventMask(eh, mask); + eh->SetEventMask(mask); // The vector could've been resized, reference can be invalid by now; don't use it events[index].events = mask_to_poll(mask); @@ -239,8 +238,3 @@ int PollEngine::DispatchEvents() return i; } - -SocketEngine* CreateSocketEngine() -{ - return new PollEngine; -} diff --git a/src/socketengines/socketengine_ports.cpp b/src/socketengines/socketengine_ports.cpp index e1fcc0e6c..d7425813e 100644 --- a/src/socketengines/socketengine_ports.cpp +++ b/src/socketengines/socketengine_ports.cpp @@ -36,27 +36,17 @@ /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports */ -class PortsEngine : public SocketEngine +namespace { -private: /** These are used by ports to hold socket events */ - std::vector<port_event_t> events; + std::vector<port_event_t> events(16); int EngineHandle; -public: - /** Create a new PortsEngine - */ - PortsEngine(); - /** Delete a PortsEngine - */ - virtual ~PortsEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual void DelFd(EventHandler* eh); - virtual int DispatchEvents(); -}; - -PortsEngine::PortsEngine() : events(1) +} + +/** Initialize ports engine + */ +void SocketEngine::Init() { int max = ulimit(4, 0); if (max > 0) @@ -81,9 +71,15 @@ PortsEngine::PortsEngine() : events(1) } } -PortsEngine::~PortsEngine() +/** Shutdown the ports engine + */ +void SocketEngine::Deinit() +{ + SocketEngine::Close(EngineHandle); +} + +void SocketEngine::RecoverFromFork() { - this->Close(EngineHandle); } static int mask_to_events(int event_mask) @@ -96,7 +92,7 @@ static int mask_to_events(int event_mask) return rv; } -bool PortsEngine::AddFd(EventHandler* eh, int event_mask) +bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -105,7 +101,7 @@ bool PortsEngine::AddFd(EventHandler* eh, int event_mask) if (!SocketEngine::AddFdRef(eh)) return false; - SocketEngine::SetEventMask(eh, event_mask); + eh->SetEventMask(event_mask); port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh); ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd); @@ -114,13 +110,13 @@ bool PortsEngine::AddFd(EventHandler* eh, int event_mask) return true; } -void PortsEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) +void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { if (mask_to_events(new_mask) != mask_to_events(old_mask)) port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh); } -void PortsEngine::DelFd(EventHandler* eh) +void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -133,7 +129,7 @@ void PortsEngine::DelFd(EventHandler* eh) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd); } -int PortsEngine::DispatchEvents() +int SocketEngine::DispatchEvents() { struct timespec poll_time; @@ -171,7 +167,7 @@ int PortsEngine::DispatchEvents() if (portev_events & POLLRDNORM) mask &= ~FD_READ_WILL_BLOCK; // reinsert port for next time around, pretending to be one-shot for writes - SetEventMask(eh, mask); + eh->SetEventMask(mask); port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh); if (portev_events & POLLRDNORM) { @@ -189,8 +185,3 @@ int PortsEngine::DispatchEvents() return (int)i; } - -SocketEngine* CreateSocketEngine() -{ - return new PortsEngine; -} diff --git a/src/socketengines/socketengine_select.cpp b/src/socketengines/socketengine_select.cpp index be380cb46..79f1b3635 100644 --- a/src/socketengines/socketengine_select.cpp +++ b/src/socketengines/socketengine_select.cpp @@ -28,32 +28,30 @@ /** A specialisation of the SocketEngine class, designed to use traditional select(). */ -class SelectEngine : public SocketEngine +namespace { fd_set ReadSet, WriteSet, ErrSet; - int MaxFD; - -public: - /** Create a new SelectEngine - */ - SelectEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void DelFd(EventHandler* eh); - void OnSetEvent(EventHandler* eh, int, int); - virtual int DispatchEvents(); -}; - -SelectEngine::SelectEngine() + int MaxFD = 0; +} + +void SocketEngine::Init() { MAX_DESCRIPTORS = FD_SETSIZE; FD_ZERO(&ReadSet); FD_ZERO(&WriteSet); FD_ZERO(&ErrSet); - MaxFD = 0; } -bool SelectEngine::AddFd(EventHandler* eh, int event_mask) +void SocketEngine::Deinit() +{ +} + +void SocketEngine::RecoverFromFork() +{ +} + +bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); if ((fd < 0) || (fd > GetMaxFds() - 1)) @@ -62,7 +60,7 @@ bool SelectEngine::AddFd(EventHandler* eh, int event_mask) if (!SocketEngine::AddFdRef(eh)) return false; - SocketEngine::SetEventMask(eh, event_mask); + eh->SetEventMask(event_mask); OnSetEvent(eh, 0, event_mask); FD_SET(fd, &ErrSet); if (fd > MaxFD) @@ -72,7 +70,7 @@ bool SelectEngine::AddFd(EventHandler* eh, int event_mask) return true; } -void SelectEngine::DelFd(EventHandler* eh) +void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); @@ -90,7 +88,7 @@ void SelectEngine::DelFd(EventHandler* eh) ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd); } -void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) +void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { int fd = eh->GetFd(); int diff = old_mask ^ new_mask; @@ -111,7 +109,7 @@ void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) } } -int SelectEngine::DispatchEvents() +int SocketEngine::DispatchEvents() { static timeval tval = { 1, 0 }; @@ -149,7 +147,7 @@ int SelectEngine::DispatchEvents() if (has_read) { stats.ReadEvents++; - SetEventMask(ev, ev->GetEventMask() & ~FD_READ_WILL_BLOCK); + ev->SetEventMask(ev->GetEventMask() & ~FD_READ_WILL_BLOCK); ev->HandleEvent(EVENT_READ); if (ev != GetRef(i)) continue; @@ -159,16 +157,11 @@ int SelectEngine::DispatchEvents() { stats.WriteEvents++; int newmask = (ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE)); - this->OnSetEvent(ev, ev->GetEventMask(), newmask); - SetEventMask(ev, newmask); + SocketEngine::OnSetEvent(ev, ev->GetEventMask(), newmask); + ev->SetEventMask(newmask); ev->HandleEvent(EVENT_WRITE); } } return sresult; } - -SocketEngine* CreateSocketEngine() -{ - return new SelectEngine; -} |