diff --git a/ixsnake/ixsnake/IXRedisServer.cpp b/ixsnake/ixsnake/IXRedisServer.cpp index 6358129f..3825d03c 100644 --- a/ixsnake/ixsnake/IXRedisServer.cpp +++ b/ixsnake/ixsnake/IXRedisServer.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -45,16 +44,11 @@ namespace ix SocketServer::stop(); } - void RedisServer::handleConnection(int fd, std::shared_ptr connectionState) + void RedisServer::handleConnection(std::shared_ptr socket, + std::shared_ptr connectionState) { _connectedClientsCount++; - std::string errorMsg; - auto socket = createSocket(fd, errorMsg); - - // Set the socket to non blocking mode + other tweaks - SocketConnect::configure(fd); - while (!_stopHandlingConnections) { std::vector tokens; @@ -105,7 +99,6 @@ namespace ix logInfo("Connection closed for connection id " + connectionState->getId()); connectionState->setTerminated(); - Socket::closeSocket(fd); _connectedClientsCount--; } diff --git a/ixsnake/ixsnake/IXRedisServer.h b/ixsnake/ixsnake/IXRedisServer.h index 13b6b11f..f410133b 100644 --- a/ixsnake/ixsnake/IXRedisServer.h +++ b/ixsnake/ixsnake/IXRedisServer.h @@ -42,7 +42,7 @@ namespace ix std::atomic _stopHandlingConnections; // Methods - virtual void handleConnection(int fd, + virtual void handleConnection(std::shared_ptr, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index b40349a6..66bbffc3 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -8,7 +8,6 @@ #include "IXNetSystem.h" #include "IXSocketConnect.h" -#include "IXSocketFactory.h" #include "IXUserAgent.h" #include #include @@ -70,16 +69,11 @@ namespace ix _onConnectionCallback = callback; } - void HttpServer::handleConnection(int fd, std::shared_ptr connectionState) + void HttpServer::handleConnection(std::shared_ptr socket, + std::shared_ptr connectionState) { _connectedClientsCount++; - std::string errorMsg; - auto socket = createSocket(fd, errorMsg); - - // Set the socket to non blocking mode + other tweaks - SocketConnect::configure(fd); - auto ret = Http::parseRequest(socket); // FIXME: handle errors in parseRequest @@ -92,7 +86,6 @@ namespace ix } } connectionState->setTerminated(); - Socket::closeSocket(fd); _connectedClientsCount--; } diff --git a/ixwebsocket/IXHttpServer.h b/ixwebsocket/IXHttpServer.h index 60da4129..5779d77f 100644 --- a/ixwebsocket/IXHttpServer.h +++ b/ixwebsocket/IXHttpServer.h @@ -42,7 +42,7 @@ namespace ix std::atomic _connectedClientsCount; // Methods - virtual void handleConnection(int fd, + virtual void handleConnection(std::shared_ptr, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index a2535811..26e51a6e 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -9,6 +9,7 @@ #include "IXNetSystem.h" #include "IXSocket.h" #include "IXSocketConnect.h" +#include "IXSocketFactory.h" #include #include #include @@ -267,11 +268,25 @@ namespace ix if (_stop) return; + // create socket + std::string errorMsg; + auto socket = createSocket(clientFd, errorMsg); + + if (socket == nullptr) + { + logError("SocketServer::run() cannot create socket: " + errorMsg); + Socket::closeSocket(clientFd); + continue; + } + + // Set the socket to non blocking mode + other tweaks + SocketConnect::configure(clientFd); + // Launch the handleConnection work asynchronously in its own thread. std::lock_guard lock(_connectionsThreadsMutex); _connectionsThreads.push_back(std::make_pair( connectionState, - std::thread(&SocketServer::handleConnection, this, clientFd, connectionState))); + std::thread(&SocketServer::handleConnection, this, socket, connectionState))); } } diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index 12f5a297..ca5d529e 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -21,6 +21,8 @@ namespace ix { + class Socket; + class SocketServer { public: @@ -96,7 +98,8 @@ namespace ix // the factory to create ConnectionState objects ConnectionStateFactory _connectionStateFactory; - virtual void handleConnection(int fd, std::shared_ptr connectionState) = 0; + virtual void handleConnection(std::shared_ptr, + std::shared_ptr connectionState) = 0; virtual size_t getConnectedClientsCount() = 0; // Returns true if all connection threads are joined diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index c33922a8..08633b17 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -201,7 +201,8 @@ namespace ix return status; } - WebSocketInitResult WebSocket::connectToSocket(int fd, int timeoutSecs) + WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr socket, + int timeoutSecs) { { std::lock_guard lock(_configMutex); @@ -212,7 +213,7 @@ namespace ix _pingTimeoutSecs); } - WebSocketInitResult status = _ws.connectToSocket(fd, timeoutSecs); + WebSocketInitResult status = _ws.connectToSocket(socket, timeoutSecs); if (!status.success) { return status; diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index e647a966..3d8df157 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -113,7 +113,8 @@ namespace ix static void invokeTrafficTrackerCallback(size_t size, bool incoming); // Server - WebSocketInitResult connectToSocket(int fd, int timeoutSecs); + WebSocketInitResult connectToSocket(std::shared_ptr, + int timeoutSecs); WebSocketTransport _ws; diff --git a/ixwebsocket/IXWebSocketHandshake.cpp b/ixwebsocket/IXWebSocketHandshake.cpp index 9459028e..0cad8ae6 100644 --- a/ixwebsocket/IXWebSocketHandshake.cpp +++ b/ixwebsocket/IXWebSocketHandshake.cpp @@ -239,18 +239,13 @@ namespace ix return WebSocketInitResult(true, status, "", headers, path); } - WebSocketInitResult WebSocketHandshake::serverHandshake(int fd, int timeoutSecs) + WebSocketInitResult WebSocketHandshake::serverHandshake(int timeoutSecs) { _requestInitCancellation = false; - // Set the socket to non blocking mode + other tweaks - SocketConnect::configure(fd); - auto isCancellationRequested = makeCancellationRequestWithTimeout(timeoutSecs, _requestInitCancellation); - std::string remote = std::string("remote fd ") + std::to_string(fd); - // Read first line auto lineResult = _socket->readLine(isCancellationRequested); auto lineValid = lineResult.first; @@ -358,7 +353,7 @@ namespace ix if (!_socket->writeBytes(ss.str(), isCancellationRequested)) { return WebSocketInitResult( - false, 0, std::string("Failed sending response to ") + remote); + false, 0, std::string("Failed sending response to remote end")); } return WebSocketInitResult(true, 200, "", headers, uri); diff --git a/ixwebsocket/IXWebSocketHandshake.h b/ixwebsocket/IXWebSocketHandshake.h index a7098353..c0bd5e2f 100644 --- a/ixwebsocket/IXWebSocketHandshake.h +++ b/ixwebsocket/IXWebSocketHandshake.h @@ -56,7 +56,7 @@ namespace ix int port, int timeoutSecs); - WebSocketInitResult serverHandshake(int fd, int timeoutSecs); + WebSocketInitResult serverHandshake(int timeoutSecs); private: std::string genRandomString(const int len); diff --git a/ixwebsocket/IXWebSocketServer.cpp b/ixwebsocket/IXWebSocketServer.cpp index dae00cd1..ce2089b0 100644 --- a/ixwebsocket/IXWebSocketServer.cpp +++ b/ixwebsocket/IXWebSocketServer.cpp @@ -63,7 +63,8 @@ namespace ix _onConnectionCallback = callback; } - void WebSocketServer::handleConnection(int fd, std::shared_ptr connectionState) + void WebSocketServer::handleConnection(std::shared_ptr socket, + std::shared_ptr connectionState) { auto webSocket = std::make_shared(); _onConnectionCallback(webSocket, connectionState); @@ -81,7 +82,7 @@ namespace ix _clients.insert(webSocket); } - auto status = webSocket->connectToSocket(fd, _handshakeTimeoutSecs); + auto status = webSocket->connectToSocket(socket, _handshakeTimeoutSecs); if (status.success) { // Process incoming messages and execute callbacks @@ -107,8 +108,6 @@ namespace ix logInfo("WebSocketServer::handleConnection() done"); connectionState->setTerminated(); - - Socket::closeSocket(fd); } std::set> WebSocketServer::getClients() diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index a3cb7a64..21921672 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -55,7 +55,7 @@ namespace ix const static bool kDefaultEnablePong; // Methods - virtual void handleConnection(int fd, + virtual void handleConnection(std::shared_ptr socket, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; }; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 47cf67ff..3fbe5248 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -171,20 +171,15 @@ namespace ix } // Server - WebSocketInitResult WebSocketTransport::connectToSocket(int fd, int timeoutSecs) + WebSocketInitResult WebSocketTransport::connectToSocket(std::shared_ptr socket, + int timeoutSecs) { std::lock_guard lock(_socketMutex); // Server should not mask the data it sends to the client _useMask = false; - std::string errorMsg; - _socket = createSocket(fd, errorMsg); - - if (!_socket) - { - return WebSocketInitResult(false, 0, errorMsg); - } + _socket = socket; WebSocketHandshake webSocketHandshake(_requestInitCancellation, _socket, @@ -192,7 +187,7 @@ namespace ix _perMessageDeflateOptions, _enablePerMessageDeflate); - auto result = webSocketHandshake.serverHandshake(fd, timeoutSecs); + auto result = webSocketHandshake.serverHandshake(timeoutSecs); if (result.success) { setReadyState(ReadyState::OPEN); diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index ee3ab107..b5d7bcac 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -77,11 +77,14 @@ namespace ix int pingIntervalSecs, int pingTimeoutSecs); - WebSocketInitResult connectToUrl( // Client + // Client + WebSocketInitResult connectToUrl( const std::string& url, const WebSocketHttpHeaders& headers, int timeoutSecs); - WebSocketInitResult connectToSocket(int fd, // Server + + // Server + WebSocketInitResult connectToSocket(std::shared_ptr socket, int timeoutSecs); PollResult poll();