38 lines
881 B
C++
38 lines
881 B
C++
#ifndef _SESSION_H_
|
|
#define _SESSION_H_
|
|
|
|
#include "threadpool.h"
|
|
#include <boost/asio.hpp>
|
|
|
|
class session: boost::asio::coroutine
|
|
{
|
|
private:
|
|
typedef boost::asio::ip::tcp tcp;
|
|
friend class server;
|
|
|
|
private:
|
|
tcp::socket _socket;
|
|
char _data[128];
|
|
threadpool _pool;
|
|
|
|
// "local" variables
|
|
std::future<int> _fut;
|
|
|
|
public:
|
|
session() = delete;
|
|
session(session&) = delete;
|
|
session(boost::asio::io_service& io_service);
|
|
void start(boost::system::error_code ec = boost::system::error_code(), std::size_t bytes_transferred = 0);
|
|
|
|
template <typename R> std::future<R> call_async(std::function<R()> func)
|
|
{
|
|
return _pool.add_task<R>([this, &func]{
|
|
R result = func();
|
|
_socket.get_io_service().dispatch(std::bind(&session::start, this, boost::system::error_code(), 0));
|
|
return result;
|
|
});
|
|
}
|
|
|
|
};
|
|
|
|
#endif // _SESSION_H_
|