CARLA
streaming/detail/tcp/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/NonCopyable.h"
10 #include "carla/Time.h"
12 
13 #include <boost/asio/io_context.hpp>
14 #include <boost/asio/ip/tcp.hpp>
15 #include <boost/asio/post.hpp>
16 
17 #include <atomic>
18 
19 namespace carla {
20 namespace streaming {
21 namespace detail {
22 namespace tcp {
23 
24  /// @warning This server cannot be destructed before its @a io_context is
25  /// stopped.
26  class Server : private NonCopyable {
27  public:
28 
29  using endpoint = boost::asio::ip::tcp::endpoint;
30  using protocol_type = endpoint::protocol_type;
31 
32  explicit Server(boost::asio::io_context &io_context, endpoint ep);
33 
35  return _acceptor.local_endpoint();
36  }
37 
38  /// Set session time-out. Applies only to newly created sessions. By default
39  /// the time-out is set to 10 seconds.
40  void SetTimeout(time_duration timeout) {
41  _timeout = timeout;
42  }
43 
44  /// Start listening for connections. On each new connection, @a
45  /// on_session_opened is called, and @a on_session_closed when the session
46  /// is closed.
47  template <typename FunctorT1, typename FunctorT2>
48  void Listen(FunctorT1 on_session_opened, FunctorT2 on_session_closed) {
49  boost::asio::post(_io_context, [=]() {
51  _timeout,
52  std::move(on_session_opened),
53  std::move(on_session_closed));
54  });
55  }
56 
57  void SetSynchronousMode(bool is_synchro) {
58  _synchronous = is_synchro;
59  }
60 
61  bool IsSynchronousMode() const {
62  return _synchronous;
63  }
64 
65  private:
66 
67  void OpenSession(
68  time_duration timeout,
69  ServerSession::callback_function_type on_session_opened,
70  ServerSession::callback_function_type on_session_closed);
71 
72  boost::asio::io_context &_io_context;
73 
74  boost::asio::ip::tcp::acceptor _acceptor;
75 
76  std::atomic<time_duration> _timeout;
77 
79  };
80 
81 } // namespace tcp
82 } // namespace detail
83 } // namespace streaming
84 } // namespace carla
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void SetTimeout(time_duration timeout)
Set session time-out.
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
Definition: ServerSession.h:48
void Listen(FunctorT1 on_session_opened, FunctorT2 on_session_closed)
Start listening for connections.
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Server(boost::asio::io_context &io_context, endpoint ep)
Definition: Server.cpp:20
Inherit (privately) to suppress copy/move construction and assignment.
void OpenSession(time_duration timeout, ServerSession::callback_function_type on_session_opened, ServerSession::callback_function_type on_session_closed)
Definition: Server.cpp:26