forked from shuai132/asio_net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_channel_t.hpp
148 lines (127 loc) · 4.1 KB
/
tcp_channel_t.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
#pragma once
#include <utility>
#include "asio.hpp"
#include "log.h"
#include "message.hpp"
#include "noncopyable.hpp"
#include "type.h"
namespace asio_net {
namespace detail {
template <typename T>
class tcp_channel_t : private noncopyable {
using socket = typename T::socket;
using endpoint = typename T::endpoint;
public:
tcp_channel_t(socket& socket, const PackOption& pack_option, const uint32_t& max_body_size)
: socket_(socket), pack_option_(pack_option), max_body_size_(max_body_size) {
asio_net_LOGD("tcp_channel: %p", this);
if (pack_option == PackOption::DISABLE) {
max_body_size_ = std::min<uint32_t>(1024, max_body_size_);
max_body_size_ = std::max<uint32_t>(32, max_body_size_);
}
}
~tcp_channel_t() {
asio_net_LOGD("~tcp_channel: %p", this);
}
void send(std::string msg) {
do_write(std::move(msg));
}
void close() {
asio::post(socket_.get_executor(), [this] {
do_close();
});
}
bool is_open() const {
return socket_.is_open();
}
endpoint local_endpoint() {
return socket_.local_endpoint();
}
endpoint remote_endpoint() {
return socket_.remote_endpoint();
}
public:
std::function<void()> on_close;
std::function<void(std::string)> on_data;
protected:
void do_read_start(std::shared_ptr<tcp_channel_t> self = nullptr) {
if (pack_option_ == PackOption::ENABLE) {
do_read_header(std::move(self));
} else {
// init read_msg_.body as buffer
read_msg_.body.resize(max_body_size_);
do_read_data(std::move(self));
}
}
private:
void do_read_header(std::shared_ptr<tcp_channel_t> self) {
asio::async_read(socket_, asio::buffer(&read_msg_.length, sizeof(read_msg_.length)),
[this, self = std::move(self)](const std::error_code& ec, std::size_t /*length*/) mutable {
if (ec) {
do_close();
}
if (read_msg_.length <= max_body_size_) {
do_read_body(std::move(self));
} else {
asio_net_LOGE("read: body size=%u > max_body_size=%u", read_msg_.length, max_body_size_);
do_close();
}
});
}
void do_read_body(std::shared_ptr<tcp_channel_t> self) {
read_msg_.body.resize(read_msg_.length);
asio::async_read(socket_, asio::buffer(read_msg_.body), [this, self = std::move(self)](const std::error_code& ec, std::size_t) mutable {
if (!ec) {
auto msg = std::move(read_msg_.body);
read_msg_.clear();
if (on_data) on_data(std::move(msg));
do_read_header(std::move(self));
} else {
do_close();
}
});
}
void do_read_data(std::shared_ptr<tcp_channel_t> self) {
auto& readBuffer = read_msg_.body;
socket_.async_read_some(asio::buffer(readBuffer), [this, self = std::move(self)](const std::error_code& ec, std::size_t length) mutable {
if (!ec) {
if (on_data) on_data(std::string(read_msg_.body.data(), length));
do_read_data(std::move(self));
} else {
do_close();
}
});
}
void do_write(std::string msg) {
auto keeper = std::make_unique<detail::message>(std::move(msg));
if (pack_option_ == PackOption::ENABLE && keeper->length > max_body_size_) {
asio_net_LOGE("write: body size=%u > max_body_size=%u", keeper->length, max_body_size_);
do_close();
}
auto buffer = {
asio::buffer(&keeper->length, sizeof(keeper->length)),
asio::buffer(keeper->body),
};
asio::async_write(socket_, buffer, [this, keeper = std::move(keeper)](const std::error_code& ec, std::size_t /*length*/) {
if (ec) {
do_close();
}
});
}
void do_close() {
if (!is_open()) return;
try {
socket_.close();
} catch (std::exception& e) {
asio_net_LOGW("do_close: %s", e.what());
}
if (on_close) on_close();
}
private:
socket& socket_;
const PackOption& pack_option_;
uint32_t max_body_size_;
detail::message read_msg_;
};
} // namespace detail
} // namespace asio_net