Socket Factory has only one function which works for server and client code, and can do tls for both

This commit is contained in:
Benjamin Sergeant 2019-09-30 22:06:46 -07:00
parent 313949f087
commit afed387bcf
18 changed files with 39 additions and 48 deletions

View File

@ -252,7 +252,7 @@ On a client, this is only necessary for connecting to servers that require a cli
On a server, this is necessary for TLS support. On a server, this is necessary for TLS support.
Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates. Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates.
- The special value of `SYSTEM` (the default) indicates that the system-configured trust bundle should be used; this is generally what you want when connecting to any publicly exposed API/server. - The special value of `SYSTEM` (the default) indicates that the system-configured trust bundle should be used; this is generally what you want when connecting to any publicly exposed API/server.
- The special value of `NONE` can be used to disable peer verification; this is only recommended to rule out certificate verification when testing connectivity. - The special value of `NONE` can be used to disable peer verification; this is only recommended to rule out certificate verification when testing connectivity.

View File

@ -121,7 +121,7 @@ namespace ix
void suspend(); void suspend();
void resume(); void resume();
/// Prepare a message for transmission /// Prepare a message for transmission
/// (update the pdu, compute a msgId, serialize json to a string) /// (update the pdu, compute a msgId, serialize json to a string)
std::pair<CobraConnection::MsgId, std::string> prePublish( std::pair<CobraConnection::MsgId, std::string> prePublish(
const Json::Value& channels, const Json::Value& channels,

View File

@ -11,6 +11,7 @@
#include <iostream> #include <iostream>
#include <ixwebsocket/IXSocket.h> #include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h> #include <ixwebsocket/IXSocketFactory.h>
#include <ixwebsocket/IXSocketTLSOptions.h>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -20,7 +21,8 @@ namespace ix
{ {
bool tls = false; bool tls = false;
std::string errorMsg; std::string errorMsg;
_socket = createSocket(tls, errorMsg); SocketTLSOptions tlsOptions;
_socket = createSocket(tls, -1, errorMsg, tlsOptions);
if (!_socket) if (!_socket)
{ {

View File

@ -40,7 +40,7 @@ namespace ix
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
_stopHandlingConnections = false; _stopHandlingConnections = false;
SocketServer::stop(); SocketServer::stop();
} }
@ -270,7 +270,7 @@ namespace ix
// now dispatch the message to subscribers (write custom method) // now dispatch the message to subscribers (write custom method)
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
auto it = _subscribers.find(channel); auto it = _subscribers.find(channel);
if (it == _subscribers.end()) if (it == _subscribers.end())
{ {
// return the number of clients that received the message, 0 in that case // return the number of clients that received the message, 0 in that case
socket->writeBytes(":0\r\n", cb); socket->writeBytes(":0\r\n", cb);

View File

@ -211,7 +211,7 @@ namespace snake
ss << "Subscribing to " << appChannel << "..."; ss << "Subscribing to " << appChannel << "...";
ix::IXCoreLogger::Log(ss.str().c_str()); ix::IXCoreLogger::Log(ss.str().c_str());
} }
if (!redisClient.subscribe(appChannel, responseCallback, callback)) if (!redisClient.subscribe(appChannel, responseCallback, callback))
{ {
std::stringstream ss; std::stringstream ss;

View File

@ -102,7 +102,7 @@ namespace snake
ss << "Received " << msg->wireSize << " bytes" << std::endl; ss << "Received " << msg->wireSize << " bytes" << std::endl;
processCobraMessage(state, webSocket, _appConfig, msg->str); processCobraMessage(state, webSocket, _appConfig, msg->str);
} }
ix::IXCoreLogger::Log(ss.str().c_str()); ix::IXCoreLogger::Log(ss.str().c_str());
}); });
}); });

View File

@ -147,7 +147,7 @@ namespace ix
bool tls = protocol == "https"; bool tls = protocol == "https";
std::string errorMsg; std::string errorMsg;
_socket = createSocket(tls, errorMsg, _tlsOptions); _socket = createSocket(tls, -1, errorMsg, _tlsOptions);
if (!_socket) if (!_socket)
{ {

View File

@ -27,6 +27,7 @@
namespace ix namespace ix
{ {
std::shared_ptr<Socket> createSocket(bool tls, std::shared_ptr<Socket> createSocket(bool tls,
int fd,
std::string& errorMsg, std::string& errorMsg,
const SocketTLSOptions& tlsOptions) const SocketTLSOptions& tlsOptions)
{ {
@ -35,19 +36,19 @@ namespace ix
if (!tls) if (!tls)
{ {
socket = std::make_shared<Socket>(); socket = std::make_shared<Socket>(fd);
} }
else else
{ {
#ifdef IXWEBSOCKET_USE_TLS #ifdef IXWEBSOCKET_USE_TLS
#if defined(IXWEBSOCKET_USE_MBED_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) #elif defined(IXWEBSOCKET_USE_OPEN_SSL)
socket = std::make_shared<SocketOpenSSL>(tlsOptions); socket = std::make_shared<SocketOpenSSL>(tlsOptions, fd);
#elif defined(_WIN32) #elif defined(_WIN32)
socket = std::make_shared<SocketSChannel>(tlsOptions); socket = std::make_shared<SocketSChannel>(tlsOptions, fd);
#elif defined(__APPLE__) #elif defined(__APPLE__)
socket = std::make_shared<SocketAppleSSL>(tlsOptions); socket = std::make_shared<SocketAppleSSL>(tlsOptions, fd);
#endif #endif
#else #else
errorMsg = "TLS support is not enabled on this platform."; errorMsg = "TLS support is not enabled on this platform.";
@ -62,17 +63,4 @@ namespace ix
return socket; 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 } // namespace ix

View File

@ -15,8 +15,7 @@ namespace ix
{ {
class Socket; class Socket;
std::shared_ptr<Socket> createSocket(bool tls, std::shared_ptr<Socket> createSocket(bool tls,
int fd,
std::string& errorMsg, std::string& errorMsg,
const SocketTLSOptions& tlsOptions); const SocketTLSOptions& tlsOptions);
std::shared_ptr<Socket> createSocket(int fd, std::string& errorMsg);
} // namespace ix } // namespace ix

View File

@ -270,7 +270,8 @@ namespace ix
// create socket // create socket
std::string errorMsg; std::string errorMsg;
auto socket = createSocket(clientFd, errorMsg); bool tls = false;
auto socket = createSocket(tls, clientFd, errorMsg, _socketTLSOptions);
if (socket == nullptr) if (socket == nullptr)
{ {

View File

@ -201,8 +201,7 @@ namespace ix
return status; return status;
} }
WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr<Socket> socket, WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs)
int timeoutSecs)
{ {
{ {
std::lock_guard<std::mutex> lock(_configMutex); std::lock_guard<std::mutex> lock(_configMutex);

View File

@ -113,8 +113,7 @@ namespace ix
static void invokeTrafficTrackerCallback(size_t size, bool incoming); static void invokeTrafficTrackerCallback(size_t size, bool incoming);
// Server // Server
WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, int timeoutSecs);
int timeoutSecs);
WebSocketTransport _ws; WebSocketTransport _ws;

View File

@ -148,7 +148,7 @@ namespace ix
std::string errorMsg; std::string errorMsg;
bool tls = protocol == "wss"; bool tls = protocol == "wss";
_socket = createSocket(tls, errorMsg, _socketTLSOptions); _socket = createSocket(tls, -1, errorMsg, _socketTLSOptions);
if (!_socket) if (!_socket)
{ {

View File

@ -78,14 +78,12 @@ namespace ix
int pingTimeoutSecs); int pingTimeoutSecs);
// Client // Client
WebSocketInitResult connectToUrl( WebSocketInitResult connectToUrl(const std::string& url,
const std::string& url, const WebSocketHttpHeaders& headers,
const WebSocketHttpHeaders& headers, int timeoutSecs);
int timeoutSecs);
// Server // Server
WebSocketInitResult connectToSocket(std::shared_ptr<Socket> socket, WebSocketInitResult connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs);
int timeoutSecs);
PollResult poll(); PollResult poll();
WebSocketSendInfo sendBinary(const std::string& message, WebSocketSendInfo sendBinary(const std::string& message,

View File

@ -61,7 +61,8 @@ TEST_CASE("socket", "[socket]")
std::string errMsg; std::string errMsg;
bool tls = false; 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::string host("127.0.0.1");
std::stringstream ss; std::stringstream ss;
@ -84,7 +85,7 @@ TEST_CASE("socket", "[socket]")
bool tls = true; bool tls = true;
SocketTLSOptions tlsOptions; SocketTLSOptions tlsOptions;
tlsOptions.caFile = "cacert.pem"; 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"); std::string host("www.google.com");
int port = 443; int port = 443;
std::string request("GET / HTTP/1.1\r\n\r\n"); std::string request("GET / HTTP/1.1\r\n\r\n");

View File

@ -92,7 +92,8 @@ TEST_CASE("Websocket_server", "[websocket_server]")
std::string errMsg; std::string errMsg;
bool tls = false; 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::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool { return false; }; auto isCancellationRequested = []() -> bool { return false; };
bool success = socket->connect(host, port, errMsg, isCancellationRequested); bool success = socket->connect(host, port, errMsg, isCancellationRequested);
@ -125,7 +126,8 @@ TEST_CASE("Websocket_server", "[websocket_server]")
std::string errMsg; std::string errMsg;
bool tls = false; 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::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool { return false; }; auto isCancellationRequested = []() -> bool { return false; };
bool success = socket->connect(host, port, errMsg, isCancellationRequested); bool success = socket->connect(host, port, errMsg, isCancellationRequested);
@ -161,7 +163,8 @@ TEST_CASE("Websocket_server", "[websocket_server]")
std::string errMsg; std::string errMsg;
bool tls = false; 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::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool { return false; }; auto isCancellationRequested = []() -> bool { return false; };
bool success = socket->connect(host, port, errMsg, isCancellationRequested); bool success = socket->connect(host, port, errMsg, isCancellationRequested);

View File

@ -15,8 +15,7 @@ namespace ix
class WebSocketPingPong class WebSocketPingPong
{ {
public: public:
WebSocketPingPong(const std::string& _url, WebSocketPingPong(const std::string& _url, const ix::SocketTLSOptions& tlsOptions);
const ix::SocketTLSOptions& tlsOptions);
void subscribe(const std::string& channel); void subscribe(const std::string& channel);
void start(); void start();

View File

@ -26,7 +26,9 @@ namespace ix
class WebSocketReceiver class WebSocketReceiver
{ {
public: public:
WebSocketReceiver(const std::string& _url, bool enablePerMessageDeflate, int delayMs, WebSocketReceiver(const std::string& _url,
bool enablePerMessageDeflate,
int delayMs,
const ix::SocketTLSOptions& tlsOptions); const ix::SocketTLSOptions& tlsOptions);
void subscribe(const std::string& channel); void subscribe(const std::string& channel);