-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.cpp
49 lines (42 loc) · 1.12 KB
/
connection.cpp
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 "server.h"
#include <string>
#include <array>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <chrono>
using namespace std::literals::chrono_literals;
Connection::Connection(boost::asio::io_service& io_service): socket_(io_service)
{
}
void Connection::handleWrite(const boost::system::error_code& /*error*/,size_t /*bytes_transferred*/)
{
}
void Connection::handleRead(const boost::system::error_code& err,size_t bytes_transferred)
{
if(!err)
{
if(bytes_transferred==0)
{
std::cout<<"Transferred 0"<<std::endl;
}
else
{
std::cout<<readBuffer_<<std::endl;;
}
start();
}
}
tcp::socket& Connection::socket()
{
return socket_;
}
void Connection::start()
{
socket_.async_read_some(
boost::asio::buffer(readBuffer_, size_),
boost::bind(&Connection::handleRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}