CARLA
streaming/low_level/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 
11 #include "carla/streaming/Stream.h"
12 
13 #include <boost/asio/io_context.hpp>
14 
15 namespace carla {
16 namespace streaming {
17 namespace low_level {
18 
19  /// A low-level streaming server. Each new stream has a token associated, this
20  /// token can be used by a client to subscribe to the stream. This server
21  /// requires an external io_context running.
22  ///
23  /// @warning This server cannot be destructed before its @a io_context is
24  /// stopped.
25  template <typename T>
26  class Server {
27  public:
28 
29  using underlying_server = T;
30 
32 
33  template <typename InternalEPType, typename ExternalEPType>
34  explicit Server(
35  boost::asio::io_context &io_context,
38  : _server(io_context, std::move(internal_ep)),
39  _dispatcher(std::move(external_ep)) {
40  StartServer();
41  }
42 
43  template <typename InternalEPType>
44  explicit Server(
45  boost::asio::io_context &io_context,
47  : _server(io_context, std::move(internal_ep)),
49  StartServer();
50  }
51 
52  template <typename... EPArgs>
53  explicit Server(boost::asio::io_context &io_context, EPArgs &&... args)
54  : Server(io_context, make_endpoint<protocol_type>(std::forward<EPArgs>(args)...)) {}
55 
57  return _server.GetLocalEndpoint();
58  }
59 
60  void SetTimeout(time_duration timeout) {
61  _server.SetTimeout(timeout);
62  }
63 
65  return _dispatcher.MakeStream();
66  }
67 
69  return _dispatcher.CloseStream(id);
70  }
71 
72  void SetSynchronousMode(bool is_synchro) {
73  _server.SetSynchronousMode(is_synchro);
74  }
75 
77  return _dispatcher.GetToken(sensor_id);
78  }
79 
80  private:
81 
82  void StartServer() {
83  auto on_session_opened = [this](auto session) {
84  if (!_dispatcher.RegisterSession(session)) {
85  session->Close();
86  }
87  };
88  auto on_session_closed = [this](auto session) {
89  log_debug("on_session_closed called");
91  };
92  _server.Listen(on_session_opened, on_session_closed);
93  }
94 
96 
98  };
99 
100 } // namespace low_level
101 } // namespace streaming
102 } // namespace carla
Server(boost::asio::io_context &io_context, EPArgs &&... args)
carla::streaming::detail::token_type GetToken(carla::streaming::detail::stream_id_type sensor_id)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition: EndPoint.h:90
uint32_t stream_id_type
Definition: Types.h:18
static void log_debug(Args &&...)
Definition: Logging.h:75
token_type GetToken(stream_id_type sensor_id)
Definition: Dispatcher.cpp:106
void CloseStream(carla::streaming::detail::stream_id_type id)
Definition: Dispatcher.cpp:60
Serializes a stream endpoint.
Definition: detail/Token.h:61
void DeregisterSession(std::shared_ptr< Session > session)
Definition: Dispatcher.cpp:91
bool RegisterSession(std::shared_ptr< Session > session)
Definition: Dispatcher.cpp:74
Keeps the mapping between streams and sessions.
Definition: Dispatcher.h:27
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
carla::streaming::Stream MakeStream()
Definition: Dispatcher.cpp:37
void CloseStream(carla::streaming::detail::stream_id_type id)
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep)
underlying_server::endpoint GetLocalEndpoint() const
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep, detail::EndPoint< protocol_type, ExternalEPType > external_ep)