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"
|
|
|
|
#include "IXWebSocketTransport.h"
|
|
|
|
#include "IXWebSocket.h"
|
2019-01-02 01:11:27 +01:00
|
|
|
#include "IXSocketConnect.h"
|
2019-01-06 05:38:43 +01:00
|
|
|
#include "IXNetSystem.h"
|
2018-12-31 07:00:49 +01:00
|
|
|
|
|
|
|
#include <sstream>
|
2019-01-01 22:47:25 +01:00
|
|
|
#include <future>
|
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-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,
|
2019-01-06 21:01:33 +01:00
|
|
|
int handshakeTimeoutSecs) : SocketServer(port, host, backlog, maxConnections),
|
|
|
|
_handshakeTimeoutSecs(handshakeTimeoutSecs)
|
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-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-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
|
|
|
}
|
|
|
|
|
2019-03-21 02:34:24 +01:00
|
|
|
void WebSocketServer::handleConnection(
|
|
|
|
int fd,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
2018-12-31 06:16:05 +01:00
|
|
|
{
|
2019-01-12 06:25:06 +01:00
|
|
|
auto webSocket = std::make_shared<WebSocket>();
|
2019-03-21 02:34:24 +01:00
|
|
|
_onConnectionCallback(webSocket, connectionState);
|
2018-12-31 07:12:13 +01:00
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
webSocket->disableAutomaticReconnection();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-01-04 03:33:08 +01:00
|
|
|
auto status = webSocket->connectToSocket(fd, _handshakeTimeoutSecs);
|
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;
|
|
|
|
ss << "WebSocketServer::handleConnection() error: "
|
2019-01-03 01:08:32 +01:00
|
|
|
<< status.http_status
|
|
|
|
<< " error: "
|
2019-01-01 23:28:41 +01:00
|
|
|
<< status.errorStr;
|
|
|
|
logError(ss.str());
|
2019-01-01 22:47:25 +01:00
|
|
|
}
|
2018-12-31 21:43:47 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
logInfo("WebSocketServer::handleConnection() done");
|
|
|
|
}
|
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
|
|
|
}
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|