60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#include "session.h"
|
|
#include <iostream>
|
|
#include <boost/asio/spawn.hpp>
|
|
#include <thread>
|
|
|
|
session::session(boost::asio::io_service &io_service, threadpool* pool): _socket(io_service), _pool(pool)
|
|
{
|
|
}
|
|
|
|
void session::start()
|
|
{
|
|
boost::asio::spawn(_socket.get_io_service(), [this](boost::asio::yield_context yield){
|
|
try
|
|
{
|
|
for (;;) {
|
|
std::size_t bytes = _socket.async_read_some(boost::asio::buffer(_data, 128), yield);
|
|
|
|
std::cout << "before async call" << std::endl;
|
|
int n = async_call<int>([this] {
|
|
std::cout << "async call start waiting" << std::endl;
|
|
std::this_thread::sleep_for(std::chrono::seconds(15));
|
|
std::cout << "async call ends waiting" << std::endl;
|
|
int n = std::atoi(_data);
|
|
return n * n;
|
|
}, yield);
|
|
|
|
std::cout << "before write: " << n << std::endl;
|
|
_socket.async_write_some(boost::asio::buffer(std::to_string(n) + "\n"), yield);
|
|
std::cout << "after write" << std::endl;
|
|
}
|
|
}
|
|
catch (boost::system::system_error& ex)
|
|
{
|
|
std::cout << "exception: " << ex.code().value() << " - " << ex.what() << std::endl;
|
|
|
|
if(ex.code().value() == 2) // end of file
|
|
{
|
|
//FIXME: Удалять сессию должен сервер
|
|
std::cout << "deleting session" << std::endl;
|
|
delete this;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
boost::asio::ip::tcp::socket& session::socket()
|
|
{
|
|
return _socket;
|
|
}
|
|
|
|
void session::stop()
|
|
{
|
|
_socket.close();
|
|
}
|
|
|
|
session::~session()
|
|
{
|
|
std::cout << "session destructor called" << std::endl;
|
|
}
|