diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0df94b07..b1350110 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [9.0.2] - 2020-03-24 + +(socket) works with unique_ptr instead of shared_ptr in many places + ## [9.0.1] - 2020-03-24 (socket) selectInterrupt member is an unique_ptr instead of being a shared_ptr diff --git a/ixsnake/ixsnake/IXRedisClient.cpp b/ixsnake/ixsnake/IXRedisClient.cpp index afb6d6a8..4d64eaaf 100644 --- a/ixsnake/ixsnake/IXRedisClient.cpp +++ b/ixsnake/ixsnake/IXRedisClient.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/ixsnake/ixsnake/IXRedisClient.h b/ixsnake/ixsnake/IXRedisClient.h index 2e628eb7..4b7c79d7 100644 --- a/ixsnake/ixsnake/IXRedisClient.h +++ b/ixsnake/ixsnake/IXRedisClient.h @@ -11,10 +11,10 @@ #include #include +#include + namespace ix { - class Socket; - class RedisClient { public: @@ -57,7 +57,7 @@ namespace ix private: std::string writeString(const std::string& str); - std::shared_ptr _socket; + std::unique_ptr _socket; std::atomic _stop; }; } // namespace ix diff --git a/ixsnake/ixsnake/IXRedisServer.cpp b/ixsnake/ixsnake/IXRedisServer.cpp index b7b8b706..ddadb471 100644 --- a/ixsnake/ixsnake/IXRedisServer.cpp +++ b/ixsnake/ixsnake/IXRedisServer.cpp @@ -43,7 +43,7 @@ namespace ix SocketServer::stop(); } - void RedisServer::handleConnection(std::shared_ptr socket, + void RedisServer::handleConnection(std::unique_ptr socket, std::shared_ptr connectionState) { _connectedClientsCount++; @@ -102,13 +102,13 @@ namespace ix _connectedClientsCount--; } - void RedisServer::cleanupSubscribers(std::shared_ptr socket) + void RedisServer::cleanupSubscribers(std::unique_ptr& socket) { std::lock_guard lock(_mutex); for (auto&& it : _subscribers) { - it.second.erase(socket); + it.second.erase(socket.get()); } for (auto it : _subscribers) @@ -145,7 +145,7 @@ namespace ix } bool RedisServer::parseRequest( - std::shared_ptr socket, + std::unique_ptr& socket, std::vector& tokens) { // Parse first line @@ -191,7 +191,7 @@ namespace ix } bool RedisServer::handleCommand( - std::shared_ptr socket, + std::unique_ptr& socket, const std::vector& tokens) { if (tokens.size() != 1) return false; @@ -230,7 +230,7 @@ namespace ix } bool RedisServer::handleSubscribe( - std::shared_ptr socket, + std::unique_ptr& socket, const std::vector& tokens) { if (tokens.size() != 2) return false; @@ -245,13 +245,13 @@ namespace ix socket->writeBytes(":1\r\n", cb); std::lock_guard lock(_mutex); - _subscribers[channel].insert(socket); + _subscribers[channel].insert(socket.get()); return true; } bool RedisServer::handlePublish( - std::shared_ptr socket, + std::unique_ptr& socket, const std::vector& tokens) { if (tokens.size() != 3) return false; diff --git a/ixsnake/ixsnake/IXRedisServer.h b/ixsnake/ixsnake/IXRedisServer.h index d6279fd6..9d06b595 100644 --- a/ixsnake/ixsnake/IXRedisServer.h +++ b/ixsnake/ixsnake/IXRedisServer.h @@ -37,13 +37,13 @@ namespace ix // Subscribers // We could store connection states in there, to add better debugging // since a connection state has a readable ID - std::map>> _subscribers; + std::map> _subscribers; std::mutex _mutex; std::atomic _stopHandlingConnections; // Methods - virtual void handleConnection(std::shared_ptr, + virtual void handleConnection(std::unique_ptr, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; @@ -51,18 +51,18 @@ namespace ix std::string writeString(const std::string& str); bool parseRequest( - std::shared_ptr socket, + std::unique_ptr& socket, std::vector& tokens); - bool handlePublish(std::shared_ptr socket, + bool handlePublish(std::unique_ptr& socket, const std::vector& tokens); - bool handleSubscribe(std::shared_ptr socket, + bool handleSubscribe(std::unique_ptr& socket, const std::vector& tokens); - bool handleCommand(std::shared_ptr socket, + bool handleCommand(std::unique_ptr& socket, const std::vector& tokens); - void cleanupSubscribers(std::shared_ptr socket); + void cleanupSubscribers(std::unique_ptr& socket); }; } // namespace ix diff --git a/ixwebsocket/IXHttp.cpp b/ixwebsocket/IXHttp.cpp index 688584f1..9db7c023 100644 --- a/ixwebsocket/IXHttp.cpp +++ b/ixwebsocket/IXHttp.cpp @@ -92,7 +92,7 @@ namespace ix return std::make_tuple(method, requestUri, httpVersion); } - std::tuple Http::parseRequest(std::shared_ptr socket) + std::tuple Http::parseRequest(std::unique_ptr& socket) { HttpRequestPtr httpRequest; @@ -133,7 +133,7 @@ namespace ix return std::make_tuple(true, "", httpRequest); } - bool Http::sendResponse(HttpResponsePtr response, std::shared_ptr socket) + bool Http::sendResponse(HttpResponsePtr response, std::unique_ptr& socket) { // Write the response to the socket std::stringstream ss; diff --git a/ixwebsocket/IXHttp.h b/ixwebsocket/IXHttp.h index 0a3bf872..329db45a 100644 --- a/ixwebsocket/IXHttp.h +++ b/ixwebsocket/IXHttp.h @@ -115,8 +115,8 @@ namespace ix { public: static std::tuple parseRequest( - std::shared_ptr socket); - static bool sendResponse(HttpResponsePtr response, std::shared_ptr socket); + std::unique_ptr& socket); + static bool sendResponse(HttpResponsePtr response, std::unique_ptr& socket); static std::pair parseStatusLine(const std::string& line); static std::tuple parseRequestLine( diff --git a/ixwebsocket/IXHttpClient.h b/ixwebsocket/IXHttpClient.h index 58144687..d7d0da20 100644 --- a/ixwebsocket/IXHttpClient.h +++ b/ixwebsocket/IXHttpClient.h @@ -95,7 +95,7 @@ namespace ix std::atomic _stop; std::thread _thread; - std::shared_ptr _socket; + std::unique_ptr _socket; std::mutex _mutex; // to protect accessing the _socket (only one socket per client) SocketTLSOptions _tlsOptions; diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index 4a197de9..d4cf951f 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -69,7 +69,7 @@ namespace ix _onConnectionCallback = callback; } - void HttpServer::handleConnection(std::shared_ptr socket, + void HttpServer::handleConnection(std::unique_ptr socket, std::shared_ptr connectionState) { _connectedClientsCount++; diff --git a/ixwebsocket/IXHttpServer.h b/ixwebsocket/IXHttpServer.h index b87f8a41..96b22d3f 100644 --- a/ixwebsocket/IXHttpServer.h +++ b/ixwebsocket/IXHttpServer.h @@ -43,7 +43,7 @@ namespace ix std::atomic _connectedClientsCount; // Methods - virtual void handleConnection(std::shared_ptr, + virtual void handleConnection(std::unique_ptr, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; diff --git a/ixwebsocket/IXSocketFactory.cpp b/ixwebsocket/IXSocketFactory.cpp index 4043bc65..bf2d24fe 100644 --- a/ixwebsocket/IXSocketFactory.cpp +++ b/ixwebsocket/IXSocketFactory.cpp @@ -24,28 +24,28 @@ namespace ix { - std::shared_ptr createSocket(bool tls, + std::unique_ptr createSocket(bool tls, int fd, std::string& errorMsg, const SocketTLSOptions& tlsOptions) { (void) tlsOptions; errorMsg.clear(); - std::shared_ptr socket; + std::unique_ptr socket; if (!tls) { - socket = std::make_shared(fd); + socket = std::make_unique(fd); } else { #ifdef IXWEBSOCKET_USE_TLS #if defined(IXWEBSOCKET_USE_MBED_TLS) - socket = std::make_shared(tlsOptions, fd); + socket = std::make_unique(tlsOptions, fd); #elif defined(IXWEBSOCKET_USE_OPEN_SSL) - socket = std::make_shared(tlsOptions, fd); + socket = std::make_unique(tlsOptions, fd); #elif defined(__APPLE__) - socket = std::make_shared(tlsOptions, fd); + socket = std::make_unique(tlsOptions, fd); #endif #else errorMsg = "TLS support is not enabled on this platform."; diff --git a/ixwebsocket/IXSocketFactory.h b/ixwebsocket/IXSocketFactory.h index b682fadc..de1eeda6 100644 --- a/ixwebsocket/IXSocketFactory.h +++ b/ixwebsocket/IXSocketFactory.h @@ -14,7 +14,7 @@ namespace ix { class Socket; - std::shared_ptr createSocket(bool tls, + std::unique_ptr createSocket(bool tls, int fd, std::string& errorMsg, const SocketTLSOptions& tlsOptions); diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 776f3857..ff6faa0e 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -340,7 +340,7 @@ namespace ix std::lock_guard lock(_connectionsThreadsMutex); _connectionsThreads.push_back(std::make_pair( connectionState, - std::thread(&SocketServer::handleConnection, this, socket, connectionState))); + std::thread(&SocketServer::handleConnection, this, std::move(socket), connectionState))); } } diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index 535f34b7..8874587b 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -101,7 +101,7 @@ namespace ix // the factory to create ConnectionState objects ConnectionStateFactory _connectionStateFactory; - virtual void handleConnection(std::shared_ptr, + virtual void handleConnection(std::unique_ptr, std::shared_ptr connectionState) = 0; virtual size_t getConnectedClientsCount() = 0; diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 5b7d1040..68974d8f 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -210,7 +210,7 @@ namespace ix return status; } - WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr socket, int timeoutSecs) + WebSocketInitResult WebSocket::connectToSocket(std::unique_ptr socket, int timeoutSecs) { { std::lock_guard lock(_configMutex); @@ -218,7 +218,7 @@ namespace ix _perMessageDeflateOptions, _socketTLSOptions, _enablePong, _pingIntervalSecs); } - WebSocketInitResult status = _ws.connectToSocket(socket, timeoutSecs); + WebSocketInitResult status = _ws.connectToSocket(std::move(socket), timeoutSecs); if (!status.success) { return status; diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 6d6c4b7f..0c288cd5 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -113,7 +113,7 @@ namespace ix static void invokeTrafficTrackerCallback(size_t size, bool incoming); // Server - WebSocketInitResult connectToSocket(std::shared_ptr, int timeoutSecs); + WebSocketInitResult connectToSocket(std::unique_ptr, int timeoutSecs); WebSocketTransport _ws; diff --git a/ixwebsocket/IXWebSocketHandshake.cpp b/ixwebsocket/IXWebSocketHandshake.cpp index 392dba72..727916a4 100644 --- a/ixwebsocket/IXWebSocketHandshake.cpp +++ b/ixwebsocket/IXWebSocketHandshake.cpp @@ -21,7 +21,7 @@ namespace ix { WebSocketHandshake::WebSocketHandshake( std::atomic& requestInitCancellation, - std::shared_ptr socket, + std::unique_ptr& socket, WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, std::atomic& enablePerMessageDeflate) diff --git a/ixwebsocket/IXWebSocketHandshake.h b/ixwebsocket/IXWebSocketHandshake.h index 62090bc3..0c8ed289 100644 --- a/ixwebsocket/IXWebSocketHandshake.h +++ b/ixwebsocket/IXWebSocketHandshake.h @@ -23,7 +23,7 @@ namespace ix { public: WebSocketHandshake(std::atomic& requestInitCancellation, - std::shared_ptr _socket, + std::unique_ptr& _socket, WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, std::atomic& enablePerMessageDeflate); @@ -46,7 +46,7 @@ namespace ix bool insensitiveStringCompare(const std::string& a, const std::string& b); std::atomic& _requestInitCancellation; - std::shared_ptr _socket; + std::unique_ptr& _socket; WebSocketPerMessageDeflatePtr& _perMessageDeflate; WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions; std::atomic& _enablePerMessageDeflate; diff --git a/ixwebsocket/IXWebSocketHttpHeaders.cpp b/ixwebsocket/IXWebSocketHttpHeaders.cpp index 4bc90972..77fd7955 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.cpp +++ b/ixwebsocket/IXWebSocketHttpHeaders.cpp @@ -32,7 +32,7 @@ namespace ix } std::pair parseHttpHeaders( - std::shared_ptr socket, const CancellationRequest& isCancellationRequested) + std::unique_ptr& socket, const CancellationRequest& isCancellationRequested) { WebSocketHttpHeaders headers; diff --git a/ixwebsocket/IXWebSocketHttpHeaders.h b/ixwebsocket/IXWebSocketHttpHeaders.h index 99cb1242..777e7a9d 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.h +++ b/ixwebsocket/IXWebSocketHttpHeaders.h @@ -29,5 +29,5 @@ namespace ix using WebSocketHttpHeaders = std::map; std::pair parseHttpHeaders( - std::shared_ptr socket, const CancellationRequest& isCancellationRequested); + std::unique_ptr& socket, const CancellationRequest& isCancellationRequested); } // namespace ix diff --git a/ixwebsocket/IXWebSocketServer.cpp b/ixwebsocket/IXWebSocketServer.cpp index db646d0b..86b0242a 100644 --- a/ixwebsocket/IXWebSocketServer.cpp +++ b/ixwebsocket/IXWebSocketServer.cpp @@ -71,7 +71,7 @@ namespace ix _onConnectionCallback = callback; } - void WebSocketServer::handleConnection(std::shared_ptr socket, + void WebSocketServer::handleConnection(std::unique_ptr socket, std::shared_ptr connectionState) { setThreadName("WebSocketServer::" + connectionState->getId()); @@ -96,7 +96,7 @@ namespace ix _clients.insert(webSocket); } - auto status = webSocket->connectToSocket(socket, _handshakeTimeoutSecs); + auto status = webSocket->connectToSocket(std::move(socket), _handshakeTimeoutSecs); if (status.success) { // Process incoming messages and execute callbacks diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index dab0172f..22bfa5b0 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -59,7 +59,7 @@ namespace ix const static bool kDefaultEnablePong; // Methods - virtual void handleConnection(std::shared_ptr socket, + virtual void handleConnection(std::unique_ptr socket, std::shared_ptr connectionState) final; virtual size_t getConnectedClientsCount() final; }; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 04e38ac9..946d3878 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -140,7 +140,7 @@ namespace ix } // Server - WebSocketInitResult WebSocketTransport::connectToSocket(std::shared_ptr socket, + WebSocketInitResult WebSocketTransport::connectToSocket(std::unique_ptr socket, int timeoutSecs) { std::lock_guard lock(_socketMutex); @@ -149,7 +149,7 @@ namespace ix _useMask = false; _blockingSend = true; - _socket = socket; + _socket = std::move(socket); _perMessageDeflate = std::make_unique(); WebSocketHandshake webSocketHandshake(_requestInitCancellation, diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index 5813e0dd..e3c5df20 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -83,7 +83,7 @@ namespace ix int timeoutSecs); // Server - WebSocketInitResult connectToSocket(std::shared_ptr socket, int timeoutSecs); + WebSocketInitResult connectToSocket(std::unique_ptr socket, int timeoutSecs); PollResult poll(); WebSocketSendInfo sendBinary(const std::string& message, @@ -171,7 +171,7 @@ namespace ix static constexpr size_t kChunkSize = 1 << 15; // Underlying TCP socket - std::shared_ptr _socket; + std::unique_ptr _socket; std::mutex _socketMutex; // Hold the state of the connection (OPEN, CLOSED, etc...) diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index b53a1269..f9bbe3d3 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "9.0.1" +#define IX_WEBSOCKET_VERSION "9.0.2"