2018-12-30 08:15:27 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketServer.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXWebSocketServer.h"
|
2018-12-31 07:00:49 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include "IXNetSystem.h"
|
2020-02-23 18:44:58 +01:00
|
|
|
#include "IXSetThreadName.h"
|
2019-09-23 19:25:23 +02:00
|
|
|
#include "IXSocketConnect.h"
|
|
|
|
#include "IXWebSocket.h"
|
|
|
|
#include "IXWebSocketTransport.h"
|
2019-01-01 22:47:25 +01:00
|
|
|
#include <future>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <sstream>
|
2019-01-02 02:13:26 +01:00
|
|
|
#include <string.h>
|
2018-12-30 08:15:27 +01:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
2019-01-04 03:33:08 +01:00
|
|
|
const int WebSocketServer::kDefaultHandShakeTimeoutSecs(3); // 3 seconds
|
2019-04-18 18:24:16 +02:00
|
|
|
const bool WebSocketServer::kDefaultEnablePong(true);
|
2019-01-01 23:28:41 +01:00
|
|
|
|
2019-01-04 02:05:44 +01:00
|
|
|
WebSocketServer::WebSocketServer(int port,
|
|
|
|
const std::string& host,
|
|
|
|
int backlog,
|
2019-01-04 03:33:08 +01:00
|
|
|
size_t maxConnections,
|
2020-01-27 01:17:26 +01:00
|
|
|
int handshakeTimeoutSecs,
|
|
|
|
int addressFamily)
|
|
|
|
: SocketServer(port, host, backlog, maxConnections, addressFamily)
|
2019-09-23 19:25:23 +02:00
|
|
|
, _handshakeTimeoutSecs(handshakeTimeoutSecs)
|
|
|
|
, _enablePong(kDefaultEnablePong)
|
2020-02-19 06:38:28 +01:00
|
|
|
, _enablePerMessageDeflate(true)
|
2018-12-30 08:15:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketServer::~WebSocketServer()
|
|
|
|
{
|
2019-01-01 23:52:14 +01:00
|
|
|
stop();
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 23:52:14 +01:00
|
|
|
void WebSocketServer::stop()
|
|
|
|
{
|
2019-04-24 18:45:53 +02:00
|
|
|
stopAcceptingConnections();
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
auto clients = getClients();
|
|
|
|
for (auto client : clients)
|
|
|
|
{
|
|
|
|
client->close();
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:01:33 +01:00
|
|
|
SocketServer::stop();
|
2019-01-01 23:52:14 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
void WebSocketServer::enablePong()
|
|
|
|
{
|
|
|
|
_enablePong = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketServer::disablePong()
|
|
|
|
{
|
|
|
|
_enablePong = false;
|
|
|
|
}
|
|
|
|
|
2020-02-19 06:38:28 +01:00
|
|
|
void WebSocketServer::disablePerMessageDeflate()
|
|
|
|
{
|
|
|
|
_enablePerMessageDeflate = false;
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:01:33 +01:00
|
|
|
void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback)
|
2018-12-31 07:00:49 +01:00
|
|
|
{
|
2019-01-06 21:01:33 +01:00
|
|
|
_onConnectionCallback = callback;
|
2018-12-31 06:16:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 04:29:41 +02:00
|
|
|
void WebSocketServer::setOnClientMessageCallback(const OnClientMessageCallback& callback)
|
|
|
|
{
|
|
|
|
_onClientMessageCallback = callback;
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:40:58 +01:00
|
|
|
void WebSocketServer::handleConnection(std::unique_ptr<Socket> socket,
|
2020-08-28 23:55:40 +02:00
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
2018-12-31 06:16:05 +01:00
|
|
|
{
|
2020-02-23 18:44:58 +01:00
|
|
|
setThreadName("WebSocketServer::" + connectionState->getId());
|
|
|
|
|
2019-01-12 06:25:06 +01:00
|
|
|
auto webSocket = std::make_shared<WebSocket>();
|
2020-07-24 04:29:41 +02:00
|
|
|
if (_onConnectionCallback)
|
|
|
|
{
|
2020-08-28 23:55:40 +02:00
|
|
|
_onConnectionCallback(webSocket, connectionState);
|
2020-08-06 13:40:32 +02:00
|
|
|
|
|
|
|
if (!webSocket->isOnMessageCallbackRegistered())
|
|
|
|
{
|
|
|
|
logError("WebSocketServer Application developer error: Server callback improperly "
|
|
|
|
"registerered.");
|
|
|
|
logError("Missing call to setOnMessageCallback inside setOnConnectionCallback.");
|
|
|
|
connectionState->setTerminated();
|
|
|
|
return;
|
|
|
|
}
|
2020-07-24 04:29:41 +02:00
|
|
|
}
|
|
|
|
else if (_onClientMessageCallback)
|
|
|
|
{
|
2021-01-03 20:44:05 +01:00
|
|
|
WebSocket* webSocketRawPtr = webSocket.get();
|
2020-07-24 04:29:41 +02:00
|
|
|
webSocket->setOnMessageCallback(
|
2021-01-03 20:44:05 +01:00
|
|
|
[this, webSocketRawPtr, connectionState](const WebSocketMessagePtr& msg) {
|
|
|
|
_onClientMessageCallback(connectionState, *webSocketRawPtr, msg);
|
2020-07-24 04:29:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logError(
|
|
|
|
"WebSocketServer Application developer error: No server callback is registerered.");
|
|
|
|
logError("Missing call to setOnConnectionCallback or setOnClientMessageCallback.");
|
|
|
|
connectionState->setTerminated();
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 07:12:13 +01:00
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
webSocket->disableAutomaticReconnection();
|
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
if (_enablePong)
|
2020-02-19 06:38:28 +01:00
|
|
|
{
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocket->enablePong();
|
2020-02-19 06:38:28 +01:00
|
|
|
}
|
2019-04-18 18:24:16 +02:00
|
|
|
else
|
2020-02-19 06:38:28 +01:00
|
|
|
{
|
2019-04-18 18:24:16 +02:00
|
|
|
webSocket->disablePong();
|
2020-02-19 06:38:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
// Add this client to our client set
|
2019-01-01 23:28:41 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
|
|
|
_clients.insert(webSocket);
|
|
|
|
}
|
2019-01-01 22:47:25 +01:00
|
|
|
|
2021-03-16 17:56:08 +01:00
|
|
|
auto status = webSocket->connectToSocket(
|
|
|
|
std::move(socket), _handshakeTimeoutSecs, _enablePerMessageDeflate);
|
2019-01-03 01:08:32 +01:00
|
|
|
if (status.success)
|
|
|
|
{
|
2019-02-21 03:59:07 +01:00
|
|
|
// Process incoming messages and execute callbacks
|
2019-01-03 01:08:32 +01:00
|
|
|
// until the connection is closed
|
|
|
|
webSocket->run();
|
|
|
|
}
|
|
|
|
else
|
2019-01-01 22:47:25 +01:00
|
|
|
{
|
2019-01-01 23:28:41 +01:00
|
|
|
std::stringstream ss;
|
2019-09-23 19:25:23 +02:00
|
|
|
ss << "WebSocketServer::handleConnection() HTTP status: " << status.http_status
|
|
|
|
<< " error: " << status.errorStr;
|
2019-01-01 23:28:41 +01:00
|
|
|
logError(ss.str());
|
2019-01-01 22:47:25 +01:00
|
|
|
}
|
2018-12-31 21:43:47 +01:00
|
|
|
|
2020-07-24 18:41:02 +02:00
|
|
|
webSocket->setOnMessageCallback(nullptr);
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
// Remove this client from our client set
|
2019-01-01 23:28:41 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
2019-01-02 06:25:15 +01:00
|
|
|
if (_clients.erase(webSocket) != 1)
|
|
|
|
{
|
|
|
|
logError("Cannot delete client");
|
|
|
|
}
|
2019-01-01 23:28:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 01:23:24 +02:00
|
|
|
connectionState->setTerminated();
|
2019-01-01 23:28:41 +01:00
|
|
|
}
|
2019-01-01 22:47:25 +01:00
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
std::set<std::shared_ptr<WebSocket>> WebSocketServer::getClients()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
|
|
|
return _clients;
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
2019-01-04 02:05:44 +01:00
|
|
|
|
|
|
|
size_t WebSocketServer::getConnectedClientsCount()
|
|
|
|
{
|
2019-01-12 06:25:06 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
|
|
|
return _clients.size();
|
2019-01-04 02:05:44 +01:00
|
|
|
}
|
2021-01-03 20:24:12 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Classic servers
|
|
|
|
//
|
|
|
|
void WebSocketServer::makeBroadcastServer()
|
|
|
|
{
|
2021-03-08 04:29:28 +01:00
|
|
|
setOnClientMessageCallback([this](std::shared_ptr<ConnectionState> connectionState,
|
|
|
|
WebSocket& webSocket,
|
|
|
|
const WebSocketMessagePtr& msg) {
|
|
|
|
auto remoteIp = connectionState->getRemoteIp();
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
for (auto&& client : getClients())
|
2021-01-03 20:24:12 +01:00
|
|
|
{
|
2021-03-08 04:29:28 +01:00
|
|
|
if (client.get() != &webSocket)
|
2021-01-03 20:24:12 +01:00
|
|
|
{
|
2021-03-08 04:29:28 +01:00
|
|
|
client->send(msg->str, msg->binary);
|
|
|
|
|
|
|
|
// Make sure the OS send buffer is flushed before moving on
|
|
|
|
do
|
2021-01-03 20:24:12 +01:00
|
|
|
{
|
2021-03-08 04:29:28 +01:00
|
|
|
std::chrono::duration<double, std::milli> duration(500);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
} while (client->bufferedAmount() != 0);
|
2021-01-03 20:24:12 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-08 04:29:28 +01:00
|
|
|
}
|
|
|
|
});
|
2021-01-03 20:24:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 19:52:41 +01:00
|
|
|
bool WebSocketServer::listenAndStart()
|
2021-01-03 20:24:12 +01:00
|
|
|
{
|
|
|
|
auto res = listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
2021-03-19 19:52:41 +01:00
|
|
|
return false;
|
2021-01-03 20:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
start();
|
2021-03-19 19:52:41 +01:00
|
|
|
return true;
|
2021-01-03 20:24:12 +01:00
|
|
|
}
|
2021-11-24 17:28:25 +01:00
|
|
|
|
|
|
|
int WebSocketServer::getHandshakeTimeoutSecs()
|
|
|
|
{
|
|
|
|
return _handshakeTimeoutSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocketServer::isPongEnabled()
|
|
|
|
{
|
|
|
|
return _enablePong;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocketServer::isPerMessageDeflateEnabled()
|
|
|
|
{
|
|
|
|
return _enablePerMessageDeflate;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|