CARLA
 
载入中...
搜索中...
未找到
router.h
浏览该文件的文档.
1// Copyright (c) 2022 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9// #include "carla/Logging.h"
11#include "carla/ThreadPool.h"
15
16#include <boost/asio/io_context.hpp>
17#include <boost/asio/ip/tcp.hpp>
18
19#include <mutex>
20#include <vector>
21#include <sstream>
22#include <unordered_map>
23
24namespace carla {
25namespace multigpu {
26
27 // class Primary;
28 class Listener;
29
30 struct SessionInfo {
31 std::shared_ptr<Primary> session;
33 };
34
35 class Router : public std::enable_shared_from_this<Router> {
36 public:
37
38 Router(void);
39 explicit Router(uint16_t port);
40 ~Router();
41
42 void Write(MultiGPUCommand id, Buffer &&buffer);
43 std::future<SessionInfo> WriteToNext(MultiGPUCommand id, Buffer &&buffer);
44 std::future<SessionInfo> WriteToOne(std::weak_ptr<Primary> server, MultiGPUCommand id, Buffer &&buffer);
45 void Stop();
46
47 void SetCallbacks();
48 void SetNewConnectionCallback(std::function<void(void)>);
49
50 void AsyncRun(size_t worker_threads);
51
52 boost::asio::ip::tcp::endpoint GetLocalEndpoint() const;
53
55 return (!_sessions.empty());
56 }
57
61
62 std::weak_ptr<Primary> GetNextServer();
63
64 private:
65 void ConnectSession(std::shared_ptr<Primary> session);
66 void DisconnectSession(std::shared_ptr<Primary> session);
67 void ClearSessions();
68
69 // mutex and thread pool must be at the beginning to be destroyed last
70 std::mutex _mutex;
72 boost::asio::ip::tcp::endpoint _endpoint;
73 std::vector<std::shared_ptr<Primary>> _sessions;
74 std::shared_ptr<Listener> _listener;
75 uint32_t _next;
76 std::unordered_map<Primary *, std::shared_ptr<std::promise<SessionInfo>>> _promises;
78 std::function<void(void)> _callback;
79 };
80
81} // namespace multigpu
82} // namespace carla
A piece of raw data.
A thread pool based on Boost.Asio's io context.
Definition ThreadPool.h:24
boost::asio::ip::tcp::endpoint GetLocalEndpoint() const
Definition router.cpp:82
PrimaryCommands _commander
Definition router.h:77
void Write(MultiGPUCommand id, Buffer &&buffer)
Definition router.cpp:112
std::vector< std::shared_ptr< Primary > > _sessions
Definition router.h:73
std::future< SessionInfo > WriteToNext(MultiGPUCommand id, Buffer &&buffer)
Definition router.cpp:132
std::function< void(void)> _callback
Definition router.h:78
std::unordered_map< Primary *, std::shared_ptr< std::promise< SessionInfo > > > _promises
Definition router.h:76
std::mutex _mutex
Definition router.h:70
std::shared_ptr< Listener > _listener
Definition router.h:74
void ConnectSession(std::shared_ptr< Primary > session)
Definition router.cpp:86
std::future< SessionInfo > WriteToOne(std::weak_ptr< Primary > server, MultiGPUCommand id, Buffer &&buffer)
Definition router.cpp:164
boost::asio::ip::tcp::endpoint _endpoint
Definition router.h:72
void DisconnectSession(std::shared_ptr< Primary > session)
Definition router.cpp:96
bool HasClientsConnected()
Definition router.h:54
void AsyncRun(size_t worker_threads)
Definition router.cpp:78
void SetNewConnectionCallback(std::function< void(void)>)
Definition router.cpp:73
std::weak_ptr< Primary > GetNextServer()
Definition router.cpp:188
PrimaryCommands & GetCommander()
Definition router.h:58
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
std::shared_ptr< Primary > session
Definition router.h:31