mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 03:20:11 +01:00
Use PollSet in SocketReactor #2092 (windows tested)
This commit is contained in:
committed by
Alex Fabijanic
parent
479bde1e46
commit
6912384422
@@ -20,7 +20,6 @@
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
using Poco::FastMutex;
|
||||
using Poco::Exception;
|
||||
using Poco::ErrorHandler;
|
||||
|
||||
@@ -62,60 +61,53 @@ SocketReactor::~SocketReactor()
|
||||
}
|
||||
|
||||
|
||||
bool SocketReactor::hasSocketHandlers()
|
||||
{
|
||||
ScopedLock lock(_mutex);
|
||||
for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it)
|
||||
{
|
||||
if (it->second->accepts(_pReadableNotification) ||
|
||||
it->second->accepts(_pWritableNotification) ||
|
||||
it->second->accepts(_pErrorNotification)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void SocketReactor::run()
|
||||
{
|
||||
_pThread = Thread::current();
|
||||
|
||||
Socket::SocketList readable;
|
||||
Socket::SocketList writable;
|
||||
Socket::SocketList except;
|
||||
|
||||
while (!_stop)
|
||||
{
|
||||
try
|
||||
{
|
||||
readable.clear();
|
||||
writable.clear();
|
||||
except.clear();
|
||||
int nSockets = 0;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it)
|
||||
{
|
||||
if (it->second->accepts(_pReadableNotification))
|
||||
{
|
||||
readable.push_back(it->first);
|
||||
nSockets++;
|
||||
}
|
||||
if (it->second->accepts(_pWritableNotification))
|
||||
{
|
||||
writable.push_back(it->first);
|
||||
nSockets++;
|
||||
}
|
||||
if (it->second->accepts(_pErrorNotification))
|
||||
{
|
||||
except.push_back(it->first);
|
||||
nSockets++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nSockets == 0)
|
||||
if (!hasSocketHandlers())
|
||||
{
|
||||
onIdle();
|
||||
Thread::trySleep(static_cast<long>(_timeout.totalMilliseconds()));
|
||||
}
|
||||
else if (Socket::select(readable, writable, except, _timeout))
|
||||
else
|
||||
{
|
||||
onBusy();
|
||||
|
||||
for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it)
|
||||
dispatch(*it, _pReadableNotification);
|
||||
for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it)
|
||||
dispatch(*it, _pWritableNotification);
|
||||
for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it)
|
||||
dispatch(*it, _pErrorNotification);
|
||||
bool readable = false;
|
||||
PollSet::SocketModeMap sm = _pollSet.poll(_timeout);
|
||||
if (sm.size() > 0)
|
||||
{
|
||||
onBusy();
|
||||
PollSet::SocketModeMap::iterator it = sm.begin();
|
||||
PollSet::SocketModeMap::iterator end = sm.end();
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if ((it->second & PollSet::POLL_READ) != 0)
|
||||
{
|
||||
dispatch(it->first, _pReadableNotification);
|
||||
readable = true;
|
||||
}
|
||||
if ((it->second & PollSet::POLL_WRITE) != 0) dispatch(it->first, _pWritableNotification);
|
||||
if ((it->second & PollSet::POLL_ERROR) != 0) dispatch(it->first, _pErrorNotification);
|
||||
}
|
||||
}
|
||||
if (!readable) onTimeout();
|
||||
}
|
||||
else onTimeout();
|
||||
}
|
||||
catch (Exception& exc)
|
||||
{
|
||||
@@ -162,7 +154,7 @@ void SocketReactor::addEventHandler(const Socket& socket, const Poco::AbstractOb
|
||||
{
|
||||
NotifierPtr pNotifier;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
ScopedLock lock(_mutex);
|
||||
|
||||
EventHandlerMap::iterator it = _handlers.find(socket);
|
||||
if (it == _handlers.end())
|
||||
@@ -171,9 +163,16 @@ void SocketReactor::addEventHandler(const Socket& socket, const Poco::AbstractOb
|
||||
_handlers[socket] = pNotifier;
|
||||
}
|
||||
else pNotifier = it->second;
|
||||
|
||||
if (!pNotifier->hasObserver(observer))
|
||||
pNotifier->addObserver(this, observer);
|
||||
}
|
||||
if (!pNotifier->hasObserver(observer))
|
||||
pNotifier->addObserver(this, observer);
|
||||
|
||||
int mode = 0;
|
||||
if (pNotifier->accepts(_pReadableNotification)) mode |= PollSet::POLL_READ;
|
||||
if (pNotifier->accepts(_pWritableNotification)) mode |= PollSet::POLL_WRITE;
|
||||
if (pNotifier->accepts(_pErrorNotification)) mode |= PollSet::POLL_ERROR;
|
||||
if (mode) _pollSet.add(socket, mode);
|
||||
}
|
||||
|
||||
|
||||
@@ -181,7 +180,7 @@ bool SocketReactor::hasEventHandler(const Socket& socket, const Poco::AbstractOb
|
||||
{
|
||||
NotifierPtr pNotifier;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
ScopedLock lock(_mutex);
|
||||
|
||||
EventHandlerMap::iterator it = _handlers.find(socket);
|
||||
if (it != _handlers.end())
|
||||
@@ -199,7 +198,7 @@ void SocketReactor::removeEventHandler(const Socket& socket, const Poco::Abstrac
|
||||
{
|
||||
NotifierPtr pNotifier;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
ScopedLock lock(_mutex);
|
||||
|
||||
EventHandlerMap::iterator it = _handlers.find(socket);
|
||||
if (it != _handlers.end())
|
||||
@@ -208,6 +207,7 @@ void SocketReactor::removeEventHandler(const Socket& socket, const Poco::Abstrac
|
||||
if (pNotifier->hasObserver(observer) && pNotifier->countObservers() == 1)
|
||||
{
|
||||
_handlers.erase(it);
|
||||
_pollSet.remove(socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,7 +246,7 @@ void SocketReactor::dispatch(const Socket& socket, SocketNotification* pNotifica
|
||||
{
|
||||
NotifierPtr pNotifier;
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
ScopedLock lock(_mutex);
|
||||
EventHandlerMap::iterator it = _handlers.find(socket);
|
||||
if (it != _handlers.end())
|
||||
pNotifier = it->second;
|
||||
@@ -262,7 +262,7 @@ void SocketReactor::dispatch(SocketNotification* pNotification)
|
||||
std::vector<NotifierPtr> delegates;
|
||||
delegates.reserve(_handlers.size());
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
ScopedLock lock(_mutex);
|
||||
for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it)
|
||||
delegates.push_back(it->second);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user