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 {
24  public:
25 
26  explicit Server(uint16_t port)
27  : _server(_pool.io_context(), make_endpoint<protocol_type>(port)) {}
28 
29  explicit Server(const std::string &address, uint16_t port)
30  : _server(_pool.io_context(), make_endpoint<protocol_type>(address, port)) {}
31 
32  explicit Server(
33  const std::string &address, uint16_t port,
34  const std::string &external_address, uint16_t external_port)
35  : _server(
36  _pool.io_context(),
37  make_endpoint<protocol_type>(address, port),
38  make_endpoint<protocol_type>(external_address, external_port)) {}
39 
40  ~Server() {
41  _pool.Stop();
42  }
43 
44  auto GetLocalEndpoint() const {
45  return _server.GetLocalEndpoint();
46  }
47 
48  void SetTimeout(time_duration timeout) {
49  _server.SetTimeout(timeout);
50  }
51 
53  return _server.MakeStream();
54  }
55 
57  return _server.CloseStream(id);
58  }
59 
60  void Run() {
61  _pool.Run();
62  }
63 
64  void AsyncRun(size_t worker_threads) {
65  _pool.AsyncRun(worker_threads);
66  }
67 
68  void SetSynchronousMode(bool is_synchro) {
69  _server.SetSynchronousMode(is_synchro);
70  }
71 
73  return _server.GetToken(sensor_id);
74  }
75 
76  private:
77 
78  // The order of these two arguments is very important.
79 
81 
83  };
84 
85 } // namespace streaming
86 } // namespace carla
void AsyncRun(size_t worker_threads)
carla::streaming::detail::token_type GetToken(carla::streaming::detail::stream_id_type sensor_id)
carla::streaming::detail::token_type GetToken(carla::streaming::detail::stream_id_type 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)
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)
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
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)
underlying_server::endpoint GetLocalEndpoint() const
void SetSynchronousMode(bool is_synchro)
void CloseStream(carla::streaming::detail::stream_id_type id)