-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameClient.cpp
More file actions
290 lines (263 loc) · 5.87 KB
/
GameClient.cpp
File metadata and controls
290 lines (263 loc) · 5.87 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "network/GameClient.h"
#include "ODSocket.h"
#include "utils/Util.h"
#include <google/protobuf/io/gzip_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#define BUF_SIZE 65535
///////////////////////////////////////////////////////////
GameClient::GameClient(string ip, int port, int timeOut, ISvrNetEvent *pAdapter)
{
m_sockfd = -1;
m_bReceive = false;
m_pAdapter = pAdapter;
m_socketState = SOCKET_CLOSE;
m_pSocket = new ODSocket();
m_ip = ip;
m_port = port;
m_timeOut = timeOut;
m_bufRecv_len = 0;
m_bufSend_len = 0;
memset(m_bufferRecv, 0, MAX_RECV_BUF_SIZE);
memset(m_bufferSend, 0, MAX_SEND_BUF_SIZE);
}
GameClient::~GameClient()
{
m_pAdapter = NULL;
m_bReceive = false;
m_bufRecv_len = 0;
if (m_pSocket)
{
m_pSocket->Close();
delete m_pSocket;
}
}
void GameClient::onConnect()
{
onStart();
}
void GameClient::relocation(string ip, int port, int timeOut, ISvrNetEvent *pAdapter)
{
m_mutex.Lock();
m_ip = ip;
m_port = port;
m_timeOut = timeOut;
m_pAdapter = pAdapter;
m_mutex.Unlock();
}
int GameClient::sendMsg(const char *sendBuff, int bufLen)
{
m_mutex.Lock();
if (m_pSocket)
{
if (m_bufSend_len+bufLen > MAX_SEND_BUF_SIZE)
{
m_mutex.Unlock();
return -1;
}
memcpy(&m_bufferSend[m_bufSend_len], sendBuff, bufLen); //协议数据放到缓冲区
m_bufSend_len += bufLen;
m_mutex.Unlock();
return bufLen;
}
m_mutex.Unlock();
return -1;
}
bool GameClient::recvMsg()
{
m_mutex.Lock();
if (m_pSocket && m_bReceive)
{
int recLenth=0;
recLenth = m_pSocket->Recv(&m_bufferRecv[m_bufRecv_len], MAX_RECV_BUF_SIZE-m_bufRecv_len); //非阻塞模式,返回-1,不做处理
if (recLenth < 0 ) //服务器关闭
{
m_mutex.Unlock();
close();
m_mutex.Lock();
m_socketState = SOCKET_CLOSE;
m_mutex.Unlock();
return false;
}
if (recLenth > 0) //接收数据成功
{
m_bufRecv_len += recLenth;
m_mutex.Unlock();
return true;
}
}
m_mutex.Unlock();
return false;
}
void GameClient::Run()
{
m_mutex.Lock();
m_bufRecv_len=0;
m_bufSend_len=0;
memset(m_bufferRecv, 0, MAX_RECV_BUF_SIZE);
memset(m_bufferSend, 0, MAX_SEND_BUF_SIZE);
if (m_pSocket)
{
m_pSocket->Close();
}
m_pSocket->Init();
m_pSocket->Create(AF_INET,SOCK_STREAM);
if (m_pSocket->Connect(m_ip.c_str(), m_port, m_timeOut))
{
m_bReceive = true; //链接成功,可以接收数据
m_socketState = SOCKET_CONNECTED;
}
else
{
m_mutex.Unlock();
if (m_pAdapter)
{
m_pAdapter->onNetConnect(false);
}
return;
}
m_mutex.Unlock();
if (m_pAdapter)
{
m_pAdapter->onNetConnect(true);
}
}
int GameClient::close(bool isUp)
{
m_mutex.Lock();
//与server断开链接
if (!m_bReceive)
{
m_mutex.Unlock();
return 0;
}
m_bReceive = false;
if (!isUp && m_pAdapter)
{
m_mutex.Unlock();
m_pAdapter->onNetClose();
}
m_mutex.Unlock();
if (m_pSocket)
{
m_pSocket->Close();
}
return 0;
}
bool GameClient::parseMessage()
{
m_mutex.Lock();
int offset = 0;
while (offset < m_bufRecv_len)
{
//解析协议头
if (offset + sizeof(MsgHead) > m_bufRecv_len) //溢出
{
break;
}
MsgHead msgRecv;
msgRecv.decodeHead(&m_bufferRecv[offset]);
offset += sizeof(msgRecv);
if (offset + msgRecv.msgLen - MSG_HEAD_LEN > m_bufRecv_len) //溢出,获取协议body
{
m_mutex.Unlock();
offset -= sizeof(msgRecv);
break;
}
m_mutex.Unlock();
if (msgRecv.msgType == MSG_ERROR) //错误包
{
printf("message error cmdid:%d", msgRecv.cmdID);
if (m_pAdapter)
{
m_pAdapter->onError(msgRecv, &m_bufferRecv[offset], msgRecv.msgLen-MSG_HEAD_LEN);
}
}
else
{
MessageLite *pMessage = m_pMessages[msgRecv.cmdID];
if (pMessage) //已注册此协议
{
if ((msgRecv.option>>2)&1)
{
char m_newbufferRecv[MAX_RECV_BUF_SIZE]; //协议接收缓冲区
memset(m_newbufferRecv, 0, MAX_RECV_BUF_SIZE);
memcpy(m_newbufferRecv, &m_bufferRecv[offset], msgRecv.msgLen-MSG_HEAD_LEN);
io::ArrayInputStream input(m_newbufferRecv, msgRecv.msgLen-MSG_HEAD_LEN);
io::GzipInputStream stream(&input,io::GzipInputStream::GZIP,-1);
bool ret = pMessage->ParseFromZeroCopyStream(&stream);
if (ret) //解析成功
{
if (m_pAdapter)
m_pAdapter->onGameserverMsg(msgRecv, pMessage);
}
else //解析失败
{
if (m_pAdapter)
m_pAdapter->onDecodeError(msgRecv);
}
}
else
{
bool ret = pMessage->ParseFromArray(&m_bufferRecv[offset], msgRecv.msgLen-MSG_HEAD_LEN);
if (ret) //解析成功
{
if (m_pAdapter)
m_pAdapter->onGameserverMsg(msgRecv, pMessage);
}
else //解析失败
{
if (m_pAdapter)
m_pAdapter->onDecodeError(msgRecv);
}
}
}
else
{
if (m_pAdapter)
{
printf("No registration protocol ID");
}
}
}
m_mutex.Lock();
offset += msgRecv.msgLen-MSG_HEAD_LEN;
}
if (offset < 0)
{
offset = 0;
}
//缓冲区移位
memcpy(m_bufferRecv, &m_bufferRecv[offset], m_bufRecv_len-offset);
m_bufRecv_len -= offset;
memset(&m_bufferRecv[m_bufRecv_len], 0, MAX_RECV_BUF_SIZE-m_bufRecv_len);
m_mutex.Unlock();
return true;
}
void GameClient::registerMessage(int cmdId, MessageLite *pMessage)
{
m_pMessages[cmdId] = pMessage;
}
int GameClient::sendMessage() //将缓冲区协议发送出去
{
m_mutex.Lock();
if (m_pSocket && m_socketState==SOCKET_CONNECTED)
{
int sendSize = m_pSocket->Send(m_bufferSend, m_bufSend_len);
if (sendSize < 0)
{
m_mutex.Unlock();
return -1;
}
memset(m_bufferSend, 0, MAX_SEND_BUF_SIZE);
m_bufSend_len = 0;
m_mutex.Unlock();
return sendSize;
}
m_mutex.Unlock();
return -1;
}
void GameClient::recvMessage() //接收协议,并解析协议
{
recvMsg(); //接收数据
parseMessage(); //解析协议
}