Repository navigation
Expand file tree
/
Copy pathbundle.cpp
More file actions
516 lines (428 loc) · 13.1 KB
/
Copy pathbundle.cpp
File metadata and controls
516 lines (428 loc) · 13.1 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bundle.h"
#include "network/network_stats.h"
#include "network/network_interface.h"
#include "network/channel.h"
#include "helper/profile.h"
#include "network/packet_sender.h"
#ifndef CODE_INLINE
#include "bundle.inl"
#endif
#include "common/blowfish.h"
namespace KBEngine {
namespace Network
{
//-------------------------------------------------------------------------------------
static ObjectPool<Bundle> _g_objPool("Bundle");
ObjectPool<Bundle>& Bundle::ObjPool()
{
return _g_objPool;
}
//-------------------------------------------------------------------------------------
Bundle* Bundle::createPoolObject()
{
return _g_objPool.createObject();
}
//-------------------------------------------------------------------------------------
void Bundle::reclaimPoolObject(Bundle* obj)
{
_g_objPool.reclaimObject(obj);
}
//-------------------------------------------------------------------------------------
void Bundle::destroyObjPool()
{
DEBUG_MSG(fmt::format("Bundle::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
//-------------------------------------------------------------------------------------
size_t Bundle::getPoolObjectBytes()
{
size_t bytes = sizeof(pCurrMsgHandler_) + sizeof(isTCPPacket_) +
sizeof(currMsgLengthPos_) + sizeof(currMsgHandlerLength_) + sizeof(currMsgLength_) +
sizeof(currMsgPacketCount_) + sizeof(currMsgID_) + sizeof(numMessages_) + sizeof(pChannel_)
+ (packets_.size() * sizeof(Packet*));
return bytes;
}
//-------------------------------------------------------------------------------------
Bundle::SmartPoolObjectPtr Bundle::createSmartPoolObj()
{
return SmartPoolObjectPtr(new SmartPoolObject<Bundle>(ObjPool().createObject(), _g_objPool));
}
//-------------------------------------------------------------------------------------
Bundle::Bundle(Channel * pChannel, ProtocolType pt):
pChannel_(pChannel),
numMessages_(0),
pCurrPacket_(NULL),
currMsgID_(0),
currMsgPacketCount_(0),
currMsgLength_(0),
currMsgHandlerLength_(0),
currMsgLengthPos_(0),
packets_(),
isTCPPacket_(pt == PROTOCOL_TCP),
packetMaxSize_(0),
pCurrMsgHandler_(NULL)
{
_calcPacketMaxSize();
newPacket();
}
//-------------------------------------------------------------------------------------
Bundle::Bundle(const Bundle& bundle)
{
// 这些必须在前面设置
// 否则中途创建packet可能错误
isTCPPacket_ = bundle.isTCPPacket_;
pChannel_ = bundle.pChannel_;
pCurrMsgHandler_ = bundle.pCurrMsgHandler_;
currMsgID_ = bundle.currMsgID_;
Packets::const_iterator iter = bundle.packets_.begin();
for (; iter != bundle.packets_.end(); ++iter)
{
newPacket();
pCurrPacket_->append(*static_cast<MemoryStream*>((*iter)));
packets_.push_back(pCurrPacket_);
}
pCurrPacket_ = NULL;
if(bundle.pCurrPacket_)
{
newPacket();
pCurrPacket_->append(*static_cast<MemoryStream*>(bundle.pCurrPacket_));
}
numMessages_ = bundle.numMessages_;
currMsgPacketCount_ = bundle.currMsgPacketCount_;
currMsgLength_ = bundle.currMsgLength_;
currMsgHandlerLength_ = bundle.currMsgHandlerLength_;
currMsgLengthPos_ = bundle.currMsgLengthPos_;
_calcPacketMaxSize();
}
//-------------------------------------------------------------------------------------
Bundle::~Bundle()
{
clear(false);
}
//-------------------------------------------------------------------------------------
void Bundle::onReclaimObject()
{
clear(true);
}
//-------------------------------------------------------------------------------------
void Bundle::_calcPacketMaxSize()
{
// 如果使用了openssl加密通讯则我们保证一个包最大能被Blowfish::BLOCK_SIZE除尽
// 这样我们在加密一个满载包时不需要额外填充字节
if(g_channelExternalEncryptType == 1)
{
packetMaxSize_ = isTCPPacket_ ? (int)(TCPPacket::maxBufferSize() - ENCRYPTTION_WASTAGE_SIZE) :
(PACKET_MAX_SIZE_UDP - ENCRYPTTION_WASTAGE_SIZE);
packetMaxSize_ -= packetMaxSize_ % KBEngine::KBEBlowfish::BLOCK_SIZE;
}
else
{
packetMaxSize_ = isTCPPacket_ ? (int)TCPPacket::maxBufferSize() : PACKET_MAX_SIZE_UDP;
}
}
//-------------------------------------------------------------------------------------
int32 Bundle::packetsLength(bool calccurr)
{
int32 len = 0;
Packets::iterator iter = packets_.begin();
for (; iter != packets_.end(); ++iter)
{
len += (int)(*iter)->length();
}
if(calccurr && pCurrPacket_)
len += (int)pCurrPacket_->length();
return len;
}
//-------------------------------------------------------------------------------------
int32 Bundle::onPacketAppend(int32 addsize, bool inseparable)
{
if(pCurrPacket_ == NULL)
{
newPacket();
}
int32 totalsize = (int32)pCurrPacket_->length();
int32 fwpos = (int32)pCurrPacket_->wpos();
if(inseparable)
fwpos += addsize;
// 如果当前包装不下本次append的数据,将其填充到新包中
if(fwpos >= packetMaxSize_)
{
packets_.push_back(pCurrPacket_);
currMsgPacketCount_++;
newPacket();
totalsize = 0;
}
int32 remainsize = packetMaxSize_ - totalsize;
int32 taddsize = addsize;
// 如果当前包剩余空间小于要添加的字节则本次填满此包
if(remainsize < addsize)
taddsize = remainsize;
currMsgLength_ += taddsize;
return taddsize;
}
//-------------------------------------------------------------------------------------
Packet* Bundle::newPacket()
{
MALLOC_PACKET(pCurrPacket_, isTCPPacket_);
pCurrPacket_->pBundle(this);
return pCurrPacket_;
}
//-------------------------------------------------------------------------------------
void Bundle::clear(bool isRecl)
{
if(pCurrPacket_ != NULL)
{
packets_.push_back(pCurrPacket_);
pCurrPacket_ = NULL;
}
Packets::iterator iter = packets_.begin();
for (; iter != packets_.end(); ++iter)
{
if(!isRecl)
{
delete (*iter);
}
else
{
RECLAIM_PACKET(isTCPPacket_, (*iter));
}
}
packets_.clear();
pChannel_ = NULL;
numMessages_ = 0;
currMsgID_ = 0;
currMsgPacketCount_ = 0;
currMsgLength_ = 0;
currMsgLengthPos_ = 0;
currMsgHandlerLength_ = 0;
pCurrMsgHandler_ = NULL;
_calcPacketMaxSize();
}
//-------------------------------------------------------------------------------------
void Bundle::clearPackets()
{
if(pCurrPacket_ != NULL)
{
packets_.push_back(pCurrPacket_);
pCurrPacket_ = NULL;
}
Packets::iterator iter = packets_.begin();
for (; iter != packets_.end(); ++iter)
{
RECLAIM_PACKET(isTCPPacket_, (*iter));
}
packets_.clear();
}
//-------------------------------------------------------------------------------------
void Bundle::newMessage(const MessageHandler& msgHandler)
{
pCurrMsgHandler_ = &msgHandler;
if(pCurrPacket_ == NULL)
this->newPacket();
finiMessage(false);
KBE_ASSERT(pCurrPacket_ != NULL);
(*this) << msgHandler.msgID;
pCurrPacket_->messageID(msgHandler.msgID);
// 此处对于非固定长度的消息来说需要先设置它的消息长度位为0, 到最后需要填充长度
if(msgHandler.msgLen == NETWORK_VARIABLE_MESSAGE)
{
MessageLength msglen = 0;
currMsgLengthPos_ = pCurrPacket_->wpos();
(*this) << msglen;
}
++numMessages_;
currMsgID_ = msgHandler.msgID;
currMsgPacketCount_ = 0;
currMsgHandlerLength_ = msgHandler.msgLen;
}
//-------------------------------------------------------------------------------------
void Bundle::finiMessage(bool isSend)
{
KBE_ASSERT(pCurrPacket_ != NULL);
pCurrPacket_->pBundle(this);
if(isSend)
{
++currMsgPacketCount_;
packets_.push_back(pCurrPacket_);
}
// 对消息进行跟踪
if(pCurrMsgHandler_){
if(isSend || numMessages_ > 1)
{
NetworkStats::getSingleton().trackMessage(NetworkStats::SEND,
*pCurrMsgHandler_, currMsgLength_);
}
}
// 此处对于非固定长度的消息来说需要设置它的最终长度信息
if(currMsgHandlerLength_ < 0 || g_packetAlwaysContainLength)
{
Packet* pPacket = pCurrPacket_;
if(currMsgPacketCount_ > 0)
pPacket = packets_[packets_.size() - currMsgPacketCount_];
currMsgLength_ -= NETWORK_MESSAGE_ID_SIZE;
currMsgLength_ -= NETWORK_MESSAGE_LENGTH_SIZE;
// 按照设计一个包最大也不可能超过NETWORK_MESSAGE_MAX_SIZE
if(g_componentType == BOTS_TYPE || g_componentType == CLIENT_TYPE)
{
KBE_ASSERT(currMsgLength_ <= NETWORK_MESSAGE_MAX_SIZE);
}
// 如果消息长度大于等于NETWORK_MESSAGE_MAX_SIZE
// 使用扩展消息长度机制,向消息长度后面再填充4字节
// 用于描述更大的长度
if(currMsgLength_ >= NETWORK_MESSAGE_MAX_SIZE)
{
MessageLength1 ex_msg_length = currMsgLength_;
KBEngine::EndianConvert(ex_msg_length);
MessageLength msgLen = NETWORK_MESSAGE_MAX_SIZE;
KBEngine::EndianConvert(msgLen);
memcpy(&pPacket->data()[currMsgLengthPos_],
(uint8*)&msgLen, NETWORK_MESSAGE_LENGTH_SIZE);
pPacket->insert(currMsgLengthPos_ + NETWORK_MESSAGE_LENGTH_SIZE, (uint8*)&ex_msg_length,
NETWORK_MESSAGE_LENGTH1_SIZE);
}
else
{
MessageLength msgLen = (MessageLength)currMsgLength_;
KBEngine::EndianConvert(msgLen);
memcpy(&pPacket->data()[currMsgLengthPos_],
(uint8*)&msgLen, NETWORK_MESSAGE_LENGTH_SIZE);
}
}
if(isSend)
{
currMsgHandlerLength_ = 0;
pCurrPacket_ = NULL;
if(Network::g_trace_packet > 0)
_debugMessages();
}
currMsgID_ = 0;
currMsgPacketCount_ = 0;
currMsgLength_ = 0;
currMsgLengthPos_ = 0;
}
//-------------------------------------------------------------------------------------
void Bundle::_debugMessages()
{
if(!pCurrMsgHandler_)
return;
Packets packets;
packets.insert(packets.end(), packets_.begin(), packets_.end());
if(pCurrPacket_)
packets.push_back(pCurrPacket_);
MemoryStream* pMemoryStream = MemoryStream::createPoolObject();
MessageID msgid = 0;
MessageLength msglen = 0;
MessageLength1 msglen1 = 0;
const Network::MessageHandler* pCurrMsgHandler = NULL;
int state = 0; // 0:读取消息ID, 1:读取消息长度, 2:读取消息扩展长度, 3:读取内容
for(Packets::iterator iter = packets.begin(); iter != packets.end(); iter++)
{
Packet* pPacket = (*iter);
if(pPacket->length() == 0)
continue;
size_t rpos = pPacket->rpos();
size_t wpos = pPacket->wpos();
while(pPacket->length() > 0)
{
if(state == 0)
{
// 一些sendto操作的包导致, 这类包也不需要追踪
if(pPacket->length() < NETWORK_MESSAGE_ID_SIZE)
{
pPacket->done();
continue;
}
(*pPacket) >> msgid;
(*pMemoryStream) << msgid;
state = 1;
continue;
}
else if(state == 1)
{
if(!pCurrMsgHandler_ || !pCurrMsgHandler_->pMessageHandlers)
{
pPacket->done();
continue;
}
pCurrMsgHandler = pCurrMsgHandler_->pMessageHandlers->find(msgid);
// 一些sendto操作的包导致找不到MsgHandler, 这类包也不需要追踪
if(!pCurrMsgHandler)
{
pPacket->done();
continue;
}
if(pCurrMsgHandler->msgLen == NETWORK_VARIABLE_MESSAGE || g_packetAlwaysContainLength)
{
(*pPacket) >> msglen;
(*pMemoryStream) << msglen;
if(msglen == NETWORK_MESSAGE_MAX_SIZE)
state = 2;
else
state = 3;
}
else
{
msglen = pCurrMsgHandler->msgLen;
(*pMemoryStream) << msglen;
state = 3;
}
continue;
}
else if(state == 2)
{
(*pPacket) >> msglen1;
(*pMemoryStream) << msglen1;
state = 3;
continue;
}
else if(state == 3)
{
MessageLength1 totallen = msglen1 > 0 ? msglen1 : msglen;
if(pPacket->length() >= totallen - pMemoryStream->length())
{
MessageLength1 len = totallen - (MessageLength1)pMemoryStream->length();
pMemoryStream->append(pPacket->data() + pPacket->rpos(), len);
pPacket->rpos((int)(pPacket->rpos() + len));
}
else
{
pMemoryStream->append(*static_cast<MemoryStream*>(pPacket));
pPacket->done();
}
if(pMemoryStream->length() == totallen)
{
state = 0;
msglen1 = 0;
msglen = 0;
msgid = 0;
TRACE_MESSAGE_PACKET(false, pMemoryStream, pCurrMsgHandler, pMemoryStream->length(),
(pChannel_ != NULL ? pChannel_->c_str() : "None"));
pMemoryStream->clear(false);
continue;
}
}
};
pPacket->rpos((int)rpos);
pPacket->wpos((int)wpos);
}
MemoryStream::reclaimPoolObject(pMemoryStream);
}
//-------------------------------------------------------------------------------------
}
}