Merge branch 'develop' of https://github.com/pocoproject/poco.git into develop

This commit is contained in:
Francis ANDRE
2018-05-31 15:19:22 +02:00
4 changed files with 20 additions and 13 deletions

View File

@@ -70,10 +70,13 @@ protected:
private: private:
typedef std::multiset<SocketNotification*> EventSet; typedef std::multiset<SocketNotification*> EventSet;
typedef Poco::FastMutex MutexType;
typedef MutexType::ScopedLock ScopedLock;
EventSet _events; EventSet _events;
Poco::NotificationCenter _nc; Poco::NotificationCenter _nc;
Socket _socket; Socket _socket;
MutexType _mutex;
}; };
@@ -82,6 +85,7 @@ private:
// //
inline bool SocketNotifier::accepts(SocketNotification* pNotification) inline bool SocketNotifier::accepts(SocketNotification* pNotification)
{ {
ScopedLock l(_mutex);
return _events.find(pNotification) != _events.end(); return _events.find(pNotification) != _events.end();
} }

View File

@@ -162,7 +162,10 @@ public:
{ {
PollSet::SocketModeMap result; PollSet::SocketModeMap result;
if(_socketMap.empty()) return result; {
Poco::FastMutex::ScopedLock lock(_mutex);
if(_socketMap.empty()) return result;
}
Poco::Timespan remainingTime(timeout); Poco::Timespan remainingTime(timeout);
int rc; int rc;

View File

@@ -35,6 +35,7 @@ SocketNotifier::~SocketNotifier()
void SocketNotifier::addObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer) void SocketNotifier::addObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
{ {
_nc.addObserver(observer); _nc.addObserver(observer);
ScopedLock l(_mutex);
if (observer.accepts(pReactor->_pReadableNotification)) if (observer.accepts(pReactor->_pReadableNotification))
_events.insert(pReactor->_pReadableNotification.get()); _events.insert(pReactor->_pReadableNotification.get());
else if (observer.accepts(pReactor->_pWritableNotification)) else if (observer.accepts(pReactor->_pWritableNotification))
@@ -49,6 +50,7 @@ void SocketNotifier::addObserver(SocketReactor* pReactor, const Poco::AbstractOb
void SocketNotifier::removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer) void SocketNotifier::removeObserver(SocketReactor* pReactor, const Poco::AbstractObserver& observer)
{ {
_nc.removeObserver(observer); _nc.removeObserver(observer);
ScopedLock l(_mutex);
EventSet::iterator it = _events.end(); EventSet::iterator it = _events.end();
if (observer.accepts(pReactor->_pReadableNotification)) if (observer.accepts(pReactor->_pReadableNotification))
it = _events.find(pReactor->_pReadableNotification.get()); it = _events.find(pReactor->_pReadableNotification.get());

View File

@@ -52,13 +52,10 @@ namespace
_reactor(reactor) _reactor(reactor)
{ {
_reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable)); _reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
_reactor.addEventHandler(_socket, Observer<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onShutdown));
} }
~EchoServiceHandler() ~EchoServiceHandler()
{ {
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onShutdown));
} }
void onReadable(ReadableNotification* pNf) void onReadable(ReadableNotification* pNf)
@@ -70,13 +67,11 @@ namespace
{ {
_socket.sendBytes(buffer, n); _socket.sendBytes(buffer, n);
} }
else delete this; else
} {
_reactor.removeEventHandler(_socket, Observer<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
void onShutdown(ShutdownNotification* pNf) delete this;
{ }
pNf->release();
delete this;
} }
private: private:
@@ -129,8 +124,11 @@ namespace
checkReadableObserverCount(1); checkReadableObserverCount(1);
_reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, ReadableNotification>(*this, &ClientServiceHandler::onReadable)); _reactor.removeEventHandler(_socket, Observer<ClientServiceHandler, ReadableNotification>(*this, &ClientServiceHandler::onReadable));
checkReadableObserverCount(0); checkReadableObserverCount(0);
if (_once || _data.size() == 8192) _reactor.stop(); if (_once || _data.size() == 8192)
delete this; {
_reactor.stop();
delete this;
}
} }
} }