-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsvr_mcd_proc.cpp
More file actions
1536 lines (1293 loc) · 39 KB
/
statsvr_mcd_proc.cpp
File metadata and controls
1536 lines (1293 loc) · 39 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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OS headers */
#include <sys/file.h>
#include <fstream>
#include <netdb.h>
/* TFC headers */
#include "tfc_object.h"
#include "tfc_base_fast_timer.h"
#include "tfc_cache_proc.h"
#include "tfc_net_ipc_mq.h"
#include "tfc_net_ccd_define.h"
#include "tfc_net_dcc_define.h"
#include "tfc_debug_log.h"
/* module headers */
#include "debug.h"
#include "statsvr_mcd_proc.h"
#include "statsvr_error.h"
#include "water_log.h"
#include "statsvr_timer_info.h"
#include "admin_timer_info.h"
#include "user_timer_info.h"
#include "service_timer_info.h"
#include "system_timer_info.h"
#include "txf_timer_info.h"
#include "statsvr_kv.h"
using namespace std;
using namespace statsvr;
#define HTTP_PACKET_MAX_LEN (20480)
#define BUFF_SIZE (2 * 1024 * 1024)
#define ARG_CNT_MAX (32)
static char r_code_200[] = "200";
static char r_reason_200[] = "OK";
//char BUF[BUFF_SIZE] = {0};
extern "C"
{
static void disp_ccd(void *papp)
{
CMCDProc *app = (CMCDProc*)papp;
app->DispatchCCD();
}
static void disp_dcc(void *papp)
{
CMCDProc *app = (CMCDProc*)papp;
app->DispatchDCC();
}
static void disp_dcc_http(void *papp)
{
CMCDProc *app = (CMCDProc*)papp;
app->DispatchDCCHttp();
}
}
void CMCDProc::run(const std::string& conf_file)
{
LogDebug("#####################################################################\n");
const char *version = "statsvr@version"
MAJOR_VERSION
"."
MIDDLE_VERSION
"."
MINOR_VERSION
" BUILD DATE:["
__DATE__
"] BUILD TIME:["
__TIME__
"]";
LogDebug("%s\n", version);
LogDebug("#####################################################################\n");
if (Init(conf_file) < 0)
{
return;
}
LogDebug("[%s] server started.....\n", MODULE_NAME);
int isPingSent = 0;
while (!obj_checkflag.IsStop())
{
DispatchUser2Service();
DispatchSessionTimer();
DispatchServiceTimeout();
run_epoll_4_mq();
CheckFlag(true);
if (isPingSent < 30)
{
if (InitSendPing())
{
LogError("Failed to send init ping!");
}
else
{
LogDebug("Success to send init ping.");
}
++isPingSent;
}
}
LogDebug("[%s] server stopped.....\n", MODULE_NAME);
}
int32_t CMCDProc::Init(const std::string& conf_file)
{
if (m_cfg.LoadCfg(conf_file) < 0)
{
LogError("Failed to LoadCfg()!");
goto err_out;
}
if (InitBuffer() < 0)
{
LogError("Failed to InitBuffer()!");
goto err_out;
}
#if 1
if (InitLog() < 0)
{
LogError("Failed to InitLog()!");
goto err_out;
}
#endif
if (InitStat() < 0)
{
LogError("Failed to InitStat()!");
goto err_out;
}
if (InitIpc() < 0)
{
LogError("Failed to InitIpc()!");
goto err_out;
}
if (InitTemplate())
{
LogError("Failed to build http response template!");
goto err_out;
}
if (InitCmdMap())
{
LogError("Failed to InitCmdMap()!");
goto err_out;
}
if (InitKVServer())
{
LogError("Failed to InitKVServer()!");
goto err_out;
}
srand((int)time(0));
m_msg_seq = rand();
signal(SIGUSR1, sigusr1_handle);
signal(SIGUSR2, sigusr2_handle);
return 0;
err_out:
return -1;
}
int32_t CMCDProc::ReloadCfg()
{
if (m_cfg.Reload() < 0)
{
LogError("Failed to ReloadCfg::Reload()!");
goto err_out;
}
if (InitLog())
{
LogError("Failed to ReloadCfg::InitLog()!");
goto err_out;
}
if (InitStat())
{
LogError("Failed to ReloadCfg::InitStat()!");
goto err_out;
}
return 0;
err_out:
return -1;
}
int32_t CMCDProc::InitBuffer()
{
if (NULL == m_recv_buf)
{
m_recv_buf = new char[BUFF_SIZE];
if (NULL == m_recv_buf)
{
return -1;
}
}
if (NULL == m_send_buf)
{
m_send_buf = new char[BUFF_SIZE];
if (NULL == m_send_buf)
{
return -1;
}
}
return 0;
}
int32_t CMCDProc::InitLog()
{
TLogPara *log_para = &(m_cfg._log_para);
int32_t ret = DEBUG_OPEN(log_para->log_level_, log_para->log_type_,
log_para->path_, log_para->name_prefix_,
log_para->max_file_size_, log_para->max_file_no_);
if (ret < 0)
return ret;
log_para = &(m_cfg._water_log);
ret = CWaterLog::Instance()->Init(log_para->path_, log_para->name_prefix_
, log_para->max_file_size_, log_para->max_file_no_);
return ret;
}
int32_t CMCDProc::InitStat()
{
TLogPara* stat_para = &(m_cfg._stat_log_para);
string stat_file = stat_para->path_ + stat_para->name_prefix_;
LogTrace("stat_file: %s", stat_file.c_str());
int32_t ret = m_stat.Inittialize((char*)stat_file.c_str()
, stat_para->max_file_size_
, stat_para->max_file_no_
, m_cfg._stat_timeout_1
, m_cfg._stat_timeout_2
, m_cfg._stat_timeout_3);
return ret;
}
int32_t CMCDProc::InitIpc()
{
m_mq_ccd_2_mcd = _mqs["mq_ccd_2_mcd"];
m_mq_mcd_2_ccd = _mqs["mq_mcd_2_ccd"];
m_mq_dcc_2_mcd = _mqs["mq_dcc_2_mcd"];
m_mq_mcd_2_dcc = _mqs["mq_mcd_2_dcc"];
/*m_mq_dcc_2_mcd_http = _mqs["mq_dcc_2_mcd_http"];
m_mq_mcd_2_dcc_http = _mqs["mq_mcd_2_dcc_http"];*/
assert(m_mq_ccd_2_mcd != NULL);
assert(m_mq_mcd_2_ccd != NULL);
assert(m_mq_dcc_2_mcd != NULL);
assert(m_mq_mcd_2_dcc != NULL);
/*assert(m_mq_dcc_2_mcd_http != NULL);
assert(m_mq_mcd_2_dcc_http != NULL);*/
if (add_mq_2_epoll(m_mq_ccd_2_mcd, disp_ccd, this))
{
LogErrPrint("Add input mq to EPOLL fail!");
err_exit();
}
if (add_mq_2_epoll(m_mq_dcc_2_mcd, disp_dcc, this))
{
LogErrPrint("Add mq_dcc_2_mcd to EPOLL fail!");
err_exit();
}
/*if (add_mq_2_epoll(m_mq_dcc_2_mcd_http, disp_dcc_http, this))
{
LogErrPrint("Add mq_dcc_2_mcd to EPOLL fail!");
err_exit();
}*/
return 0;
}
int CMCDProc::InitTemplate()
{
// init http args begin
m_arg_cnt = 3;
m_arg_vals = new char*[m_arg_cnt];
if (!m_arg_vals)
{
LogErrPrint("Alloc memory for HTTP template arg values fail!");
return -1;
}
m_arg_vals[2] = m_r_clength;
// init http args end
char http_head[HTTP_HEAD_MAX];
char *data = http_head;
char *head = NULL;
http_template_arg_t args[ARG_CNT_MAX];
int head_len;
data += sprintf(data, "HTTP/1.1 ");
args[0].offset = data - http_head;
head = data;
data += sprintf(data, " ");
args[0].max_length = data - head - 1;
args[1].offset = data - http_head;
head = data;
data += sprintf(data, " \r\n");
args[1].max_length = data - head - 2;
data += sprintf(data, "Server: MCP-Simple-HTTP\r\n");
data += sprintf(data, "Content-Length: ");
args[2].offset = data - http_head;
head = data;
data += sprintf(data, " \r\n");
args[2].max_length = data - head - 2;
data += sprintf(data, "Cache-Control: no-cache\r\n");
data += sprintf(data, "Connection: Keep-Alive\r\n");
data += sprintf(data, "\r\n");
head_len = data - http_head;
return m_http_template.Init(http_head, head_len, args, 3);
}
int32_t CMCDProc::InitCmdMap()
{
m_cmdMap["echo"] = ECHO;
m_cmdMap["getUserInfo"] = GET_USER_INFO;
m_cmdMap["userOnline"] = USER_ONLINE;
m_cmdMap["cancelQueue"] = CANCEL_QUEUE;
m_cmdMap["closeSession"] = CLOSE_SESSION;
m_cmdMap["connectService"] = CONNECT_SERVICE;
m_cmdMap["getServiceInfo"] = GET_SERVICE_INFO;
m_cmdMap["serviceLogin"] = SERVICE_LOGIN;
m_cmdMap["serviceChangeStatus"] = SERVICE_CHANGESTATUS;
m_cmdMap["changeService"] = CHANGE_SERVICE;
m_cmdMap["overload"] = SERVICE_PULLNEXT;
m_cmdMap["refreshSession"] = REFRESH_SESSION;
m_cmdMap["getChatProxyAddress"] = GET_CP_ADDR;
return 0;
}
int32_t CMCDProc::InitKVServer()
{
int ret = SS_ERROR;
int init_type;
ret = kv_cache.Init(m_cfg.m_conf_cache_size, m_cfg.m_conf_shmkey,
m_cfg.m_node_num, m_cfg.m_block_size, 0, &init_type);
if (ret)
{
LogError("Failed to init shared memory, size:[%d], key[%d]!",
m_cfg.m_conf_cache_size, m_cfg.m_conf_shmkey);
return ret;
}
else
{
LogTrace("Success to init shared memory, size:[%d], key[%d].",
m_cfg.m_conf_cache_size, m_cfg.m_conf_shmkey);
return SS_OK;
}
}
void CMCDProc::DispatchCCD()
{
int32_t ret = 0;
int32_t deal_count = 0;
unsigned data_len = 0;
unsigned long long flow = 0;
TCCDHeader* ccdheader = (TCCDHeader*)m_recv_buf;
timeval ccd_time;
while (deal_count < 1000)
{
data_len = 0;
ret = m_mq_ccd_2_mcd->try_dequeue(m_recv_buf, BUFF_SIZE, data_len, flow);
if (ret || data_len < CCD_HEADER_LEN)
{
++deal_count;
continue;
}
//LogDebug("CCD receive a packet");
uint32_t client_ip = ccdheader->_ip;
ccd_time.tv_sec = ccdheader->_timestamp;
ccd_time.tv_usec = ccdheader->_timestamp_msec * 1000;
if (ccd_rsp_data != ccdheader->_type)
{
LogError("[DispatchCcd] ccdheader->_type invalid "
"expect: %d actual: %d client_ip: %s\n",
ccd_rsp_data, ccdheader->_type, INET_ntoa(client_ip).c_str());
++deal_count;
continue;
}
HandleRequest(m_recv_buf + CCD_HEADER_LEN,
data_len - CCD_HEADER_LEN,
flow,
client_ip,
ccd_time);
++deal_count;
}
}
int32_t CMCDProc::HttpGetBu(const char *uri_buf, string &bu_name, string ¶m_str)
{
if (uri_buf == NULL)
return -1;
string http_url(uri_buf);
string::size_type start = 1, qm_idx, slash_idx;
if (':' == http_url[0])
{
start = http_url.find_first_of("/", 0);
if (start == string::npos)
{
return -1;
}
}
slash_idx = http_url.find_first_of("/", start+1);
bu_name = http_url.substr(start, slash_idx - start);
qm_idx = http_url.find_first_of("?", start+1);
if (qm_idx == string::npos)
{
param_str = "";
}else{
param_str = http_url.substr (qm_idx + 1, http_url.size() - qm_idx - 1);
}
return 0;
}
int32_t CMCDProc::HttpParseCmd(char* data, unsigned data_len, string& outdata, unsigned& out_len)
{
if (data_len < 5)
{
LogError("http data too small: %s", data);
return -1;
}
CHttpParse http_parse;
int ret = http_parse.Init((void*)((const void*)data), data_len);
if (ret)
{
LogError("Failed to parse http data: %s!", data);
return -1;
}
string request_url = string(http_parse.HttpUri());
unsigned long pos = request_url.find("?");
if (pos != string::npos)
{
string path = request_url.substr (0, pos);
if (path.find("../") != string::npos)
{
LogError( "path has ../: %s", request_url.c_str());
return -1;
}
}
string m_business_name;
string req_params;
HttpGetBu(request_url.c_str(), m_business_name, req_params);
int m_http_method = http_parse.HttpMethod();
string identity = "";
string cmd;
if (HTTP_POST == m_http_method)
{
int body_len = http_parse.BodyLength();
req_params.assign(data + data_len - body_len, body_len);
outdata = req_params;
out_len = req_params.size();
Json::Reader reader;
Json::Value root;
Json::Value rootdata;
//LogDebug("==>parse root");
if (!reader.parse(req_params, root))
{
LogError("Failed to parse params: %s!", req_params.c_str());
return -1;
}
//LogDebug("==>parse seq");
if (!root["seq"].isNull())
{
if (root["seq"].isUInt())
{
unsigned int seq_num = root["seq"].asUInt();
m_seq = ui2str(seq_num);
}
else
{
m_seq = root["seq"].asString();
}
}
//LogDebug("==>parse method");
if (!root["method"].isNull())
{
cmd = root["method"].asString();
}
else
{
cmd = root["cmd"].asString();
}
LogWarn("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
LogWarn("==>method: %s", cmd.c_str());
if (!root["data"].isNull())
{
rootdata = root["data"];
if (!rootdata["identity"].isNull())
{
identity = rootdata["identity"].asString();
}
}
//LogDebug("===>parse data finish");
}
else
{
return -1;
}
//LogDebug("==>identity: %s", identity.c_str());
#if 0
if (identity == "admin")
{
return 0;
}
/*else if(m_workMode == statsvr::WORKMODE_READY)
{
LogDebug("m_workMode :%d", m_workMode);
return -2;
}*/
else if (identity == "user")
{
return m_userMap[cmd];
}
else if (identity == "service")
{
return m_serviceMap[cmd];
}
else
{
return m_replyMap[cmd];
}
#else
if (identity == "admin")
{
return 0;
}
else if (statsvr::WORKMODE_READY == m_workMode)
{
LogError("====> [%s] has not been working yet, m_workMode :%d", MODULE_NAME, m_workMode);
return -2;
}
else
{
return m_cmdMap[cmd];
}
#endif
}
int32_t CMCDProc::HandleRequest(char* data, unsigned data_len,
unsigned long long flow, uint32_t client_ip, timeval& ccd_time)
{
string str_client_ip;
int cmd;
string outdata;
unsigned out_len = 0;
str_client_ip = INET_ntoa(client_ip);
cmd = HttpParseCmd(data, data_len, outdata, out_len);
CWaterLog::Instance()->WriteLog(ccd_time, 0, (char *)str_client_ip.c_str(), 0, cmd, (char *)outdata.c_str());
CTimerInfo* ti = NULL;
unsigned msg_seq = GetMsgSeq();
switch (cmd)
{
case ECHO:
ti = new EchoTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case ADMIN_CONFIG:
ti = new AdminConfigTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case GET_USER_INFO:
ti = new GetUserInfoTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case USER_ONLINE:
ti = new UserOnlineTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case CANCEL_QUEUE:
ti = new CancelQueueTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case CLOSE_SESSION:
ti = new CloseSessionTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case CONNECT_SERVICE:
ti = new ConnectServiceTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case GET_SERVICE_INFO:
ti = new GetServiceInfoTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case SERVICE_LOGIN:
ti = new ServiceLoginTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case SERVICE_CHANGESTATUS:
ti = new ServiceChangeStatusTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case CHANGE_SERVICE:
ti = new ChangeServiceTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case SERVICE_PULLNEXT:
ti = new ServicePullNextTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case REFRESH_SESSION:
ti = new RefreshSessionTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
case GET_CP_ADDR:
ti = new TransferTimer(this, msg_seq, ccd_time, str_client_ip, flow, m_cfg._time_out);
break;
default:
LogError( "Unknown cmd[%d] from IP[%s]!", cmd, str_client_ip.c_str());
Json::Value resp;
resp["method"] = "-reply";
if (cmd == -2)
{
resp["code"] = -61001;
resp["msg"] = "System not ready";
}
else
{
resp["code"] = -61003;
resp["msg"] = "Unknown command";
}
Json::FastWriter writer;
string strRsp = writer.write(resp);
EnququeHttp2CCD(flow, (char *)strRsp.c_str(), strRsp.size());
return -1;
}
if (ti != NULL)
{
if (ti->do_next_step(outdata) == 0)
{
m_timer_queue.set(ti->GetMsgSeq(), ti, ti->GetTimeGap());
}
else
{
delete ti;
}
}
return 0;
}
void CMCDProc::DispatchDCC()
{
int ret = 0, deal_count = 0;
unsigned data_len = 0;
unsigned long long flow = 0;
TDCCHeader *dccheader = (TDCCHeader*)m_recv_buf;
timeval dcc_time;
while (deal_count < 1000) {
data_len = 0;
ret = m_mq_dcc_2_mcd->try_dequeue(m_recv_buf, BUFF_SIZE, data_len, flow);
if (ret || data_len < DCC_HEADER_LEN) {
deal_count++;
continue;
}
uint32_t down_ip = dccheader->_ip;
unsigned down_port = dccheader->_port;
dcc_time.tv_sec = dccheader->_timestamp;
dcc_time.tv_usec = dccheader->_timestamp_msec * 1000;
if (dcc_rsp_data != dccheader->_type) {
LogError("[DispatchDCC] Invalid DCC header type! expect type: %d, actual type: %d, IP: %s.",
dcc_rsp_data, dccheader->_type, INET_ntoa(down_ip).c_str());
deal_count++;
continue;
}
ret = HandleResponse(m_recv_buf + DCC_HEADER_LEN, data_len - DCC_HEADER_LEN, flow, down_ip, down_port, dcc_time);
if ( ret < 0)
{
LogError("[DispatchDCC] Failed to HandleResponse() from IP: %s, ret: %d!", INET_ntoa(down_ip).c_str(), ret);
}
deal_count++;
}
}
int32_t CMCDProc::HandleResponse(char* data,
unsigned data_len,
unsigned long long flow,
uint32_t down_ip, unsigned down_port, timeval& dcc_time)
{
#if 1
return 0;
#else
string outdata;
unsigned out_len = 0;
int ret = HttpParseCmd(data, data_len, outdata, out_len);
if (ret < 0)
{
LogError("Failed to parse http, ret: %d", ret);
return -1;
}
Json::Reader reader;
Json::Value root;
if (!reader.parse(outdata, root))
{
LogError("Failed to parse response: %s", outdata.c_str());
return -1;
}
uint32_t msg_seq = 0;
if (!root["innerSeq"].isNull() && root["innerSeq"].isUInt())
{
msg_seq = root["innerSeq"].asUInt();
}
else if (!root["seq"].isNull() && root["seq"].isString())
{
msg_seq = root["seq"].asString();
}
else
{
LogError("Failed to parse seq from HTTP response!");
return -1;
}
LogTrace("======>seq: %u", msg_seq);
CTimerInfo* ti = NULL;
if (m_timer_queue.get(msg_seq, (CFastTimerInfo**)&ti))
{
LogError("[CMCDProc] seq_no=%u m_timer_queue.get fail [%s:%u] dcc_time[%s]\n"
, msg_seq, INET_ntoa(down_ip).c_str(), down_port, GetFormatTime(dcc_time).c_str());
return -1;
}
string req_data = string(BUF, out_len);
if (ti->do_next_step(req_data) !=0 )
{
delete ti;
}
else
{
m_timer_queue.set(ti->GetMsgSeq(), ti, ti->GetTimeGap());
}
return 0;
#endif
}
#if 0
int32_t CMCDProc::HandleResponseHttp(char* data,
unsigned data_len,
unsigned long long flow,
uint32_t down_ip, unsigned down_port, timeval& dcc_time)
{
return 0;
}
void CMCDProc::DispatchDCCHttp()
{
int32_t ret = 0;
int32_t deal_count = 0;
unsigned data_len = 0;
unsigned long long flow = 0;
TDCCHeader* dccheader = (TDCCHeader*)m_recv_buf;
timeval dcc_time;
while (deal_count < 1000)
{
data_len = 0;
ret = m_mq_dcc_2_mcd_http->try_dequeue(m_recv_buf, BUFF_SIZE, data_len, flow);
if (ret || data_len < DCC_HEADER_LEN)
{
++deal_count;
continue;
}
uint32_t down_ip = dccheader->_ip;
uint32_t down_port = dccheader->_port;
dcc_time.tv_sec = dccheader->_timestamp;
dcc_time.tv_usec = dccheader->_timestamp_msec * 1000;
if (dcc_rsp_data != dccheader->_type)
{
LogError("dccheader->_type invalid, expect: %d, actual: %d, down_ip: %s, down_port: %d",
dcc_rsp_data, dccheader->_type, INET_ntoa(down_ip).c_str(), down_port);
++deal_count;
continue;
}
HandleResponseHttp(m_recv_buf + DCC_HEADER_LEN,
data_len - DCC_HEADER_LEN,
flow,
down_ip,
down_port,
dcc_time);
++deal_count;
}
}
#endif
int32_t CMCDProc::EnququeHttp2CCD(unsigned long long flow, char *data, unsigned data_len)
{
m_arg_vals[0] = r_code_200;
m_arg_vals[1] = r_reason_200;
sprintf(m_r_clength, "%u", data_len);
int ret_len = 0;
char* head_msg = NULL;
int msg_len = m_http_template.ProduceRef(&head_msg, &ret_len, m_arg_vals, m_arg_cnt);
if (NULL == head_msg || msg_len <= 0)
{
LogError("Failed to enqueue HTTP to CCD! flow:%lu, datalen:%u, data:%s", flow, data_len ,data);
return -1;
}
TCCDHeader* header = (TCCDHeader*)m_send_buf;
char* data_buff = m_send_buf + CCD_HEADER_LEN;
unsigned data_max = BUFF_SIZE- CCD_HEADER_LEN;
/* 包大小检查 */
if (data_len + msg_len > data_max)
{
LogError("data_len+msg_len > data_max (%u+%d > %u)!", data_len, msg_len, data_max);
return -1;
}
memcpy(data_buff, head_msg, msg_len);
data_buff += msg_len;
memcpy(data_buff, data, data_len);
header->_type = ccd_req_data;
int totallen = CCD_HEADER_LEN + data_len + msg_len;
if (m_mq_mcd_2_ccd->enqueue(header, totallen, flow))
{
LogError("Failed to enqueue to CCD!");
timeval nowTime;
gettimeofday(&nowTime, NULL);
CWaterLog::Instance()->WriteLog(nowTime, 1, (char *)"", 0, -1, data);
return -1;
}
else
{
/*LogDebug("Success to enqueue to CCD, total_len:%d, ccd_header:%d.", totallen, CCD_HEADER_LEN);*/
timeval nowTime;
gettimeofday(&nowTime, NULL);
CWaterLog::Instance()->WriteLog(nowTime, 1, (char *)"", 0, 0, data);
}
return 0;
}
int32_t CMCDProc::InitSendPing()
{
char uri[HTTP_PACKET_MAX_LEN] = {0};
char *httpPostFmt = ""
"GET /api/v2/configs/ping?name=yiServer HTTP/1.1\r\n"
"Host:%s\r\n"
"User-Agent:curl/7.45.0\r\n"
"Accept:*/*\r\n"
"\r\n";
snprintf(uri, HTTP_PACKET_MAX_LEN, httpPostFmt, m_cfg._config_domin.c_str());
LogTrace("===>uri: %s", uri);
unsigned msg_len = strlen(uri);
TDCCHeader* header = (TDCCHeader*)m_send_buf;
char* data_buff = m_send_buf + DCC_HEADER_LEN;
unsigned data_max = BUFF_SIZE- DCC_HEADER_LEN;
memcpy(data_buff, uri, msg_len);
data_buff += msg_len;
unsigned iip = INET_aton(m_cfg._config_ip.c_str());
unsigned long long flow = make_flow64(iip, m_cfg._config_port);
header->_type = dcc_req_send;
header->_ip = iip;
header->_port = m_cfg._config_port;
int totallen = DCC_HEADER_LEN + msg_len;
if (m_mq_mcd_2_dcc->enqueue(header, totallen, flow))
{
LogError("Failed to Enqueue to DCC!");
timeval nowTime;
gettimeofday(&nowTime, NULL);
CWaterLog::Instance()->WriteLog(nowTime, 1, (char *)m_cfg._config_ip.c_str(), m_cfg._config_port, -1, uri);
return -1;
}
else
{
LogDebug("Success to Enqueue HTTP to DCC, total_len:%d, dcc_header_len:%d.", totallen, DCC_HEADER_LEN);
timeval nowTime;
gettimeofday(&nowTime, NULL);
CWaterLog::Instance()->WriteLog(nowTime, 1, (char *)m_cfg._config_ip.c_str(), m_cfg._config_port, 0, uri);
return 0;
}
}
#if 0
int32_t CMCDProc::EnququeConfigHttp2DCC()
{
char uri[HTTP_PACKET_MAX_LEN] = {0};
char *httpPostFmt = ""
"POST /api/v2/configs/getConfigForIM HTTP/1.1\r\n"
"Host:%s\r\n"
"User-Agent:curl/7.45.0\r\n"
"Content-Length:%d\r\n"
"Accept:*/*\r\n"
"\r\n"
"%s";
unsigned msg_seq = GetMsgSeq();
string seqStr = ui2str(msg_seq);
Json::Value data;
Json::Value req;
string reqStr;
data["identity"] = "";
data["name"] = "yiServer";
req["cmd"] = "getConfigForIM";
req["data"] = data;
req["seq"] = seqStr;
reqStr = req.toStyledString();