问题描述
我正在尝试将一些旧代码从使用io_service迁移到基本tcp接受器的io_context,但是在将get_io_service()切换到get_executor()。context()时遇到问题,导致以下错误:
cannot convert ‘boost::asio::execution_context’ to ‘boost::asio::io_context&’
这是侦听器:
ImageServerListener::ImageServerListener(boost::asio::io_context& io)
{
_acceptor = new boost::asio::ip::tcp::acceptor(io,boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),sConfig.net.imageServerPort));
StartAccept();
}
ImageServerListener::~ImageServerListener()
{
delete _acceptor;
}
void ImageServerListener::StartAccept()
{
std::shared_ptr<ImageServerConnection> connection = ImageServerConnection::create(_acceptor->get_executor().context());
_acceptor->async_accept(connection->socket(),std::bind(&ImageServerListener::HandleAccept,this,connection));
}
void ImageServerListener::HandleAccept(std::shared_ptr<ImageServerConnection> connection)
{
connection->Process();
StartAccept();
}
为了返回io_context而不是execute_context,必须进行哪些更改?
解决方法
您将要专注于执行者而不是上下文。
传授执行者很便宜,可以复制,而不是上下文。
此外,它抽象出(多态性)执行器附加到的执行上下文的类型,因此您无需打扰。
但是,执行程序的 static 类型不是固定的。这意味着接受一个典型的方法是通过模板参数:
struct MyThing {
template <typename Executor>
explicit MyThing(Executor ex)
: m_socket(ex)
{ }
void do_stuff(std::string caption) {
post(m_socket.get_executor(),[=] { std::cout << ("Doing stuff " + caption + "\n") << std::flush; });
}
// ...
private:
tcp::socket m_socket;
};
现在,您可以通过多种方式使用它而无需更改:
int main() {
boost::asio::thread_pool pool;
MyThing a(pool.get_executor());
MyThing b(make_strand(pool));
a.do_stuff("Pool a");
b.do_stuff("Pool b");
boost::asio::io_context ioc;
MyThing c(ioc.get_executor());
MyThing d(make_strand(ioc));
c.do_stuff("IO c");
d.do_stuff("IO d");
pool.join();
ioc.run();
}
哪个会打印类似
Doing stuff Pool a
Doing stuff Pool b
Doing stuff IO c
Doing stuff IO d
类型擦除
您可能已经猜到了,m_socket
内部有一种类型擦除,用于存储执行程序。如果您想这样做,可以使用
boost::asio::any_io_executor ex;
ex = m_socket.get_executor();