提升asio和协同程序2示例
boost asio and coroutine2 example
在阅读coroutine2
的文档时,我发现了一段很好的代码片段,展示了如何将其与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
的最小客户端/服务器实现,如上面的示例所示?
AFAIK boost.asio仅支持boost.coroutine,不支持boost-coroutine2
这里给出了一个使用协程的基于Boost.Asio的服务器示例。
Boost.Coroutine文档中显示的示例缺少boost::asio::spawn
用于创建可以作为异步处理程序传递的yield_context
的部分。
通过在<boost/asio/spawn.hpp>
中遵循#include
链,它似乎只包括Boost.Coroutine v1。
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 适用于 WebView2 旧版本的示例应用程序
- 有人可以在下面的示例 C++ 程序中解释字符数组 KCHAR1 的奇怪行为吗?
- [[可能]]和[[不太可能]]影响程序汇编的简单示例?
- 多线程 gtkmm 应用程序最简单的示例
- 从 ParaView 构建自定义 Qt 应用程序示例时出错
- 示例程序崩溃
- Qt在安装Visual Studio 2019和虚幻引擎后无法运行示例应用程序
- 如何从示例 C 程序发送 HTTP 发布请求
- 尝试从Octave的独立程序示例中编译示例代码,在第一行出现段错误
- OpenGL ES模型加载程序示例
- SDL程序示例给出了一个空窗口
- Android JNI和iOS中封装的C/C++应用程序示例
- 如何使用 Visual Studio 2012 生成 Windows 7 凭据提供程序示例
- C++ 在同一台计算机中使用多个终端的多线程程序示例
- 微软虚拟音频设备驱动程序示例(MSVAD)只创建44字节的文件
- ActiveQt Com应用程序示例- Com服务器未在Windows注册表中注册(Qt4.7.4)
- Linux编程接口中的信号处理程序示例
- 提升asio和协同程序2示例
- c++中测试套接字库的线程应用程序示例