-
Notifications
You must be signed in to change notification settings - Fork 85
/
connection.hpp
157 lines (135 loc) · 2.76 KB
/
connection.hpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp>
#include "common.h"
using boost::asio::ip::tcp;
class connection : public std::enable_shared_from_this<connection>, private boost::noncopyable
{
public:
connection(boost::asio::io_service& io_service, std::size_t timeout_milli) : socket_(io_service), message_{ boost::asio::buffer(head_),
boost::asio::buffer(data_) }, timer_(io_service), timeout_milli_(timeout_milli)
{
}
void start()
{
read_head();
}
tcp::socket& socket()
{
return socket_;
}
void read_head()
{
reset_timer();
auto self(this->shared_from_this());
boost::asio::async_read(socket_, boost::asio::buffer(head_), [this, self](boost::system::error_code ec, std::size_t length)
{
if (!socket_.is_open())
{
cancel_timer();
return;
}
if (!ec)
{
const int body_len = *(int*)head_;
if (body_len > 0 && body_len< 65536)
{
read_body(body_len);
return;
}
if (body_len == 0) //nobody, just head.
{
read_head();
}
else
{
//log //invalid body len
cancel_timer();
}
}
else
{
//log
cancel_timer();
}
});
}
void read_body(std::size_t size)
{
auto self(this->shared_from_this());
boost::asio::async_read(socket_, boost::asio::buffer(data_, size), [this, self](boost::system::error_code ec, std::size_t length)
{
cancel_timer();
if (!socket_.is_open())
return;
if (!ec)
{
router& _router = router::get();
_router.route(data_, length, self);
}
else
{
//log
}
});
}
//add timeout later
void response(const char* json_str)
{
auto self(this->shared_from_this());
int len = strlen(json_str);
message_[0] = boost::asio::buffer(&len, 4);
message_[1] = boost::asio::buffer((char*)json_str, len);
boost::asio::async_write(socket_, message_, [this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
read_head();
}
else
{
//log
}
});
}
void reset_timer()
{
if (timeout_milli_ == 0)
return;
auto self(this->shared_from_this());
timer_.expires_from_now(boost::posix_time::milliseconds(timeout_milli_));
timer_.async_wait([this, self](const boost::system::error_code& ec)
{
if (!socket_.is_open())
{
return;
}
if (ec)
{
//log
return;
}
std::cout << "timeout" << std::endl;
close();
});
}
void cancel_timer()
{
if (timeout_milli_ == 0)
return;
timer_.cancel();
}
void close()
{
boost::system::error_code ignored_ec;
socket_.close(ignored_ec);
}
tcp::socket socket_;
char head_[4];
char data_[MAX_BUF_LEN];
std::array<boost::asio::mutable_buffer, 2> message_;
boost::asio::deadline_timer timer_;
std::size_t timeout_milli_;
};