Repository navigation
Expand file tree
/
Copy pathtelnet_handler.cpp
More file actions
886 lines (730 loc) · 20.3 KB
/
Copy pathtelnet_handler.cpp
File metadata and controls
886 lines (730 loc) · 20.3 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 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 "telnet_handler.hpp"
#include "telnet_server.hpp"
#include "network/bundle.hpp"
#include "network/endpoint.hpp"
#include "network/network_interface.hpp"
#include "pyscript/script.hpp"
#ifndef CODE_INLINE
#include "telnet_handler.ipp"
#endif
namespace KBEngine {
char _g_state_str[][256] = {
"password",
"root",
"python",
"readonly"
};
/*
格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m"
例如:
echo "\033[41;36m something here \033[0m"
其中41的位置代表底色, 36的位置是代表字的颜色
那些ascii code 是对颜色调用的始末.
\033[ ; m …… \033[0m
字背景颜色范围:40----49
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色
字颜色:30-----------39
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色
\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m -- \33[37m 设置前景色
\33[40m -- \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标
使用格式能更复杂:
^[[..m;..m;..m;..m
例如: \033[2;7;1m高亮\033[2;7;0m
*/
#define TELNET_CMD_LEFT "\033[D" // 左
#define TELNET_CMD_RIGHT "\033[C" // 右
#define TELNET_CMD_UP "\033[A" // 上
#define TELNET_CMD_DOWN "\033[B" // 下
#define TELNET_CMD_DEL "\033[K" // 删除字符
#define TELNET_CMD_NEWLINE "\r\n" // 新行
#define TELNET_CMD_MOVE_FOCUS_LEFT_MAX "\33[9999999999D" // 左移光标到最前面
#define TELNET_CMD_MOVE_FOCUS_RIGHT_MAX "\33[9999999999C" // 右移光标到最后面
//-------------------------------------------------------------------------------------
TelnetHandler::TelnetHandler(Mercury::EndPoint* pEndPoint, TelnetServer* pTelnetServer, Mercury::NetworkInterface* pNetworkInterface, TELNET_STATE defstate):
buffer_(),
historyCommand_(),
historyCommandIndex_(0),
command_(),
pEndPoint_(pEndPoint),
pTelnetServer_(pTelnetServer),
state_(defstate),
currPos_(0),
pProfileHandler_(NULL),
pNetworkInterface_(pNetworkInterface),
getingHistroyCmd_(false)
{
}
//-------------------------------------------------------------------------------------
TelnetHandler::~TelnetHandler(void)
{
if(pProfileHandler_) {
pProfileHandler_->destroy();
pProfileHandler_ = NULL;
}
}
//-------------------------------------------------------------------------------------
std::string TelnetHandler::getInputStartString()
{
return(boost::format("[%1%@%2% ~]%3% ") % COMPONENT_NAME_EX(g_componentType) %
_g_state_str[(int)state_] % (state_ == TELNET_STATE_PYTHON ? " >>>" : "#")).str();
}
//-------------------------------------------------------------------------------------
std::string TelnetHandler::getWelcome()
{
return (boost::format("\033[1;32mwelcome to %1% \r\n"
"Version: %2%. "
"Config: %3%. "
"Built: %4% %5%. "
"AppUID: %6%. "
"UID: %7%. "
"PID: %8%"
"\r\n---------------------------------------------"
"%9%"
"\r\n---------------------------------------------"
" \033[0m\r\n%10%") %
COMPONENT_NAME_EX(g_componentType) % KBEVersion::versionString().c_str() %
KBE_CONFIG % __TIME__ % __DATE__ %
g_componentID % getUserUID() % getProcessPID() % help() % getInputStartString()).str();
}
//-------------------------------------------------------------------------------------
std::string TelnetHandler::help()
{
return "\033[1;32m\r\nCommand List:"
"\r\n[:help ]: list commands."
"\r\n[:quit ]: quit the server."
"\r\n[:python ]: python console."
"\r\n[:root ]: return to the root layer."
"\r\n[:cprofile ]: collects and reports the internal c++ profiles \r\n\t\tof a server process over a period of time."
"\r\n\t\t usage: \":cprofile 30\""
"\r\n[:pyprofile ]: collects and reports the python profiles \r\n\t\tof a server process over a period of time."
"\r\n\t\t usage: \":pyprofile 30\""
"\r\n[:eventprofile ]: a server process over a period of time, \r\n\t\tcollects and reports the all non-volatile cummunication \r\n\t\tdown to the client."
"\r\n\t\t usage: \":eventprofile 30\""
"\r\n[:mercuryprofile]: collects and reports the mercury profiles \r\n\t\tof a server process over a period of time."
"\r\n\t\t usage: \":mercuryprofile 30\""
"\r\n\r\n\033[0m";
};
//-------------------------------------------------------------------------------------
void TelnetHandler::historyCommandCheck()
{
if(historyCommand_.size() > 50)
historyCommand_.pop_front();
if(historyCommandIndex_ < 0)
historyCommandIndex_ = historyCommand_.size() - 1;
if(historyCommandIndex_ > (int)historyCommand_.size() - 1)
historyCommandIndex_ = 0;
}
//-------------------------------------------------------------------------------------
std::string TelnetHandler::getHistoryCommand(bool isNextCommand)
{
if(isNextCommand)
historyCommandIndex_++;
else
historyCommandIndex_--;
historyCommandCheck();
if(historyCommand_.size() == 0)
return "";
return historyCommand_[historyCommandIndex_];
}
//-------------------------------------------------------------------------------------
int TelnetHandler::handleInputNotification(int fd)
{
KBE_ASSERT((*pEndPoint_) == fd);
char data[1024] = {0};
int recvsize = pEndPoint_->recv(data, sizeof(data));
if(recvsize == -1)
{
return 0;
}
else if(recvsize == 0)
{
onRecvInput();
pTelnetServer_->onTelnetHandlerClosed(fd, this);
return 0;
}
if(state_ == TELNET_STATE_READONLY)
return 0;
for(int i = 0; i < recvsize; i++)
{
buffer_.push_back(data[i]);
}
onRecvInput();
return 0;
}
//-------------------------------------------------------------------------------------
void TelnetHandler::onRecvInput()
{
while (buffer_.size() > 0)
{
int c = (unsigned char)buffer_.front();
buffer_.pop_front();
switch(c)
{
case '\r':
getingHistroyCmd_ = false;
if(buffer_.size() > 0)
{
int cc = (unsigned char)buffer_.front();
if(cc == '\n')
{
buffer_.pop_front();
if(!processCommand())
return;
}
}
break;
case 8: // 退格
sendDelChar();
checkAfterStr();
break;
case ' ': // 空格
default:
{
std::string s = "";
s += c;
command_.insert(currPos_, s);
currPos_++;
if(!checkUDLR())
{
checkAfterStr();
}
break;
}
};
}
}
//-------------------------------------------------------------------------------------
void TelnetHandler::checkAfterStr()
{
if(currPos_ != (int32)command_.size())
{
std::string s = "";
s = command_.substr(currPos_, command_.size() - currPos_);
s += (boost::format("\33[%1%D") % s.size()).str();
pEndPoint_->send(s.c_str(), s.size());
}
}
//-------------------------------------------------------------------------------------
bool TelnetHandler::checkUDLR()
{
if(command_.find(TELNET_CMD_UP) != std::string::npos) // 上
{
pEndPoint_->send(TELNET_CMD_MOVE_FOCUS_LEFT_MAX, strlen(TELNET_CMD_MOVE_FOCUS_LEFT_MAX));
sendDelChar();
std::string startstr = getInputStartString();
pEndPoint_->send(startstr.c_str(), startstr.size());
resetStartPosition();
if(!getingHistroyCmd_)
{
++historyCommandIndex_;
getingHistroyCmd_ = true;
}
std::string s = getHistoryCommand(false);
pEndPoint_->send(s.c_str(), s.size());
command_ = s;
buffer_.clear();
currPos_ = s.size();
return true;
}
else if(command_.find(TELNET_CMD_DOWN) != std::string::npos) // 下
{
pEndPoint_->send(TELNET_CMD_MOVE_FOCUS_LEFT_MAX, strlen(TELNET_CMD_MOVE_FOCUS_LEFT_MAX));
sendDelChar();
std::string startstr = getInputStartString();
pEndPoint_->send(startstr.c_str(), startstr.size());
resetStartPosition();
if(!getingHistroyCmd_)
{
--historyCommandIndex_;
getingHistroyCmd_ = true;
}
std::string s = getHistoryCommand(true);
pEndPoint_->send(s.c_str(), s.size());
command_ = s;
buffer_.clear();
currPos_ = s.size();
return true;
}
else if(command_.find(TELNET_CMD_RIGHT) != std::string::npos) // 右
{
int cmdlen = strlen(TELNET_CMD_RIGHT);
currPos_-= cmdlen;
command_.erase(command_.find(TELNET_CMD_RIGHT), cmdlen);
if(currPos_ < (int)command_.size())
{
currPos_++;
pEndPoint_->send(TELNET_CMD_RIGHT, cmdlen);
}
return true;
}
else if(command_.find(TELNET_CMD_LEFT) != std::string::npos) // 左
{
int cmdlen = strlen(TELNET_CMD_LEFT);
currPos_-= (int)(cmdlen + 1);
if(currPos_ < 0)
{
currPos_ = 0;
}
else
{
pEndPoint_->send(TELNET_CMD_LEFT, cmdlen);
}
command_.erase(command_.find(TELNET_CMD_LEFT), cmdlen);
return true;
}
return false;
}
//-------------------------------------------------------------------------------------
bool TelnetHandler::processCommand()
{
if(command_.size() == 0)
{
sendNewLine();
return true;
}
if(state_ == TELNET_STATE_PASSWD)
{
if(command_ == pTelnetServer_->passwd())
{
state_ = (TELNET_STATE)pTelnetServer_->deflayer();
std::string s = getWelcome();
pEndPoint_->send(s.c_str(), s.size());
command_ = "";
sendEnter();
sendNewLine();
return true;
}
else
{
command_ = "";
sendNewLine();
return true;
}
}
bool logcmd = true;
//for(int i=0; i<(int)historyCommand_.size(); i++)
{
//if(historyCommand_[i] == command_)
//{
// logcmd = false;
// break;
//}
}
if(logcmd)
{
historyCommand_.push_back(command_);
historyCommandCheck();
historyCommandIndex_ = historyCommand_.size() - 1;
}
std::string cmd = command_;
command_ = "";
if(cmd == ":python")
{
if(pTelnetServer_->pScript() == NULL)
return true;
state_ = TELNET_STATE_PYTHON;
sendNewLine();
return true;
}
else if(cmd == ":help")
{
std::string str = help();
pEndPoint_->send(str.c_str(), str.size());
sendNewLine();
return true;
}
else if(cmd == ":root")
{
sendNewLine();
state_ = TELNET_STATE_ROOT;
return true;
}
else if(cmd == ":quit")
{
state_ = TELNET_STATE_QUIT;
pTelnetServer_->closeHandler((*pEndPoint_), this);
return false;
}
else if(cmd.find(":cprofile") == 0)
{
uint32 timelen = 10;
cmd.erase(cmd.find(":cprofile"), strlen(":cprofile"));
if(cmd.size() > 0)
{
try
{
KBEngine::StringConv::str2value(timelen, cmd.c_str());
}
catch(...)
{
timelen = 10;
}
if(timelen < 1 || timelen > 999999999)
timelen = 10;
}
std::string str = (boost::format("Waiting for %1% secs.\r\n") % timelen).str();
pEndPoint_->send(str.c_str(), str.size());
std::string profileName = KBEngine::StringConv::val2str(KBEngine::genUUID64());
if(pProfileHandler_) pProfileHandler_->destroy();
pProfileHandler_ = new TelnetCProfileHandler(this, *pTelnetServer_->pNetworkInterface(),
timelen, profileName, pEndPoint_->addr());
readonly();
return false;
}
else if(cmd.find(":pyprofile") == 0)
{
uint32 timelen = 10;
cmd.erase(cmd.find(":pyprofile"), strlen(":pyprofile"));
if(cmd.size() > 0)
{
try
{
KBEngine::StringConv::str2value(timelen, cmd.c_str());
}
catch(...)
{
timelen = 10;
}
if(timelen < 1 || timelen > 999999999)
timelen = 10;
}
std::string str = (boost::format("Waiting for %1% secs.\r\n") % timelen).str();
pEndPoint_->send(str.c_str(), str.size());
std::string profileName = KBEngine::StringConv::val2str(KBEngine::genUUID64());
if(pProfileHandler_) pProfileHandler_->destroy();
pProfileHandler_ = new TelnetPyProfileHandler(this, *pTelnetServer_->pNetworkInterface(),
timelen, profileName, pEndPoint_->addr());
readonly();
return false;
}
else if(cmd.find(":eventprofile") == 0)
{
uint32 timelen = 10;
cmd.erase(cmd.find(":eventprofile"), strlen(":eventprofile"));
if(cmd.size() > 0)
{
try
{
KBEngine::StringConv::str2value(timelen, cmd.c_str());
}
catch(...)
{
timelen = 10;
}
if(timelen < 1 || timelen > 999999999)
timelen = 10;
}
std::string str = (boost::format("Waiting for %1% secs.\r\n") % timelen).str();
pEndPoint_->send(str.c_str(), str.size());
std::string profileName = KBEngine::StringConv::val2str(KBEngine::genUUID64());
if(pProfileHandler_) pProfileHandler_->destroy();
pProfileHandler_ = new TelnetEventProfileHandler(this, *pTelnetServer_->pNetworkInterface(),
timelen, profileName, pEndPoint_->addr());
readonly();
return false;
}
else if(cmd.find(":mercuryprofile") == 0)
{
uint32 timelen = 10;
cmd.erase(cmd.find(":mercuryprofile"), strlen(":mercuryprofile"));
if(cmd.size() > 0)
{
try
{
KBEngine::StringConv::str2value(timelen, cmd.c_str());
}
catch(...)
{
timelen = 10;
}
if(timelen < 1 || timelen > 999999999)
timelen = 10;
}
std::string str = (boost::format("Waiting for %1% secs.\r\n") % timelen).str();
pEndPoint_->send(str.c_str(), str.size());
std::string profileName = KBEngine::StringConv::val2str(KBEngine::genUUID64());
if(pProfileHandler_) pProfileHandler_->destroy();
pProfileHandler_ = new TelnetMercuryProfileHandler(this, *pTelnetServer_->pNetworkInterface(),
timelen, profileName, pEndPoint_->addr());
readonly();
return false;
}
if(state_ == TELNET_STATE_PYTHON)
{
processPythonCommand(cmd);
}
sendNewLine();
return true;
}
//-------------------------------------------------------------------------------------
void TelnetHandler::processPythonCommand(std::string command)
{
if(pTelnetServer_->pScript() == NULL || command.size() == 0)
return;
command += "\n";
PyObject* pycmd = PyUnicode_DecodeUTF8(command.data(), command.size(), NULL);
if(pycmd == NULL)
{
SCRIPT_ERROR_CHECK();
return;
}
DEBUG_MSG(boost::format("TelnetHandler::processPythonCommand: size(%1%), command=%2%.\n") %
command.size() % command);
std::string retbuf = "";
PyObject* pycmd1 = PyUnicode_AsEncodedString(pycmd, "utf-8", NULL);
pTelnetServer_->pScript()->run_simpleString(PyBytes_AsString(pycmd1), &retbuf);
if(retbuf.size() > 0)
{
// 将结果返回给客户端
Mercury::Bundle bundle;
bundle << retbuf;
bundle.send(*pEndPoint_);
sendEnter();
}
Py_DECREF(pycmd);
Py_DECREF(pycmd1);
}
//-------------------------------------------------------------------------------------
void TelnetHandler::sendEnter()
{
pEndPoint_->send(TELNET_CMD_NEWLINE, strlen(TELNET_CMD_NEWLINE));
}
//-------------------------------------------------------------------------------------
void TelnetHandler::sendDelChar()
{
if(command_.size() > 0)
{
if(currPos_ > 0)
{
command_.erase(currPos_ - 1, 1);
currPos_--;
pEndPoint_->send(TELNET_CMD_DEL, strlen(TELNET_CMD_DEL));
}
}
else
{
resetStartPosition();
}
}
//-------------------------------------------------------------------------------------
void TelnetHandler::sendNewLine()
{
std::string startstr = getInputStartString();
pEndPoint_->send(startstr.c_str(), startstr.size());
resetStartPosition();
currPos_ = 0;
}
//-------------------------------------------------------------------------------------
void TelnetHandler::resetStartPosition()
{
pEndPoint_->send(TELNET_CMD_MOVE_FOCUS_LEFT_MAX, strlen(TELNET_CMD_MOVE_FOCUS_LEFT_MAX));
std::string startstr = getInputStartString();
std::string backcmd = (boost::format("\33[%1%C") % startstr.size()).str();
pEndPoint_->send(backcmd.c_str(), backcmd.size());
}
//-------------------------------------------------------------------------------------
void TelnetHandler::setReadWrite()
{
state_ = TELNET_STATE_ROOT;
}
//-------------------------------------------------------------------------------------
void TelnetHandler::readonly()
{
state_ = TELNET_STATE_READONLY;
}
//-------------------------------------------------------------------------------------
void TelnetHandler::onProfileEnd(const std::string& datas)
{
sendEnter();
pEndPoint()->send(datas.c_str(), datas.size());
setReadWrite();
sendEnter();
sendNewLine();
pProfileHandler_ = NULL;
}
//-------------------------------------------------------------------------------------
void TelnetPyProfileHandler::sendStream(MemoryStream* s)
{
if(isDestroyed_) return;
std::string datas;
(*s) >> datas;
pTelnetHandler_->onProfileEnd(datas);
}
//-------------------------------------------------------------------------------------
void TelnetCProfileHandler::sendStream(MemoryStream* s)
{
if(isDestroyed_) return;
std::string datas;
uint32 timinglen;
ArraySize size;
(*s) >> timinglen >> size;
datas = "ncalls\ttottime\tpercall\tcumtime\tpercall\tfilename:lineno(function)\r\n";
while(size-- > 0)
{
uint32 count;
float lastTime;
float sumTime;
float lastIntTime;
float sumIntTime;
std::string name;
(*s) >> name >> count >> lastTime >> sumTime >> lastIntTime >> sumIntTime;
char buf[256];
kbe_snprintf(buf, 256, "%u", count);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%.3f", sumTime);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%.3f", lastTime);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%.3f", sumIntTime);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%.3f", lastIntTime);
datas += buf;
datas += "\t";
datas += name;
datas += "\r\n";
};
pTelnetHandler_->onProfileEnd(datas);
}
//-------------------------------------------------------------------------------------
void TelnetEventProfileHandler::sendStream(MemoryStream* s)
{
if(isDestroyed_) return;
std::string datas;
uint32 timinglen;
ArraySize size;
(*s) >> timinglen >> size;
if(size == 0)
datas += "results is empty!";
while(size-- > 0)
{
std::string type_name;
(*s) >> type_name;
datas += (boost::format("Event Type:%1%\r\n\r\n(name|count|size)\r\n---------------------\r\n\r\n") % type_name).str();
KBEngine::ArraySize size1;
(*s) >> size1;
while(size1-- > 0)
{
uint32 count;
uint32 eventSize;
std::string name;
(*s) >> name >> count >> eventSize;
if(count == 0)
continue;
datas += (boost::format("%1%\t\t\t\t\t%2%\t%3%\r\n") % name % count % eventSize).str();
}
datas += "\r\n\r\n";
};
pTelnetHandler_->onProfileEnd(datas);
}
//-------------------------------------------------------------------------------------
void TelnetMercuryProfileHandler::sendStream(MemoryStream* s)
{
if(isDestroyed_) return;
std::string datas;
uint32 timinglen;
ArraySize size;
(*s) >> timinglen >> size;
datas = "name\tsent#\tsize\tavg\ttotal#\ttotalsize\trecv#\tsize\tavg\ttotal#\ttotalsize\r\n";
while(size-- > 0)
{
std::string name;
uint32 send_size;
uint32 send_avgsize;
uint32 send_count;
uint32 total_send_size;
uint32 total_send_count;
uint32 recv_size;
uint32 recv_count;
uint32 recv_avgsize;
uint32 total_recv_size;
uint32 total_recv_count;
(*s) >> name >> send_count >> send_size >> send_avgsize >> total_send_size >> total_send_count;
(*s) >> recv_count >> recv_size >> recv_avgsize >> total_recv_size >> total_recv_count;
char buf[256];
datas += name;
datas += "\t";
kbe_snprintf(buf, 256, "%u", send_count);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", send_size);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", send_avgsize);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", total_send_count);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", total_send_size);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", recv_count);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", recv_size);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", recv_avgsize);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", total_recv_count);
datas += buf;
datas += "\t";
kbe_snprintf(buf, 256, "%u", total_recv_size);
datas += buf;
datas += "\r\n";
};
pTelnetHandler_->onProfileEnd(datas);
}
//-------------------------------------------------------------------------------------
}