在閱讀的coroutine2
的文檔,我發現的代碼演示瞭如何使用asio
升壓ASIO和coroutine2例如
用它來這裏引用一個不錯的snippet是從文檔代碼:
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
的最小客戶端/服務器實現?
然後,coroutine2文檔中的示例會產生誤導。這太糟糕了,支持它會很棒。 –
這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/14285746) – kiamlaluno
@kiamlaluno對我來說,它是回答這個問題的 – Danh