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
Types Aliases
SocketMap
using SocketMap = std::map < Poco::Net::Socket, SocketInfo::Ptr >;
ThreadPtr
using ThreadPtr = Poco::SharedPtr < Poco::Thread >;
ThreadVec
using ThreadVec = std::vector < ThreadPtr >;
Enumerations
Anonymous
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
void addSocketImpl(
const Poco::Net::StreamSocket & socket,
SocketHandler::Ptr pHandler,
Poco::Timespan timeout
);
closeSocketImpl
void closeSocketImpl(
Poco::Net::StreamSocket & socket
);
exception
void exception(
const Poco::Net::StreamSocket & socket,
const SocketInfo::Ptr & pInfo
);
exceptionImpl
void exceptionImpl(
Poco::Net::StreamSocket & socket,
SocketInfo::Ptr pInfo
);
readable
void readable(
const Poco::Net::StreamSocket & socket,
const SocketInfo::Ptr & pInfo
);
readableImpl
void readableImpl(
Poco::Net::StreamSocket & socket,
SocketInfo::Ptr pInfo
);
removeSocketImpl
void removeSocketImpl(
const Poco::Net::StreamSocket & socket
);
resetImpl
void resetImpl();
runMain
void runMain();
runWorker
void runWorker();
stopped
bool stopped();
timeout
void timeout(
const Poco::Net::StreamSocket & socket,
const SocketInfo::Ptr & pInfo
);
timeoutImpl
void timeoutImpl(
Poco::Net::StreamSocket & socket,
SocketInfo::Ptr pInfo
);