13#include <boost/asio/read.hpp>
14#include <boost/asio/write.hpp>
15#include <boost/asio/bind_executor.hpp>
16#include <boost/asio/post.hpp>
29 boost::asio::io_context &io_context,
38 _deadline(io_context),
39 _strand(io_context) {}
49 const boost::asio::ip::tcp::no_delay option(
true);
53 auto self = shared_from_this();
54 boost::asio::post(
_strand, [=]() {
56 auto handle_query = [
this, self, callback=std::move(on_opened)](
57 const boost::system::error_code &ec,
62 boost::asio::post(
_strand.context(), [=]() { callback(self); });
71 boost::asio::async_read(
74 boost::asio::bind_executor(
_strand, handle_query));
81 auto self = shared_from_this();
82 boost::asio::post(
_strand, [=]() {
90 std::this_thread::yield();
100 auto handle_sent = [
this, self, message](
const boost::system::error_code &ec,
size_t DEBUG_ONLY(bytes)) {
114 boost::asio::async_write(
116 message->GetBufferSequence(),
121 void ServerSession::Close() {
122 boost::asio::post(_strand, [self=shared_from_this()]() { self->CloseNow(); });
125 void ServerSession::StartTimer() {
126 if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now()) {
127 log_debug(
"session", _session_id,
"timed out");
130 _deadline.async_wait([
this, self=shared_from_this()](boost::system::error_code ec) {
134 log_debug(
"session", _session_id,
"timed out error:", ec.message());
140 void ServerSession::CloseNow(boost::system::error_code ec) {
144 if (_socket.is_open()) {
145 boost::system::error_code ec2;
146 _socket.shutdown(boost::asio::socket_base::shutdown_both, ec2);
150 _on_closed(shared_from_this());
151 log_debug(
"session", _session_id,
"closed");
#define DEBUG_ASSERT(predicate)
#define DEBUG_ASSERT_EQ(lhs, rhs)
#define LIBCARLA_INITIALIZE_LIFETIME_PROFILER(display_name)
boost::asio::deadline_timer _deadline
void CloseNow(boost::system::error_code ec=boost::system::error_code())
ServerSession(boost::asio::io_context &io_context, time_duration timeout, Server &server)
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...
callback_function_type _on_closed
boost::asio::io_context::strand _strand
stream_id_type _stream_id
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
void Write(std::shared_ptr< const Message > message)
Writes some data to the socket.
bool IsSynchronousMode() const
Positive time duration up to milliseconds resolution.
static std::atomic_size_t SESSION_COUNTER
uint32_t message_size_type
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
static void log_info(Args &&... args)
static void log_debug(Args &&... args)