2016-08-17 193 views
5

在閱讀的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的最小客戶端/服務器實現?

回答

5

AFAIK Boost.Asio的僅支持boost.coroutine,不boost.coroutine2

+0

然後,coroutine2文檔中的示例會產生誤導。這太糟糕了,支持它會很棒。 –

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/14285746) – kiamlaluno

+3

@kiamlaluno對我來說,它是回答這個問題的 – Danh

3

一個例子Boost.Asio的基於使用協同程序服務器給出here

Boost.Coroutine文檔中顯示的示例缺少使用boost::asio::spawn創建可作爲異步處理程序傳遞的yield_context的部分。

通過關注<boost/asio/spawn.hpp>中的#include鏈,它似乎只包含Boost.Coroutine v1。