Poco::Net

template < class ServiceHandler, class SR >

class ParallelSocketAcceptor

File Information

Library: Net
Package: Reactor
Header: Poco/Net/ParallelSocketAcceptor.h

Description

This class implements the Acceptor part of the Acceptor-Connector design pattern. Only the difference from single-threaded version is documented here, For full description see Poco::Net::SocketAcceptor documentation.

This is a multi-threaded version of SocketAcceptor, it differs from the single-threaded version in number of reactors (defaulting to number of processors) that can be specified at construction time and is rotated in a round-robin fashion by event handler. See ParallelSocketAcceptor::onAccept and ParallelSocketAcceptor::createServiceHandler documentation and implementation for details.

Member Summary

Member Functions: createServiceHandler, init, next, onAccept, reactor, reactors, registerAcceptor, setReactor, socket, unregisterAcceptor

Types

ReactorVec protected

typedef std::vector < typename ParallelReactor::Ptr > ReactorVec;

Types Aliases

Observer

using Observer = Poco::Observer < ParallelSocketAcceptor, ReadableNotification >;

ParallelReactor

using ParallelReactor = Poco::Net::ParallelSocketReactor < SR >;

Constructors

ParallelSocketAcceptor inline

explicit ParallelSocketAcceptor(
    ServerSocket & socket,
    unsigned threads = Poco::Environment::processorCount ()
);

Creates a ParallelSocketAcceptor using the given ServerSocket, sets number of threads and populates the reactors vector.

ParallelSocketAcceptor inline

ParallelSocketAcceptor(
    ServerSocket & socket,
    SocketReactor & reactor,
    unsigned threads = Poco::Environment::processorCount ()
);

Creates a ParallelSocketAcceptor using the given ServerSocket, sets the number of threads, populates the reactors vector and registers itself with the given SocketReactor.

Destructor

~ParallelSocketAcceptor virtual inline

virtual ~ParallelSocketAcceptor();

Destroys the ParallelSocketAcceptor.

Member Functions

onAccept inline

void onAccept(
    ReadableNotification * pNotification
);

Accepts connection and creates event handler.

registerAcceptor virtual inline

virtual void registerAcceptor(
    SocketReactor & reactor
);

Registers the ParallelSocketAcceptor with a SocketReactor.

A subclass can override this function to e.g. register an event handler for timeout event.

The overriding method must either call the base class implementation or register the accept handler on its own.

setReactor inline

void setReactor(
    SocketReactor & reactor
);

Sets the reactor for this acceptor.

unregisterAcceptor virtual inline

virtual void unregisterAcceptor();

Unregisters the ParallelSocketAcceptor.

A subclass can override this function to e.g. unregister its event handler for a timeout event.

The overriding method must either call the base class implementation or unregister the accept handler on its own.

createServiceHandler protected virtual inline

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.

init protected inline

void init();

Populates the reactors vector.

next protected inline

std::size_t next();

Returns the next reactor index.

reactor protected inline

SocketReactor * reactor(
    const Socket & socket
);

Returns reactor where this socket is already registered for polling, if found; otherwise returns null pointer.

reactor protected inline

SocketReactor * reactor();

Returns a pointer to the SocketReactor where this SocketAcceptor is registered.

The pointer may be null.

reactor protected inline

SocketReactor * reactor(
    std::size_t idx
);

Returns reference to the reactor at position idx.

reactors protected inline

ReactorVec & reactors();

Returns reference to vector of reactors.

socket protected inline

Socket & socket();

Returns a reference to the SocketAcceptor's socket.

Securely control IoT edge devices from anywhere   Connect a Device