CARLA
Loading...
Searching...
No Matches
primary.h
Go to the documentation of this file.
1// Copyright (c) 2026 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/io_context.hpp>
18#include <boost/asio/ip/tcp.hpp>
19#include <boost/asio/steady_timer.hpp>
20#include <boost/asio/strand.hpp>
21
22#include <deque>
23#include <functional>
24#include <memory>
25
26namespace carla {
27namespace multigpu {
28
29 /// A TCP server session. When a session opens, it reads from the socket a
30 /// stream id object and passes itself to the callback functor. The session
31 /// closes itself after @a timeout of inactivity is met.
32 class Primary
33 : public std::enable_shared_from_this<Primary>,
35 private NonCopyable {
36 public:
37
38 using socket_type = boost::asio::ip::tcp::socket;
39
40 explicit Primary(
41 boost::asio::io_context &io_context,
42 time_duration timeout,
43 Listener &server);
44
45 ~Primary();
46
47 /// Starts the session and calls @a on_opened after successfully reading the
48 /// stream id, and @a on_closed once the session is closed.
49 void Open(
53
54 template <typename... Buffers>
55 static auto MakeMessage(Buffers... buffers) {
56 static_assert(
57 are_same<SharedBufferView, Buffers...>::value,
58 "This function only accepts arguments of type BufferView.");
59 return std::make_shared<const carla::streaming::detail::tcp::Message>(buffers...);
60 }
61
62 /// Writes some data to the socket.
63 void Write(std::shared_ptr<const carla::streaming::detail::tcp::Message> message);
64
65 /// Writes a string
66 void Write(std::string text);
67
68 /// read data
69 void ReadData();
70
71 /// Writes some data to the socket.
72 template <typename... Buffers>
73 void Write(Buffers... buffers) {
74 Write(MakeMessage(buffers...));
75 }
76
77 /// Post a job to close the session.
78 void Close();
79
80 private:
81
82 using message_type = std::shared_ptr<const carla::streaming::detail::tcp::Message>;
83
84 /// Starts writing the front of the write queue. Strand only.
85 void WriteNext();
86
87 void StartTimer();
88
89 void CloseNow(boost::system::error_code ec = boost::system::error_code());
90
91 friend class Listener;
92
94
95 const size_t _session_id;
96
98
100
101 boost::asio::steady_timer _deadline;
102
103 boost::asio::io_context::strand _strand;
104
106
108
109 std::shared_ptr<BufferPool> _buffer_pool;
110
111 /// Messages waiting to be written, oldest first, and whether one is being
112 /// written. Strand only: a single write is ever in flight, so two large
113 /// frames can never interleave on the socket.
114 std::deque<message_type> _write_queue;
115
116 bool _is_writing = false;
117
118 };
119
120} // namespace multigpu
121} // namespace carla
Inherit (privately) to suppress copy/move construction and assignment.
std::function< void(std::shared_ptr< Primary >)> callback_function_type
Definition listener.h:32
std::function< void(std::shared_ptr< Primary >, carla::Buffer)> callback_function_type_response
Definition listener.h:33
A TCP server session.
Definition primary.h:35
const size_t _session_id
Definition primary.h:95
std::deque< message_type > _write_queue
Messages waiting to be written, oldest first, and whether one is being written.
Definition primary.h:114
void WriteNext()
Starts writing the front of the write queue. Strand only.
Definition primary.cpp:89
std::shared_ptr< const carla::streaming::detail::tcp::Message > message_type
Definition primary.h:82
socket_type _socket
Definition primary.h:97
time_duration _timeout
Definition primary.h:99
void Write(Buffers... buffers)
Writes some data to the socket.
Definition primary.h:73
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:52
std::shared_ptr< BufferPool > _buffer_pool
Definition primary.h:109
Listener::callback_function_type _on_closed
Definition primary.h:105
boost::asio::io_context::strand _strand
Definition primary.h:103
static auto MakeMessage(Buffers... buffers)
Definition primary.h:55
boost::asio::ip::tcp::socket socket_type
Definition primary.h:38
Listener::callback_function_type_response _on_response
Definition primary.h:107
boost::asio::steady_timer _deadline
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:72
void CloseNow(boost::system::error_code ec=boost::system::error_code())
Definition primary.cpp:215
void Close()
Post a job to close the session.
Definition primary.cpp:188
void ReadData()
read data
Definition primary.cpp:130
Positive time duration up to milliseconds resolution.
Definition Time.h:20
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< BufferView > SharedBufferView
Definition BufferView.h:151