Skip to content

Commit

Permalink
chore: remove threads parameter from SocketDispatcher, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Nov 20, 2024
1 parent 6721b55 commit ba8fcdf
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 25 deletions.
5 changes: 0 additions & 5 deletions WebTunnel/WebTunnelAgent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,6 @@ will be passed as command-line argument to the executable and will be one of:
* `error`: there has been an error establishing the tunnel. A second parameter will
also be passed containing more information about the error.

#### webtunnel.threads

The number of I/O threads the `WebTunnelAgent` should use. Should be left at the default
(4).

#### webtunnel.properties

This setting specifies additional device properties that are sent to the
Expand Down
5 changes: 1 addition & 4 deletions WebTunnel/WebTunnelAgent/src/WebTunnelAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
_appPort(0),
_useProxy(false),
_proxyPort(0),
_threads(8),
_retryDelay(MIN_RETRY_DELAY),
_status(STATUS_DISCONNECTED)
{
Expand Down Expand Up @@ -405,7 +404,7 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
}
pWebSocket->setNoDelay(true);
_retryDelay = MIN_RETRY_DELAY;
_pDispatcher = new Poco::WebTunnel::SocketDispatcher(_threads);
_pDispatcher = new Poco::WebTunnel::SocketDispatcher;
_pForwarder = new Poco::WebTunnel::RemotePortForwarder(*_pDispatcher, pWebSocket, _host, _ports, _remoteTimeout, _pSocketFactory);
_pForwarder->webSocketClosed += Poco::delegate(this, &WebTunnelAgent::onClose);
_pForwarder->setConnectTimeout(_connectTimeout);
Expand Down Expand Up @@ -825,7 +824,6 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
_localTimeout = Poco::Timespan(config().getInt("webtunnel.localTimeout"s, 7200), 0);
_connectTimeout = Poco::Timespan(config().getInt("webtunnel.connectTimeout"s, 10), 0);
_remoteTimeout = Poco::Timespan(config().getInt("webtunnel.remoteTimeout"s, 300), 0);
_threads = config().getInt("webtunnel.threads"s, 8);
_httpPath = config().getString("webtunnel.httpPath"s, ""s);
_httpPort = config().getUInt16("webtunnel.httpPort"s, 0);
_httpsRequired = config().getBool("webtunnel.https.enable"s, false);
Expand Down Expand Up @@ -975,7 +973,6 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
Poco::Timespan _httpTimeout;
Poco::Timespan _propertiesUpdateInterval;
std::string _notifyExec;
int _threads;
Poco::SharedPtr<Poco::WebTunnel::SocketDispatcher> _pDispatcher;
Poco::SharedPtr<Poco::WebTunnel::RemotePortForwarder> _pForwarder;
Poco::SharedPtr<Poco::Net::HTTPClientSession> _pHTTPClientSession;
Expand Down
28 changes: 19 additions & 9 deletions WebTunnel/WebTunnelAgentLib/src/Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace WebTunnelAgentLib {
const std::string Tunnel::SEC_WEBSOCKET_PROTOCOL("Sec-WebSocket-Protocol");
const std::string Tunnel::WEBTUNNEL_PROTOCOL("com.appinf.webtunnel.server/1.0");
const std::string Tunnel::WEBTUNNEL_AGENT("WebTunnelAgentLib/1.0.0");
const std::string Tunnel::X_PTTH_SET_PROPERTY("X-PTTH-Set-Property");
const std::string Tunnel::X_PTTH_ERROR("X-PTTH-Error");
const std::string Tunnel::X_WEBTUNNEL_KEEPALIVE("X-WebTunnel-KeepAlive");


class ReconnectTask: public Poco::Util::TimerTask
Expand Down Expand Up @@ -127,11 +130,11 @@ void Tunnel::stop()

void Tunnel::addProperties(Poco::Net::HTTPRequest& request, const std::map<std::string, std::string>& props)
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;targetHost=%s"s, _host.toString()));
request.add("X-PTTH-Set-Property"s, Poco::format("device;targetPorts=%s"s, formatPorts()));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;targetHost=%s"s, _host.toString()));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;targetPorts=%s"s, formatPorts()));
if (!_httpPath.empty())
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;httpPath=%s"s, quoteString(_httpPath)));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;httpPath=%s"s, quoteString(_httpPath)));
}

addPortProperty(request, "http"s, _httpPort);
Expand All @@ -142,33 +145,34 @@ void Tunnel::addProperties(Poco::Net::HTTPRequest& request, const std::map<std::

if (!_deviceName.empty())
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;name=%s"s, quoteString(_deviceName)));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;name=%s"s, quoteString(_deviceName)));
}
if (!_tenant.empty())
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;tenant=%s"s, quoteString(_tenant)));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;tenant=%s"s, quoteString(_tenant)));
}

if (!props.empty())
{
for (std::map<std::string, std::string>::const_iterator it = props.begin(); it != props.end(); ++it)
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;%s=%s"s, it->first, quoteString(it->second)));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;%s=%s"s, it->first, quoteString(it->second)));
}
}
request.set("User-Agent"s, _userAgent);
request.set(X_WEBTUNNEL_KEEPALIVE, Poco::NumberFormatter::format(_remoteTimeout.totalSeconds()));
}


void Tunnel::addPortProperty(Poco::Net::HTTPRequest& request, const std::string& proto, Poco::UInt16 port)
{
if (port != 0)
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;%sPort=%hu"s, proto, port));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;%sPort=%hu"s, proto, port));
}
else
{
request.add("X-PTTH-Set-Property"s, Poco::format("device;%sPort="s, proto));
request.add(X_PTTH_SET_PROPERTY, Poco::format("device;%sPort="s, proto));
}
}

Expand Down Expand Up @@ -265,6 +269,12 @@ void Tunnel::connect()
if (response.get(SEC_WEBSOCKET_PROTOCOL, ""s) == WEBTUNNEL_PROTOCOL)
{
_logger.debug("WebSocket established. Creating RemotePortForwarder..."s);
if (response.has(X_WEBTUNNEL_KEEPALIVE))
{
int keepAlive = Poco::NumberParser::parse(response.get(X_WEBTUNNEL_KEEPALIVE));
_remoteTimeout.assign(keepAlive, 0);
_logger.debug("Server has requested a keep-alive timeout (remoteTimeout) of %d seconds."s, keepAlive);
}
pWebSocket->setNoDelay(true);
_retryDelay = MIN_RETRY_DELAY;
_pForwarder = new Poco::WebTunnel::RemotePortForwarder(*_pDispatcher, pWebSocket, _host, _ports, _remoteTimeout, _pSocketFactory);
Expand Down Expand Up @@ -314,7 +324,7 @@ void Tunnel::connect()
}
else
{
std::string msg = response.get("X-PTTH-Error"s, exc.displayText());
std::string msg = response.get(X_PTTH_ERROR, exc.displayText());
statusChanged(STATUS_ERROR, Poco::format("Cannot connect to reflector at %s: %s"s, reflectorURI.toString(), msg));
_logger.error(_lastError);
if (_retryDelay < MAX_RETRY_DELAY)
Expand Down
3 changes: 3 additions & 0 deletions WebTunnel/WebTunnelAgentLib/src/Tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class Tunnel: public Poco::RefCountedObject
static const std::string SEC_WEBSOCKET_PROTOCOL;
static const std::string WEBTUNNEL_PROTOCOL;
static const std::string WEBTUNNEL_AGENT;
static const std::string X_PTTH_SET_PROPERTY;
static const std::string X_PTTH_ERROR;
static const std::string X_WEBTUNNEL_KEEPALIVE;

private:
std::string _id;
Expand Down
3 changes: 1 addition & 2 deletions WebTunnel/WebTunnelAgentLib/src/webtunnelagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ webtunnel.password =
webtunnel.connectTimeout = 10
webtunnel.localTimeout = 7200
webtunnel.remoteTimeout = 900
webtunnel.threads = 4
http.timeout = 30
tls.acceptUnknownCertificate = false
tls.ciphers = HIGH:!DSS:!aNULL@STRENGTH
Expand Down Expand Up @@ -414,7 +413,7 @@ webtunnel_agent WebTunnelAgent_API webtunnel_agent_create(const char* reflector_
pConfig->setString("webtunnel.ports"s, portsList);
}

Poco::SharedPtr<Poco::WebTunnel::SocketDispatcher> pDispatcher = new Poco::WebTunnel::SocketDispatcher(pConfig->getInt("webtunnel.threads"s, 4));
Poco::SharedPtr<Poco::WebTunnel::SocketDispatcher> pDispatcher = new Poco::WebTunnel::SocketDispatcher;
Poco::WebTunnel::SocketFactory::Ptr pSocketFactory;
#if defined(WEBTUNNEL_ENABLE_TLS)
if (pConfig->getBool("webtunnel.https.enable"s, false))
Expand Down
4 changes: 2 additions & 2 deletions WebTunnel/include/Poco/WebTunnel/SocketDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class WebTunnel_API SocketDispatcher: public Poco::Runnable
virtual void timeout(SocketDispatcher& dispatcher, Poco::Net::StreamSocket& socket) = 0;
};

SocketDispatcher(int threadCount, Poco::Timespan timeout = Poco::Timespan(5000));
/// Creates the SocketDispatcher, using the given number of worker threads.
explicit SocketDispatcher(Poco::Timespan timeout = Poco::Timespan(5000));
/// Creates the SocketDispatcher.
///
/// 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
Expand Down
4 changes: 2 additions & 2 deletions WebTunnel/src/LocalPortForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ LocalPortForwarder::LocalPortForwarder(Poco::UInt16 localPort, Poco::UInt16 remo
_pWebSocketFactory(pWebSocketFactory),
_serverSocket(_localAddr),
_tcpServer(new LocalPortForwarderConnectionFactory(*this), _serverSocket),
_pDispatcher(new SocketDispatcher),
_logger(Poco::Logger::get("WebTunnel.LocalPortForwarder"s))
{
_localAddr = _serverSocket.address();
_tcpServer.start();
_pDispatcher = new SocketDispatcher(10);
}


Expand All @@ -464,7 +464,7 @@ LocalPortForwarder::LocalPortForwarder(const Poco::Net::SocketAddress& localAddr
_pWebSocketFactory(pWebSocketFactory),
_serverSocket(_localAddr),
_tcpServer(new LocalPortForwarderConnectionFactory(*this), _serverSocket, pServerParams),
_pDispatcher(new SocketDispatcher(16)),
_pDispatcher(new SocketDispatcher),
_logger(Poco::Logger::get("WebTunnel.LocalPortForwarder"s))
{
_localAddr = _serverSocket.address();
Expand Down
2 changes: 1 addition & 1 deletion WebTunnel/src/SocketDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ShutdownSendNotification: public SocketDispatcher::TaskNotification
};


SocketDispatcher::SocketDispatcher(int threadCount, Poco::Timespan timeout):
SocketDispatcher::SocketDispatcher(Poco::Timespan timeout):
_timeout(timeout),
_stopped(false),
_logger(Poco::Logger::get("WebTunnel.SocketDispatcher"s))
Expand Down

0 comments on commit ba8fcdf

Please sign in to comment.