Repository navigation
Expand file tree
/
Copy pathTcpServer.cpp
More file actions
153 lines (143 loc) · 5.08 KB
/
Copy pathTcpServer.cpp
File metadata and controls
153 lines (143 loc) · 5.08 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
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
#include "TcpServer.h"
#include "Acceptor.h"
#include "Channel.h"
#include "Connection.h"
#include "ThreadPool.h"
#include <cstdio>
#include <functional>
#include <mutex>
TcpServer::TcpServer(const std::string &ip, const uint16_t port, int threadnum)
: mainloop_(new EventLoop(true)),
acceptor_(mainloop_.get(), ip, port), threadnum_(threadnum), threadpool_(threadnum, "IO")
{
// 设置epoll_wait()超时的回调函数
mainloop_->setepolltimeoutcallback(std::bind(&TcpServer::epolltimeout, this, std::placeholders::_1));
// 设置处理新客户端连接请求的回调函数
acceptor_.setnewconnenctioncb(std::bind(&TcpServer::newconnection, this, std::placeholders::_1));
// 创建从事件循环
for (int i = 0; i < threadnum_; i++)
{
subloops_.emplace_back(new EventLoop(false, 5, 10)); // 创建从事件循环 存入subloops_容器中
subloops_[i]->setepolltimeoutcallback(std::bind(&TcpServer::epolltimeout, this, std::placeholders::_1));
subloops_[i]->settimercallback(std::bind(&TcpServer::removeconn, this, std::placeholders::_1));
threadpool_.addtask(std::bind(&EventLoop::run, subloops_[i].get())); // 在线程池在运行从事件循环
}
}
TcpServer::~TcpServer()
{
}
void TcpServer::start() // 运行事件循环
{
mainloop_->run();
}
void TcpServer::stop() // 停止IO线程和事件循环
{
mainloop_->stop(); // 停止主事件循环
printf("mainloop_ has stop.\n");
// 停止从事件循环
for (int i = 0; i < threadnum_; i++)
{
subloops_[i]->stop();
}
printf("subloop has stop.\n");
// 停止IO线程
threadpool_.stop();
printf("IO thread has stop.\n");
}
void TcpServer::newconnection(std::unique_ptr<Socket> clientsock) // 处理新客户端连接请求 在Acceptor类中回调此函数
{
// 把新建的conn分配给从事件循环
int fd = clientsock->fd();
EventLoop *ioloop = subloops_[fd % threadnum_].get();
spConnection conn(new Connection(ioloop, std::move(clientsock)));
conn->setclosecallback(std::bind(&TcpServer::closeconnection, this, std::placeholders::_1));
conn->seterrorcallback(std::bind(&TcpServer::errorconnection, this, std::placeholders::_1));
conn->setonmessagecallback(std::bind(&TcpServer::onmessage, this, std::placeholders::_1, std::placeholders::_2));
conn->setsendcompletecallback(std::bind(&TcpServer::sendcomplete, this, std::placeholders::_1));
{
std::lock_guard<std::mutex> gd(mmutex_);
conns_[fd] = conn; // 把conn存放到TcpServer的map容器中
}
ioloop->newconnection(conn); // 把conn存放到EventLoop的map容器中
// 交给IO线程注册读事件 保证shared_ptr已经构造完成 且channel只被IO线程操作
ioloop->queueinloop(std::bind(&Connection::start, conn));
if (newconnectioncb_)
{
newconnectioncb_(conn); // 回调EchoServer::HandleNewConnection()
}
}
void TcpServer::closeconnection(spConnection conn) // 关闭客户端的连接 在Connection类中回调此函数
{
if (closeconnectioncb_)
{
closeconnectioncb_(conn); // 回调EchoServer::HandleClose()
}
{
std::lock_guard<std::mutex> gd(mmutex_);
conns_.erase(conn->fd()); // 从map中删除conn
}
}
void TcpServer::errorconnection(spConnection conn) // 客户端的连接错误 在Connection类中回调此函数
{
if (errorconnectioncb_)
{
errorconnectioncb_(conn); // 回调EchoServer::HandleError()
}
{
std::lock_guard<std::mutex> gd(mmutex_);
conns_.erase(conn->fd()); // 从map中删除conn
}
}
void TcpServer::onmessage(spConnection conn, std::string &message) // 处理客户端的请求报文 在Connection类中回调此函数
{
// todo
if (onmessagecb_)
{
onmessagecb_(conn, message); // 回调EchoServer::HandleMessage()
}
}
void TcpServer::sendcomplete(spConnection conn) // 数据发送完成后 在Connection类中回调此函数
{
if (sendcompletecb_)
{
sendcompletecb_(conn); // 回调EchoServer::HandleSendCompelete()
}
}
void TcpServer::epolltimeout(EventLoop *loop) // epoll_wait()超时 在EventLoop类中回调此函数
{
if (timeoutcb_)
{
timeoutcb_(loop); // 回调EchoServer::HandleTimeOut()
}
}
void TcpServer::setnewconnenctioncb(std::function<void(spConnection)> fn)
{
newconnectioncb_ = fn;
}
void TcpServer::setcloseconnectioncb(std::function<void(spConnection)> fn)
{
closeconnectioncb_ = fn;
}
void TcpServer::seterrorconnectioncb(std::function<void(spConnection)> fn)
{
errorconnectioncb_ = fn;
}
void TcpServer::setonmessagecb(std::function<void(spConnection, std::string &message)> fn)
{
onmessagecb_ = fn;
}
void TcpServer::setsendcompeletecb(std::function<void(spConnection)> fn)
{
sendcompletecb_ = fn;
}
void TcpServer::settimeoutcb(std::function<void(EventLoop *)> fn)
{
timeoutcb_ = fn;
}
void TcpServer::removeconn(int fd) // 删除conns_中的Connection对象 在EventLoop::handletimer()中将回调此函数
{
{
std::lock_guard<std::mutex> gd(mmutex_);
conns_.erase(fd); // 从map中删除conn
}
}