CARLA
primary.h
Go to the documentation of this file.
1 // Copyright (c) 2022 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"
16 
17 #include <boost/asio/deadline_timer.hpp>
18 #include <boost/asio/io_context.hpp>
19 #include <boost/asio/ip/tcp.hpp>
20 #include <boost/asio/strand.hpp>
21 
22 #include <functional>
23 #include <memory>
24 
25 namespace carla {
26 namespace multigpu {
27 
28  /// A TCP server session. When a session opens, it reads from the socket a
29  /// stream id object and passes itself to the callback functor. The session
30  /// closes itself after @a timeout of inactivity is met.
31  class Primary
32  : public std::enable_shared_from_this<Primary>,
34  private NonCopyable {
35  public:
36 
37  using socket_type = boost::asio::ip::tcp::socket;
38 
39  explicit Primary(
40  boost::asio::io_context &io_context,
41  time_duration timeout,
42  Listener &server);
43 
44  ~Primary();
45 
46  /// Starts the session and calls @a on_opened after successfully reading the
47  /// stream id, and @a on_closed once the session is closed.
48  void Open(
52 
53  template <typename... Buffers>
54  static auto MakeMessage(Buffers... buffers) {
55  static_assert(
57  "This function only accepts arguments of type BufferView.");
58  return std::make_shared<const carla::streaming::detail::tcp::Message>(buffers...);
59  }
60 
61  /// Writes some data to the socket.
62  void Write(std::shared_ptr<const carla::streaming::detail::tcp::Message> message);
63 
64  /// Writes a string
65  void Write(std::string text);
66 
67  /// read data
68  void ReadData();
69 
70  /// Writes some data to the socket.
71  template <typename... Buffers>
72  void Write(Buffers... buffers) {
73  Write(MakeMessage(buffers...));
74  }
75 
76  /// Post a job to close the session.
77  void Close();
78 
79  private:
80 
81  void StartTimer();
82 
83  void CloseNow(boost::system::error_code ec = boost::system::error_code());
84 
85  friend class Listener;
86 
88 
89  const size_t _session_id;
90 
92 
94 
95  boost::asio::deadline_timer _deadline;
96 
97  boost::asio::io_context::strand _strand;
98 
100 
102 
103  std::shared_ptr<BufferPool> _buffer_pool;
104 
105  bool _is_writing = false;
106 
107  };
108 
109 } // namespace multigpu
110 } // namespace carla
std::function< void(std::shared_ptr< Primary >, carla::Buffer)> callback_function_type_response
Definition: listener.h:33
A TCP server session.
Definition: primary.h:31
boost::asio::io_context::strand _strand
Definition: primary.h:97
const size_t _session_id
Definition: primary.h:89
std::function< void(std::shared_ptr< Primary >)> callback_function_type
Definition: listener.h:32
static auto MakeMessage(Buffers... buffers)
Definition: primary.h:54
time_duration _timeout
Definition: primary.h:93
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void Write(Buffers... buffers)
Writes some data to the socket.
Definition: primary.h:72
void Close()
Post a job to close the session.
Definition: primary.cpp:176
std::shared_ptr< BufferPool > _buffer_pool
Definition: primary.h:103
void CloseNow(boost::system::error_code ec=boost::system::error_code())
Definition: primary.cpp:203
void ReadData()
read data
Definition: primary.cpp:122
Primary(boost::asio::io_context &io_context, time_duration timeout, Listener &server)
Definition: primary.cpp:27
boost::asio::deadline_timer _deadline
Definition: primary.h:95
socket_type _socket
Definition: primary.h:91
Listener & _server
Definition: primary.h:87
Listener::callback_function_type _on_closed
Definition: primary.h:99
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Inherit (privately) to suppress copy/move construction and assignment.
boost::asio::ip::tcp::socket socket_type
Definition: primary.h:37
Listener::callback_function_type_response _on_response
Definition: primary.h:101
void Write(std::shared_ptr< const carla::streaming::detail::tcp::Message > message)
Writes some data to the socket.
Definition: primary.cpp:68
void Open(Listener::callback_function_type on_opened, Listener::callback_function_type on_closed, Listener::callback_function_type_response on_response)
Starts the session and calls on_opened after successfully reading the stream id, and on_closed once t...
Definition: primary.cpp:49