CARLA
streaming/Client.h
Go to the documentation of this file.
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 
9 #include "carla/Logging.h"
10 #include "carla/ThreadPool.h"
11 #include "carla/streaming/Token.h"
14 
15 #include <boost/asio/io_context.hpp>
16 
17 namespace carla {
18 namespace streaming {
19 
21 
22  /// A client able to subscribe to multiple streams.
23  class Client {
25  public:
26 
27  Client() = default;
28 
29  explicit Client(const std::string &fallback_address)
30  : _client(fallback_address) {}
31 
32  ~Client() {
33  _service.Stop();
34  }
35 
36  /// @warning cannot subscribe twice to the same stream (even if it's a
37  /// MultiStream).
38  template <typename Functor>
39  void Subscribe(const Token &token, Functor &&callback) {
40  _client.Subscribe(_service.io_context(), token, std::forward<Functor>(callback));
41  }
42 
43  void UnSubscribe(const Token &token) {
44  _client.UnSubscribe(token);
45  }
46 
47  void Run() {
48  _service.Run();
49  }
50 
51  void AsyncRun(size_t worker_threads) {
52  _service.AsyncRun(worker_threads);
53  }
54 
55  private:
56 
57  // The order of these two arguments is very important.
58 
60 
62  };
63 
64 } // namespace streaming
65 } // namespace carla
void UnSubscribe(const Token &token)
Client(const std::string &fallback_address)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void AsyncRun(size_t worker_threads)
Serializes a stream endpoint.
Definition: detail/Token.h:61
void Run()
Run tasks in this thread.
Definition: ThreadPool.h:63
A token that uniquely identify a stream.
Definition: Token.h:17
void Subscribe(const Token &token, Functor &&callback)
A thread pool based on Boost.Asio&#39;s io context.
Definition: ThreadPool.h:24
A client able to subscribe to multiple streams.
void Stop()
Stop the ThreadPool and join all its threads.
Definition: ThreadPool.h:76
void AsyncRun(size_t worker_threads)
Launch threads to run tasks asynchronously.
Definition: ThreadPool.h:51
underlying_client _client
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)
auto & io_context()
Return the underlying io_context.
Definition: ThreadPool.h:35
carla::streaming::detail::token_type token_type