Cleaned up Socket/Timer code.

This commit is contained in:
Gunnar Beutner 2012-04-19 08:46:41 +02:00
parent f9bf1c3b75
commit f3f582ab61
8 changed files with 28 additions and 26 deletions

View File

@ -59,7 +59,7 @@ void Application::RunEventLoop(void)
FD_ZERO(&writefds); FD_ZERO(&writefds);
FD_ZERO(&exceptfds); FD_ZERO(&exceptfds);
for (list<Socket::WeakPtr>::iterator i = Socket::Sockets.begin(); i != Socket::Sockets.end(); i++) { for (Socket::CollectionType::iterator i = Socket::Sockets.begin(); i != Socket::Sockets.end(); i++) {
Socket::Ptr socket = i->lock(); Socket::Ptr socket = i->lock();
if (socket == NULL) if (socket == NULL)
@ -104,7 +104,7 @@ void Application::RunEventLoop(void)
EventArgs ea; EventArgs ea;
ea.Source = shared_from_this(); ea.Source = shared_from_this();
list<Socket::WeakPtr>::iterator prev, i; Socket::CollectionType::iterator prev, i;
for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) { for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
prev = i; prev = i;
i++; i++;

View File

@ -18,6 +18,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <set>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <typeinfo> #include <typeinfo>

View File

@ -2,7 +2,7 @@
using namespace icinga; using namespace icinga;
list<Socket::WeakPtr> Socket::Sockets; Socket::CollectionType Socket::Sockets;
Socket::Socket(void) Socket::Socket(void)
{ {
@ -20,12 +20,16 @@ void Socket::Start(void)
OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this()); OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this());
Sockets.push_front(static_pointer_cast<Socket>(shared_from_this())); Sockets.insert(static_pointer_cast<Socket>(shared_from_this()));
} }
void Socket::Stop(void) void Socket::Stop(void)
{ {
Sockets.remove_if(weak_ptr_eq_raw<Socket>(this)); Socket::Ptr self = static_pointer_cast<Socket>(shared_from_this());
Socket::CollectionType::iterator i = Sockets.find(self);
if (i != Sockets.end())
Sockets.erase(i);
} }
void Socket::SetFD(SOCKET fd) void Socket::SetFD(SOCKET fd)
@ -112,7 +116,7 @@ int Socket::ExceptionEventHandler(const EventArgs& ea)
void Socket::CloseAllSockets(void) void Socket::CloseAllSockets(void)
{ {
for (list<Socket::WeakPtr>::iterator i = Sockets.begin(); i != Sockets.end(); ) { for (Socket::CollectionType::iterator i = Sockets.begin(); i != Sockets.end(); ) {
Socket::Ptr socket = i->lock(); Socket::Ptr socket = i->lock();
i++; i++;

View File

@ -31,7 +31,9 @@ public:
typedef shared_ptr<Socket> Ptr; typedef shared_ptr<Socket> Ptr;
typedef weak_ptr<Socket> WeakPtr; typedef weak_ptr<Socket> WeakPtr;
static list<Socket::WeakPtr> Sockets; typedef set< Socket::WeakPtr, owner_less<Socket::WeakPtr> > CollectionType;
static Socket::CollectionType Sockets;
~Socket(void); ~Socket(void);

View File

@ -32,8 +32,6 @@ void TCPServer::Listen(void)
Close(); Close();
return; return;
} }
Start();
} }
int TCPServer::ReadableEventHandler(const EventArgs& ea) int TCPServer::ReadableEventHandler(const EventArgs& ea)
@ -45,13 +43,6 @@ int TCPServer::ReadableEventHandler(const EventArgs& ea)
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen); fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
if (fd == INVALID_SOCKET) { if (fd == INVALID_SOCKET) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK)
#else /* _WIN32 */
if (errno == EINPROGRESS)
#endif /* _WIN32 */
return 0;
SocketErrorEventArgs sea; SocketErrorEventArgs sea;
#ifdef _WIN32 #ifdef _WIN32
sea.Code = WSAGetLastError(); sea.Code = WSAGetLastError();

View File

@ -5,7 +5,7 @@
using namespace icinga; using namespace icinga;
time_t Timer::NextCall; time_t Timer::NextCall;
list<Timer::WeakPtr> Timer::Timers; Timer::CollectionType Timer::Timers;
Timer::Timer(void) Timer::Timer(void)
{ {
@ -25,7 +25,7 @@ void Timer::RescheduleTimers(void)
/* Make sure we wake up at least once every 30 seconds */ /* Make sure we wake up at least once every 30 seconds */
NextCall = time(NULL) + 30; NextCall = time(NULL) + 30;
for (list<Timer::WeakPtr>::iterator i = Timers.begin(); i != Timers.end(); i++) { for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); i++) {
Timer::Ptr timer = i->lock(); Timer::Ptr timer = i->lock();
if (timer == NULL) if (timer == NULL)
@ -42,7 +42,7 @@ void Timer::CallExpiredTimers(void)
time(&now); time(&now);
for (list<Timer::WeakPtr>::iterator i = Timers.begin(); i != Timers.end(); ) { for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); ) {
Timer::Ptr timer = Timer::Ptr(*i); Timer::Ptr timer = Timer::Ptr(*i);
i++; i++;
@ -58,7 +58,7 @@ void Timer::CallExpiredTimers(void)
void Timer::StopAllTimers(void) void Timer::StopAllTimers(void)
{ {
for (list<Timer::WeakPtr>::iterator i = Timers.begin(); i != Timers.end(); ) { for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); ) {
Timer::Ptr timer = i->lock(); Timer::Ptr timer = i->lock();
i++; i++;
@ -103,16 +103,18 @@ EventArgs Timer::GetUserArgs(void) const
void Timer::Start(void) void Timer::Start(void)
{ {
Stop(); Timers.insert(static_pointer_cast<Timer>(shared_from_this()));
Timers.push_front(static_pointer_cast<Timer>(shared_from_this()));
Reschedule(time(NULL) + m_Interval); Reschedule(time(NULL) + m_Interval);
} }
void Timer::Stop(void) void Timer::Stop(void)
{ {
Timers.remove_if(weak_ptr_eq_raw<Timer>(this)); Timer::Ptr self = static_pointer_cast<Timer>(shared_from_this());
Timer::CollectionType::iterator i = Timers.find(self);
if (i != Timers.end())
Timers.erase(i);
} }
void Timer::Reschedule(time_t next) void Timer::Reschedule(time_t next)

View File

@ -30,7 +30,9 @@ public:
typedef shared_ptr<Timer> Ptr; typedef shared_ptr<Timer> Ptr;
typedef weak_ptr<Timer> WeakPtr; typedef weak_ptr<Timer> WeakPtr;
static list<Timer::WeakPtr> Timers; typedef set< Timer::WeakPtr, owner_less<Timer::WeakPtr> > CollectionType;
static Timer::CollectionType Timers;
Timer(void); Timer(void);

View File

@ -32,7 +32,7 @@ void VirtualEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest&
map<string, Event<NewRequestEventArgs> >::iterator i = m_MethodHandlers.find(method); map<string, Event<NewRequestEventArgs> >::iterator i = m_MethodHandlers.find(method);
if (i == m_MethodHandlers.end()) if (i == m_MethodHandlers.end())
throw InvalidArgumentException(); return;
NewRequestEventArgs nrea; NewRequestEventArgs nrea;
nrea.Source = shared_from_this(); nrea.Source = shared_from_this();