cpputil/test/server/session.h

41 lines
950 B
C++

#ifndef _SESSION_H_
#define _SESSION_H_
#include "threadpool.h"
#include <boost/asio.hpp>
#include <iostream>
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, threadpool* pool);
~session();
void start();
void stop();
tcp::socket& socket();
template <typename R, typename CompletionToken> R async_call(std::function<R()> func, CompletionToken&& token)
{
using namespace boost::asio;
BOOST_ASIO_HANDLER_TYPE(CompletionToken, void(R)) handler(std::forward<CompletionToken>(token));
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_