本文整理汇总了C++中socket_ptr::async_read_some方法的典型用法代码示例。如果您正苦于以下问题:C++ socket_ptr::async_read_some方法的具体用法?C++ socket_ptr::async_read_some怎么用?C++ socket_ptr::async_read_some使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket_ptr
的用法示例。
在下文中一共展示了socket_ptr::async_read_some方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: session
void session( socket_ptr sock) {
boost::fibers::fiber::id id = boost::this_fiber::get_id();
std::cout << "fiber " << id << " : echo-handler started" << std::endl;
try {
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
std::cerr << "fiber " << id << " : error occured : " << ec.message() << std::endl;
return;
}
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::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 ( boost::fibers::fiber_interrupted const&) {
std::cout << "fiber " << id << " : interrupted" << std::endl;
} catch ( std::exception const& ex) {
std::cout << "fiber " << id << " : catched exception : " << ex.what() << std::endl;
}
std::cout << "fiber " << id << " terminates" << std::endl;
}
示例2: session
void session( socket_ptr sock) {
try {
std::cout << "handler request" << std::endl;
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::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
}
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::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 in fiber: " << e.what() << "\n";
}
}
示例3: session
/*****************************************************************************
* fiber function per server connection
*****************************************************************************/
void session( socket_ptr sock) {
try {
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::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
}
print( tag(), ": handled: ", std::string(data, length));
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::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
}
}
print( tag(), ": connection closed");
} catch ( std::exception const& ex) {
print( tag(), ": caught exception : ", ex.what());
}
}