简体   繁体   English

使用async_read的boost.asio多线程I / O服务器

[英]boost.asio multi-thread I/O server with async_read

The basic logic is as follows 基本逻辑如下

main_thread: main_thread:

for(;;) { for(;;){
accept socket 接受套接字
async_read head(length of packet),binded to head_handler async_read head(包长度),绑定到head_handler
io_service.run() } io_service.run()}

head_handler(non-thread): head_handler(非螺纹):

async_read body(using length read from head) ,binded to body_handler async_read body(使用从头部读取的长度),绑定到body_handler

body_handler(non-thread): body_handler(非螺纹):

parse message into Request objects (custom class). 将消息解析为Request对象(自定义类)。
push Request to request_pool push请求request_pool

read_thread: read_thread:

for(;;) { for(;;){
if request_pool not empty (lock otherwise) { perform actions described by the Request } } 如果request_pool不为空(否则锁定){执行请求描述的操作}}

So I have a simple echo server using above logics. 所以我有一个使用上述逻辑的简单回声服务器。 It prints out whatever messages passed in.However, only the first message will be displayed, all others will have "xxx.xxx.xxx.xxx connected" message printed out correctly.But no messages are printed 它打印出传入的任何消息。但是,只会显示第一条消息,其他所有消息都会正确打印出“xxx.xxx.xxx.xxx已连接”消息。但是没有打印消息

If you do not issue any async. 如果您不发出任何异步。 operation in body_handler , io_service runs out of work and io_service::run() exits. body_handler运行, io_service用完了, io_service::run()退出了。 Before any subsequent call to io_service::run() you have to call io_service::reset() . 在对io_service::run()任何后续调用之前,您必须调用io_service::reset()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM