Socket Factory has only one function which works for server and client code, and can do tls for both
This commit is contained in:
		| @@ -11,6 +11,7 @@ | ||||
| #include <iostream> | ||||
| #include <ixwebsocket/IXSocket.h> | ||||
| #include <ixwebsocket/IXSocketFactory.h> | ||||
| #include <ixwebsocket/IXSocketTLSOptions.h> | ||||
| #include <sstream> | ||||
| #include <vector> | ||||
|  | ||||
| @@ -20,7 +21,8 @@ namespace ix | ||||
|     { | ||||
|         bool tls = false; | ||||
|         std::string errorMsg; | ||||
|         _socket = createSocket(tls, errorMsg); | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         _socket = createSocket(tls, -1, errorMsg, tlsOptions); | ||||
|  | ||||
|         if (!_socket) | ||||
|         { | ||||
|   | ||||
| @@ -147,7 +147,7 @@ namespace ix | ||||
|  | ||||
|         bool tls = protocol == "https"; | ||||
|         std::string errorMsg; | ||||
|         _socket = createSocket(tls, errorMsg, _tlsOptions); | ||||
|         _socket = createSocket(tls, -1, errorMsg, _tlsOptions); | ||||
|  | ||||
|         if (!_socket) | ||||
|         { | ||||
|   | ||||
| @@ -27,6 +27,7 @@ | ||||
| namespace ix | ||||
| { | ||||
|     std::shared_ptr<Socket> createSocket(bool tls, | ||||
|                                          int fd, | ||||
|                                          std::string& errorMsg, | ||||
|                                          const SocketTLSOptions& tlsOptions) | ||||
|     { | ||||
| @@ -35,19 +36,19 @@ namespace ix | ||||
|  | ||||
|         if (!tls) | ||||
|         { | ||||
|             socket = std::make_shared<Socket>(); | ||||
|             socket = std::make_shared<Socket>(fd); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
| #ifdef IXWEBSOCKET_USE_TLS | ||||
| #if defined(IXWEBSOCKET_USE_MBED_TLS) | ||||
|             socket = std::make_shared<SocketMbedTLS>(tlsOptions); | ||||
|             socket = std::make_shared<SocketMbedTLS>(tlsOptions, fd); | ||||
| #elif defined(IXWEBSOCKET_USE_OPEN_SSL) | ||||
|             socket = std::make_shared<SocketOpenSSL>(tlsOptions); | ||||
|             socket = std::make_shared<SocketOpenSSL>(tlsOptions, fd); | ||||
| #elif defined(_WIN32) | ||||
|             socket = std::make_shared<SocketSChannel>(tlsOptions); | ||||
|             socket = std::make_shared<SocketSChannel>(tlsOptions, fd); | ||||
| #elif defined(__APPLE__) | ||||
|             socket = std::make_shared<SocketAppleSSL>(tlsOptions); | ||||
|             socket = std::make_shared<SocketAppleSSL>(tlsOptions, fd); | ||||
| #endif | ||||
| #else | ||||
|             errorMsg = "TLS support is not enabled on this platform."; | ||||
| @@ -62,17 +63,4 @@ namespace ix | ||||
|  | ||||
|         return socket; | ||||
|     } | ||||
|  | ||||
|     std::shared_ptr<Socket> createSocket(int fd, std::string& errorMsg) | ||||
|     { | ||||
|         errorMsg.clear(); | ||||
|  | ||||
|         std::shared_ptr<Socket> socket = std::make_shared<Socket>(fd); | ||||
|         if (!socket->init(errorMsg)) | ||||
|         { | ||||
|             socket.reset(); | ||||
|         } | ||||
|  | ||||
|         return socket; | ||||
|     } | ||||
| } // namespace ix | ||||
|   | ||||
| @@ -15,8 +15,7 @@ namespace ix | ||||
| { | ||||
|     class Socket; | ||||
|     std::shared_ptr<Socket> createSocket(bool tls, | ||||
|                                          int fd, | ||||
|                                          std::string& errorMsg, | ||||
|                                          const SocketTLSOptions& tlsOptions); | ||||
|  | ||||
|     std::shared_ptr<Socket> createSocket(int fd, std::string& errorMsg); | ||||
| } // namespace ix | ||||
|   | ||||
| @@ -270,7 +270,8 @@ namespace ix | ||||
|  | ||||
|             // create socket | ||||
|             std::string errorMsg; | ||||
|             auto socket = createSocket(clientFd, errorMsg); | ||||
|             bool tls = false; | ||||
|             auto socket = createSocket(tls, clientFd, errorMsg, _socketTLSOptions); | ||||
|  | ||||
|             if (socket == nullptr) | ||||
|             { | ||||
|   | ||||
| @@ -201,8 +201,7 @@ namespace ix | ||||
|         return status; | ||||
|     } | ||||
|  | ||||
|     WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr<Socket> socket, | ||||
|                                                    int timeoutSecs) | ||||
|     WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs) | ||||
|     { | ||||
|         { | ||||
|             std::lock_guard<std::mutex> lock(_configMutex); | ||||
|   | ||||
| @@ -113,8 +113,7 @@ namespace ix | ||||
|         static void invokeTrafficTrackerCallback(size_t size, bool incoming); | ||||
|  | ||||
|         // Server | ||||
|         WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, | ||||
|                                             int timeoutSecs); | ||||
|         WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, int timeoutSecs); | ||||
|  | ||||
|         WebSocketTransport _ws; | ||||
|  | ||||
|   | ||||
| @@ -148,7 +148,7 @@ namespace ix | ||||
|  | ||||
|         std::string errorMsg; | ||||
|         bool tls = protocol == "wss"; | ||||
|         _socket = createSocket(tls, errorMsg, _socketTLSOptions); | ||||
|         _socket = createSocket(tls, -1, errorMsg, _socketTLSOptions); | ||||
|  | ||||
|         if (!_socket) | ||||
|         { | ||||
|   | ||||
| @@ -78,14 +78,12 @@ namespace ix | ||||
|                        int pingTimeoutSecs); | ||||
|  | ||||
|         // Client | ||||
|         WebSocketInitResult connectToUrl( | ||||
|             const std::string& url, | ||||
|         WebSocketInitResult connectToUrl(const std::string& url, | ||||
|                                          const WebSocketHttpHeaders& headers, | ||||
|                                          int timeoutSecs); | ||||
|  | ||||
|         // Server | ||||
|         WebSocketInitResult connectToSocket(std::shared_ptr<Socket> socket, | ||||
|                                             int timeoutSecs); | ||||
|         WebSocketInitResult connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs); | ||||
|  | ||||
|         PollResult poll(); | ||||
|         WebSocketSendInfo sendBinary(const std::string& message, | ||||
|   | ||||
| @@ -61,7 +61,8 @@ TEST_CASE("socket", "[socket]") | ||||
|  | ||||
|         std::string errMsg; | ||||
|         bool tls = false; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, errMsg); | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions); | ||||
|         std::string host("127.0.0.1"); | ||||
|  | ||||
|         std::stringstream ss; | ||||
| @@ -84,7 +85,7 @@ TEST_CASE("socket", "[socket]") | ||||
|         bool tls = true; | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         tlsOptions.caFile = "cacert.pem"; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, errMsg, tlsOptions); | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions); | ||||
|         std::string host("www.google.com"); | ||||
|         int port = 443; | ||||
|         std::string request("GET / HTTP/1.1\r\n\r\n"); | ||||
|   | ||||
| @@ -92,7 +92,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") | ||||
|  | ||||
|         std::string errMsg; | ||||
|         bool tls = false; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, errMsg); | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions); | ||||
|         std::string host("127.0.0.1"); | ||||
|         auto isCancellationRequested = []() -> bool { return false; }; | ||||
|         bool success = socket->connect(host, port, errMsg, isCancellationRequested); | ||||
| @@ -125,7 +126,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") | ||||
|  | ||||
|         std::string errMsg; | ||||
|         bool tls = false; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, errMsg); | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions); | ||||
|         std::string host("127.0.0.1"); | ||||
|         auto isCancellationRequested = []() -> bool { return false; }; | ||||
|         bool success = socket->connect(host, port, errMsg, isCancellationRequested); | ||||
| @@ -161,7 +163,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") | ||||
|  | ||||
|         std::string errMsg; | ||||
|         bool tls = false; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, errMsg); | ||||
|         SocketTLSOptions tlsOptions; | ||||
|         std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions); | ||||
|         std::string host("127.0.0.1"); | ||||
|         auto isCancellationRequested = []() -> bool { return false; }; | ||||
|         bool success = socket->connect(host, port, errMsg, isCancellationRequested); | ||||
|   | ||||
| @@ -15,8 +15,7 @@ namespace ix | ||||
|     class WebSocketPingPong | ||||
|     { | ||||
|     public: | ||||
|         WebSocketPingPong(const std::string& _url, | ||||
|                           const ix::SocketTLSOptions& tlsOptions); | ||||
|         WebSocketPingPong(const std::string& _url, const ix::SocketTLSOptions& tlsOptions); | ||||
|  | ||||
|         void subscribe(const std::string& channel); | ||||
|         void start(); | ||||
|   | ||||
| @@ -26,7 +26,9 @@ namespace ix | ||||
|     class WebSocketReceiver | ||||
|     { | ||||
|     public: | ||||
|         WebSocketReceiver(const std::string& _url, bool enablePerMessageDeflate, int delayMs, | ||||
|         WebSocketReceiver(const std::string& _url, | ||||
|                           bool enablePerMessageDeflate, | ||||
|                           int delayMs, | ||||
|                           const ix::SocketTLSOptions& tlsOptions); | ||||
|  | ||||
|         void subscribe(const std::string& channel); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user