add PollSet::has/empty(); ParallelAcceptor: always use same reactor for a socket, if registered

This commit is contained in:
Alex Fabijanic
2018-05-01 13:23:21 -05:00
committed by Alex Fabijanic
parent f6e6bec32d
commit df46368413
8 changed files with 139 additions and 31 deletions

View File

@@ -159,15 +159,39 @@ public:
}
protected:
typedef std::vector<typename ParallelReactor::Ptr> ReactorVec;
virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
/// Create and initialize a new ServiceHandler instance.
/// If socket is already registered with a reactor, the new
/// ServiceHandler instance is given that reactor; otherwise,
/// the next reactor is used. Reactors are rotated in round-robin
/// fashion.
///
/// Subclasses can override this method.
{
std::size_t next = _next++;
if (_next == _reactors.size()) _next = 0;
_reactors[next]->wakeUp();
return new ServiceHandler(socket, *_reactors[next]);
SocketReactor* pReactor = reactor(socket);
if (!pReactor)
{
std::size_t next = _next++;
if (_next == _reactors.size()) _next = 0;
pReactor = _reactors[next];
}
pReactor->wakeUp();
return new ServiceHandler(socket, *pReactor);
}
SocketReactor* reactor(const Socket& socket)
/// Returns reactor where this socket is already registered
/// for polling, if found; otherwise returns null pointer.
{
ReactorVec::iterator it = _reactors.begin();
ReactorVec::iterator end = _reactors.end();
for (; it != end; ++it)
{
if ((*it)->has(socket)) return it->get();
}
return 0;
}
SocketReactor* reactor()
@@ -194,8 +218,6 @@ protected:
_reactors.push_back(new ParallelReactor);
}
typedef std::vector<typename ParallelReactor::Ptr> ReactorVec;
ReactorVec& reactors()
/// Returns reference to vector of reactors.
{
@@ -208,8 +230,8 @@ protected:
return _reactors.at(idx).get();
}
std::size_t& next()
/// Returns reference to the next reactor index.
std::size_t next()
/// Returns the next reactor index.
{
return _next;
}

View File

@@ -63,6 +63,12 @@ public:
void update(const Poco::Net::Socket& socket, int mode);
/// Updates the mode of the given socket.
bool has(const Socket& socket) const;
/// Returns true if socket is registered for polling.
bool empty() const;
/// Returns true if no socket is registered for polling.
void clear();
/// Removes all sockets from the PollSet.

View File

@@ -20,6 +20,11 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketNotification.h"
<<<<<<< HEAD
=======
#include "Poco/Net/SocketReactor.h"
#include "Poco/Net/ParallelSocketAcceptor.h"
>>>>>>> 0d7f39f... add PollSet::has/empty(); ParallelAcceptor: always use same reactor for a socket, if registered
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Observer.h"
@@ -70,7 +75,7 @@ class SocketConnector
/// Subclasses can override the createServiceHandler() factory method
/// if special steps are necessary to create a ServiceHandler object.
{
public:
public:
explicit SocketConnector(SocketAddress& address):
_pReactor(0)
/// Creates a SocketConnector, using the given Socket.
@@ -78,13 +83,13 @@ public:
_socket.connectNB(address);
}
SocketConnector(SocketAddress& address, SocketReactor& reactor):
SocketConnector(SocketAddress& address, SocketReactor& reactor, bool doRegister = true) :
_pReactor(0)
/// Creates an acceptor, using the given ServerSocket.
/// The SocketConnector registers itself with the given SocketReactor.
{
_socket.connectNB(address);
registerConnector(reactor);
if (doRegister) registerConnector(reactor);
}
virtual ~SocketConnector()
@@ -99,7 +104,7 @@ public:
poco_unexpected();
}
}
virtual void registerConnector(SocketReactor& reactor)
/// Registers the SocketConnector with a SocketReactor.
///
@@ -113,7 +118,7 @@ public:
_pReactor->addEventHandler(_socket, Poco::Observer<SocketConnector, WritableNotification>(*this, &SocketConnector::onWritable));
_pReactor->addEventHandler(_socket, Poco::Observer<SocketConnector, ErrorNotification>(*this, &SocketConnector::onError));
}
virtual void unregisterConnector()
/// Unregisters the SocketConnector.
///
@@ -129,7 +134,7 @@ public:
_pReactor->removeEventHandler(_socket, Poco::Observer<SocketConnector, ErrorNotification>(*this, &SocketConnector::onError));
}
}
void onReadable(ReadableNotification* pNotification)
{
pNotification->release();
@@ -200,7 +205,7 @@ private:
SocketConnector();
SocketConnector(const SocketConnector&);
SocketConnector& operator = (const SocketConnector&);
StreamSocket _socket;
SocketReactor* _pReactor;
};

View File

@@ -166,6 +166,9 @@ public:
/// Poco::Observer<MyEventHandler, SocketNotification> obs(*this, &MyEventHandler::handleMyEvent);
/// reactor.removeEventHandler(obs);
bool has(const Socket& socket) const;
/// Returns true if socket is registered with this rector.
protected:
virtual void onTimeout();
/// Called if the timeout expires and no other events are available.