1.00.15
C++ Simulated Airline Inventory Management System Library
Toggle main menu visibility
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
10
#include <
airinv/server/RequestHandler.hpp
>
11
#include <
airinv/server/Connection.hpp
>
12
13
namespace
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
// //////////////////////////////////////////////////////////////////////
27
void
Connection::start
() {
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.hpp
RequestHandler.hpp
AIRINV::Connection::Connection
Connection(boost::asio::io_service &, RequestHandler &)
Definition
Connection.cpp:16
AIRINV::Connection::start
void start()
Definition
Connection.cpp:27
AIRINV::Connection::socket
boost::asio::ip::tcp::socket & socket()
Definition
Connection.cpp:22
AIRINV::RequestHandler
The common handler for all incoming requests.
Definition
RequestHandler.hpp:28
AIRINV::RequestHandler::handleRequest
bool handleRequest(Request &, Reply &) const
Definition
RequestHandler.cpp:26
AIRINV
Definition
AIRINV_Master_Service.hpp:38
AIRINV::Reply::to_buffers
std::vector< boost::asio::const_buffer > to_buffers()
Definition
Reply.cpp:15
AIRINV::Request::_flightDetails
std::string _flightDetails
Definition
Request.hpp:29
Generated on
for AirInv by
1.17.0