-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionHandler.cpp
More file actions
49 lines (43 loc) · 1.41 KB
/
Copy pathConnectionHandler.cpp
File metadata and controls
49 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "ConnectionHandler.h"
#include <iostream>
#include <boost/log/trivial.hpp>
ConnectionHandler::ConnectionHandler(boost::asio::io_service& io_service): sock_(io_service)
{
}
ConnectionHandler::Pointer ConnectionHandler::create(boost::asio::io_service& io_service)
{
return Pointer(new ConnectionHandler(io_service));
}
void ConnectionHandler::start()
{
BOOST_LOG_TRIVIAL(debug) << __PRETTY_FUNCTION__;
sock_.async_receive(
boost::asio::buffer(data_),
boost::bind(&ConnectionHandler::handleRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void ConnectionHandler::handleRead(const boost::system::error_code& err, size_t bytes_transferred)
{
BOOST_LOG_TRIVIAL(debug) << __PRETTY_FUNCTION__;
if (!err)
{
BOOST_LOG_TRIVIAL(info) << "data: " << data_.data();
// TODO remove this
if (strcmp(data_.data(),"bye"))
{
sock_.close();
}
}
else if ((boost::asio::error::eof == err) ||
(boost::asio::error::connection_reset == err))
{
BOOST_LOG_TRIVIAL(info) << "ConnectionHandler: Client disconnected: " << err.message();
}
else
{
BOOST_LOG_TRIVIAL(error) << "ConnectionHandler error: " << err.message();
sock_.close();
}
}