IXWebSocket/ixwebsocket/IXWebSocketServer.h

63 lines
1.8 KiB
C
Raw Normal View History

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>
#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
namespace ix
2018-12-30 08:15:27 +01:00
{
2019-05-30 17:46:50 +02:00
class WebSocketServer final : public SocketServer
{
2018-12-30 08:15:27 +01:00
public:
using OnConnectionCallback =
std::function<void(std::shared_ptr<WebSocket>, std::shared_ptr<ConnectionState>)>;
WebSocketServer(int port = SocketServer::kDefaultPort,
const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections,
2019-01-04 03:33:08 +01:00
int handshakeTimeoutSecs = WebSocketServer::kDefaultHandShakeTimeoutSecs);
2018-12-30 08:15:27 +01:00
virtual ~WebSocketServer();
virtual void stop() final;
2018-12-30 08:15:27 +01:00
void enablePong();
void disablePong();
void setOnConnectionCallback(const OnConnectionCallback& callback);
2018-12-30 08:15:27 +01:00
// Get all the connected clients
std::set<std::shared_ptr<WebSocket>> getClients();
2019-01-01 22:47:25 +01:00
private:
// Member variables
2019-01-04 03:33:08 +01:00
int _handshakeTimeoutSecs;
bool _enablePong;
OnConnectionCallback _onConnectionCallback;
std::mutex _clientsMutex;
2019-01-01 22:53:13 +01:00
std::set<std::shared_ptr<WebSocket>> _clients;
2019-01-04 03:33:08 +01:00
const static int kDefaultHandShakeTimeoutSecs;
const static bool kDefaultEnablePong;
// Methods
virtual void handleConnection(int fd,
std::shared_ptr<ConnectionState> connectionState) final;
virtual size_t getConnectedClientsCount() final;
2018-12-30 08:15:27 +01:00
};
2019-05-30 17:46:50 +02:00
} // namespace ix