asio之服务的理解

ops/2024/10/9 15:19:45/

服务组件

asio中的服务抽象为io_service::service

io_service::service
- boost::asio::io_service& owner_
- service* next_
- key key_
+boost::asio::io_service& get_io_service()
#~service()
#service(boost::asio::io_service& owner)
-void shutdown_service()
-void fork_service(boost::asio::io_service::fork_event event)

shutdown_service为虚方法
next_表示下一个服务,主要用于服务的管理,通过将所有的服务连接起来构成一个链表
key的定义为

  struct key{key() : type_info_(0), id_(0) {}const std::type_info* type_info_;const boost::asio::io_service::id* id_;} key_;

key用于区分服务,有两种使用方式,一种是使用service::id,一种是使用type_info

#if !defined(BOOST_ASIO_NO_TYPEID)
template <typename Service>
void service_registry::init_key(boost::asio::io_service::service::key& key,const boost::asio::detail::service_id<Service>& /*id*/)
{key.type_info_ = &typeid(typeid_wrapper<Service>);key.id_ = 0;
}
#endif // !defined(BOOST_ASIO_NO_TYPEID)void service_registry::init_key(boost::asio::io_service::service::key& key,const boost::asio::io_service::id& id)
{key.type_info_ = 0;key.id_ = &id;
}

服务标识

通过id来区分不同的服务

io_service::id
+id()

通过类模板来区分不同的服务类型

service_id<Type>
io_service::id

带有标识的服务

通过类模板service_base来表示带有标识的服务,包含静态类型service_id的成员

service_base<Type>
+ static boost::asio::detail::service_id<Type> id
io_service::service

服务管理

service_registry用于注册管理服务

service_registry
- boost::asio::detail::mutex mutex_
- boost::asio::io_service& owner_
- boost::asio::io_service::service* first_service_
+template ~typename Service, typename Arg~ service_registry(boost::asio::io_service& o, Service* initial_service, Arg arg)
+void notify_fork(boost::asio::io_service::fork_event fork_ev)
+template ~typename Service~ Service& first_service()
+template ~typename Service~ Service& use_service()
+template ~typename Service~ void add_service(Service* new_service)
+template ~typename Service~ bool has_service()
-static void init_key(boost::asio::io_service::service::key& key,const boost::asio::io_service::id& id)
-static bool keys_match(const boost::asio::io_service::service::key& key1,const boost::asio::io_service::service::key& key2)
-template ~typename Service~ static boost::asio::io_service::service* create(boost::asio::io_service& owner)
-static void destroy(boost::asio::io_service::service* service)
-boost::asio::io_service::service* do_use_service(const boost::asio::io_service::service::key& key,factory_type factory)
-void do_add_service(const boost::asio::io_service::service::key& key,boost::asio::io_service::service* new_service)
-bool do_has_service(const boost::asio::io_service::service::key& key)

first_service():返回服务链表中的第一个服务
use_service():如果链表中有对应的服务则直接使用,没有就添加到链表中
add_service(Service* new_service):链表中有则抛出异常,否则添加到链表中
has_service():链表中是否有服务

服务相关函数模板

template <typename Service> Service& use_service(io_service& ios);
template <typename Service> void add_service(io_service& ios, Service* svc);
template <typename Service> bool has_service(io_service& ios);

http://www.ppmy.cn/ops/103832.html

相关文章

Redis高可用方案:使用Keepalived实现主备双活

注意&#xff1a;请确保已经安装Redis和keepalived&#xff0c;本文不在介绍如何安装。 1、使用版本说明 Redis版本&#xff1a;5.0.2 Keepalived版本&#xff1a;1.3.5 Linux 版本&#xff1a;Centos7.9 查看Redis版本&#xff1a; /usr/local/redis/bin/redis-cli -v查…

HarmonyOS NEXT 实战开发:实现日常提醒应用

为什么要开发这个日常提醒应用&#xff1f; 最近鸿蒙热度一直不减&#xff0c;而且前端的就业环境越来越差&#xff0c;所以心里面萌生了换一个赛道的想法。HarmonyOS NEXT 是华为打造的国产之光&#xff0c;而且是纯血版不再是套壳&#xff0c;更加激起了我的好奇心。ArkTS是…

【网络安全】服务基础第一阶段——第三节:Windows系统管理基础----服务器远程管理与安全权限

服务器远程管理是一种技术&#xff0c;服务器远程管理是指通过远程连接来监控、配置和维护服务器的过程&#xff0c;而无需直接在物理服务器前操作。它允许IT管理员在不同的地点对服务器进行配置、监控和维护&#xff0c;无需物理上接触到服务器本身。这种能力对于管理分布在多…

安卓蓝牙技术的使用和场景

1.蓝牙协议应用领域 蓝牙协议是一种无线通信技术&#xff0c;广泛应用于短距离的数据和语音传输。以下是蓝牙协议的主要使用场景&#xff1a; 个人设备互联&#xff1a;蓝牙技术允许智能手机、平板电脑、笔记本电脑等设备之间进行数据传输和共享。例如&#xff0c;通过蓝牙将手…

nginx平滑升级与回滚

华子目录 升级实验环境准备测试内容准备实验要求实验步骤1.解压包2.检测1.26版本的环境3.make编译4.备份之前的nginx启动脚本5.将1.26中的nginx启动脚本覆盖掉1.24中的6.kill -USR2 旧主进程pid7.kill -WINCH 旧主进程pid 实验测试 回滚1.kill -HUP 旧主进程pid2.kill -WINCH 新…

Python 中用线程执行阻塞式 I/O,不做并行计算

Python 中用线程执行阻塞式 I/O&#xff0c;不做并行计算 尽管 Python 也支持多线程&#xff0c;但这些线程受 GIL&#xff08;global interpreter lock&#xff0c;全局解释器锁&#xff09; 约束&#xff0c;所以每次或许只能有一条线程向前推进&#xff0c;而无法实现多头并…

Tailwind CSS @apply Unknown at rule @apply scss(unknownAtRules) 解决方案

vscode 警告提示如下&#xff1a; tailwind base; ^^^^^^^^^ Unknown at rule tailwind css(unknownAtRules)layer base {* {apply text-gray-dark;^^^^^^^^ Unknown at rule apply css(unknownAtRules)} }解决方法&#xff1a; 在根目录 .vscode 文件夹下 .vscode/settings…

深度学习100问11:什么是one-hot编码

在机器学习和数据处理中&#xff0c;one-hot 编码也叫独热编码。 一、定义及原理 它是一种将类别变量表示为二进制向量的方法。假设有 n 个不同的类别&#xff0c;对于一个特定的类别&#xff0c;会创建一个长度为 n 的向量&#xff0c;其中只有一个位置为 1&#xff0c;其…