80 lines
1.7 KiB
C++
80 lines
1.7 KiB
C++
|
|
#include <vector>
|
|
#include <boost/bind.hpp>
|
|
|
|
#include "RequestHandler.hpp"
|
|
|
|
#include "ConnectionManager.hpp"
|
|
#include "Connection.hpp"
|
|
|
|
namespace Remote {
|
|
namespace Server {
|
|
|
|
Connection::Connection(boost::asio::io_service& ioService, Connection_manager& manager, RequestHandler& handler)
|
|
: _socket(ioService),
|
|
_connectionManager(manager),
|
|
request_handler_(handler)
|
|
{
|
|
|
|
}
|
|
|
|
boost::asio::ip::tcp::socket&
|
|
Connection::socket()
|
|
{
|
|
return _socket;
|
|
}
|
|
|
|
void
|
|
Connection::start()
|
|
{
|
|
_socket.async_read_some(boost::asio::buffer(_headerBuffer),
|
|
boost::bind(&Connection::handleRead, shared_from_this(),
|
|
boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred));
|
|
}
|
|
|
|
void
|
|
Connection::stop()
|
|
{
|
|
_socket.close();
|
|
}
|
|
|
|
void
|
|
Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred)
|
|
{
|
|
if (!error)
|
|
{
|
|
/* boost::asio::async_write(_socket, reply_.to_buffers(),
|
|
boost::bind(&Connection::handleWrite, shared_from_this(),
|
|
boost::asio::placeholders::error));*/
|
|
|
|
|
|
start();
|
|
}
|
|
else if (error != boost::asio::error::operation_aborted)
|
|
{
|
|
std::cerr << "Connection::handleRead: " << error.message() << std::endl;
|
|
_connectionManager.stop(shared_from_this());
|
|
}
|
|
}
|
|
|
|
void Connection::handleWrite(const boost::system::error_code& e)
|
|
{
|
|
if (!e)
|
|
{
|
|
// Initiate graceful Connection closure.
|
|
boost::system::error_code ignored_ec;
|
|
_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
|
|
}
|
|
|
|
if (e != boost::asio::error::operation_aborted)
|
|
{
|
|
std::cerr << "Connection::handleWrite: " << error.message() << std::endl;
|
|
_connectionManager.stop(shared_from_this());
|
|
}
|
|
}
|
|
|
|
} // namespace Server
|
|
|
|
} // namespace Remote
|