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