CARLA
Loading...
Searching...
No Matches
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"
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
32namespace carla {
33namespace streaming {
34namespace detail {
35namespace 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.
43 : public std::enable_shared_from_this<ServerSession>,
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.
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
97
98 const size_t _session_id;
99
101
103
105
106 boost::asio::steady_timer _deadline;
107
108 boost::asio::io_context::strand _strand;
109
111
112 bool _is_writing = false;
113 std::atomic_bool _is_closed{false};
114 };
115
116} // namespace tcp
117} // namespace detail
118} // namespace streaming
119} // namespace carla
Inherit (privately) to suppress copy/move construction and assignment.
void CloseNow(boost::system::error_code ec=boost::system::error_code())
void Close()
Post a job to close the session.
boost::asio::ip::tcp::socket socket_type
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...
boost::asio::io_context::strand _strand
static auto MakeMessage(Buffers... buffers)
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
void Write(Buffers... buffers)
Writes some data to the socket.
void Write(std::shared_ptr< const Message > message)
Writes some data to the socket.
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