unique_ptr for sockets
This commit is contained in:
		| @@ -9,7 +9,6 @@ | ||||
| #include <cstring> | ||||
| #include <iomanip> | ||||
| #include <iostream> | ||||
| #include <ixwebsocket/IXSocket.h> | ||||
| #include <ixwebsocket/IXSocketFactory.h> | ||||
| #include <ixwebsocket/IXSocketTLSOptions.h> | ||||
| #include <sstream> | ||||
|   | ||||
| @@ -11,10 +11,10 @@ | ||||
| #include <memory> | ||||
| #include <string> | ||||
|  | ||||
| #include <ixwebsocket/IXSocket.h> | ||||
|  | ||||
| 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> _socket; | ||||
|         std::unique_ptr<Socket> _socket; | ||||
|         std::atomic<bool> _stop; | ||||
|     }; | ||||
| } // namespace ix | ||||
|   | ||||
| @@ -43,7 +43,7 @@ namespace ix | ||||
|         SocketServer::stop(); | ||||
|     } | ||||
|  | ||||
|     void RedisServer::handleConnection(std::shared_ptr<Socket> socket, | ||||
|     void RedisServer::handleConnection(std::unique_ptr<Socket> socket, | ||||
|                                        std::shared_ptr<ConnectionState> connectionState) | ||||
|     { | ||||
|         _connectedClientsCount++; | ||||
| @@ -102,13 +102,13 @@ namespace ix | ||||
|         _connectedClientsCount--; | ||||
|     } | ||||
|  | ||||
|     void RedisServer::cleanupSubscribers(std::shared_ptr<Socket> socket) | ||||
|     void RedisServer::cleanupSubscribers(std::unique_ptr<Socket>& socket) | ||||
|     { | ||||
|         std::lock_guard<std::mutex> 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> socket, | ||||
|         std::unique_ptr<Socket>& socket, | ||||
|         std::vector<std::string>& tokens) | ||||
|     { | ||||
|         // Parse first line | ||||
| @@ -191,7 +191,7 @@ namespace ix | ||||
|     } | ||||
|  | ||||
|     bool RedisServer::handleCommand( | ||||
|         std::shared_ptr<Socket> socket, | ||||
|         std::unique_ptr<Socket>& socket, | ||||
|         const std::vector<std::string>& tokens) | ||||
|     { | ||||
|         if (tokens.size() != 1) return false; | ||||
| @@ -230,7 +230,7 @@ namespace ix | ||||
|     } | ||||
|  | ||||
|     bool RedisServer::handleSubscribe( | ||||
|         std::shared_ptr<Socket> socket, | ||||
|         std::unique_ptr<Socket>& socket, | ||||
|         const std::vector<std::string>& tokens) | ||||
|     { | ||||
|         if (tokens.size() != 2) return false; | ||||
| @@ -245,13 +245,13 @@ namespace ix | ||||
|         socket->writeBytes(":1\r\n", cb); | ||||
|  | ||||
|         std::lock_guard<std::mutex> lock(_mutex); | ||||
|         _subscribers[channel].insert(socket); | ||||
|         _subscribers[channel].insert(socket.get()); | ||||
|  | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     bool RedisServer::handlePublish( | ||||
|         std::shared_ptr<Socket> socket, | ||||
|         std::unique_ptr<Socket>& socket, | ||||
|         const std::vector<std::string>& tokens) | ||||
|     { | ||||
|         if (tokens.size() != 3) return false; | ||||
|   | ||||
| @@ -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<std::string, std::set<std::shared_ptr<Socket>>> _subscribers; | ||||
|         std::map<std::string, std::set<Socket*>> _subscribers; | ||||
|         std::mutex _mutex; | ||||
|  | ||||
|         std::atomic<bool> _stopHandlingConnections; | ||||
|  | ||||
|         // Methods | ||||
|         virtual void handleConnection(std::shared_ptr<Socket>, | ||||
|         virtual void handleConnection(std::unique_ptr<Socket>, | ||||
|                                       std::shared_ptr<ConnectionState> 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> socket, | ||||
|             std::unique_ptr<Socket>& socket, | ||||
|             std::vector<std::string>& tokens); | ||||
|  | ||||
|         bool handlePublish(std::shared_ptr<Socket> socket, | ||||
|         bool handlePublish(std::unique_ptr<Socket>& socket, | ||||
|                            const std::vector<std::string>& tokens); | ||||
|  | ||||
|         bool handleSubscribe(std::shared_ptr<Socket> socket, | ||||
|         bool handleSubscribe(std::unique_ptr<Socket>& socket, | ||||
|                              const std::vector<std::string>& tokens); | ||||
|  | ||||
|         bool handleCommand(std::shared_ptr<Socket> socket, | ||||
|         bool handleCommand(std::unique_ptr<Socket>& socket, | ||||
|                            const std::vector<std::string>& tokens); | ||||
|  | ||||
|         void cleanupSubscribers(std::shared_ptr<Socket> socket); | ||||
|         void cleanupSubscribers(std::unique_ptr<Socket>& socket); | ||||
|     }; | ||||
| } // namespace ix | ||||
|   | ||||
		Reference in New Issue
	
	Block a user