/* * IXWebSocketServer.h * Author: Benjamin Sergeant * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ #pragma once #include // pair #include #include #include #include #include #include #include "IXWebSocket.h" namespace ix { using OnConnectionCallback = std::function)>; class WebSocketServer { public: WebSocketServer(int port = 8080, const std::string& host = WebSocketServer::kDefaultHost, int backlog = 5); virtual ~WebSocketServer(); void setOnConnectionCallback(const OnConnectionCallback& callback); void start(); std::pair listen(); // Get all the connected clients std::set> getClients(); private: // Member variables int _port; std::string _host; int _backlog; OnConnectionCallback _onConnectionCallback; // socket for accepting connections int _serverFd; std::mutex _clientsMutex; std::set> _clients; std::mutex _logMutex; std::atomic _stop; std::thread _thread; const static std::string kDefaultHost; // Methods void run(); void stop(); void handleConnection(int fd); // Logging void logError(const std::string& str); void logInfo(const std::string& str); }; }