CARLA
streaming/Server.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/ThreadPool.h"
13 
14 #include <boost/asio/io_context.hpp>
15 
16 namespace carla {
17 namespace streaming {
18 
19  /// A streaming server. Each new stream has a token associated, this token can
20  /// be used by a client to subscribe to the stream.
21  class Server {
26  public:
27 
28  explicit Server(uint16_t port)
29  : _server(_pool.io_context(), make_endpoint<protocol_type>(port)) {}
30 
31  explicit Server(const std::string &address, uint16_t port)
32  : _server(_pool.io_context(), make_endpoint<protocol_type>(address, port)) {}
33 
34  explicit Server(
35  const std::string &address, uint16_t port,
36  const std::string &external_address, uint16_t external_port)
37  : _server(
38  _pool.io_context(),
39  make_endpoint<protocol_type>(address, port),
40  make_endpoint<protocol_type>(external_address, external_port)) {}
41 
42  ~Server() {
43  _pool.Stop();
44  }
45 
46  auto GetLocalEndpoint() const {
47  return _server.GetLocalEndpoint();
48  }
49 
50  void SetTimeout(time_duration timeout) {
51  _server.SetTimeout(timeout);
52  }
53 
55  return _server.MakeStream();
56  }
57 
59  return _server.CloseStream(id);
60  }
61 
62  void Run() {
63  _pool.Run();
64  }
65 
66  void AsyncRun(size_t worker_threads) {
67  _pool.AsyncRun(worker_threads);
68  }
69 
70  void SetSynchronousMode(bool is_synchro) {
71  _server.SetSynchronousMode(is_synchro);
72  }
73 
75  return _server.GetToken(sensor_id);
76  }
77 
78  void EnableForROS(stream_id sensor_id) {
79  _server.EnableForROS(sensor_id);
80  }
81 
82  void DisableForROS(stream_id sensor_id) {
83  _server.DisableForROS(sensor_id);
84  }
85 
86  bool IsEnabledForROS(stream_id sensor_id) {
87  return _server.IsEnabledForROS(sensor_id);
88  }
89 
90  private:
91 
92  // The order of these two arguments is very important.
93 
95 
97  };
98 
99 } // namespace streaming
100 } // namespace carla
void AsyncRun(size_t worker_threads)
void DisableForROS(stream_id sensor_id)
A streaming server.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
low_level::Server< detail::tcp::Server >::protocol_type protocol_type
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition: EndPoint.h:90
void SetTimeout(time_duration timeout)
bool IsEnabledForROS(stream_id sensor_id)
uint32_t stream_id_type
Definition: Types.h:18
Server(const std::string &address, uint16_t port, const std::string &external_address, uint16_t external_port)
token_type GetToken(stream_id sensor_id)
void EnableForROS(stream_id sensor_id)
Serializes a stream endpoint.
Definition: detail/Token.h:61
Server(const std::string &address, uint16_t port)
void Run()
Run tasks in this thread.
Definition: ThreadPool.h:63
A thread pool based on Boost.Asio&#39;s io context.
Definition: ThreadPool.h:24
token_type GetToken(stream_id sensor_id)
void Stop()
Stop the ThreadPool and join all its threads.
Definition: ThreadPool.h:76
typename underlying_server::protocol_type protocol_type
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
underlying_server _server
void AsyncRun(size_t worker_threads)
Launch threads to run tasks asynchronously.
Definition: ThreadPool.h:51
void CloseStream(carla::streaming::detail::stream_id_type id)
carla::streaming::detail::stream_id_type stream_id
underlying_server::endpoint GetLocalEndpoint() const
carla::streaming::detail::token_type token_type
void SetSynchronousMode(bool is_synchro)
void CloseStream(carla::streaming::detail::stream_id_type id)