diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index 538ce4c..197a507 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -135,7 +135,8 @@ void Engine::start(ProxyConfiguration *configuration) qCDebug(dcEngine()) << "Starting the tunnel proxy manager..."; m_tunnelProxyServer->startServer(); - // Start the monitor server + // Monitor server + // ------------------------------------- m_monitorServer = new MonitorServer(configuration->monitorSocketFileName(), this); m_monitorServer->startServer(); @@ -266,7 +267,6 @@ Engine::Engine(QObject *parent) : m_timer->setInterval(50); connect(m_timer, &QTimer::timeout, this, &Engine::onTimerTick); - m_logEngine = new LogEngine(this); } @@ -357,6 +357,9 @@ void Engine::setRunning(bool running) if (m_proxyServer) m_proxyServer->setRunning(running); + if (m_tunnelProxyServer) + m_tunnelProxyServer->setRunning(running); + qCDebug(dcEngine()) << "Engine is" << (running ? "now running." : "not running any more."); if (running) { diff --git a/libnymea-remoteproxy/server/jsonrpcserver.cpp b/libnymea-remoteproxy/server/jsonrpcserver.cpp index 72e63cd..a5c5950 100644 --- a/libnymea-remoteproxy/server/jsonrpcserver.cpp +++ b/libnymea-remoteproxy/server/jsonrpcserver.cpp @@ -189,6 +189,11 @@ void JsonRpcServer::unregisterHandler(JsonHandler *handler) m_handlers.remove(handler->name()); } +uint JsonRpcServer::registeredClientCount() const +{ + return m_clients.count(); +} + void JsonRpcServer::processDataPacket(TransportClient *transportClient, const QByteArray &data) { QJsonParseError error; diff --git a/libnymea-remoteproxy/server/jsonrpcserver.h b/libnymea-remoteproxy/server/jsonrpcserver.h index bed18ce..a18a941 100644 --- a/libnymea-remoteproxy/server/jsonrpcserver.h +++ b/libnymea-remoteproxy/server/jsonrpcserver.h @@ -54,6 +54,8 @@ public: void registerHandler(JsonHandler *handler); void unregisterHandler(JsonHandler *handler); + uint registeredClientCount() const; + signals: void TunnelEstablished(const QVariantMap ¶ms); diff --git a/libnymea-remoteproxy/server/tcpsocketserver.cpp b/libnymea-remoteproxy/server/tcpsocketserver.cpp index 74b4144..453797b 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.cpp +++ b/libnymea-remoteproxy/server/tcpsocketserver.cpp @@ -85,8 +85,14 @@ bool TcpSocketServer::running() const bool TcpSocketServer::startServer() { + if (m_server) { + m_server->close(); + delete m_server; + m_server = nullptr; + } + qCDebug(dcTcpSocketServer()) << "Starting TCP server" << m_serverUrl.toString(); - m_server = new SslServer(m_sslEnabled, m_sslConfiguration); + m_server = new SslServer(m_sslEnabled, m_sslConfiguration, this); if(!m_server->listen(QHostAddress(m_serverUrl.host()), static_cast(m_serverUrl.port()))) { qCWarning(dcTcpSocketServer()) << "Tcp server error: can not listen on" << m_serverUrl.toString(); delete m_server; @@ -98,11 +104,13 @@ bool TcpSocketServer::startServer() connect(m_server, &SslServer::socketDisconnected, this, &TcpSocketServer::onSocketDisconnected); connect(m_server, &SslServer::dataAvailable, this, &TcpSocketServer::onDataAvailable); qCDebug(dcTcpSocketServer()) << "Server started successfully."; + qCDebug(dcTcpSocketServer()) << m_server; return true; } bool TcpSocketServer::stopServer() { + qCDebug(dcTcpSocketServer()) << "Stopping server" << m_serverUrl.toString() << m_server; if (!m_server) return true; @@ -111,8 +119,6 @@ bool TcpSocketServer::stopServer() killClientConnection(clientId, "Stop server"); } - qCDebug(dcTcpSocketServer()) << "Stop server" << m_serverUrl.toString(); - m_server->close(); m_server->deleteLater(); m_server = nullptr; @@ -162,8 +168,11 @@ void SslServer::incomingConnection(qintptr socketDescriptor) { QSslSocket *sslSocket = new QSslSocket(this); qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket; + connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected); + connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead); + typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError); connect(sslSocket, static_cast(&QAbstractSocket::error), this, &SslServer::onSocketError); @@ -180,7 +189,6 @@ void SslServer::incomingConnection(qintptr socketDescriptor) } }); - connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead); if (!sslSocket->setSocketDescriptor(socketDescriptor)) { qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor."; @@ -188,6 +196,8 @@ void SslServer::incomingConnection(qintptr socketDescriptor) return; } + addPendingConnection(sslSocket); + if (m_sslEnabled) { qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket; sslSocket->setSslConfiguration(m_config); @@ -218,7 +228,7 @@ void SslServer::onSocketError(QAbstractSocket::SocketError error) QSslSocket *sslSocket = static_cast(sender()); qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString(); qCWarning(dcTcpSocketServer()) << "Explicitly closing the socket connection."; - sslSocket->close(); + sslSocket->abort(); } } diff --git a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp index 986e814..02ac1d2 100644 --- a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp +++ b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp @@ -67,10 +67,11 @@ void TunnelProxyServer::setRunning(bool running) void TunnelProxyServer::registerTransportInterface(TransportInterface *interface) { qCDebug(dcTunnelProxyServer()) << "Register transport interface" << interface->serverName(); - + //interface->setParent(this); if (m_transportInterfaces.contains(interface)) { qCWarning(dcTunnelProxyServer()) << "Transport interface already registerd."; return; + } connect(interface, &TransportInterface::clientConnected, this, &TunnelProxyServer::onClientConnected); @@ -109,9 +110,15 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerServer(const QUui // Enable SLIP from now on tunnelProxyClient->enableSlipAfterResponse(); - TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, this); + TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, tunnelProxyClient); m_tunnelProxyServerConnections.insert(serverUuid, serverConnection); + qCDebug(dcTunnelProxyServer()) << "New server connection registered successfully" << serverConnection; + + // For debugging + qCDebug(dcTunnelProxyServer()) << "####" << "Total clients" << m_proxyClients.count() << "JSON RPC clients" << m_jsonRpcServer->registeredClientCount() << "interface connections" << tunnelProxyClient->interface()->connectionsCount() + << "Servers" << m_tunnelProxyServerConnections.count() << "Clients" << m_tunnelProxyClientConnections.count(); + return TunnelProxyServer::TunnelProxyErrorNoError; } @@ -153,12 +160,17 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerClient(const QUui tunnelProxyClient->setUuid(clientUuid); tunnelProxyClient->setName(clientName); - TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, this); + TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, tunnelProxyClient); clientConnection->setServerConnection(serverConnection); m_tunnelProxyClientConnections.insert(clientUuid, clientConnection); - qCDebug(dcTunnelProxyServer()) << "Register client" << clientConnection << "-->" << serverConnection; serverConnection->registerClientConnection(clientConnection); + qCDebug(dcTunnelProxyServer()) << "New client connection registered successfully" << clientConnection << "-->" << serverConnection;; + + // For debugging + qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount() + << "Interface connections:" << tunnelProxyClient->interface()->connectionsCount() + << "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count(); // Tell the server a new client want's to connect QVariantMap params; @@ -286,6 +298,11 @@ void TunnelProxyServer::onClientConnected(const QUuid &clientId, const QHostAddr TunnelProxyClient *tunnelProxyClient = new TunnelProxyClient(interface, clientId, address, this); m_proxyClients.insert(clientId, tunnelProxyClient); m_jsonRpcServer->registerClient(tunnelProxyClient); + + // For debugging + qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount() + << "Interface connections:" << tunnelProxyClient->interface()->connectionsCount() + << "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count(); } void TunnelProxyServer::onClientDisconnected(const QUuid &clientId) @@ -333,6 +350,11 @@ void TunnelProxyServer::onClientDisconnected(const QUuid &clientId) // Unregister from json rpc server m_jsonRpcServer->unregisterClient(tunnelProxyClient); + // For debugging + qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount() + << "Interface connections:" << tunnelProxyClient->interface()->connectionsCount() + << "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count(); + // Delete the proxy client tunnelProxyClient->deleteLater(); } @@ -395,10 +417,6 @@ void TunnelProxyServer::onClientDataAvailable(const QUuid &clientId, const QByte TunnelProxyClientConnection *clientConnection = serverConnection->getClientConnection(frame.socketAddress); if (!clientConnection) { qCWarning(dcTunnelProxyServer()) << "The server connection wants to send data to a client connection which has not been registered to the server."; - qCWarning(dcTunnelProxyServer()) << "Notifying the server that there is no longer any socket connected with address" << frame.socketAddress; - QVariantMap params; - params.insert("socketAddress", frame.socketAddress); - m_jsonRpcServer->sendNotification("TunnelProxy", "ClientDisconnected", params, serverConnection->transportClient()); continue; } diff --git a/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp b/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp index 7185a0a..4483605 100644 --- a/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp +++ b/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp @@ -179,6 +179,10 @@ void RemoteProxyTestsTunnelProxy::apiBasicCalls() QFETCH(int, responseId); QFETCH(QString, responseStatus); + resetDebugCategories(); + addDebugCategory("TunnelProxyServer.debug=true"); + addDebugCategory("*.debug=true"); + // Start the server startServer();