CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
streaming
detail
tcp
ServerSession.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
"
12
#include "
carla/profiler/LifetimeProfiled.h
"
13
#include "
carla/streaming/detail/Types.h
"
14
#include "
carla/streaming/detail/tcp/Message.h
"
15
16
#if defined(__clang__)
17
# pragma clang diagnostic push
18
# pragma clang diagnostic ignored "-Wshadow"
19
#endif
20
#include <boost/asio/io_context.hpp>
21
#include <boost/asio/ip/tcp.hpp>
22
#include <boost/asio/steady_timer.hpp>
23
#include <boost/asio/strand.hpp>
24
#if defined(__clang__)
25
# pragma clang diagnostic pop
26
#endif
27
28
#include <atomic>
29
#include <functional>
30
#include <memory>
31
32
namespace
carla
{
33
namespace
streaming
{
34
namespace
detail
{
35
namespace
tcp
{
36
37
class
Server
;
38
39
/// A TCP server session. When a session opens, it reads from the socket a
40
/// stream id object and passes itself to the callback functor. The session
41
/// closes itself after @a timeout of inactivity is met.
42
class
ServerSession
43
:
public
std::enable_shared_from_this<ServerSession>,
44
private
profiler::LifetimeProfiled
,
45
private
NonCopyable
{
46
public
:
47
48
using
socket_type
= boost::asio::ip::tcp::socket;
49
using
callback_function_type
= std::function<void(std::shared_ptr<ServerSession>)>;
50
51
explicit
ServerSession
(
52
boost::asio::io_context &io_context,
53
time_duration
timeout,
54
Server
&server);
55
56
/// Starts the session and calls @a on_opened after successfully reading the
57
/// stream id, and @a on_closed once the session is closed.
58
void
Open
(
59
callback_function_type
on_opened,
60
callback_function_type
on_closed);
61
62
/// @warning This function should only be called after the session is
63
/// opened. It is safe to call this function from within the @a callback.
64
stream_id_type
get_stream_id
()
const
{
65
return
_stream_id
;
66
}
67
68
template
<
typename
... Buffers>
69
static
auto
MakeMessage
(Buffers... buffers) {
70
static_assert
(
71
are_same
<
SharedBufferView
, Buffers...>::value,
72
"This function only accepts arguments of type BufferView."
);
73
return
std::make_shared<const Message>(buffers...);
74
}
75
76
/// Writes some data to the socket.
77
void
Write
(std::shared_ptr<const Message> message);
78
79
/// Writes some data to the socket.
80
template
<
typename
... Buffers>
81
void
Write
(Buffers... buffers) {
82
Write
(
MakeMessage
(buffers...));
83
}
84
85
/// Post a job to close the session.
86
void
Close
();
87
88
private
:
89
90
void
StartTimer
();
91
92
void
CloseNow
(boost::system::error_code ec = boost::system::error_code());
93
94
friend
class
Server
;
95
96
Server
&
_server
;
97
98
const
size_t
_session_id
;
99
100
stream_id_type
_stream_id
= 0u;
101
102
socket_type
_socket
;
103
104
time_duration
_timeout
;
105
106
boost::asio::steady_timer
_deadline
;
107
108
boost::asio::io_context::strand
_strand
;
109
110
callback_function_type
_on_closed
;
111
112
/// Set by the sending thread, cleared by the write handler on the strand.
113
std::atomic_bool
_is_writing
{
false
};
114
std::atomic_bool
_is_closed
{
false
};
115
};
116
117
}
// namespace tcp
118
}
// namespace detail
119
}
// namespace streaming
120
}
// namespace carla
NonCopyable.h
LifetimeProfiled.h
Message.h
Time.h
TypeTraits.h
carla::NonCopyable::NonCopyable
NonCopyable()=default
carla::profiler::LifetimeProfiled
Definition
LifetimeProfiled.h:14
carla::streaming::detail::tcp::ServerSession::get_stream_id
stream_id_type get_stream_id() const
Definition
ServerSession.h:64
carla::streaming::detail::tcp::ServerSession::CloseNow
void CloseNow(boost::system::error_code ec=boost::system::error_code())
Definition
ServerSession.cpp:166
carla::streaming::detail::tcp::ServerSession::Close
void Close()
Post a job to close the session.
Definition
ServerSession.cpp:147
carla::streaming::detail::tcp::ServerSession::socket_type
boost::asio::ip::tcp::socket socket_type
Definition
ServerSession.h:48
carla::streaming::detail::tcp::ServerSession::_server
Server & _server
Definition
ServerSession.h:96
carla::streaming::detail::tcp::ServerSession::ServerSession
ServerSession(boost::asio::io_context &io_context, time_duration timeout, Server &server)
Definition
ServerSession.cpp:29
carla::streaming::detail::tcp::ServerSession::Open
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...
Definition
ServerSession.cpp:42
carla::streaming::detail::tcp::ServerSession::_on_closed
callback_function_type _on_closed
Definition
ServerSession.h:110
carla::streaming::detail::tcp::ServerSession::_strand
boost::asio::io_context::strand _strand
Definition
ServerSession.h:108
carla::streaming::detail::tcp::ServerSession::_deadline
boost::asio::steady_timer _deadline
Definition
ServerSession.h:106
carla::streaming::detail::tcp::ServerSession::_is_writing
std::atomic_bool _is_writing
Set by the sending thread, cleared by the write handler on the strand.
Definition
ServerSession.h:113
carla::streaming::detail::tcp::ServerSession::_socket
socket_type _socket
Definition
ServerSession.h:102
carla::streaming::detail::tcp::ServerSession::_stream_id
stream_id_type _stream_id
Definition
ServerSession.h:100
carla::streaming::detail::tcp::ServerSession::MakeMessage
static auto MakeMessage(Buffers... buffers)
Definition
ServerSession.h:69
carla::streaming::detail::tcp::ServerSession::_session_id
const size_t _session_id
Definition
ServerSession.h:98
carla::streaming::detail::tcp::ServerSession::callback_function_type
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
Definition
ServerSession.h:49
carla::streaming::detail::tcp::ServerSession::StartTimer
void StartTimer()
Definition
ServerSession.cpp:151
carla::streaming::detail::tcp::ServerSession::Server
friend class Server
Definition
ServerSession.h:94
carla::streaming::detail::tcp::ServerSession::_is_closed
std::atomic_bool _is_closed
Definition
ServerSession.h:114
carla::streaming::detail::tcp::ServerSession::Write
void Write(Buffers... buffers)
Writes some data to the socket.
Definition
ServerSession.h:81
carla::streaming::detail::tcp::ServerSession::_timeout
time_duration _timeout
Definition
ServerSession.h:104
carla::streaming::detail::tcp::ServerSession::Write
void Write(std::shared_ptr< const Message > message)
Writes some data to the socket.
Definition
ServerSession.cpp:79
carla::streaming::detail::tcp::Server
Definition
streaming/detail/tcp/Server.h:26
carla::time_duration
Positive time duration up to milliseconds resolution.
Definition
Time.h:20
carla::streaming::detail::tcp
Definition
streaming/detail/tcp/Client.cpp:26
carla::streaming::detail
Definition
Dispatcher.cpp:17
carla::streaming::detail::stream_id_type
uint32_t stream_id_type
Definition
streaming/detail/Types.h:18
carla::streaming
Definition
client/detail/Client.h:61
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::SharedBufferView
std::shared_ptr< BufferView > SharedBufferView
Definition
BufferView.h:151
Types.h
carla::are_same
Definition
TypeTraits.h:14
Generated by
1.18.0