Go to the documentation of this file.00001
00002
00003
00004
00005 #include <cassert>
00006 #include <vector>
00007
00008 #include <boost/bind.hpp>
00009
00010 #include <airinv/server/RequestHandler.hpp>
00011 #include <airinv/server/Connection.hpp>
00012
00013 namespace AIRINV {
00014
00015
00016 Connection::Connection (boost::asio::io_service& ioService,
00017 RequestHandler& ioHandler)
00018 : _strand (ioService), _socket (ioService), _requestHandler (ioHandler) {
00019 }
00020
00021
00022 boost::asio::ip::tcp::socket& Connection::socket() {
00023 return _socket;
00024 }
00025
00026
00027 void Connection::start() {
00028
00029 _socket.async_read_some (boost::asio::buffer (_buffer),
00030 _strand.wrap (boost::bind (&Connection::handleRead,
00031 shared_from_this(),
00032 boost::asio::placeholders::error,
00033 boost::asio::placeholders::bytes_transferred)));
00034 }
00035
00036
00037 void Connection::handleRead (const boost::system::error_code& iErrorCode,
00038 std::size_t bytes_transferred) {
00039 if (!iErrorCode) {
00040 _request._flightDetails = _buffer.data();
00041 const bool hasBeenSuccessfull = _requestHandler.handleRequest (_request,
00042 _reply);
00043
00044 if (hasBeenSuccessfull == true) {
00045
00046 boost::asio::async_write (_socket, _reply.to_buffers(),
00047 _strand.wrap (boost::bind (&Connection::handleWrite,
00048 shared_from_this(),
00049 boost::asio::placeholders::error)));
00050
00051 } else {
00052
00053 boost::asio::async_write (_socket, _reply.to_buffers(),
00054 _strand.wrap (boost::bind (&Connection::handleWrite,
00055 shared_from_this(),
00056 boost::asio::placeholders::error)));
00057
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066 }
00067
00068
00069 void Connection::handleWrite (const boost::system::error_code& iErrorCode) {
00070
00071 if (!iErrorCode) {
00072
00073 boost::system::error_code ignored_ec;
00074 _socket.shutdown (boost::asio::ip::tcp::socket::shutdown_both,
00075 ignored_ec);
00076 }
00077
00078
00079
00080
00081
00082
00083 }
00084
00085 }