Poco::WebTunnel

class SocketDispatcher

File Information

Library: WebTunnel
Package: WebTunnel
Header: Poco/WebTunnel/SocketDispatcher.h

Description

SocketDispatcher implements a multi-threaded variant of the Reactor pattern, optimized for forwarding data from one socket to another.

The SocketDispatcher runs a select() loop in a separate thread. As soon as a socket becomes readable, it will be put into a work queue. A number of worker threads dequeue work queue items and process the data received over the socket, using registered SocketHandler instances.

Member Summary

Member Functions: addSocket, addSocketImpl, closeSocket, closeSocketImpl, exception, exceptionImpl, readable, readableImpl, removeSocket, removeSocketImpl, reset, resetImpl, runMain, runWorker, stop, stopped, timeout, timeoutImpl

Nested Classes

class SocketHandler

 

struct SocketInfo protected

 

Types Aliases

SocketMap protected

using SocketMap = std::map < Poco::Net::Socket, SocketInfo::Ptr >;

ThreadPtr protected

using ThreadPtr = Poco::SharedPtr < Poco::Thread >;

ThreadVec protected

using ThreadVec = std::vector < ThreadPtr >;

Enumerations

Anonymous protected

MAIN_QUEUE_TIMEOUT = 1000

WORKER_QUEUE_TIMEOUT = 2500

Constructors

SocketDispatcher

SocketDispatcher(
    int threadCount,
    Poco::Timespan timeout = Poco::Timespan (5000),
    int maxReadsPerWorker = 10
);

Creates the SocketDispatcher, using the given number of worker threads.

The given timeout is used for the main select loop, as well as by workers to poll if more reads are possible, up to the given maximum number of reads per worker.

Destructor

~SocketDispatcher

~SocketDispatcher();

Destroys the SocketDispatcher.

Member Functions

addSocket

void addSocket(
    const Poco::Net::StreamSocket & socket,
    SocketHandler::Ptr pHandler,
    Poco::Timespan timeout = 0
);

Adds a socket and its handler to the SocketDispatcher.

closeSocket

void closeSocket(
    const Poco::Net::StreamSocket & socket
);

Closes and removes a socket and its associated handler from the SocketDispatcher.

removeSocket

void removeSocket(
    const Poco::Net::StreamSocket & socket
);

Removes a socket and its associated handler from the SocketDispatcher.

reset

void reset();

Removes all sockets but does not stop the SocketDispatcher.

stop

void stop();

Stops the SocketDispatcher and removes all sockets.

addSocketImpl protected

void addSocketImpl(
    const Poco::Net::StreamSocket & socket,
    SocketHandler::Ptr pHandler,
    Poco::Timespan timeout
);

closeSocketImpl protected

void closeSocketImpl(
    Poco::Net::StreamSocket & socket
);

exception protected

void exception(
    const Poco::Net::StreamSocket & socket,
    const SocketInfo::Ptr & pInfo
);

exceptionImpl protected

void exceptionImpl(
    Poco::Net::StreamSocket & socket,
    SocketInfo::Ptr pInfo
);

readable protected

void readable(
    const Poco::Net::StreamSocket & socket,
    const SocketInfo::Ptr & pInfo
);

readableImpl protected

void readableImpl(
    Poco::Net::StreamSocket & socket,
    SocketInfo::Ptr pInfo
);

removeSocketImpl protected

void removeSocketImpl(
    const Poco::Net::StreamSocket & socket
);

resetImpl protected

void resetImpl();

runMain protected

void runMain();

runWorker protected

void runWorker();

stopped protected inline

bool stopped();

timeout protected

void timeout(
    const Poco::Net::StreamSocket & socket,
    const SocketInfo::Ptr & pInfo
);

timeoutImpl protected

void timeoutImpl(
    Poco::Net::StreamSocket & socket,
    SocketInfo::Ptr pInfo
);

Securely control IoT edge devices from anywhere   Connect a Device