From fa5d0feff929c0e40334f9ff78e3469d332b7b24 Mon Sep 17 00:00:00 2001 From: sima-fastly Date: Wed, 24 Jan 2024 11:06:42 -0800 Subject: [PATCH 1/2] boostification of wscontrol session --- src/cpp/proxy/wscontrolsession.cpp | 14 +++++++------- src/cpp/proxy/wscontrolsession.h | 16 +++++++++------- src/cpp/proxy/wsproxysession.cpp | 18 +++++++++++------- src/cpp/proxy/wsproxysession.h | 11 ++++++++++- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/cpp/proxy/wscontrolsession.cpp b/src/cpp/proxy/wscontrolsession.cpp index f8a8400a..100759d8 100644 --- a/src/cpp/proxy/wscontrolsession.cpp +++ b/src/cpp/proxy/wscontrolsession.cpp @@ -201,7 +201,7 @@ class WsControlSession::Private : public QObject else pendingSendEventWrites += QByteArray(); // placeholder - emit q->sendEventReceived(type, item.message, item.queue); + q->sendEventReceived(type, item.message, item.queue); } else if(item.type == WsControlPacket::Item::KeepAliveSetup) { @@ -212,22 +212,22 @@ class WsControlSession::Private : public QObject mode = WsControl::Interval; else // idle mode = WsControl::Idle; - emit q->keepAliveSetupEventReceived(mode, item.timeout); + q->keepAliveSetupEventReceived(mode, item.timeout); } else - emit q->keepAliveSetupEventReceived(WsControl::NoKeepAlive); + q->keepAliveSetupEventReceived(WsControl::NoKeepAlive, -1); } else if(item.type == WsControlPacket::Item::Close) { - emit q->closeEventReceived(item.code, item.reason); + q->closeEventReceived(item.code, item.reason); } else if(item.type == WsControlPacket::Item::Detach) { - emit q->detachEventReceived(); + q->detachEventReceived(); } else if(item.type == WsControlPacket::Item::Cancel) { - emit q->cancelEventReceived(); + q->cancelEventReceived(); } else if(item.type == WsControlPacket::Item::Ack) { @@ -261,7 +261,7 @@ private slots: pendingRequests.clear(); setupRequestTimer(); - emit q->error(); + q->error(); } }; diff --git a/src/cpp/proxy/wscontrolsession.h b/src/cpp/proxy/wscontrolsession.h index 4454868e..6c44af20 100644 --- a/src/cpp/proxy/wscontrolsession.h +++ b/src/cpp/proxy/wscontrolsession.h @@ -28,6 +28,9 @@ #include "websocket.h" #include "wscontrol.h" #include "packet/wscontrolpacket.h" +#include + +using Signal = boost::signals2::signal; class WsControlManager; @@ -48,13 +51,12 @@ class WsControlSession : public QObject // tell session that a received sendEvent has been written void sendEventWritten(); -signals: - void sendEventReceived(WebSocket::Frame::Type type, const QByteArray &message, bool queue); - void keepAliveSetupEventReceived(WsControl::KeepAliveMode mode, int timeout = -1); - void closeEventReceived(int code, const QByteArray &reason); // -1 for no code - void detachEventReceived(); - void cancelEventReceived(); - void error(); + boost::signals2::signal sendEventReceived; + boost::signals2::signal keepAliveSetupEventReceived; + boost::signals2::signal closeEventReceived; // Use -1 for no code + Signal detachEventReceived; + Signal cancelEventReceived; + Signal error; private: class Private; diff --git a/src/cpp/proxy/wsproxysession.cpp b/src/cpp/proxy/wsproxysession.cpp index 0044a1be..a681c0ee 100644 --- a/src/cpp/proxy/wsproxysession.cpp +++ b/src/cpp/proxy/wsproxysession.cpp @@ -281,6 +281,7 @@ class WsProxySession::Private : public QObject Callback> finishedByPassthroughCallback; Connection keepAliveConneciton; Connection aboutToSendRequestConnection; + map wsProxyConnectionMap; Private(WsProxySession *_q, ZRoutes *_zroutes, ConnectionManager *_connectionManager, const LogUtil::Config &_logConfig, StatsManager *_statsManager, WsControlManager *_wsControlManager) : QObject(_q), @@ -325,6 +326,7 @@ class WsProxySession::Private : public QObject delete outSock; outSock = 0; + wsProxyConnectionMap.erase(wsControl); delete wsControl; wsControl = 0; @@ -900,12 +902,14 @@ private slots: if(wsControlManager) { wsControl = wsControlManager->createSession(publicCid); - connect(wsControl, &WsControlSession::sendEventReceived, this, &Private::wsControl_sendEventReceived); - connect(wsControl, &WsControlSession::keepAliveSetupEventReceived, this, &Private::wsControl_keepAliveSetupEventReceived); - connect(wsControl, &WsControlSession::closeEventReceived, this, &Private::wsControl_closeEventReceived); - connect(wsControl, &WsControlSession::detachEventReceived, this, &Private::wsControl_detachEventReceived); - connect(wsControl, &WsControlSession::cancelEventReceived, this, &Private::wsControl_cancelEventReceived); - connect(wsControl, &WsControlSession::error, this, &Private::wsControl_error); + wsProxyConnectionMap[wsControl] = { + wsControl->sendEventReceived.connect(boost::bind(&Private::wsControl_sendEventReceived, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3)), + wsControl->keepAliveSetupEventReceived.connect(boost::bind(&Private::wsControl_keepAliveSetupEventReceived, this, boost::placeholders::_1, boost::placeholders::_2)), + wsControl->closeEventReceived.connect(boost::bind(&Private::wsControl_closeEventReceived, this, boost::placeholders::_1, boost::placeholders::_2)), + wsControl->detachEventReceived.connect(boost::bind(&Private::wsControl_detachEventReceived, this)), + wsControl->cancelEventReceived.connect(boost::bind(&Private::wsControl_cancelEventReceived, this)), + wsControl->error.connect(boost::bind(&Private::wsControl_error, this)) + }; wsControl->start(route.id, route.separateStats, channelPrefix, inSock->requestUri()); foreach(const QString &subChannel, target.subscriptions) @@ -1014,6 +1018,7 @@ private slots: woh->setHeaders(requestData.headers); } +private: void wsControl_sendEventReceived(WebSocket::Frame::Type type, const QByteArray &message, bool queue) { // this method accepts a full message, which must be typed @@ -1114,7 +1119,6 @@ private slots: wsControl_cancelEventReceived(); } -private: void keepAliveTimer_timeout() { wsControl->sendNeedKeepAlive(); diff --git a/src/cpp/proxy/wsproxysession.h b/src/cpp/proxy/wsproxysession.h index da0ed695..6620cde2 100644 --- a/src/cpp/proxy/wsproxysession.h +++ b/src/cpp/proxy/wsproxysession.h @@ -27,9 +27,9 @@ #include "callback.h" #include "logutil.h" #include "domainmap.h" - #include +using std::map; using Connection = boost::signals2::scoped_connection; namespace Jwt { @@ -81,4 +81,13 @@ class WsProxySession : public QObject Private *d; }; +struct WSProxyConnections { + Connection sendEventReceivedConnection; + Connection keepAliveSetupEventReceivedConnection; + Connection closeEventReceivedConnection; + Connection detachEventReceivedConnection; + Connection cancelEventReceivedConnection; + Connection errorConnection; +}; + #endif From 5e9e6eaf5c29dcb6f2c71a24899990cd8b02ab5f Mon Sep 17 00:00:00 2001 From: sima-fastly Date: Wed, 24 Jan 2024 14:53:24 -0800 Subject: [PATCH 2/2] comments addressed --- src/cpp/proxy/wscontrolsession.h | 12 ++++++------ src/cpp/proxy/wsproxysession.cpp | 9 +++++++++ src/cpp/proxy/wsproxysession.h | 9 --------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cpp/proxy/wscontrolsession.h b/src/cpp/proxy/wscontrolsession.h index 6c44af20..acc503da 100644 --- a/src/cpp/proxy/wscontrolsession.h +++ b/src/cpp/proxy/wscontrolsession.h @@ -51,12 +51,12 @@ class WsControlSession : public QObject // tell session that a received sendEvent has been written void sendEventWritten(); - boost::signals2::signal sendEventReceived; - boost::signals2::signal keepAliveSetupEventReceived; - boost::signals2::signal closeEventReceived; // Use -1 for no code - Signal detachEventReceived; - Signal cancelEventReceived; - Signal error; + boost::signals2::signal sendEventReceived; + boost::signals2::signal keepAliveSetupEventReceived; + boost::signals2::signal closeEventReceived; // Use -1 for no code + Signal detachEventReceived; + Signal cancelEventReceived; + Signal error; private: class Private; diff --git a/src/cpp/proxy/wsproxysession.cpp b/src/cpp/proxy/wsproxysession.cpp index a681c0ee..cccc2a28 100644 --- a/src/cpp/proxy/wsproxysession.cpp +++ b/src/cpp/proxy/wsproxysession.cpp @@ -50,6 +50,15 @@ #define ACTIVITY_TIMEOUT 60000 #define KEEPALIVE_RAND_MAX 1000 +struct WSProxyConnections { + Connection sendEventReceivedConnection; + Connection keepAliveSetupEventReceivedConnection; + Connection closeEventReceivedConnection; + Connection detachEventReceivedConnection; + Connection cancelEventReceivedConnection; + Connection errorConnection; +}; + class HttpExtension { public: diff --git a/src/cpp/proxy/wsproxysession.h b/src/cpp/proxy/wsproxysession.h index 6620cde2..03d59d3a 100644 --- a/src/cpp/proxy/wsproxysession.h +++ b/src/cpp/proxy/wsproxysession.h @@ -81,13 +81,4 @@ class WsProxySession : public QObject Private *d; }; -struct WSProxyConnections { - Connection sendEventReceivedConnection; - Connection keepAliveSetupEventReceivedConnection; - Connection closeEventReceivedConnection; - Connection detachEventReceivedConnection; - Connection cancelEventReceivedConnection; - Connection errorConnection; -}; - #endif