$treeview $search $mathjax
00001 // ////////////////////////////////////////////////////////////////////// 00002 // Import section 00003 // ////////////////////////////////////////////////////////////////////// 00004 // STL 00005 #include <cassert> 00006 #include <vector> 00007 // Boost 00008 #include <boost/bind.hpp> 00009 // AirInv 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 // If an error occurs then no new asynchronous operations are 00062 // started. This means that all shared_ptr references to the 00063 // connection object will disappear and the object will be 00064 // destroyed automatically after this handler returns. The 00065 // connection class's destructor closes the socket. 00066 } 00067 00068 // ////////////////////////////////////////////////////////////////////// 00069 void Connection::handleWrite (const boost::system::error_code& iErrorCode) { 00070 00071 if (!iErrorCode) { 00072 // Initiate graceful connection closure. 00073 boost::system::error_code ignored_ec; 00074 _socket.shutdown (boost::asio::ip::tcp::socket::shutdown_both, 00075 ignored_ec); 00076 } 00077 00078 // No new asynchronous operations are started. This means that all 00079 // shared_ptr references to the connection object will disappear 00080 // and the object will be destroyed automatically after this 00081 // handler returns. The connection class's destructor closes the 00082 // socket. 00083 } 00084 00085 }