c++ - boost::asio::acceptor - 在旧连接仍然打开时接受新的传入连接

标签 c++ boost boost-asio

我正在编写基于 boost asio 的代理服务器。在负责接受从浏览器到代理服务器的传入连接的代码部分,我面临着我不完全理解的行为。

所以 - 我正在使用下一个构造函数创建接受器对象:

_acceptor(_io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port ), true)

从这里开始收听 (start_accept):

_new_connection.reset(new connection(*_io_services.front(), _connection_id));
_acceptor.async_accept(_new_connection->get_socket(),
                            boost::bind(&server::handle_accept, this,
                                        boost::asio::placeholders::error));

和handle_accept

if (!error) {
    _new_connection->start();
}

// continue waiting for incoming connections
start_accept();

一般来说,我接受传入连接的代码与 HTTP Server 2 中的相同。例子

只有当第一个传入连接未关闭时才会出现问题,然后第二个传入连接将排队等待,直到第一个传入连接关闭。

根据这两个答案: boost::asio acceptor reopen and async read after EOF

How to launch an "event" when my Boost::asio tcp server just start running ( AKA io_service.run() )?

acceptor 对象会将所有传入的连接添加到队列中,并且不会接受它们,直到挂起的连接不会被关闭。

我想实现对所有传入连接的立即处理——这样它们就不会在接受者的队列中挂起,到目前为止我还没有找到任何解决方案。

你能帮我看看,什么是正确的实现方式?

连接->start()函数

void
connection::start() {
    _bsocket.async_read_some(boost::asio::buffer(_bbuffer),
            boost::bind(&connection::handle_browser_read_headers,
                        shared_from_this(),
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred
        ));
}

Graphical representation 更新: boost asio 日志

@asio|1368460995.389629|0*1|socket@00CCFBE4.async_accept
@asio|1368461003.855113|>1|ec=system:0
@asio|1368461003.855113|1*2|socket@00E26850.async_receive
@asio|1368461003.855113|>2|ec=system:0,bytes_transferred=318
@asio|1368461003.856113|1*3|socket@00CCFBE4.async_accept
@asio|1368461003.856113|<1|
@asio|1368461003.856113|2*4|resolver@00E268D8.async_resolve
@asio|1368461003.856113|<2|
@asio|1368461003.866114|>4|ec=system:0,...
@asio|1368461003.866114|4*5|socket@00E26894.async_connect
@asio|1368461003.868114|<4|
@asio|1368461004.204133|>5|ec=system:0
@asio|1368461004.204133|5*6|socket@00E26894.async_send
@asio|1368461004.204133|<5|
@asio|1368461004.204133|>6|ec=system:0,bytes_transferred=302
@asio|1368461004.204133|6*7|socket@00E26894.async_receive
@asio|1368461004.204133|<6|
@asio|1368461004.613156|>7|ec=system:0,bytes_transferred=16384
@asio|1368461004.613156|7*8|socket@00E26850.async_send
@asio|1368461004.614157|<7|
@asio|1368461004.614157|>8|ec=system:0,bytes_transferred=16384
@asio|1368461004.614157|8*9|socket@00E26894.async_receive
@asio|1368461004.614157|<8|
@asio|1368461004.614157|>9|ec=system:0,bytes_transferred=1946
@asio|1368461004.614157|9*10|socket@00E26850.async_send
@asio|1368461004.614157|<9|
@asio|1368461004.614157|>10|ec=system:0,bytes_transferred=1946
@asio|1368461004.614157|10*11|socket@00E26894.async_receive
@asio|1368461004.614157|<10|
@asio|1368461004.618157|>11|ec=system:0,bytes_transferred=14080
@asio|1368461004.618157|11*12|socket@00E26850.async_send
@asio|1368461004.619157|<11|
@asio|1368461004.619157|>12|ec=system:0,bytes_transferred=14080
@asio|1368461004.619157|12*13|socket@00E26894.async_receive
@asio|1368461004.619157|<12|
@asio|1368461019.248994|>13|ec=asio.misc:2,bytes_transferred=0
@asio|1368461019.248994|13|socket@00E26894.close
@asio|1368461019.248994|13|socket@00E26850.close
@asio|1368461019.248994|<13|
@asio|1368461019.253994|0|resolver@00E268D8.cancel
@asio|1368461019.253994|>3|ec=system:0
@asio|1368461019.253994|3*14|socket@00E32688.async_receive
@asio|1368461019.254994|3*15|socket@00CCFBE4.async_accept
@asio|1368461019.254994|<3|
@asio|1368461019.254994|>14|ec=system:0,bytes_transferred=489
@asio|1368461019.254994|14*16|resolver@00E32710.async_resolve
@asio|1368461019.254994|<14|
@asio|1368461019.281995|>16|ec=system:0,...
@asio|1368461019.281995|16*17|socket@00E326CC.async_connect
@asio|1368461019.282996|<16|
@asio|1368461019.293996|>17|ec=system:0
@asio|1368461019.293996|17*18|socket@00E326CC.async_send
@asio|1368461019.293996|<17|
@asio|1368461019.293996|>18|ec=system:0,bytes_transferred=470
@asio|1368461019.293996|18*19|socket@00E326CC.async_receive
@asio|1368461019.293996|<18|
@asio|1368461019.315997|>19|ec=system:0,bytes_transferred=11001
@asio|1368461019.315997|19*20|socket@00E32688.async_send
@asio|1368461019.349999|<19|
@asio|1368461019.349999|>20|ec=system:0,bytes_transferred=11001
@asio|1368461019.349999|20|socket@00E326CC.close
@asio|1368461019.349999|20|socket@00E32688.close
@asio|1368461019.349999|<20|
@asio|1368461019.349999|0|resolver@00E32710.cancel

我发现接受器的行为取决于我用于从服务器套接字读取数据的函数。连接类从浏览器读取数据,修改请求url,连接到主机并发送请求,然后从服务器读取响应并将其写回浏览器。所以在我需要读取服务器主体的那一刻——我使用这个函数

    _ssocket.async_read_some(boost::asio::buffer(_sbuffer),
            boost::bind(&connection::handle_server_read_body,
                    shared_from_this(),
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred
        ));

如果服务响应 header 中未指定内容长度,我将一直读到 EOF。如果调用了 async_read_some 函数并且套接字上没有更多数据可读,它会等待约 15 秒,然后 EOF 才会被引发。 acceptor 不会接受这 15 秒内的所有新传入连接。

但是如果我使用 async_read 的另一种变体 -

        boost::asio::async_read(_ssocket, boost::asio::buffer(_sbuffer),
            boost::bind(&connection::handle_server_read_body,
                    shared_from_this(),
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred
        ));

传入的连接被接受得很好。但是它 boost::asio::async_read 工作起来有点慢,它正在等待从套接字读取一堆数据并且在读取数据之前不会调用处理程序,所以 - 我想我会指定 transfer_at_least

        boost::asio::async_read(_ssocket, boost::asio::buffer(_sbuffer), boost::asio::transfer_at_least(1),
            boost::bind(&connection::handle_server_read_body,
                    shared_from_this(),
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred
        ));

是的,它变得更好了 - 但接受新连接的问题返回:/

async_read_some 和 boost::asio::async_read 之间的真正区别是什么 - 感觉好像有什么东西被阻塞了。

最佳答案

我不知道这是否有帮助,但在我的服务器中,我将以下内容用于 session 的读取请求:

boost::asio::async_read( socket(), boost::asio::buffer( _incoming ),
    boost::asio::transfer_at_least( 1 ),
    boost::bind( &server_class::handle_read, shared_from_this(),
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred ) );

然后我将收到的所有内容转储到解析器中以确保它是正常的(处理状态等)。

否则,我相信我正在做你正在做的一切,基于我在这里看到的。

如果这可行,那么 asio 的行为似乎不直观。

关于c++ - boost::asio::acceptor - 在旧连接仍然打开时接受新的传入连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16521070/

相关文章:

c++ - glGetActiveUniform 报告制服存在,但 glGetUniformLocation 返回 -1

c++ - 后继AVL树c++

c++ - 成员指针或引用参数?

c++ - Boost asio tcp socket available 报告不正确的字节数

c++ - Boost.Asio 异步服务器。限制为一个连接

c++ - FFmpeg:av_opt_set 和 av_dict_set 之间有什么区别?

gcc - BOOST_LIKELY 和 __builtin_expect 仍然相关吗?

c++ - boost 程序选项: Read required parameter from config file

c++ - error LNK2019: 未解析的外部符号“public: __thiscall Server::Server(class boost::asio::io_service &)

c++ - 同时读取 100 个 telnet 连接 c++