Repository navigation
Expand file tree
/
Copy pathlogin_impl.cpp
More file actions
1862 lines (1601 loc) · 61.5 KB
/
Copy pathlogin_impl.cpp
File metadata and controls
1862 lines (1601 loc) · 61.5 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
#include <errno.h>
extern "C" {
#include <kf/player_attr.hpp>
#include <libtaomee/tm_dirty/tm_dirty.h>
#include <async_serv/dll.h>
}
#include "login_impl.hpp"
#include "kf/vip_config_data_mgr.hpp"
using namespace taomee;
#define gf_dll_handle
#define gf_client_cmd
#define gf_switch
#define gf_del_server
#define gf_call_back
#define gf_utility
const char *version_str="2010-08-05 22:00";
//----------------------------------------------------------
//inline func
//----------------------------------------------------------
/**
* @brief add here for platform of statstics
*/
inline void do_stat_log_child_lv_num(userid_t userid, uint32_t lv)
{
statistic_msglog(gf_report_child_lv_num + lv, &userid, sizeof(userid));
TRACE_LOG("stat log: [cmd=%x] [lv=%u] [buf=%u]",
gf_report_child_lv_num , lv, userid);
}
inline void do_master_num(userid_t userid)
{
statistic_msglog(0x09819105, &userid, sizeof(userid));
}
inline void do_enter_del_role()
{
uint32_t buf[2] = {1 , 0};
statistic_msglog(0x0901040D, &buf, sizeof(buf));
}
inline void do_leave_del_role()
{
uint32_t buf[2] = {0 , 1};
statistic_msglog(0x0901040D, &buf, sizeof(buf));
}
/**
* @brief add here for platform of statstics
*/
inline void do_stat_log_interface(uint32_t cmd, userid_t userid, uint32_t role_type, uint32_t exp)
{
uint32_t buf[2] = {0};
buf[0] = userid;
buf[1] = exp;
statistic_msglog(cmd+role_type , buf, sizeof(buf));
TRACE_LOG("stat log: [cmd=%x] [type=%u] [buf=%u %u]",
gf_report_user_role_exp, role_type, buf[0], buf[1]);
}
#ifdef gf_dll_handle
//-----------------------------------------------------------
// dll_handles definations
//-----------------------------------------------------------
/**
* @brief 副登录如果需要初始化额外的内容,比如说读取配置文件等等,可以通过实现这个接口来完成
* @return 正常的话,返回true,如果出现任何初始化失败的状况,请返回false。
*/
bool KfLogin::init_service()
{
INFO_LOG("ol cur VERSION:%s", version_str);
if (tm_load_dirty("./data/tm_dirty.dat") < 0) {
ERROR_LOG("Failed to load dirty word!");
return -1;
}
init_timer();
my_read_conf();
multicast_init();
//vip_config_data_mgr::getInstance()->init();
mgr = new vip_config_data_mgr();
mgr->init();
return true;
}
/**
* @brief 副登录如果需要清理额外的内容,比如说释放内存等等,可以通过实现这个接口来完成
*/
void KfLogin::fini_service()
{
//vip_config_data_mgr::getInstance()->final();
mgr->final();
delete mgr;
mgr = NULL;
return;
}
/**
* @brief 副登录如果需要解析其它服务器来包(比如说Switch)的长度,可以通过实现这个接口来完成。
* 一般来说,因为服务器来包包头都一样,可以直接在这里调用get_dbproxy_pkg_len。
* @return 解析出来的数据包长度,或者如果失败的话,返回-1
*/
int KfLogin::get_other_svrs_pkg_len(int fd, const void* avail_data, int)
{
return get_dbproxy_pkg_len(avail_data);
}
/**
* @brief 副登录如果需要处理其它的客户端协议命令,比如说拉去服务器列表/创建角色,可以通过实现这个接口来完成
* @return 一切正常的话,返回0,如果客户端发包非法,或者处理过程中出现系统级的错误,请返回-1
*/
int KfLogin::proc_other_cmds(usr_info_t* usr, uint8_t* body, uint32_t bodylen)
{
TRACE_LOG("proc_other_cmds uid=%u, cmd=%u, len=%u", usr->uid, usr->waitcmd, bodylen);
switch (usr->waitcmd) {
case proto_get_recommend_svr_list:
return get_recommeded_svr_list_cmd(usr, body, bodylen);
case proto_get_ranged_svr_list:
return get_ranged_svr_list_cmd(usr, body, bodylen);
case proto_get_role_list:
return get_role_list_cmd(usr, body, bodylen);
case proto_create_role:
return create_role_cmd(usr, body, bodylen);
case proto_gray_delete_role:
return gray_delete_role_cmd(usr, body, bodylen);
// case proto_logic_delete_role:
// return logic_delete_role_cmd(usr, body, bodylen);
case proto_resume_gray_role:
return resume_gray_role_cmd(usr, body, bodylen);
case proto_get_version:
return get_version_cmd(usr, body, bodylen);
/*
INVITE_VESION
case proto_check_invite_code:
return check_invite_code_cmd(usr, body, bodylen);
case proto_create_role_with_inv_code:
return create_role_with_invite_code_cmd(usr, body, bodylen);//create_role_cmd(usr, body, bodylen);
*/
default:
ERROR_LOG("invalid command=%u", usr->waitcmd);
}
return -1;
}
/**
* @brief 副登录如果需要处理其它的dbproxy回报,比如说创建角色的返回包,可以通过实现这个接口来完成
* @return 一切正常的话,返回0,如果DBproxy返回包非法,或者处理过程中出现系统级的错误,请返回-1
*/
int KfLogin::proc_other_dbproxy_return(usr_info_t* p, uint16_t cmd, const uint8_t* body, uint32_t bodylen, uint32_t ret)
{
TRACE_LOG("proc_other_dbproxy_return cmd:%02X id:%u ret:%u", cmd, p->uid, ret);
int err = -1;
switch(cmd) {
case db_gf_login:
err = login_gf_callback(p, body, bodylen, ret);
break;
case db_gf_get_role_list:
err = get_role_list_callback(p, body, bodylen, ret);
break;
case db_gf_register:
err = gf_create_role_callback(p, body, bodylen, ret);
break;
/*case db_gf_add_game_flag:
case db_gf_add_game_flag_ex:
err = add_game_flag_callback(p, body, bodylen, ret);
break;*/
case db_gf_add_childid:
err = add_childid_callback(p, body, bodylen, ret);
break;
#ifdef INVITE_CODE_VESION
case db_userinfo_get_mee_fans:
err = get_mee_fans_callback(p, body, bodylen, ret);
break;
case db_other_check_invite_code:
err = check_invite_code_callback(p, body, bodylen, ret);
break;
case db_other_check_user_invited:
err = check_user_invited_callback(p, body, bodylen, ret);
break;
#endif
case db_sess_check_session:
err = check_session_callback(p, body, bodylen, ret);
break;
case db_gf_get_amb_info:
err = get_amb_info_callback(p, body, bodylen, ret);
break;
//delete role
case db_gf_gray_delete_role:
do_enter_del_role();
err = gray_delete_role_callback(p, body, bodylen, ret);
break;
// case db_gf_logic_delete_role:
// err = logic_delete_role_callback(p, body, bodylen, ret);
// break;
case db_gf_resume_gray_role:
do_leave_del_role();
err = resume_gray_role_callback(p, body, bodylen, ret);
//err = delete_role_callback(p, body, bodylen, ret);
break;
case db_gf_verify_pay_passwd:
err = verify_pay_passwd_callback(p, body, bodylen, ret);
break;
default:
ERROR_LOG("err db cmd [%d %d]", cmd, p->uid);
}
if(err) {
return -1;
}
return 0;
}
/**
* @brief 副登录如果需要处理其它服务器的返回,比如说Switch的返回,可以通过实现这个接口来完成
*/
void KfLogin::proc_other_svrs_return(int fd, void* dbpkg, uint32_t pkglen)
{
#if 1
if (fd == m_switch_fd) {
TRACE_LOG("m_switch_fd svrs return %u %u", fd, pkglen);
handle_switch_return(reinterpret_cast<svr_proto_t*>(dbpkg), pkglen);
} else if (fd == m_deluser_fd) {
TRACE_LOG("m_deluser_fd svrs return %u %u", fd, pkglen);
handle_deluser_return(reinterpret_cast<svr_proto_t*>(dbpkg), pkglen);
}
#endif
}
/**
* @brief 副登录如果需要处理其它服务器断开连接的情况,比如说Switch断开连接,可以通过实现这个接口来完成
*/
void KfLogin::on_other_svrs_fd_closed(int fd)
{
if (fd == m_switch_fd) {
DEBUG_LOG("SWITCH CONNECTION CLOSED\t[fd=%d]", m_switch_fd);
m_switch_fd = -1;
} else if (fd == m_timestamp_fd) {
DEBUG_LOG("CACHE SERVER CONNECTION CLOSED\t[fd=%d]", m_timestamp_fd);
m_timestamp_fd = -1;
} else if (fd == m_deluser_fd) {
DEBUG_LOG("GF DELUSER SERVER CONNECTION CLOSED\t[fd=%d]", m_deluser_fd);
m_deluser_fd = -1;
}
}
#endif
#ifdef gf_client_cmd
//-----------------------------------------------------------
// client definations
//-----------------------------------------------------------
//--! recv
int KfLogin::check_invite_code_cmd(usr_info_t* p, uint8_t* body, uint32_t bodylen)
{
CHECK_VAL(bodylen, max_invi_code_len);
DEBUG_LOG("check_invite_code:%u %s", p->uid, reinterpret_cast<char*>(body));
return send_to_gf_db(p, 0, db_other_check_invite_code, body, max_invi_code_len);
}
int KfLogin::get_role_list_cmd(usr_info_t* p, uint8_t* body, uint32_t bodylen)
{
CHECK_VAL(bodylen, 16);
memcpy(p->tmpinfo, body, bodylen);
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
//varify the session
login_session_t sess;
(void)unpkg_auth(body, 16, &sess);
if ( verify_session(p->uid, &sess) != gf_success) {
return gf_close_conn;
}
TRACE_LOG("%u %u", p->uid, bodylen);
uint32_t role_tm = 0;
return send_to_dbproxy(p, p->uid, db_gf_get_role_list, &role_tm, sizeof(role_tm));
}
return 0;
}
int KfLogin::create_role_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, sizeof(role_info_t));
role_info_t *role_info = (role_info_t*)body;
role_info->parentid = ntohl(role_info->parentid);
role_info->channel = ntohl(role_info->channel);
role_info->parent_role_tm = ntohl(role_info->parent_role_tm);
memcpy(p->tmpinfo, body, bodylen);
//DEBUG_LOG("%u puid: %u %u", p->uid, role_info->parentid, role_info->channel);
DEBUG_LOG("%u puid: %u %u %u", p->uid, role_info->parentid, role_info->parent_role_tm, role_info->channel);
if (db_check_session(p, gf_game_flag, (uint8_t*)(role_info->sess), gf_sess_len, 0) != 0) {
char nick[gf_nick_len + 1];
memcpy(nick, role_info->nick, gf_nick_len);
nick[gf_nick_len] = '\0';
if( check_dirty_word(p, nick)!=0 ) {
return 0;
}
role_info->role_type = ntohl(role_info->role_type);
if( check_role_type(p, role_info->role_type) ) {
return -1;
}
login_session_t sess;
struct gf_register_in reg;
memcpy(reg.nick, role_info->nick, gf_nick_len);
reg.role_tm = time(NULL);
reg.role_type = role_info->role_type;
reg.level = 1;
reg.hp = calc_hp[reg.role_type]( calc_body_quality[reg.role_type](reg.level) );
reg.mp = calc_mp[reg.role_type]( calc_stamina[reg.role_type](reg.level) );
reg.parent_role_tm = role_info->parent_role_tm;
//varify the session
(void)unpkg_auth((uint8_t*)role_info->sess, 16, &sess);
if ( verify_session(p->uid, &sess) != gf_success) {
return gf_close_conn;
}
if (reg.role_type == 4) {
do_stat_log_interface(gf_report_dragon_exp, p->uid, 0, 0);
} else {
do_stat_log_interface(gf_report_user_role_exp, p->uid, reg.role_type, 0);
}
DEBUG_LOG("LOCAL SESSION ROLE REGISTER\t[uid=%u, role_type=%d, nick=%16s, level=%d, hp=%u, mp=%u]",
p->uid, reg.role_type, reg.nick, reg.level, reg.hp ,reg.mp);
#ifdef VERSION_KAIXIN
uint32_t tmp_buf[4];
tmp_buf[0] = 0;
tmp_buf[1] = 0;
tmp_buf[2] = 0;
tmp_buf[3] = 0;
tmp_buf[reg.role_type - 1] = 1;
statistic_msglog(gf_role_type_distr, tmp_buf, sizeof(tmp_buf));
DEBUG_LOG("regrole\t[uid=%u, role_type=%d]",
p->uid, reg.role_type);
#endif
if (p->create_role_flag) {
return send_to_dbproxy(p, p->uid, db_gf_register, ®, sizeof(gf_register_in));
} else {
return do_get_amb_info(p);
}
} else {
return 0;
}
}
int KfLogin::create_role_with_invite_code_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, sizeof(role_info_t));
memcpy(p->tmpinfo, body, bodylen);
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
role_info_t *role_info = (role_info_t*)body;
//varify the session
login_session_t sess;
(void)unpkg_auth((uint8_t*)role_info->sess, 16, &sess);
if ( verify_session(p->uid, &sess) != gf_success) {
return gf_close_conn;
}
DEBUG_LOG("LOCAL SESSION ROLE REGISTER\t[uid=%u]",
p->uid);
#ifdef INVITE_CODE_VESION
return do_check_user_invited(p);
#else
return do_add_childid(p, 0, 0, 0);
#endif
//return send_to_dbproxy(p, p->uid, db_userinfo_get_mee_fans, 0, 0);
//return send_to_gf_db(p, 0, db_other_check_user_invited, 0, 0);
}
return 0;
}
int KfLogin::gray_delete_role_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, 52);
memcpy(p->tmpinfo, body, bodylen);
gf_gay_del_role_in* p_in = (gf_gay_del_role_in*)(p->tmpinfo);
p_in->role_tm = ntohl(p_in->role_tm);
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
return gf_close_conn;
}
return 0;
}
int KfLogin::logic_delete_role_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, 20);
memcpy(p->tmpinfo, body, bodylen);
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
return gf_close_conn;
}
return 0;
}
int KfLogin::resume_gray_role_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, 20);
memcpy(p->tmpinfo, body, bodylen);
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
return gf_close_conn;
}
return 0;
}
int KfLogin::get_version_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
m_clipkg->init(p->waitcmd, p->uid, gf_success);
m_clipkg->pack(version_str, sizeof(version_str));
return send_to_user(p);
}
int KfLogin::get_recommeded_svr_list_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
CHECK_VAL(bodylen, 28);
char *sessbody = (char*)body;
p->channel = ntohl(*(uint32_t*)(body + gf_sess_len));
uint32_t role_tm = ntohl(*(uint32_t*)(body + gf_sess_len + 4));
uint32_t role_lv = ntohl(*(uint32_t*)(body + gf_sess_len + 8));
gf_login_out *user_login_info = reinterpret_cast<gf_login_out*>(p->tmpinfo);
user_login_info->vip_flag = 0;
user_login_info->friendcount = 0;
DEBUG_LOG("RECOMMD SVR CHECK SESS\t[uid=%d,role_tm=%u channel=%u]", p->uid, role_tm, p->channel);
if (p->uid != 0) {
if (db_check_session(p, gf_game_flag, body, gf_sess_len, 0) != 0) {
login_session_t sess;
//varify the session
unpkg_auth((uint8_t*)sessbody, 16, &sess);
if ( verify_session(p->uid, &sess) != gf_success ) {
return gf_close_conn;
}
return send_to_dbproxy(p, p->uid, db_gf_login, &role_tm, sizeof(role_tm));
}
memcpy(p->tmpinfo, body, bodylen);
return 0;
} else {
return get_recommended_svr_list(p, role_lv);
}
}
int KfLogin::get_ranged_svr_list_cmd(usr_info_t *p, uint8_t *body, uint32_t bodylen)
{
TRACE_LOG("enter get_ranged_svr_list_cmd");
//check protocol defined data, to prevent invalid access.
CHECK_VAL_GE(bodylen, 12);
//get the client's request package body
//uint32_t friendcount;
uint32_t startid;
uint32_t endid;
int idx = 0;
taomee::unpack(body, startid, idx);
taomee::unpack(body, endid, idx);
//if id is invalid, then make notification to the flash client.
if ((startid < gf_min_valid_svr) || (endid > gf_max_valid_svr)
|| (startid > endid) || ((endid - startid) > 100)) {
INFO_LOG("INVALID SVR ID\t[startid=%u endid=%u uid=%u]", startid, endid, p->uid);
m_clipkg->init(p->waitcmd, p->uid, gf_success);
m_clipkg->pack(static_cast<uint32_t>(0));
return send_to_user(p);
}
DEBUG_LOG("RANGED SVR\t[uid=%u startid=%u endid=%u]", p->uid, startid, endid);
//make a request to switch server, and get the response data.
return get_ranged_svr_list(p, startid, endid);
}
//--! send
int KfLogin::send_head_to_user(usr_info_t* p,uint32_t cmd, uint32_t err_no)
{
m_clipkg->init(cmd, p->uid, err_no);
TRACE_LOG("send head to user %u %u %u", cmd, p->waitcmd, err_no);
return send_to_user(p);
}
int KfLogin::send_error_to_user(usr_info_t* p, uint32_t err_no)
{
return send_head_to_user(p, p->waitcmd, err_no);
}
int KfLogin::send_to_gf_db(usr_info_t* p, uint32_t role_tm, uint16_t cmd, const void* dbpkgbuf, uint32_t body_len)
{
*(uint32_t*)m_gf_db_sendbuf = role_tm;
memcpy(m_gf_db_sendbuf + sizeof(role_tm), dbpkgbuf, body_len);
return send_to_dbproxy(p, p->uid, cmd, m_gf_db_sendbuf, body_len + sizeof(role_tm));
}
int KfLogin::send_to_gf_db(usr_info_t* p, uint32_t userid, uint32_t role_tm, uint16_t cmd, const void* dbpkgbuf, uint32_t body_len)
{
*(uint32_t*)m_gf_db_sendbuf = role_tm;
memcpy(m_gf_db_sendbuf + sizeof(role_tm), dbpkgbuf, body_len);
return send_to_dbproxy(p, userid, cmd, m_gf_db_sendbuf, body_len + sizeof(role_tm));
}
int KfLogin::send_recommeded_svr_list(usr_info_t* p, svr_info_t* svrs, uint32_t online_cnt, int max_online_id)
{
m_clipkg->init(proto_get_recommend_svr_list, p->uid, gf_success);
m_clipkg->pack(static_cast<uint32_t>(max_online_id));
m_clipkg->pack(*(uint32_t*)(p->tmpinfo));
m_clipkg->pack(online_cnt);
TRACE_LOG("max_online=%u online_cnt=%u",max_online_id,online_cnt);
for (uint32_t i = 0; i != online_cnt; ++i) {
m_clipkg->pack(svrs[i].id);
m_clipkg->pack(svrs[i].users);
m_clipkg->pack(svrs[i].ip, 16);
m_clipkg->pack(svrs[i].port);
m_clipkg->pack(svrs[i].domain);
m_clipkg->pack(svrs[i].friends);
TRACE_LOG("online=[%u %u %16s %u %u]",svrs[i].id,svrs[i].users,svrs[i].ip,svrs[i].port,svrs[i].friends);
}
// pkg the frd id and timestamp
uint32_t friendcount = *(uint32_t*)(p->tmpinfo + 4);
uint32_t blackcount = *(uint32_t*)(p->tmpinfo + 8);
uint32_t* friend_id = (uint32_t*)(p->tmpinfo + 12);
uint32_t* black_id = (uint32_t*)(p->tmpinfo + 12 + sizeof(userid_t) * friendcount);
m_clipkg->pack(friendcount);
TRACE_LOG("friend_cnt=%u",friendcount);
for (uint32_t i = 0; i < friendcount; i++ ) {
m_clipkg->pack(friend_id[i]);
TRACE_LOG("friend_id=%u",friend_id[i]);
}
m_clipkg->pack(blackcount);
TRACE_LOG("black_cnt=%u",blackcount);
for (uint32_t i = 0; i < blackcount; i++ ) {
m_clipkg->pack(black_id[i]);
TRACE_LOG("black_id=%u",black_id[i]);
}
statistic_info_t info;
uint32_t msglog_type;
info.user_id = p->uid;
msglog_type = gf_login_offset + p->channel;
statistic_msglog(msglog_type, (void *)&info, sizeof info);
TRACE_LOG("statictic msglog :%s:msgid:%x uid:%u p->channel:%u", stat_file, msglog_type, p->uid, p->channel);
return send_to_user(p);
}
int KfLogin::send_ranged_svrlist(usr_info_t* p, svr_info_t* svrs, uint32_t online_cnt)
{
//make a response package to the flash client
m_clipkg->init(proto_get_ranged_svr_list, p->uid, gf_success);
m_clipkg->pack(online_cnt);
for (uint32_t i = 0; i != online_cnt; ++i) {
m_clipkg->pack(svrs[i].id);
m_clipkg->pack(svrs[i].users);
m_clipkg->pack(svrs[i].ip, 16);
m_clipkg->pack(svrs[i].port);
m_clipkg->pack(svrs[i].domain);
m_clipkg->pack(svrs[i].friends);
TRACE_LOG("ranged online=[%u %u %16s %u %u]",svrs[i].id, svrs[i].users, svrs[i].ip, svrs[i].port, svrs[i].friends);
}
return send_to_user(p);
}
#endif
#ifdef gf_db_proxy
//-----------------------------------------------------------
// db_proxy definations
//-----------------------------------------------------------
#endif
#ifdef gf_switch
//-----------------------------------------------------------
// switch definations
//-----------------------------------------------------------
int switch_timeout(void* owner, void* data)
{
usr_info_t* p = reinterpret_cast<usr_info_t*>(owner);
KfLogin* plogin = reinterpret_cast<KfLogin*>(data);
TRACE_LOG("SWITCH TIMEOUT [%d %d]", p->uid, p->waitcmd);
if(p->waitcmd) {
plogin->get_backup_svr_list(p);
}
//send_warning(p, warning_switch, p->waitcmd);
uint32_t nip = get_remote_ip(plogin->m_switch_fd);
char ip[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, &nip, ip, INET_ADDRSTRLEN);
send_warning("switch", p->uid, p->waitcmd, 0, ip);
return 0;
}
int KfLogin::send_to_switch(usr_info_t* p, const uint8_t *buf, uint32_t length)
{
if (m_switch_fd == -1) {
m_switch_fd = connect_to_service(m_my_opt.master_ser, 0, 65535, 1);
TRACE_LOG("connect to switch %s fd=%d", m_my_opt.master_ser, m_switch_fd);
}
if (m_switch_fd == -1 && p) {
TRACE_LOG("connect to switch failed %s", m_my_opt.master_ser);
return get_backup_svr_list(p);
}
if(p) {
ADD_TIMER_EVENT(p, switch_timeout, this, get_now_tv()->tv_sec + 10);
}
return net_send(m_switch_fd , buf, length);
}
/**
* @brierf Send a request package to switch server, then get the respond, but do not parse it.
* @param uid. user id.
* @param friendcount. haw many friends.
* @param allfriendid. store the ids in an array
* @return int. 0 on success. less than 0 on error.
*/
int KfLogin::get_ranged_svr_list(usr_info_t* p, int start_id, int end_id)
{
TRACE_LOG("enter get_ranged_svr_list");
// pack up the request data
int friendcount = 0;
int idx = sizeof(svr_proto_t);
taomee::pack_h(m_login_switch_sendbuf, static_cast<uint16_t>(0), idx);
taomee::pack_h(m_login_switch_sendbuf, start_id, idx);
taomee::pack_h(m_login_switch_sendbuf, end_id, idx);
taomee::pack_h(m_login_switch_sendbuf, friendcount, idx);
init_switch_head(p, m_login_switch_sendbuf, idx, gf_switch_get_ranged_svr_list);
return send_to_switch(p, m_login_switch_sendbuf, idx);
}
int KfLogin::get_backup_svr_list(usr_info_t* p)
{
TRACE_LOG("enter get backup svr list");
switch(p->waitcmd) {
case proto_get_recommend_svr_list:
return send_recommeded_svr_list(p, m_backup_svrlist, m_backup_svrlist_cnt, m_backup_svrlist_cnt);
case proto_get_ranged_svr_list:
return send_ranged_svrlist(p, m_backup_svrlist, m_backup_svrlist_cnt);
default:
ERROR_LOG("invalid command=%u", p->waitcmd);
}
return 0;
}
/**
* @brierf Send a request package to switch server, then get the respond, but do not parse it.
* @param uid. user id.
* @param friendcount. haw many friends.
* @param allfriendid. store the ids in an array
* @return int. 0 on success. less than 0 on error.
*/
int KfLogin::get_recommended_svr_list(usr_info_t* p, uint32_t lv)
{
TRACE_LOG("enter get_recommended_svr_list %u %u", p->uid, lv);
// pack up the request data
int idx = sizeof(svr_proto_t);
uint32_t friendcount = *(uint32_t*)(p->tmpinfo + 4);
taomee::pack_h(m_login_switch_sendbuf, static_cast<uint16_t>(m_my_opt.dx_or_wt), idx);
taomee::pack_h(m_login_switch_sendbuf, static_cast<uint8_t>(1), idx);
taomee::pack_h(m_login_switch_sendbuf, lv, idx);
taomee::pack_h(m_login_switch_sendbuf, friendcount, idx);
taomee::pack(m_login_switch_sendbuf, (void*)(p->tmpinfo + 12), friendcount * sizeof(userid_t), idx);
init_switch_head(p, m_login_switch_sendbuf, idx, gf_switch_get_recommend_svr_list);
return send_to_switch(p, m_login_switch_sendbuf, idx);
}
void KfLogin::init_switch_head(usr_info_t* p, void* buf, uint32_t len, uint32_t cmd)
{
svr_proto_t* pkg = reinterpret_cast<svr_proto_t*>(buf);
pkg->len = len;
pkg->seq = (p->session->fd << 16) | p->counter;
pkg->cmd = cmd;
pkg->ret = 0;
pkg->id = p->uid;
}
int update_backup_svrlist(void* owner, void* data)
{
KfLogin* plogin = reinterpret_cast<KfLogin*>(data);
int idx = sizeof(svr_proto_t);
taomee::pack_h(plogin->m_login_switch_sendbuf, static_cast<uint16_t>(0), idx);
taomee::pack_h(plogin->m_login_switch_sendbuf, gf_min_backup_svr, idx);
taomee::pack_h(plogin->m_login_switch_sendbuf, gf_max_backup_svr, idx);
taomee::pack_h(plogin->m_login_switch_sendbuf, static_cast<uint32_t>(0), idx);
svr_proto_t* pkg = reinterpret_cast<svr_proto_t*>(plogin->m_login_switch_sendbuf);
pkg->len = idx;
if(plogin->m_switch_fd == -1) {
plogin->m_switch_fd = connect_to_service(plogin->m_my_opt.master_ser, 0, 65535, 1);
}
if(plogin->m_switch_fd == -1) {
ADD_TIMER_EVENT(&(plogin->m_get_backup_tmr), update_backup_svrlist, plogin, get_now_tv()->tv_sec + 60);
return 0;
}
pkg->seq = (0xFFFF<< 16) | 0;
pkg->cmd = gf_switch_get_ranged_svr_list;
pkg->ret = 0;
pkg->id = 0;
plogin->send_to_switch(NULL, plogin->m_login_switch_sendbuf, idx);
//TRACE_LOG("update backup svrlist");
ADD_TIMER_EVENT(&(plogin->m_get_backup_tmr), update_backup_svrlist, plogin, get_now_tv()->tv_sec + 60);
return 0;
}
#endif
#ifdef gf_del_server
//-----------------------------------------------------------
// del_server definations
//-----------------------------------------------------------
int mydeluser_timeout(void* owner, void* data)
{
usr_info_t* p = reinterpret_cast<usr_info_t*>(owner);
KfLogin* plogin = reinterpret_cast<KfLogin*>(data);
ERROR_LOG("DELUSER TIMEOUT [%d %d]", p->uid, p->waitcmd);
if(p->waitcmd) {
uint32_t nip = get_remote_ip(plogin->m_deluser_fd);
char ip[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, &nip, ip, INET_ADDRSTRLEN);
send_warning("delser", p->uid, p->waitcmd, 0, ip);
// send_warning(p, warning_db, p->waitcmd);
plogin->send_error_to_user(p, clierr_system_error);
// send_to_self_error(p, clierr_system_error);
}
return 0;
}
int KfLogin::send_request_to_deluser(usr_info_t* p, userid_t uid, uint16_t cmd, const void* pkgbuf, uint32_t body_len)
{
static uint8_t buf[1024];
if (m_deluser_fd == -1) {
m_deluser_fd = connect_to_svr(m_my_opt.gf_deluser_ip,
m_my_opt.gf_deluser_port, 65535, 1);
}
if ((m_deluser_fd == -1) || (body_len > (sizeof(buf) - sizeof(svr_proto_t)))) {
ERROR_LOG("send to deluser failed: fd=%d len=%d", m_deluser_fd, body_len);
uint32_t nip = get_remote_ip(m_deluser_fd);
char ip[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, &nip, ip, INET_ADDRSTRLEN);
send_warning("delser", uid, p ? p->waitcmd : 0, 0, ip);
return -1;
}
svr_proto_t* pkg = reinterpret_cast<svr_proto_t*>(buf);
pkg->len = sizeof(svr_proto_t) + body_len;
pkg->seq = p ? ((p->session->fd<< 16) | p->counter) : 0;
pkg->cmd = cmd;
pkg->ret = 0;
pkg->id = uid;
//*(reinterpret_cast<uint32_t*>(pkg->body)) = 0;//role_tm
memcpy((pkg->body), pkgbuf, body_len);
DEBUG_LOG("DELUSER SEND\t[uid=%u cmd=0x%X]", uid ,cmd);
if (p) {
ADD_TIMER_EVENT(p, mydeluser_timeout, p, get_now_tv()->tv_sec + 10);
}
return net_send(m_deluser_fd, buf, pkg->len);
}
#endif
#ifdef gf_call_back
//-----------------------------------------------------------
// call back definations
//-----------------------------------------------------------
int KfLogin::check_session_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
if ((ret && ret != dberr_session_check && verify_login_session(p->uid, p->tmpinfo) == -1) ||
(ret == dberr_session_check)) {
ERROR_LOG("local:invalid session: uid=%u ret=%u", p->uid, ret);
//send_header_to_player(p, p->waitcmd, cli_err_invalid_session, 1);
return -1;
}
switch (p->waitcmd) {
case proto_get_role_list:
return do_get_role_list(p);
case proto_create_role:
case proto_create_role_with_inv_code:
{
const role_info_t *role_info = (role_info_t*)(p->tmpinfo);
if (p->create_role_flag || !(role_info->parentid) || !(role_info->parent_role_tm)) {
return do_create_role(p, 0);
} else {
return do_get_amb_info(p);
}
}
break;
case proto_get_recommend_svr_list:
return do_get_recommend_svr_list(p);
case proto_gray_delete_role:
return verify_pay_passwd(p);
case proto_resume_gray_role:
return do_resume_gray_role(p);
default:
break;
}
return -1;
}
int KfLogin::get_amb_info_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
role_info_t *role_info = (role_info_t*)p->tmpinfo;
if (ret) {
//if (p->create_role_flag || !(role_info->parentid)) {
return do_create_role(p, 0);
//} else {
// return do_add_childid(p, role_info->parentid);
//}
} else {
//uint32_t parentid = *(uint32_t*)(body + 4);
//role_info->parentid = parentid;
DEBUG_LOG("child create: %u parentid %u %u", p->uid, role_info->parentid, role_info->parent_role_tm);
return do_create_role(p, 1);
//return do_add_childid(p, role_info->parentid);
}
return -1;
}
int KfLogin::check_invite_code_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
if (ret)
{
TRACE_LOG("CHECK INVITE FAILED\t[err=%d uid=%u]", ret, p->uid);
switch (ret) {
case gf_invalid_invite_code_err:
DEBUG_LOG("invite invalid:%u", p->uid);
send_error_to_user(p, gf_invalid_invite_code);
break;
case gf_invite_code_used_err:
DEBUG_LOG("invite used:%u", p->uid);
send_error_to_user(p, gf_invite_code_used);
break;
default:
send_error_to_user(p, clierr_system_error);
ERROR_LOG("UNRESOLVABLE DB ERR\t[err=%d uid=%u]", ret, p->uid);
break;
}
return gf_success;
}
return send_error_to_user(p, gf_success);
}
int KfLogin::get_mee_fans_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
if (ret)
{
TRACE_LOG("CHECK USER INFO ERR\t[err=%d uid=%u] uid may not exist!", ret, p->uid);
return -1;
}
uint32_t mee_fans = *(uint32_t*)body;
if (p->waitcmd == proto_get_role_list) {
if (mee_fans) {
m_clipkg->init(proto_get_role_list, p->uid, gf_success);
m_clipkg->pack(0);
return send_to_user(p);
} else {
return do_check_user_invited(p);
}
} else if (p->waitcmd == proto_create_role_with_inv_code || p->waitcmd == proto_create_role) {
if (mee_fans) {
return do_add_childid(p, 0, 0, 0);
} else {
return do_check_user_invited(p);
}
}
return gf_success;
}
int KfLogin::check_user_invited_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
if (ret)
{
TRACE_LOG("CHECK USER INVITED FAILED\t[err=%d uid=%u]", ret, p->uid);
send_error_to_user(p, gf_user_never_input_invite_code);
return gf_success;
}
switch(p->waitcmd) {
case proto_get_role_list:
m_clipkg->init(proto_get_role_list, p->uid, gf_success);
m_clipkg->pack(0);
return send_to_user(p);
break;
case proto_create_role:
case proto_create_role_with_inv_code:
return do_add_childid(p, 0, 0, 0);
break;
default:
return -1;
break;
}
return gf_success;
}
int KfLogin::update_backup_svrlist_callback(uint8_t* body, uint32_t bodylen, int ret)
{
CHECK_VAL_GE(bodylen, sizeof(svr_list_t));
svr_list_t* svrlist = reinterpret_cast<svr_list_t*>(body);
//chk if the pkg len is valid
CHECK_VAL(bodylen, sizeof(svr_list_t) + svrlist->online_cnt * sizeof(svr_info_t));
m_backup_svrlist_cnt = svrlist->online_cnt;
memcpy(m_backup_svrlist, svrlist->svrs, svrlist->online_cnt * sizeof(svr_info_t));
//for(uint32_t i= 0; i< svrlist->online_cnt; i++) {
// DEBUG_LOG("BACKUP svr ID [%d]", backup_svrlist[i].id);
// DEBUG_LOG("BACKUP svr usr [%d]", backup_svrlist[i].users);
// DEBUG_LOG("BACKUP svr port[%d]", backup_svrlist[i].port);
//}
return 0;
}
int KfLogin::login_gf_callback(usr_info_t* p, const uint8_t* body, uint32_t bodylen, int ret)
{
if ( ret) {
DEBUG_LOG("LOGIN FAILED\t[err=%d uid=%u]", ret, p->uid);
switch (ret) {
case gf_user_id_nofind_err:
case gf_role_id_nofind_err:
send_error_to_user(p, gf_role_not_find);
break;
default:
send_error_to_user(p, clierr_system_error);
ERROR_LOG("UNRESOLVABLE DB ERR\t[err=%d uid=%u]", ret, p->uid);
break;
}
return gf_success;
//return gf_close_conn;
}
CHECK_VAL_GE(bodylen, 16);
const gf_login_out* user_login_info = reinterpret_cast<const gf_login_out*>(body);
uint32_t friendcount = user_login_info->friendcount;
uint32_t blackcount = user_login_info->blackcount;
CHECK_VAL(bodylen, 16 + 4 * friendcount + 4 * blackcount);
TRACE_LOG("uid=%u, frdcnt=%u, blkcnt=%u",p->uid,friendcount,blackcount);
if (friendcount > gf_max_friend_num) {
ERROR_LOG("severe db error[friendcount]: uid=%u frdcnt=%u", p->uid, friendcount);
friendcount = gf_max_friend_num;
}
if (blackcount > gf_max_friend_num) {
ERROR_LOG("severe db error[blackcount]: uid=%u frdcnt=%u", p->uid, blackcount);