/
tcp_handler.cpp
242 lines (198 loc) · 7.24 KB
/
tcp_handler.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "tcp_handler.h"
#include "net_thread.h"
#include "protocol.h"
#include <stdio.h>
#include <string.h>
#include "common.h"
namespace NSQTOOL
{
CTcpHandler::CTcpHandler(int iCmdType, int iCmdId,
uint64_t iHandlerId, CThread *pThread, int iConnType)
:CHandler(iHandlerId, pThread)
, m_iCmdType(iCmdType)
, m_iCmdId(iCmdId)
, m_iConnType(iConnType)
{
m_pProtocol = g_pFactory->GenProtocol(
iCmdType, iCmdId);
}
CTcpHandler::~CTcpHandler()
{
close(bufferevent_getfd(GetBufferevent()));
bufferevent_free(GetBufferevent());
delete m_pProtocol;
// CThread::DestoryHandler(iHandlerId);
}
void CTcpHandler::OnConnect()
{
}
void CTcpHandler::OnError(int iErrorNo)
{
GetThread()->DestoryHandler(GetHandlerId());
}
int CTcpHandler::OnRead(const char *pData, int iLength)
{
int iNeed = m_pProtocol->Need(pData, iLength);
NsqLogPrintf(LOG_DEBUG, "OnRead iNeed = %d", iNeed);
if (iLength == 0)
{
}
int i = 0;
while (iNeed == 0)
{
m_pProtocol->Decode();
if (ProcessRead() != 0)
{
return -1;
}
m_pProtocol->NextPkg();
iNeed = m_pProtocol->Need(NULL, 0);
}
return iNeed;
}
int CTcpHandler::OnWrite(int iLength)
{
return 0;
}
int CTcpHandler::ProcessRead()
{
}
void CTcpHandler::TcpWrite()
{
int iLength = evbuffer_get_length(bufferevent_get_output(m_pBufevt));
OnWrite(iLength);
}
void CTcpHandler::TcpRead()
{
//内存copy三次,太浪费,后续改进
int iLength = evbuffer_get_length(bufferevent_get_input(m_pBufevt));
char *pData = new char[iLength + 1];
bufferevent_read(m_pBufevt, pData, iLength);
pData[iLength] = '\0';
NsqLogPrintf(LOG_DEBUG, "TcpRead iLength = %d\n", iLength);
//iNeedLength 证明包处理出错,已经析构掉了handler相关的一些
int iNeedLength = OnRead(pData, iLength);
if (iNeedLength > 0)
{
bufferevent_setwatermark(m_pBufevt, EV_READ, iNeedLength, 0);
}
delete pData;
}
void CTcpHandler::TcpDelete(int iErrorType)
{
if (iErrorType & BEV_EVENT_TIMEOUT)
{
NsqLogPrintf(LOG_ERROR, "Timed out\n");
}
else if (iErrorType & BEV_EVENT_EOF)
{
NsqLogPrintf(LOG_ERROR, "connection closed\n");
}
else if (iErrorType & BEV_EVENT_ERROR)
{
NsqLogPrintf(LOG_ERROR, "some other error\n");
}
if (iErrorType & BEV_EVENT_CONNECTED)
{
OnConnect();
return;
}
OnError(iErrorType);
}
void CTcpHandler::SendData(const char *pData, int32_t iLength)
{
int iRet = bufferevent_write(m_pBufevt, pData,
iLength);
if (iRet != 0)
{
}
}
void CTcpHandler::SetTimeout()
{
struct timeval sTimeval;
if (m_iConnType == ESHORTCONNECT)
{
sTimeval.tv_sec = g_iShortConnectTimeout / 1000000;
sTimeval.tv_usec = g_iShortConnectTimeout % 1000000;
}
else
{
sTimeval.tv_sec = g_iLongConnectTimeout / 1000000;
sTimeval.tv_usec = g_iLongConnectTimeout % 1000000;
}
bufferevent_set_timeouts(m_pBufevt, &sTimeval, &sTimeval);
}
void CTcpHandler::TcpSend(CCommand *pCmd)
{
CTcpSendCommand *pSendCommand = dynamic_cast<CTcpSendCommand *>(pCmd);
int iRet = bufferevent_write(m_pBufevt, pSendCommand->m_pSendData,
pSendCommand->m_iLength);
if (iRet != 0)
{
}
delete [] pSendCommand->m_pSendData;
}
void CTcpHandler::TcpConnect(CCommand *pCmd)
{
NsqLogPrintf(LOG_DEBUG, "TcpConnect\n");
CTcpConnectCommand *pConnectCmd = dynamic_cast<CTcpConnectCommand *>(pCmd);
m_strHost = pConnectCmd->m_strHost;
m_iPort = pConnectCmd->m_iPort;
sockaddr_in sAddr;
memset(&sAddr, 0, sizeof(sockaddr_in));
sAddr.sin_addr.s_addr = inet_addr(pConnectCmd->m_strHost.c_str());
sAddr.sin_port = htons(pConnectCmd->m_iPort);
sAddr.sin_family = AF_INET;
CNetThread *pThread = dynamic_cast<CNetThread *>(GetThread());
m_pBufevt = bufferevent_socket_new(pThread->GetEventBase(), -1, BEV_OPT_THREADSAFE);
int32_t iRet = bufferevent_socket_connect(m_pBufevt, (sockaddr*)&sAddr, sizeof(sockaddr_in));
if (iRet != 0)
{
OnError(errno);
return ;
}
bufferevent_setcb(m_pBufevt, CNetThread::OnStaticRead, CNetThread::OnStaticWrite, CNetThread::OnStaticError, this);
bufferevent_enable(m_pBufevt, EV_READ|EV_PERSIST|EV_ET);
//设置读入最低水位,防止无效回调
bufferevent_setwatermark(m_pBufevt, EV_READ,
OnRead(NULL, 0), 0);
SetTimeout();
}
void CTcpHandler::TcpAdd(CCommand *pCmd)
{
CTcpAddCommand *pConnectCmd = dynamic_cast<CTcpAddCommand *>(pCmd);
CEventThread *pThread = dynamic_cast<CEventThread *>(GetThread());
m_pBufevt = bufferevent_socket_new(pThread->GetEventBase(),
pConnectCmd->m_iFd, BEV_OPT_THREADSAFE);
bufferevent_setcb(m_pBufevt, CNetThread::OnStaticRead, CNetThread::OnStaticWrite, CNetThread::OnStaticError, this);
bufferevent_enable(m_pBufevt, EV_READ|EV_PERSIST|EV_ET);
bufferevent_setwatermark(m_pBufevt, EV_READ,
OnRead(NULL, 0), 0);
OnConnect();
//告诉listen线程
SetTimeout();
}
void CTcpHandler::ProcessCmd(CCommand *pCmd)
{
NsqLogPrintf(LOG_DEBUG, "CTcpHandler::ProcessCmd ProcessCmd Type = %d", pCmd->GetCmdType());
switch(pCmd->GetCmdType())
{
case TCP_CONNECT_TYPE:
{
TcpConnect(pCmd);
break;
}
case TCP_ADD_TYPE:
{
TcpAdd(pCmd);
break;
}
case TCP_SEND_TYPE:
{
TcpSend(pCmd);
break;
}
}
delete pCmd;
}
};