CARLA
ServerSession.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"
11 #include "carla/TypeTraits.h"
15 
16 #if defined(__clang__)
17 # pragma clang diagnostic push
18 # pragma clang diagnostic ignored "-Wshadow"
19 #endif
20 #include <boost/asio/deadline_timer.hpp>
21 #include <boost/asio/io_context.hpp>
22 #include <boost/asio/ip/tcp.hpp>
23 #include <boost/asio/strand.hpp>
24 #if defined(__clang__)
25 # pragma clang diagnostic pop
26 #endif
27 
28 #include <functional>
29 #include <memory>
30 
31 namespace carla {
32 namespace streaming {
33 namespace detail {
34 namespace tcp {
35 
36  class Server;
37 
38  /// A TCP server session. When a session opens, it reads from the socket a
39  /// stream id object and passes itself to the callback functor. The session
40  /// closes itself after @a timeout of inactivity is met.
42  : public std::enable_shared_from_this<ServerSession>,
44  private NonCopyable {
45  public:
46 
47  using socket_type = boost::asio::ip::tcp::socket;
48  using callback_function_type = std::function<void(std::shared_ptr<ServerSession>)>;
49 
50  explicit ServerSession(
51  boost::asio::io_context &io_context,
52  time_duration timeout,
53  Server &server);
54 
55  /// Starts the session and calls @a on_opened after successfully reading the
56  /// stream id, and @a on_closed once the session is closed.
57  void Open(
58  callback_function_type on_opened,
59  callback_function_type on_closed);
60 
61  /// @warning This function should only be called after the session is
62  /// opened. It is safe to call this function from within the @a callback.
64  return _stream_id;
65  }
66 
67  template <typename... Buffers>
68  static auto MakeMessage(Buffers... buffers) {
69  static_assert(
71  "This function only accepts arguments of type BufferView.");
72  return std::make_shared<const Message>(buffers...);
73  }
74 
75  /// Writes some data to the socket.
76  void Write(std::shared_ptr<const Message> message);
77 
78  /// Writes some data to the socket.
79  template <typename... Buffers>
80  void Write(Buffers... buffers) {
81  Write(MakeMessage(buffers...));
82  }
83 
84  /// Post a job to close the session.
85  void Close();
86 
87  private:
88 
89  void StartTimer();
90 
91  void CloseNow(boost::system::error_code ec = boost::system::error_code());
92 
93  friend class Server;
94 
96 
97  const size_t _session_id;
98 
100 
102 
104 
105  boost::asio::deadline_timer _deadline;
106 
107  boost::asio::io_context::strand _strand;
108 
110 
111  bool _is_writing = false;
112  };
113 
114 } // namespace tcp
115 } // namespace detail
116 } // namespace streaming
117 } // namespace carla
void CloseNow(boost::system::error_code ec=boost::system::error_code())
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void Close()
Post a job to close the session.
uint32_t stream_id_type
Definition: Types.h:18
void Write(Buffers... buffers)
Writes some data to the socket.
Definition: ServerSession.h:80
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
Definition: ServerSession.h:48
void Write(std::shared_ptr< const Message > message)
Writes some data to the socket.
boost::asio::ip::tcp::socket socket_type
Definition: ServerSession.h:47
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Inherit (privately) to suppress copy/move construction and assignment.
static auto MakeMessage(Buffers... buffers)
Definition: ServerSession.h:68
void Open(callback_function_type on_opened, callback_function_type on_closed)
Starts the session and calls on_opened after successfully reading the stream id, and on_closed once t...
ServerSession(boost::asio::io_context &io_context, time_duration timeout, Server &server)
boost::asio::io_context::strand _strand