-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_connection.cpp
More file actions
48 lines (39 loc) · 1.28 KB
/
tcp_connection.cpp
File metadata and controls
48 lines (39 loc) · 1.28 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
#include "tcp_connection.h"
#include "http_header.h"
using namespace Illuminati;
tcp_connection::pointer tcp_connection::create(
boost::asio::io_service &io_service) {
static int count;
count++;
LOG("Create socket #" << count);
return std::shared_ptr<tcp_connection>(
new tcp_connection(io_service), [k = count](auto p) {
LOG("Del socket #" << k);
delete p;
});
}
boost::asio::ip::tcp::socket &tcp_connection::socket() { return socket_; }
void tcp_connection::start() {
boost::asio::async_read_until(
socket_, m_request,
'\r', [ptr = shared_from_this()](const auto &e, auto) {
ptr->handle_read(e);
});
}
tcp_connection::~tcp_connection() { LOG("Del connection with"); }
tcp_connection::tcp_connection(boost::asio::io_service &io_service)
: socket_(io_service) {}
void tcp_connection::handle_write(const boost::system::error_code &) {}
void tcp_connection::handle_read(const boost::system::error_code &e) {
if (!e) {
http_header header;
header << m_request;
header >> m_response;
boost::asio::async_write(
socket_, m_response, [ptr = shared_from_this()](auto &e, auto) {
ptr->handle_write(e);
});
} else {
LOG(e.message());
}
}