boost.asio中有async_read_some,async_accept,async_write_some,async_connect这几个函数,那么他们的回调函数都在同一线程吗?如果io_context在单线程运行,那么是的,测试如下。
class LIBTEST_BOOST_DLL TcpServer
{
public:
typedef boost::asio::ip::tcp::socket tcp_socket_t;
typedef boost::asio::ip::tcp::acceptor tcp_acceptor_t;
typedef boost::asio::ip::tcp::endpoint tcp_endpoint_t;
typedef boost::asio::io_context io_context_t;
TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint);
void Accept();
void ReadHandler(tcp_socket_t *sock);
private:
tcp_acceptor_t acceptor_;
io_context_t *io_;
std::vector<char> buf_;
uint64_t cnt_ = 0;
};
class LIBTEST_BOOST_DLL TcpClient
{
public:
typedef boost::asio::ip::tcp::socket tcp_socket_t;
typedef boost::asio::ip::tcp::endpoint tcp_endpoint_t;
typedef boost::asio::io_context io_context_t;
TcpClient(io_context_t &io);
void Connect(const tcp_endpoint_t& endpoint);
void Write();
private:
tcp_socket_t sock_;
io_context_t *io_;
uint64_t cnt_ = 0;
};
TcpServer::TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint)
:acceptor_(io,endpoint),io_(&io)
{
}
void TcpServer::ReadHandler(tcp_socket_t *sock)
{
char *buf = new char[1024];
sock->async_read_some(boost::asio::buffer(buf,1024), [sock, this,buf](const std::error_code &ec, std::size_t sz) {
delete buf;
if (ec) {
delete sock;
cout << "value=" << ec.value() << ",msg=" << ec.message()<< endl;
return;
}
//cout << "[" << this_thread::get_id() << "] " << &buf_[0];// << endl;
cnt_++;
cout << "[threadId=" << this_thread::get_id() << "] " << "async_read_some callback cnt=" << cnt_ << ",size=" << sz << endl;
ReadHandler(sock);
});
}
void TcpServer::Accept()
{
//auto sock = make_shared<tcp_socket_t>(*io_);
auto sock = new tcp_socket_t(*io_);
auto acceptHandler = [this,sock](const std::error_code &ec) {
if (ec) {
cout << "accept error " << endl;
return;
}
cout << "[threadId=" << this_thread::get_id() << "]";
cout << " async_accept callback client from: ";
cout << sock->remote_endpoint().address() <<":" << sock->remote_endpoint().port() << endl;
ReadHandler(sock);
Accept();
};
acceptor_.async_accept(*sock, acceptHandler);
}
TcpClient::TcpClient(io_context_t &io)
:sock_(io),io_(&io)
{
}
void TcpClient::Write()
{
auto buf = new char[1024];
memset(buf,'q',1024);
sock_.async_write_some(boost::asio::buffer(buf,1024), [this,buf](const std::error_code &ec, std::size_t sz) {
delete buf;
if (ec) {
cout << "[threadId=" << this_thread::get_id() << "] " << "write error" << endl;
return;
}
cnt_++;
cout << "[threadId=" << this_thread::get_id() << "] " << "async_write_some callback cnt=" << cnt_ << ",size=" << sz<< endl;
});
}
void TcpClient::Connect(const tcp_endpoint_t& endpoint)
{
auto connHandler = [this,endpoint](const std::error_code &ec) {
if (ec) {
cout << "connect error " << "[threadId=" << this_thread::get_id() << "]"<<endl;
Connect(endpoint);
return;
}
cout << "[threadId=" << this_thread::get_id() << "] ";
cout << "async_connect callback to ";
cout << this->sock_.remote_endpoint().address();
cout << " success" << endl;;
std::thread t([this] {
for(int i=0;i<1;i++)
Write();
});
t.detach();
};
sock_.async_connect(endpoint, connHandler);
}
int main(int argc, char *argv[])
{
boost::asio::io_context io;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 1234);
TcpServer tcp(io,endpoint);
TcpClient client(io);
tcp.Accept();
client.Connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),1234));
boost::asio::io_context::work worker(io);
io.run();
#ifdef WIN32
system("pause");
#endif
return 0;
}