CARLA
 
载入中...
搜索中...
未找到
streaming/low_level/Client.h
浏览该文件的文档.
1// Copyright (c) 2017 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
11
12#include <boost/asio/io_context.hpp>
13
14#include <memory>
15#include <unordered_map>
16
17namespace carla {
18namespace streaming {
19namespace low_level {
20
21 /// A client able to subscribe to multiple streams. Accepts an external
22 /// io_context.
23 ///
24 /// @warning The client should not be destroyed before the @a io_context is
25 /// stopped.
26 template <typename T>
27 class Client {
28 public:
29
31 using protocol_type = typename underlying_client::protocol_type;
33
34 explicit Client(boost::asio::ip::address fallback_address)
35 : _fallback_address(std::move(fallback_address)) {}
36
37 explicit Client(const std::string &fallback_address)
38 : Client(carla::streaming::make_address(fallback_address)) {}
39
40 explicit Client()
41 : Client(carla::streaming::make_localhost_address()) {}
42
44 for (auto &pair : _clients) {
45 pair.second->Stop();
46 }
47 }
48
49 /// @warning cannot subscribe twice to the same stream (even if it's a
50 /// MultiStream).
51 template <typename Functor>
53 boost::asio::io_context &io_context,
54 token_type token,
55 Functor &&callback) {
56 DEBUG_ASSERT_EQ(_clients.find(token.get_stream_id()), _clients.end());
57 if (!token.has_address()) {
59 }
60 auto client = std::make_shared<underlying_client>(
61 io_context,
62 token,
63 std::forward<Functor>(callback));
64 client->Connect();
65 _clients.emplace(token.get_stream_id(), std::move(client));
66 }
67
68 void UnSubscribe(token_type token) {
69 log_debug("calling sensor UnSubscribe()");
70 auto it = _clients.find(token.get_stream_id());
71 if (it != _clients.end()) {
72 it->second->Stop();
73 _clients.erase(it);
74 }
75 }
76
77 private:
78
79 boost::asio::ip::address _fallback_address;
80
81 std::unordered_map<
83 std::shared_ptr<underlying_client>> _clients;
84 };
85
86} // namespace low_level
87} // namespace streaming
88} // namespace carla
#define DEBUG_ASSERT_EQ(lhs, rhs)
Definition Debug.h:76
Serializes a stream endpoint.
void set_address(const boost::asio::ip::address &addr)
Definition Token.cpp:18
A client able to subscribe to multiple streams.
std::unordered_map< detail::stream_id_type, std::shared_ptr< underlying_client > > _clients
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)
typename underlying_client::protocol_type protocol_type
Client(boost::asio::ip::address fallback_address)
Client(const std::string &fallback_address)
uint32_t stream_id_type
Definition Types.h:18
static auto make_address(const std::string &address)
Definition EndPoint.h:75
static auto make_localhost_address()
Definition EndPoint.h:71
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
static void log_debug(Args &&... args)
Definition Logging.h:68