37 lines
885 B
C++
37 lines
885 B
C++
#ifndef _SESSION_H_
|
|
#define _SESSION_H_
|
|
|
|
#include "threadpool.h"
|
|
#include <boost/asio.hpp>
|
|
|
|
class session
|
|
{
|
|
private:
|
|
typedef boost::asio::ip::tcp tcp;
|
|
|
|
private:
|
|
tcp::socket _socket;
|
|
char _data[128];
|
|
threadpool _pool;
|
|
|
|
public:
|
|
session() = delete;
|
|
session(session&) = delete;
|
|
session(boost::asio::io_service& io_service);
|
|
void start();
|
|
tcp::socket& socket();
|
|
|
|
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();
|
|
}
|
|
};
|
|
|
|
#endif // _SESSION_H_
|