-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpserver.cpp
executable file
·139 lines (121 loc) · 3.17 KB
/
tcpserver.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
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
#include "tcpserver.h"
#include <iostream>
namespace cpptcp
{
TcpServer::TcpServer(uint16_t port)
: acceptor_(ioctx_, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
{
}
TcpServer::~TcpServer()
{
ioctx_.stop();
}
void TcpServer::start()
{
accept();
while (1) {
try {
ioctx_.run();
break;
} catch (const std::exception &e) {
//std::cerr << "io_context run occur exception: " << e.what() << std::endl;
}
}
}
void TcpServer::destroy(std::shared_ptr<Session> pSession)
{
sessions_.erase(pSession);
}
void TcpServer::accept()
{
std::shared_ptr<Session> pSession = std::make_shared<Session>(*this, ioctx_);
if (!pSession) {
return;
}
auto callback = [pSession, this](asio::error_code ec){
if (!ec) {
sessions_.insert(pSession);
pSession->set_keepalive();
onConnect(pSession);
pSession->async_recv_msg();
}
accept();
};
acceptor_.async_accept(pSession->getSocket(), callback);
}
Session::Session(TcpServer &srv, asio::io_context &ioctx)
: server_(srv), socket_(ioctx)
{
}
Session::~Session()
{
close();
}
void Session::close()
{
if (socket_.is_open()) {
asio::error_code ec;
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket_.close(ec);
server_.onClose(ip_);
}
}
const std::string& Session::address()
{
if (ip_.empty() && socket_.is_open()) {
asio::error_code ec;
auto endpoint = socket_.remote_endpoint(ec);
if (!ec) {
ip_ = endpoint.address().to_string();
}
}
return ip_;
}
asio::ip::tcp::socket& Session::getSocket()
{
return socket_;
}
void Session::async_send_msg(const std::string &message)
{
auto self(this->shared_from_this());
auto callback = [self, this](asio::error_code ec, std::size_t n){
if (!ec) {
server_.onSend(self);
} else {
server_.destroy(self);
}
};
asio::async_write(socket_, asio::buffer(message), callback);
}
void Session::Session::async_recv_msg()
{
std::shared_ptr<std::string> message = std::make_shared<std::string>();
message->resize(kMaxMessageSize);
auto self(this->shared_from_this());
auto callback = [self, this, message](asio::error_code ec, std::size_t n){
if (!ec) {
message->resize(n);
server_.onMessage(*message, self);
async_recv_msg();
} else {
server_.destroy(self);
}
};
asio::async_read(socket_, asio::buffer(*message), asio::transfer_at_least(1), callback);
}
void Session::set_keepalive()
{
#ifdef __linux__
auto handle = socket_.native_handle();
int keepAlive = 1; // 开启keepalive属性
int keepIdle = 60; // 如该连接在60秒内没有任何数据往来,则进行探测
int keepInterval = 3; //探测时发包的时间间隔为3秒
int keepCount = 3; //探测尝试的次数.如果第1次探测包就收到响应了,则后2次的不再发.
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
setsockopt(handle, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));
setsockopt(handle, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));
setsockopt(handle, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));
#endif
}
}