cpputil/test/server/session.h

41 lines
980 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;
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();
template <typename R, typename CompletionToken> R async_call(std::function<R()> func, CompletionToken&& token)
{
typename boost::asio::handler_type<CompletionToken, void(R)>::type handler(std::forward<CompletionToken>(token));
boost::asio::async_result<decltype(handler)> result(handler);
_pool.add_task<void>([this, handler, &func]{
_socket.get_io_service().dispatch(std::bind(handler, func()));
});
return result.get();
}
boost::asio::ip::tcp::socket& socket();
};
#endif // _SESSION_H_