// Copyright (c) 2022 Computer Vision Center (CVC) at the Universitat Autonoma // de Barcelona (UAB). // // This work is licensed under the terms of the MIT license. // For a copy, see . #pragma once // #include "carla/Logging.h" #include "carla/streaming/detail/tcp/Message.h" #include "carla/ThreadPool.h" #include "carla/multigpu/primary.h" #include "carla/multigpu/primaryCommands.h" #include "carla/multigpu/commands.h" #include #include #include #include #include #include namespace carla { namespace multigpu { // class Primary; class Listener; struct SessionInfo { std::shared_ptr session; carla::Buffer buffer; }; class Router : public std::enable_shared_from_this { public: Router(void); explicit Router(uint16_t port); ~Router(); void Write(MultiGPUCommand id, Buffer &&buffer); std::future WriteToNext(MultiGPUCommand id, Buffer &&buffer); std::future WriteToOne(std::weak_ptr server, MultiGPUCommand id, Buffer &&buffer); void Stop(); void SetCallbacks(); void SetNewConnectionCallback(std::function); void AsyncRun(size_t worker_threads); boost::asio::ip::tcp::endpoint GetLocalEndpoint() const; bool HasClientsConnected() { return (!_sessions.empty()); } PrimaryCommands &GetCommander() { return _commander; } std::weak_ptr GetNextServer(); private: void ConnectSession(std::shared_ptr session); void DisconnectSession(std::shared_ptr session); void ClearSessions(); // mutex and thread pool must be at the beginning to be destroyed last std::mutex _mutex; ThreadPool _pool; boost::asio::ip::tcp::endpoint _endpoint; std::vector> _sessions; std::shared_ptr _listener; uint32_t _next; std::unordered_map>> _promises; PrimaryCommands _commander; std::function _callback; }; } // namespace multigpu } // namespace carla