38 lines
1017 B
C++
38 lines
1017 B
C++
#include "session.h"
|
|
|
|
#include <iostream>
|
|
#include <boost/asio/yield.hpp>
|
|
|
|
session::session(boost::asio::io_service &io_service): boost::asio::coroutine(), _socket(io_service)
|
|
{
|
|
}
|
|
|
|
void session::start(boost::system::error_code ec, std::size_t bytes_transferred)
|
|
{
|
|
if(!ec)
|
|
{
|
|
reenter(this) for(;;) // псевдолинейный код
|
|
{
|
|
std::memset(_data, 0, 128);
|
|
yield _socket.async_read_some(boost::asio::buffer(_data, 128), std::bind(&session::start, this, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
yield _fut = call_async<int>([this]{
|
|
int n = std::atoi(_data);
|
|
return n*n;
|
|
});
|
|
|
|
yield {
|
|
std::string res = std::to_string(_fut.get()) + "\n";
|
|
_socket.async_write_some(boost::asio::buffer(res), std::bind(&session::start, this, std::placeholders::_1, std::placeholders::_2));
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::cout << "error: " << ec.message() << std::endl;
|
|
delete this;
|
|
}
|
|
}
|
|
|
|
#include <boost/asio/unyield.hpp>
|