在阅读coroutine2的文档时,我发现了一个很好的
snippet代码,展示了如何在asio中使用它
这里的参考是文档中的代码:
void session(boost::asio::io_service& io_service){ // construct TCP-socket from io_service boost::asio::ip::tcp::socket socket(io_service); try{ for(;;){ // local data-buffer char data[max_length]; boost::system::error_code ec; // read asynchronous data from socket // execution context will be suspended until // some bytes are read from socket std::size_t length=socket.async_read_some( boost::asio::buffer(data),boost::asio::yield[ec]); if (ec==boost::asio::error::eof) break; //connection closed cleanly by peer else if(ec) throw boost::system::system_error(ec); //some other error // write some bytes asynchronously boost::asio::async_write( socket,boost::asio::buffer(data,length),boost::asio::yield[ec]); if (ec==boost::asio::error::eof) break; //connection closed cleanly by peer else if(ec) throw boost::system::system_error(ec); //some other error } } catch(std::exception const& e){ std::cerr<<"Exception: "<<e.what()<<"\n"; } }
但是我找不到关于asio文档的工作示例,并且尝试在coliru上编译这个片段给出了与yield相关的编译器错误
您是否了解使用coroutine2的最小客户端/服务器实现,如上例所示?