2018-12-30 08:15:27 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketServer.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-30 17:46:50 +02:00
|
|
|
#include "IXSocketServer.h"
|
|
|
|
#include "IXWebSocket.h"
|
|
|
|
#include <condition_variable>
|
2018-12-31 07:12:13 +01:00
|
|
|
#include <functional>
|
2019-01-01 22:53:13 +01:00
|
|
|
#include <memory>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <utility> // pair
|
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
|
|
|
{
|
2020-06-16 03:29:05 +02:00
|
|
|
class WebSocketServer : public SocketServer
|
2019-05-30 17:46:50 +02:00
|
|
|
{
|
2018-12-30 08:15:27 +01:00
|
|
|
public:
|
2019-06-23 23:54:21 +02:00
|
|
|
using OnConnectionCallback =
|
2020-07-08 21:10:35 +02:00
|
|
|
std::function<void(std::shared_ptr<WebSocket>, std::shared_ptr<ConnectionState>,
|
|
|
|
std::unique_ptr<ConnectionInfo> connectionInfo)>;
|
2019-06-23 23:54:21 +02:00
|
|
|
|
2019-01-06 21:01:33 +01:00
|
|
|
WebSocketServer(int port = SocketServer::kDefaultPort,
|
|
|
|
const std::string& host = SocketServer::kDefaultHost,
|
|
|
|
int backlog = SocketServer::kDefaultTcpBacklog,
|
|
|
|
size_t maxConnections = SocketServer::kDefaultMaxConnections,
|
2020-01-27 01:17:26 +01:00
|
|
|
int handshakeTimeoutSecs = WebSocketServer::kDefaultHandShakeTimeoutSecs,
|
|
|
|
int addressFamily = SocketServer::kDefaultAddressFamily);
|
2018-12-30 08:15:27 +01:00
|
|
|
virtual ~WebSocketServer();
|
2019-01-06 21:01:33 +01:00
|
|
|
virtual void stop() final;
|
2018-12-30 08:15:27 +01:00
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
void enablePong();
|
|
|
|
void disablePong();
|
2020-02-19 06:38:28 +01:00
|
|
|
void disablePerMessageDeflate();
|
2019-04-18 18:24:16 +02:00
|
|
|
|
2018-12-31 07:12:13 +01:00
|
|
|
void setOnConnectionCallback(const OnConnectionCallback& callback);
|
2018-12-30 08:15:27 +01:00
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
// Get all the connected clients
|
|
|
|
std::set<std::shared_ptr<WebSocket>> getClients();
|
2019-01-01 22:47:25 +01:00
|
|
|
|
2020-01-27 01:44:44 +01:00
|
|
|
const static int kDefaultHandShakeTimeoutSecs;
|
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
private:
|
2019-01-01 23:28:41 +01:00
|
|
|
// Member variables
|
2019-01-04 03:33:08 +01:00
|
|
|
int _handshakeTimeoutSecs;
|
2019-04-18 18:24:16 +02:00
|
|
|
bool _enablePong;
|
2020-02-19 06:38:28 +01:00
|
|
|
bool _enablePerMessageDeflate;
|
2018-12-31 07:00:49 +01:00
|
|
|
|
2018-12-31 07:12:13 +01:00
|
|
|
OnConnectionCallback _onConnectionCallback;
|
|
|
|
|
2019-01-01 23:28:41 +01:00
|
|
|
std::mutex _clientsMutex;
|
2019-01-01 22:53:13 +01:00
|
|
|
std::set<std::shared_ptr<WebSocket>> _clients;
|
2019-01-01 23:28:41 +01:00
|
|
|
|
2019-04-18 18:24:16 +02:00
|
|
|
const static bool kDefaultEnablePong;
|
2019-01-01 23:28:41 +01:00
|
|
|
|
|
|
|
// Methods
|
2020-03-24 20:40:58 +01:00
|
|
|
virtual void handleConnection(std::unique_ptr<Socket> socket,
|
2020-07-08 21:10:35 +02:00
|
|
|
std::shared_ptr<ConnectionState> connectionState,
|
|
|
|
std::unique_ptr<ConnectionInfo> connectionInfo);
|
2019-01-06 21:01:33 +01:00
|
|
|
virtual size_t getConnectedClientsCount() final;
|
2018-12-30 08:15:27 +01:00
|
|
|
};
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|