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>
32 boost::asio::io_context &io_context,
41 _deadline(io_context),
42 _strand(io_context) {}
55 const boost::asio::ip::tcp::no_delay option(
true);
60 auto self = shared_from_this();
61 boost::asio::post(
_strand, [=]() {
63 auto handle_query = [
this, self, callback=std::move(on_opened)](
64 const boost::system::error_code &ec,
72 boost::asio::post(
_strand.context(), [=]() { callback(self); });
84 boost::asio::async_read(
87 boost::asio::bind_executor(
_strand, handle_query));
96 auto self = shared_from_this();
104 std::this_thread::yield();
114 auto handle_sent = [
this, self, message](
const boost::system::error_code &ec,
size_t DEBUG_ONLY(bytes)) {
131 boost::asio::async_write(
_socket, message->GetBufferSequence(),
132 boost::asio::bind_executor(
_strand, handle_sent));
136 boost::asio::post(
_strand, [self=shared_from_this()]() { self->CloseNow(); });
140 if (
_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now()) {
144 _deadline.async_wait([
this, self=shared_from_this()](boost::system::error_code ec) {
159 boost::system::error_code ec2;
160 _socket.shutdown(boost::asio::socket_base::shutdown_both, ec2);
#define DEBUG_ASSERT(predicate)
#define DEBUG_ASSERT_EQ(lhs, rhs)
#define LIBCARLA_INITIALIZE_LIFETIME_PROFILER(display_name)
包含Carla流处理模块中TCP通信相关类的头文件依赖。
boost::asio::deadline_timer _deadline
定时器,用于在会话超时后触发关闭操作。
void CloseNow(boost::system::error_code ec=boost::system::error_code())
立即关闭会话。
Server & _server
对 Server 对象的引用。
ServerSession(boost::asio::io_context &io_context, time_duration timeout, Server &server)
构造函数。
void Open(callback_function_type on_opened, callback_function_type on_closed)
启动会话。
callback_function_type _on_closed
会话关闭时的回调函数。
boost::asio::io_context::strand _strand
用于保证异步操作顺序的 strand 对象。
socket_type _socket
套接字类型,用于网络通信。
stream_id_type _stream_id
流标识符,用于标识会话中传输的数据流。
const size_t _session_id
会话的唯一标识符。
std::function< void(std::shared_ptr< ServerSession >)> callback_function_type
回调函数类型别名。
time_duration _timeout
会话超时时长,表示会话在多长时间内无活动将被关闭。
bool _is_writing
表示当前是否正在进行写入操作的标志。
void Write(std::shared_ptr< const Message > message)
向套接字写入一些数据。
警告:在io_context停止之前,不能销毁这个服务器实例
bool IsSynchronousMode() const
Positive time duration up to milliseconds resolution.
static std::atomic_size_t SESSION_COUNTER
uint32_t message_size_type
消息大小的类型定义。
static void log_error(Args &&... args)
static void log_info(Args &&... args)
static void log_debug(Args &&... args)