-
Notifications
You must be signed in to change notification settings - Fork 10
/
tcpserver.cpp
182 lines (159 loc) · 5.93 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <unistd.h>
#include <stdint.h>
#include "tcpserver.h"
#include "netutil.h"
const int TcpServer::kMaxConnectionCnt = 10000;
void TcpServer::ReadFromFdToBuffer(EventLoop & eventLoop, std::shared_ptr<Channel> ptChannel)
{
SimpleBuffer & inBuffer = ptChannel->GetInBuffer();
SimpleBuffer & outBuffer = ptChannel->GetOutBuffer();
int nRead = inBuffer.ReadFromFd(ptChannel->GetFd());
int readCallbackResult = 0;
if (0 < nRead)
{
DEBUGLOG("%s %s %d, read from fd=%d", __FILE__, __func__, __LINE__, ptChannel->GetFd());
if (m_readCallback)
{
readCallbackResult = m_readCallback(ptChannel);
}
if (0 < outBuffer.BufferSize())
{
DEBUGLOG("%s %s %d, %d bytes in the buffer of fd=%d need to be writen", __FILE__, __func__, __LINE__, outBuffer.BufferSize(), ptChannel->GetFd());
eventLoop.AddChannel(ptChannel, EPOLLOUT);
}
eventLoop.ChangeLastActivedTime(ptChannel, eventLoop.GetLastActiveTime());
}
if (1 != nRead || 1 != readCallbackResult)
{
if (0 == readCallbackResult && 0 < outBuffer.BufferSize()) // should close, but maybe there is some data in outBuffer, we should write them to fd, then close
{
for (; 1 == outBuffer.WriteToFd(ptChannel->GetFd()); )
{
}
DEBUGLOG("%s %s %d, fd=%d read callback function return close", __FILE__, __func__, __LINE__, ptChannel->GetFd());
}
else
{
DEBUGLOG("%s %s %d, fd=%d %s", __FILE__, __func__, __LINE__, ptChannel->GetFd(), (0 == nRead ? "socket close/shoutdown" : (-1 == nRead ? "socket gets error" : "read callback function return error")));
}
// peer socket close/shoutdown or get error
eventLoop.RemoveChannel(ptChannel, EPOLLIN | EPOLLOUT);
eventLoop.DeleteLastActivedTime(ptChannel);
close(ptChannel->GetFd());
}
}
void TcpServer::WriteFromBufferToFd(EventLoop & eventLoop, std::shared_ptr<Channel> ptChannel)
{
SimpleBuffer & outBuffer = ptChannel->GetOutBuffer();
int nWrite = outBuffer.WriteToFd(ptChannel->GetFd());
if (0 > nWrite)
{
DEBUGLOG("%s %s %d, fd=%d gets error", __FILE__, __func__, __LINE__, ptChannel->GetFd());
// peer socket get error
eventLoop.RemoveChannel(ptChannel, EPOLLIN | EPOLLOUT);
eventLoop.DeleteLastActivedTime(ptChannel);
close(ptChannel->GetFd());
return;
}
else if (0 == nWrite)
{
DEBUGLOG("%s %s %d, fd=%d write done", __FILE__, __func__, __LINE__, ptChannel->GetFd());
// write all data in the outbuffer done
if (m_writeCallback)
{
m_writeCallback(ptChannel);
}
eventLoop.RemoveChannel(ptChannel, EPOLLOUT);
}
eventLoop.ChangeLastActivedTime(ptChannel, eventLoop.GetLastActiveTime());
}
void TcpServer::ErrorCallBack(EventLoop & eventLoop, std::shared_ptr<Channel> ptChannel)
{
m_errorCallback(ptChannel);
eventLoop.RemoveChannel(ptChannel, EPOLLIN | EPOLLOUT);
eventLoop.DeleteLastActivedTime(ptChannel);
close(ptChannel->GetFd());
}
void TcpServer::NewConnectReadHandler(EventLoop & eventLoop, std::shared_ptr<Channel> ptChannel)
{
int fd;
struct sockaddr_in peerAddr;
char ipStr[INET_ADDRSTRLEN];
socklen_t peerAddrLen = 1;
while(-1 != (fd = accept(m_listenFd, reinterpret_cast<struct sockaddr*>(&peerAddr), &peerAddrLen)))
{
// limit connection nums
if (kMaxConnectionCnt < fd)
{
close(fd);
continue;
}
NetUtil::SetNonblock(fd);
NetUtil::SetNoDelay(fd);
ChannelPtr ptConnChannel(new Channel(fd));
/*
uint32_t ip = ntohl(peerAddr.sin_addr.s_addr);
uint16_t port = ntohs(peerAddr.sin_port);
snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", ip >> 24, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF);
*/
memset(ipStr, 0, sizeof(ipStr));
if (NULL == inet_ntop(AF_INET, &peerAddr.sin_addr.s_addr, ipStr, sizeof(ipStr)))
{
ERRLOG("inet_ntop error, %s", strerror(errno));
close(fd);
return;
}
uint16_t port = ntohs(peerAddr.sin_port);
DEBUGLOG("%s %s %d, accept new socket, socket=%d from %s:%d", __FILE__, __func__, __LINE__, fd, ipStr, port);
if (m_eventThreadPool.Empty())
{
ERRLOG("%s %s %d, m_eventThreadPool.GetThreadCnt()=%d", __FILE__, __func__, __LINE__, (int)(m_eventThreadPool.GetThreadCnt()));
using namespace std::placeholders;
ptConnChannel->SetReadCallback(std::bind(&TcpServer::ReadFromFdToBuffer, this, _1, _2));
ptConnChannel->SetWriteCallback(std::bind(&TcpServer::WriteFromBufferToFd, this, _1, _2));
ptConnChannel->SetErrorCallback(std::bind(&TcpServer::ErrorCallBack, this, _1, _2));
m_eventLoop.AddChannel(ptConnChannel, EPOLLIN);
m_eventLoop.AddLastActivedTime(ptConnChannel, m_eventLoop.GetLastActiveTime());
}
else
{
ERRLOG("%s %s %d, m_eventThreadPool.GetThreadCnt()=%d", __FILE__, __func__, __LINE__, (int)(m_eventThreadPool.GetThreadCnt()));
m_eventThreadPool.PushFd(fd);
}
peerAddrLen = 1; // to avoid accept(): Invalid Argument
}
//ERRLOG("%s %s %d, m_listenFd=%d, listen error. %s", __FILE__, __func__, __LINE__, m_listenFd, strerror(errno));
}
TcpServer::TcpServer(const char * serverIp, const uint16_t port, int threadCnt /* = 0 */) : m_listenFd(NetUtil::Listen(serverIp, port)),
m_eventThreadPool(threadCnt),
m_ptListenChannel(new Channel(m_listenFd))
{
using namespace std::placeholders;
m_ptListenChannel->SetReadCallback(std::bind(&TcpServer::NewConnectReadHandler, this, _1, _2));
m_eventLoop.AddChannel(m_ptListenChannel, EPOLLIN);
}
TcpServer::~TcpServer()
{
close(m_listenFd);
}
void TcpServer::Loop()
{
using namespace std::placeholders;
m_eventThreadPool.SetReadCallback(std::bind(&TcpServer::ReadFromFdToBuffer, this, _1, _2));
m_eventThreadPool.SetWriteCallback(std::bind(&TcpServer::WriteFromBufferToFd, this, _1, _2));
m_eventThreadPool.SetErrorCallback(std::bind(&TcpServer::ErrorCallBack, this, _1, _2));
m_eventThreadPool.Loop();
m_eventLoop.Loop();
}
void TcpServer::SetReadCallback(ReadCallbackType readCallback)
{
m_readCallback = readCallback;
}
void TcpServer::SetWriteCallback(CallbackType writeCallback)
{
m_writeCallback = writeCallback;
}
void TcpServer::SetErrorCallback(CallbackType errorCallback)
{
m_errorCallback = errorCallback;
}