AirInv Logo  1.00.12
C++ Simulated Airline Inventory Management System Library
Loading...
Searching...
No Matches
Connection.cpp
Go to the documentation of this file.
1// //////////////////////////////////////////////////////////////////////
2// Import section
3// //////////////////////////////////////////////////////////////////////
4// STL
5#include <cassert>
6#include <vector>
7// Boost
8#include <boost/bind.hpp>
9// AirInv
12
13namespace AIRINV {
14
15 // //////////////////////////////////////////////////////////////////////
16 Connection::Connection (boost::asio::io_service& ioService,
17 RequestHandler& ioHandler)
18 : _strand (ioService), _socket (ioService), _requestHandler (ioHandler) {
19 }
20
21 // //////////////////////////////////////////////////////////////////////
22 boost::asio::ip::tcp::socket& Connection::socket() {
23 return _socket;
24 }
25
26 // //////////////////////////////////////////////////////////////////////
28
29 _socket.async_read_some (boost::asio::buffer (_buffer),
30 _strand.wrap (boost::bind (&Connection::handleRead,
31 shared_from_this(),
32 boost::asio::placeholders::error,
33 boost::asio::placeholders::bytes_transferred)));
34 }
35
36 // //////////////////////////////////////////////////////////////////////
37 void Connection::handleRead (const boost::system::error_code& iErrorCode,
38 std::size_t bytes_transferred) {
39 if (!iErrorCode) {
40 _request._flightDetails = _buffer.data();
41 const bool hasBeenSuccessfull = _requestHandler.handleRequest (_request,
42 _reply);
43
44 if (hasBeenSuccessfull == true) {
45
46 boost::asio::async_write (_socket, _reply.to_buffers(),
47 _strand.wrap (boost::bind (&Connection::handleWrite,
48 shared_from_this(),
49 boost::asio::placeholders::error)));
50
51 } else {
52
53 boost::asio::async_write (_socket, _reply.to_buffers(),
54 _strand.wrap (boost::bind (&Connection::handleWrite,
55 shared_from_this(),
56 boost::asio::placeholders::error)));
57
58 }
59 }
60
61 // If an error occurs then no new asynchronous operations are
62 // started. This means that all shared_ptr references to the
63 // connection object will disappear and the object will be
64 // destroyed automatically after this handler returns. The
65 // connection class's destructor closes the socket.
66 }
67
68 // //////////////////////////////////////////////////////////////////////
69 void Connection::handleWrite (const boost::system::error_code& iErrorCode) {
70
71 if (!iErrorCode) {
72 // Initiate graceful connection closure.
73 boost::system::error_code ignored_ec;
74 _socket.shutdown (boost::asio::ip::tcp::socket::shutdown_both,
75 ignored_ec);
76 }
77
78 // No new asynchronous operations are started. This means that all
79 // shared_ptr references to the connection object will disappear
80 // and the object will be destroyed automatically after this
81 // handler returns. The connection class's destructor closes the
82 // socket.
83 }
84
85}
Connection(boost::asio::io_service &, RequestHandler &)
boost::asio::ip::tcp::socket & socket()
The common handler for all incoming requests.
bool handleRequest(Request &, Reply &) const
std::vector< boost::asio::const_buffer > to_buffers()
Definition Reply.cpp:15
std::string _flightDetails
Definition Request.hpp:29