IXWebSocket/ixwebsocket/IXWebSocketServer.h

58 lines
1.7 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
#include <utility> // pair
#include <string>
2019-01-01 22:47:25 +01:00
#include <set>
#include <thread>
2019-01-01 22:47:25 +01:00
#include <mutex>
#include <functional>
2019-01-01 22:53:13 +01:00
#include <memory>
#include <condition_variable>
#include "IXWebSocket.h"
#include "IXSocketServer.h"
2018-12-30 08:15:27 +01:00
namespace ix
2018-12-30 08:15:27 +01:00
{
using OnConnectionCallback = std::function<void(std::shared_ptr<WebSocket>,
std::shared_ptr<ConnectionState>)>;
class WebSocketServer : public SocketServer {
2018-12-30 08:15:27 +01:00
public:
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 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;
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;
// 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
};
}