-
Notifications
You must be signed in to change notification settings - Fork 36
/
net.c
4123 lines (3775 loc) · 108 KB
/
net.c
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
#define __NET_C__
#include "version.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
//ttom+1
#include <sys/timeb.h>
#include "net.h"
#include "buf.h"
#include "link.h"
#include "common.h"
#include "msignal.h"
#include "configfile.h"
#include "util.h"
#include "saacproto_cli.h"
#include "lssproto_serv.h"
#include "char.h"
#include "handletime.h"
#include "log.h"
#include "object.h"
#include "item_event.h"
#include "enemy.h"
// Arminius 7.31 cursed stone
#include "battle.h"
#include "version.h"
#include "pet_event.h"
#include "char_talk.h"
#include "petmail.h"
#ifdef _TEST_PETCREATE
#include "chatmagic.h"
#endif
#ifdef _M_SERVER
#include "mclient.h"
#endif
#ifdef _NPCSERVER_NEW
#include "npcserver.h"
#endif
#define MIN(x,y) ( ( (x) < (y) ) ? (x) : (y) )
#define IS_2BYTEWORD( _a_ ) ( (char)(0x80) <= (_a_) && (_a_) <= (char)(0xFF) )
#ifdef _NEW_SERVER_
BOOL bNewServer = TRUE;
#else
BOOL bNewServer = FALSE;
#endif
// Nuke +1 0901: For state monitor
int StateTable[WHILESAVEWAIT+1];
int ITEM_getRatio();
int CHAR_players();
#define CONO_CHECK_LOGIN 0x001
#define CONO_CHECK_ITEM 0x010
#define CONO_CHECK_PET 0x100
int cono_check=0x111;
int AC_WBSIZE = (65536*16);
//ttom+1 for the performatce
static unsigned int MAX_item_use=0;
int i_shutdown_time=0;//ttom
BOOL b_first_shutdown=FALSE;//ttom
int mfdfulll = 0;
/*------------------------------------------------------------
* 扔□田及橇谪
------------------------------------------------------------*/
typedef struct tag_serverState
{
BOOL acceptmore; /* 1分匀凶日}accept 仄凶丐午
切斤仁匹}close 允月 */
unsigned int fdid; /* fd 及骚曰袄 */
unsigned int closeallsocketnum; /* closeallsocket 及酸曰及
醒*/
int shutdown; /* 扔□田毛shutdown允月乒□玉
* 0:骚橘 公木动陆:扑乓永玄扑乓永玄乒□玉
* 乒□玉卞卅匀凶凛棉互 匀化月[
*/
int dsptime; /* shutdown 乒□玉及伐□弁 醒*/
int limittime; /* 仇木手 */
}ServerState;
typedef struct tagCONNECT
{
BOOL use;
#if USE_MTIO
//#define MTIO_FIXED_BUFSIZE (65536)
pthread_mutex_t mutex; /* Connect毛中元月午五卞勾井丹夫永弁 */
#endif
char *rb;
int rbuse;
char *wb;
int wbuse;
int check_rb_oneline_b;
int check_rb_time;
struct sockaddr_in sin; /* 涛粮燮及失玉伊旦 */
ConnectType ctype; /* 戊生弁扑亦件及潘 */
char cdkey[CDKEYLEN]; /* CDKEY */
char passwd[PASSWDLEN]; /* 由旦伐□玉 */
LoginType state; /* 蜇箕及夫弘奶件橇谪 */
int nstatecount;
char charname[CHARNAMELEN]; /* 夫弘奶件 及平乓仿 */
int charaindex; /* char 尺及奶件犯永弁旦[
* 夫弘奶件 卞袄互涩烂今木月[-1互犯白巧伙玄
* 卞卅中凛[
*/
char CAbuf[2048]; /* CA() 毛做谅允月啃及田永白央 */
int CAbufsiz; /* CAbuf 及扔奶术 */
struct timeval lastCAsendtime; /* 卞CA毛霜匀凶凛棉 */
char CDbuf[2048]; /* CD() 毛做谅允月啃及田永白央 */
int CDbufsiz; /* CDbuf 及扔奶术 */
struct timeval lastCDsendtime; /* 卞CD毛霜匀凶凛棉 */
struct timeval lastCharSaveTime; /* 卞平乓仿犯□正毛本□皮仄凶凛棉 */
struct timeval lastprocesstime; /* 卞皿夫玄戊伙毛质 仄凶凛棉*/
struct timeval lastreadtime; /* 卞read仄凶凛棉[晓午反切互丹*/
// Nuke start 08/27 : For acceleration avoidance
// WALK_TOLERANCE: Permit n W messages in a second (3: is the most restricted)
#define WALK_TOLERANCE 4
#define WALK_SPOOL 5
#define WALK_RESTORE 100
unsigned int Walktime;
unsigned int lastWalktime;
unsigned int Walkcount;
int Walkspool; // For walk burst after release key F10
int Walkrestore;
// B3_TOLERANCE: Time distance between recently 3 B message (8: is the latgest)
// BEO_TOLERANCE: Time distance between the lastmost B and EO (5: is the largest)
#define B3_TOLERANCE 5
#define BEO_TOLERANCE 3
#define BEO_SPOOL 10
#define BEO_RESTORE 100
unsigned int Btime;
unsigned int lastBtime;
unsigned int lastlastBtime;
unsigned int EOtime;
#ifdef _BATTLE_TIMESPEED
// unsigned int DefBtime;
int BDTime;
int CBTime;
#endif
#ifdef _TYPE_TOXICATION
int toxication;
#endif
#ifdef _CHECK_GAMESPEED
int gamespeed;
#endif
#ifdef _ITEM_ADDEXP //vincent 经验提升
int EDTime;
#endif
// unsigned int BEO;
int BEOspool;
int BEOrestore;
// Nuke 0219: Avoid cheating
int die;
// Nuke end
// Nuke 0310
int credit;
int fcold;
// Nuke 0406: New Flow Control
int nu;
int nu_decrease;
int ke;
// Nuke 1213: Flow Control 2
int packetin;
// Nuke 0624: Avoid Null Connection
unsigned int cotime;
// Nuke 0626: For no enemy
int noenemy;
// Arminius 7.2: Ra's amulet
int eqnoenemy;
#ifdef _Item_MoonAct
int eqrandenemy;
#endif
#ifdef _CHIKULA_STONE
int chistone;
#endif
// Arminius 7.31: cursed stone
int stayencount;
/* close 允月屯五井升丹井及筏盛 */
#if USE_MTIO
int closed;
#endif
int battlecharaindex[CONNECT_WINDOWBUFSIZE];
int duelcharaindex[CONNECT_WINDOWBUFSIZE];
int tradecardcharaindex[CONNECT_WINDOWBUFSIZE];
int joinpartycharaindex[CONNECT_WINDOWBUFSIZE];
// CoolFish: Trade 2001/4/18
int tradecharaindex[CONNECT_WINDOWBUFSIZE];
int errornum;
int fdid;
int close_request; //the second have this
int appendwb_overflow_flag; /* 1荚匹手appendWb互撩 仄凶日1卞允月 */
//ttom+1 avoidance the watch the battle be kept out
BOOL in_watch_mode;
BOOL b_shut_up;//for avoid the user wash the screen
BOOL b_pass; //for avoid the unlimited area
struct timeval Wtime;
struct timeval WLtime;
BOOL b_first_warp;
int state_trans;
// CoolFish: Trade 2001/4/18
char TradeTmp[256];
#ifdef _ITEM_PILEFORTRADE
int tradelist;
#endif
// Shan Recvdata Time
struct timeval lastrecvtime; // 'FM' Stream Control time
struct timeval lastrecvtime_d; // DENGON Talk Control time
// Arminius: 6.22 encounter
int CEP; // Current Encounter Probability
// Arminius 7.12 login announce
int announced;
// shan battle delay time 2001/12/26
struct timeval battle_recvtime;
#ifdef _NO_WARP
// shan hjj add Begin
int seqno;
int selectbutton;
// shan End
#endif
BOOL confirm_key; // shan trade(DoubleCheck)
#ifdef _BLACK_MARKET
int BMSellList[12];
#endif
}
CONNECT;
CONNECT *Connect; /*戊生弁扑亦件忡切迕*/
/* 楮醒及燮 卞勾仃化歹井月方丹卞允月分仃及穴弁夫 */
#define SINGLETHREAD
#define MUTLITHREAD
#define ANYTHREAD
ServerState servstate;
#if USE_MTIO
pthread_mutex_t MTIO_servstate_m; /* servstate 及夫永弁 */
#define SERVSTATE_LOCK() pthread_mutex_lock( &MTIO_servstate_m );
#define SERVSTATE_UNLOCK() pthread_mutex_unlock( &MTIO_servstate_m );
#if 0
#define CONNECT_LOCK_ARG2(i,j) fprintf(stderr,"LO T:%d(%d:%d) %s %d", (int)pthread_self(), i,j, __FILE__,__LINE__ );pthread_mutex_lock( &Connect[i].mutex );fprintf( stderr, "CK T:%d(%d:%d)\n" , (int)pthread_self(), i,j );
#define CONNECT_UNLOCK_ARG2(i,j) fprintf(stderr,"UNLO T:%d(%d:%d) %s %d", (int)pthread_self(), i,j, __FILE__,__LINE__ );pthread_mutex_unlock( &Connect[i].mutex );fprintf( stderr, "CK T:%d(%d:%d)\n",(int)pthread_self(), i,j);
#define CONNECT_LOCK(i) fprintf(stderr,"LO T:%d(%d) %s %d", (int)pthread_self(), i, __FILE__,__LINE__ );pthread_mutex_lock( &Connect[i].mutex );fprintf( stderr, "CK T:%d(%d)\n" ,(int)pthread_self(), i );
#define CONNECT_UNLOCK(i) fprintf(stderr,"UNLO T:%d(%d) %s %d", (int)pthread_self(), i, __FILE__,__LINE__ );pthread_mutex_unlock( &Connect[i].mutex );fprintf( stderr, "CK T:%d(%d)\n",(int)pthread_self(), i);
/* 晓筏及夫弘反户切扎仁切扎犯奴旦弁毛罹丹及匹旦伉□皿毛中木凶曰 */
#define MTIO_DEBUG_LOG_REDUCE 1
#else
#define CONNECT_LOCK_ARG2(i,j)if(i>=0 && i< ConnectLen)pthread_mutex_lock( &Connect[i].mutex );
#define CONNECT_UNLOCK_ARG2(i,j) if(i>=0 && i< ConnectLen)pthread_mutex_unlock( &Connect[i].mutex );
#define CONNECT_LOCK(i) if(i>=0 && i< ConnectLen)pthread_mutex_lock( &Connect[i].mutex );
#define CONNECT_UNLOCK(i) if(i>=0 && i< ConnectLen)pthread_mutex_unlock( &Connect[i].mutex );
#define MTIO_DEBUG_LOG_REDUCE 0
#endif
#else
#define SERVSTATE_LOCK()
#define SERVSTATE_UNLOCK()
#define CONNECT_LOCK_ARG2(i,j)
#define CONNECT_UNLOCK_ARG2(i,j)
#define CONNECT_LOCK(i)
#define CONNECT_UNLOCK(i)
#endif
#ifdef _CHECK_BATTLE_IO
int InBattleLoop =FALSE;
int battle_write =0;
int other_write =0;
int battle_write_cnt =0;
int other_write_cnt =0;
#endif
/*------------------------------------------------------------
* servstate毛赓渝祭允月[
* 娄醒}忒曰袄
* 卅仄
------------------------------------------------------------*/
ANYTHREAD static void SERVSTATE_initserverState( void )
{
SERVSTATE_LOCK();
servstate.acceptmore = TRUE;
servstate.fdid = 0;
servstate.closeallsocketnum = -1;
servstate.shutdown = 0;
servstate.limittime = 0;
servstate.dsptime = 0;
SERVSTATE_UNLOCK();
}
ANYTHREAD int SERVSTATE_SetAcceptMore( int nvalue )
{
BOOL buf;
SERVSTATE_LOCK();
buf = servstate.acceptmore;
servstate.acceptmore = nvalue;
SERVSTATE_UNLOCK();
return buf;
}
ANYTHREAD static int SERVSTATE_incrementFdid( void )
{
int ret;
SERVSTATE_LOCK();
ret = servstate.fdid++;
SERVSTATE_UNLOCK();
return ret;
}
ANYTHREAD static void SERVSTATE_setCloseallsocketnum( int a )
{
SERVSTATE_LOCK();
servstate.closeallsocketnum = a;
SERVSTATE_UNLOCK();
}
ANYTHREAD static void SERVSTATE_incrementCloseallsocketnum(void)
{
SERVSTATE_LOCK();
servstate.closeallsocketnum ++;
SERVSTATE_UNLOCK();
}
ANYTHREAD void SERVSTATE_decrementCloseallsocketnum(void)
{
SERVSTATE_LOCK();
servstate.closeallsocketnum --;
SERVSTATE_UNLOCK();
}
ANYTHREAD int SERVSTATE_getCloseallsocketnum( void )
{
int a;
SERVSTATE_LOCK();
a = servstate.closeallsocketnum;
SERVSTATE_UNLOCK();
return a;
}
ANYTHREAD static int SERVSTATE_getAcceptmore(void)
{
int a;
SERVSTATE_LOCK();
a = servstate.acceptmore;
SERVSTATE_UNLOCK();
return a;
}
ANYTHREAD int SERVSTATE_getShutdown(void)
{
int a;
SERVSTATE_LOCK();
a = servstate.shutdown;
SERVSTATE_UNLOCK();
return a;
}
ANYTHREAD void SERVSTATE_setShutdown(int a)
{
SERVSTATE_LOCK();
servstate.shutdown = a;
SERVSTATE_UNLOCK();
}
ANYTHREAD int SERVSTATE_getLimittime(void)
{
int a;
SERVSTATE_LOCK();
a = servstate.limittime;
SERVSTATE_UNLOCK();
return a;
}
ANYTHREAD void SERVSTATE_setLimittime(int a)
{
SERVSTATE_LOCK();
servstate.limittime = a;
SERVSTATE_UNLOCK();
}
ANYTHREAD int SERVSTATE_getDsptime(void)
{
int a;
SERVSTATE_LOCK();
a = servstate.dsptime;
SERVSTATE_UNLOCK();
return a;
}
ANYTHREAD void SERVSTATE_setDsptime(int a)
{
SERVSTATE_LOCK();
servstate.dsptime = a;
SERVSTATE_UNLOCK();
}
#if USE_MTIO == 0
static int appendWB( int fd, char *buf, int size )
{
if( fd != acfd ) {
if( Connect[fd].wbuse + size >= WBSIZE ) {
print( "appendWB:err buffer over[%d]:%s \n",
Connect[fd].wbuse + size, Connect[fd].cdkey );
return -1;
}
}else {
if( Connect[fd].wbuse + size > AC_WBSIZE ) {
FILE *fp=NULL;
print( "appendWB:err buffer over[%d+%d]:(SAAC) \n", Connect[fd].wbuse, size);
if( (fp=fopen("appendWBerr.log", "a+"))==NULL) return -1;
fprintf( fp, "(SAAC) appendWB:err buffer over[%d+%d/%d]:\n", Connect[fd].wbuse, size, AC_WBSIZE);
fclose( fp);
return -1;
}
}
memcpy( Connect[fd].wb + Connect[fd].wbuse ,
buf, size );
Connect[fd].wbuse += size;
return size;
}
static int appendRB( int fd, char *buf, int size )
{
if( fd != acfd ) {
if( Connect[fd].rbuse + size > RBSIZE ) {
if( fd == mfd ) print( "appendRB:MSERVER err buffer over \n");
else print( "appendRB:OTHER(%d) err buffer over \n", fd);
return -1;
}
}else {
if( strlen( buf) > size ){
print( "appendRB AC buffer len err : %d/%d=\n(%s)!!\n", strlen( buf), size, buf);
}
if( Connect[fd].rbuse + size > AC_RBSIZE ) {
print( "appendRB AC err buffer over:\n(%s)\n len:%d - rbuse:%d \n",
buf, strlen(buf), Connect[fd].rbuse);
return -1;
}
}
memcpy( Connect[fd].rb + Connect[fd].rbuse , buf, size );
Connect[fd].rbuse += size;
return size;
}
static int shiftWB( int fd, int len )
{
if( Connect[fd].wbuse < len ) {
print( "shiftWB: err\n");
return -1;
}
memmove( Connect[fd].wb, Connect[fd].wb + len, Connect[fd].wbuse - len );
Connect[fd].wbuse -= len;
if( Connect[fd].wbuse < 0 ) {
print( "shiftWB:wbuse err\n");
Connect[fd].wbuse = 0;
}
return len;
}
static int shiftRB( int fd, int len )
{
if( Connect[fd].rbuse < len ) {
print( "shiftRB: err\n");
return -1;
}
memmove( Connect[fd].rb, Connect[fd].rb + len, Connect[fd].rbuse - len );
Connect[fd].rbuse -= len;
if( Connect[fd].rbuse < 0 ) {
print( "shiftRB:rbuse err\n");
Connect[fd].rbuse = 0;
}
return len;
}
SINGLETHREAD int lsrpcClientWriteFunc( int fd , char* buf , int size )
{
int r;
if( Connect[fd].use == FALSE ){
return FALSE;
}
if( Connect[fd].appendwb_overflow_flag ){
print( "lsrpcClientWriteFunc: buffer overflow fd:%d\n" , fd );
return -1;
}
r = appendWB( fd, buf , size);
// Nuke *1 0907: Ignore acfd from WB error
if(( r < 0 ) && (fd != acfd)) {
Connect[fd].appendwb_overflow_flag = 1;
CONNECT_endOne_debug(fd);
close(fd);
// Nuke + 1 0901: Why close
// print("closed in lsrpcClientWriteFunc");
}
return r;
}
static int logRBuseErr = 0;
SINGLETHREAD BOOL GetOneLine_fix( int fd, char *buf, int max )
{
int i;
if( Connect[fd].rbuse == 0 ) return FALSE;
if( Connect[fd].check_rb_oneline_b == 0 &&
Connect[fd].check_rb_oneline_b == Connect[fd].rbuse ){
return FALSE;
}
for( i = 0; i < Connect[fd].rbuse && i < ( max -1); i ++ ){
if( Connect[fd].rb[i] == '\n' ){
memcpy( buf, Connect[fd].rb, i+1);
buf[i+1]='\0';
shiftRB( fd, i+1 );
//--------
/*
//andy_log
if( strstr( Connect[fd].rb , "ACCharLoad") != NULL &&
Connect[fd].check_rb_oneline_b != 0 )//Connect[fd].rb
LogAcMess( fd, "GetOne", Connect[fd].rb );
*/
//--------
logRBuseErr = 0;
Connect[fd].check_rb_oneline_b=0;
Connect[fd].check_rb_time = 0;
return TRUE;
}
}
//print("rbuse lens: %d!!\n", Connect[fd].rbuse);
logRBuseErr++;
//--------
//andy_log
if( fd == acfd && strstr( Connect[fd].rb , "ACCharLoad") != NULL &&
logRBuseErr >= 50 ){//Connect[fd].rb
char buf[AC_RBSIZE];
memcpy( buf, Connect[fd].rb, Connect[fd].rbuse+1);
buf[Connect[fd].rbuse+1]=0;
LogAcMess( fd, "RBUFFER", buf );
logRBuseErr=0;
}
//--------
Connect[fd].check_rb_oneline_b = Connect[fd].rbuse;
return FALSE;
}
#endif /* if USE_MTIO == 0*/
ANYTHREAD BOOL initConnectOne( int sockfd, struct sockaddr_in* sin ,int len )
{
CONNECT_LOCK(sockfd);
Connect[sockfd].use = TRUE;
Connect[sockfd].ctype = NOTDETECTED;
#if USE_MTIO
Connect[sockfd].closed = 0;
#else
Connect[sockfd].wbuse = Connect[sockfd].rbuse = 0;
Connect[sockfd].check_rb_oneline_b = 0;
Connect[sockfd].check_rb_time = 0;
#endif
memset( Connect[sockfd].cdkey , 0 , sizeof( Connect[sockfd].cdkey ) );
memset( Connect[sockfd].passwd, 0 , sizeof( Connect[sockfd].passwd) );
Connect[sockfd].state = NOTLOGIN;
Connect[sockfd].nstatecount = 0;
memset( Connect[sockfd].charname,0, sizeof(Connect[sockfd].charname));
Connect[sockfd].charaindex = -1;
Connect[sockfd].CAbufsiz = 0;
Connect[sockfd].CDbufsiz = 0;
Connect[sockfd].rbuse = 0;
Connect[sockfd].wbuse = 0;
Connect[sockfd].check_rb_oneline_b = 0;
Connect[sockfd].check_rb_time = 0;
Connect[sockfd].close_request = 0; /* 濠蝇邰菲白仿弘 */
// Nuke 08/27 For acceleration avoidance
Connect[sockfd].Walktime = 0;
Connect[sockfd].lastWalktime = 0;
Connect[sockfd].Walkcount = 0;
Connect[sockfd].Walkspool = WALK_SPOOL;
Connect[sockfd].Walkrestore = WALK_RESTORE;
Connect[sockfd].Btime = 0;
Connect[sockfd].lastBtime = 0;
Connect[sockfd].lastlastBtime = 0;
Connect[sockfd].EOtime = 0;
Connect[sockfd].nu_decrease = 0;
#ifdef _BATTLE_TIMESPEED
// Connect[sockfd].DefBtime = 0;
Connect[sockfd].BDTime = 0;
Connect[sockfd].CBTime = 0;
#endif
#ifdef _CHECK_GAMESPEED
Connect[sockfd].gamespeed = 0;
#endif
#ifdef _TYPE_TOXICATION
Connect[sockfd].toxication = 0;
#endif
#ifdef _ITEM_ADDEXP //vincent 经验提升
Connect[sockfd].EDTime = 0;
#endif
// Connect[sockfd].BEO = 0;
Connect[sockfd].BEOspool = BEO_SPOOL;
Connect[sockfd].BEOrestore = BEO_RESTORE;
//ttom
Connect[sockfd].b_shut_up=FALSE;
Connect[sockfd].Wtime.tv_sec=0;//
Connect[sockfd].Wtime.tv_usec=0;//
Connect[sockfd].WLtime.tv_sec=0;//
Connect[sockfd].WLtime.tv_usec=0;//
Connect[sockfd].b_first_warp=FALSE;
Connect[sockfd].state_trans=0;//avoid the trans
// Nuke
Connect[sockfd].die=0;
Connect[sockfd].credit=3;
Connect[sockfd].fcold=0;
// Nuke 0406: New Flow Control
Connect[sockfd].nu=30;
Connect[sockfd].ke=10;
// Nuke 1213: Flow Control 2
Connect[sockfd].packetin=30; // if 10x10 seconds no packet, drop the line
// Nuke 0624: Avoid Useless Connection
Connect[sockfd].cotime=0;
// Nuke 0626: For no enemy
Connect[sockfd].noenemy=0;
// Arminius 7.2: Ra's amulet
Connect[sockfd].eqnoenemy = 0;
#ifdef _Item_MoonAct
Connect[sockfd].eqrandenemy = 0;
#endif
#ifdef _CHIKULA_STONE
Connect[sockfd].chistone = 0;
#endif
// Arminius 7.31: cursed stone
Connect[sockfd].stayencount = 0;
// CoolFish: Init Trade 2001/4/18
memset(&Connect[sockfd].TradeTmp, 0, sizeof(Connect[sockfd].TradeTmp));
#ifdef _ITEM_PILEFORTRADE
Connect[sockfd].tradelist = -1;
#endif
// Arminius 6.22 Encounter
Connect[sockfd].CEP = 0;
// Arminius 7.12 login announce
Connect[sockfd].announced=0;
#ifdef _NO_WARP
// shan hjj add Begin
Connect[sockfd].seqno=-1;
Connect[sockfd].selectbutton=1;
// shan End
#endif
Connect[sockfd].confirm_key=FALSE; // shan trade(DoubleCheck)
if( sin != NULL )memcpy( &Connect[sockfd].sin , sin , len );
memset( &Connect[sockfd].lastprocesstime, 0 ,
sizeof(Connect[sockfd].lastprocesstime) );
memcpy( &Connect[sockfd].lastCAsendtime, &NowTime ,
sizeof(Connect[sockfd].lastCAsendtime) );
memcpy( &Connect[sockfd].lastCDsendtime, &NowTime ,
sizeof(Connect[sockfd].lastCDsendtime) );
memcpy( &Connect[sockfd].lastCharSaveTime, &NowTime ,
sizeof(Connect[sockfd].lastCharSaveTime) );
// Shan Add
memcpy( &Connect[sockfd].lastrecvtime, &NowTime ,
sizeof(Connect[sockfd].lastrecvtime) );
memcpy( &Connect[sockfd].lastrecvtime_d, &NowTime ,
sizeof(Connect[sockfd].lastrecvtime_d) );
memcpy( &Connect[sockfd].battle_recvtime, &NowTime ,
sizeof(Connect[sockfd].battle_recvtime) );
#ifdef _BLACK_MARKET
{
int i;
for(i=0; i<12; i++)
Connect[sockfd].BMSellList[i] = -1;
}
#endif
memcpy( &Connect[sockfd].lastreadtime , &NowTime,
sizeof(struct timeval));
Connect[sockfd].lastreadtime.tv_sec -= DEBUG_ADJUSTTIME;
Connect[sockfd].errornum = 0;
Connect[sockfd].fdid = SERVSTATE_incrementFdid();
CONNECT_UNLOCK(sockfd);
Connect[sockfd].appendwb_overflow_flag = 0;
return TRUE;
}
ANYTHREAD BOOL _CONNECT_endOne( char *file, int fromline, int sockfd , int line )
{
CONNECT_LOCK_ARG2(sockfd,line);
if( Connect[sockfd].use == FALSE ){
CONNECT_UNLOCK_ARG2(sockfd,line);
//andy_log
print("ANDY already Connect[%d] be FALSE !!\n", sockfd );
return TRUE;
}
print("ctype:%d char:%d\n",Connect[sockfd].ctype,Connect[sockfd].charaindex);
if( Connect[sockfd].ctype == CLI && Connect[sockfd].charaindex >= 0 ){
print("GOONTO logout\n");
CONNECT_UNLOCK_ARG2( sockfd,line );
if( !CHAR_logout( sockfd,TRUE )) {
print( "err %s:%d from %s:%d \n", __FILE__, __LINE__, file, fromline);
}
CONNECT_LOCK_ARG2( sockfd ,line);
}
Connect[sockfd].use = FALSE;
print( "cdkey=%s fd=%d\n", Connect[sockfd].cdkey,sockfd );
#if USE_MTIO == 0
Connect[sockfd].rbuse = Connect[sockfd].wbuse = 0;
#else
Connect[sockfd].wbuse = Connect[sockfd].rbuse = 0;
#endif
Connect[sockfd].CAbufsiz = 0;
Connect[sockfd].CDbufsiz = 0;
CONNECT_UNLOCK_ARG2(sockfd,line);
return TRUE;
}
SINGLETHREAD BOOL initConnect( int size )
{
int i,j;
ConnectLen = size;
Connect = calloc( 1, sizeof( CONNECT ) * size );
if( Connect == NULL )return FALSE;
for( i = 0 ; i < size ; i ++ ){
memset( &Connect[i] , 0 , sizeof( CONNECT ) );
Connect[i].charaindex = -1;
Connect[i].rb = calloc( 1, RBSIZE);
if( Connect[i].rb == NULL ) {
fprint( "calloc err\n");
for( j = 0; j < i ; j ++ ) {
free( Connect[j].rb);
free( Connect[j].wb);
}
return FALSE;
}
memset( Connect[i].rb, 0, RBSIZE);
Connect[i].wb = calloc( 1, WBSIZE);
if( Connect[i].wb == NULL ) {
fprint( "calloc err\n");
for( j = 0; j < i ; j ++ ) {
free( Connect[j].rb);
free( Connect[j].wb);
}
free( Connect[j].rb);
return FALSE;
}
memset( Connect[i].wb, 0, WBSIZE);
}
#if USE_MTIO
for( i = 0 ;i < size ; i ++ ){
pthread_mutex_init( & Connect[i].mutex , NULL );
}
#endif
SERVSTATE_initserverState( );
//ttom for the performance of gmsv
MAX_item_use=getItemnum()*0.98;
return TRUE;
}
BOOL CONNECT_acfdInitRB( int fd )
{
if( fd != acfd ) return FALSE;
Connect[fd].rb = realloc( Connect[acfd].rb, AC_RBSIZE);
if( Connect[acfd].rb == NULL ) {
fprint( "realloc err\n");
return FALSE;
}
memset( Connect[acfd].rb, 0, AC_RBSIZE);
return TRUE;
}
BOOL CONNECT_acfdInitWB( int fd )
{
if( fd != acfd ) return FALSE;
Connect[fd].wb = realloc( Connect[acfd].wb, AC_WBSIZE);
if( Connect[acfd].wb == NULL ) {
fprint( "realloc err\n");
return FALSE;
}
memset( Connect[acfd].wb, 0, AC_WBSIZE);
return TRUE;
}
ANYTHREAD void endConnect( void )
{
int i;
int lco;
for(i = 0 ; i < ConnectLen ; i ++ ){
lco = close( i );
if( lco == 0 ){
CONNECT_endOne_debug(i);
}
free( Connect[i].rb);
free( Connect[i].wb);
}
free(Connect);
}
ANYTHREAD BOOL CONNECT_appendCAbuf( int fd , char* data, int size )
{
CONNECT_LOCK(fd);
/* 及犯伉立正及 ',' 及坌聂仁割忡仄卅中井氏仪卞镗啦 */
if( (Connect[fd].CAbufsiz + size) >= sizeof( Connect[fd].CAbuf ) ){
CONNECT_UNLOCK(fd);
return FALSE;
}
memcpy( Connect[fd].CAbuf + Connect[fd].CAbufsiz , data , size );
Connect[fd].CAbuf[Connect[fd].CAbufsiz+size]=',';
Connect[fd].CAbufsiz += (size + 1);
CONNECT_UNLOCK(fd);
return TRUE;
}
ANYTHREAD static int CONNECT_getCAbuf( int fd, char *out, int outmax,
int *outlen )
{
CONNECT_LOCK(fd);
if( Connect[fd].use == TRUE ){
int cplen = MIN( outmax, Connect[fd].CAbufsiz );
memcpy( out, Connect[fd].CAbuf , cplen );
*outlen = cplen;
CONNECT_UNLOCK(fd);
return 0;
} else {
CONNECT_UNLOCK(fd);
return -1;
}
}
ANYTHREAD static int CONNECT_getCDbuf( int fd, char *out, int outmax,
int *outlen )
{
CONNECT_LOCK(fd);
if( Connect[fd].use == TRUE ){
int cplen = MIN( outmax, Connect[fd].CDbufsiz );
memcpy( out, Connect[fd].CDbuf, cplen );
*outlen = cplen;
CONNECT_UNLOCK(fd);
return 0;
} else {
CONNECT_UNLOCK(fd);
return 0;
}
}
ANYTHREAD static int CONNECT_setCAbufsiz( int fd, int len )
{
CONNECT_LOCK(fd);
if( Connect[fd].use == TRUE ){
Connect[fd].CAbufsiz = len;
CONNECT_UNLOCK(fd);
return 0;
} else {
CONNECT_UNLOCK(fd);
return -1;
}
}
ANYTHREAD static int CONNECT_setCDbufsiz( int fd, int len )
{
CONNECT_LOCK(fd);
if( Connect[fd].use == TRUE ){
Connect[fd].CDbufsiz = len;
CONNECT_UNLOCK(fd);
return 0;
} else {
CONNECT_UNLOCK(fd);
return -1;
}
}
ANYTHREAD static void CONNECT_setLastCAsendtime( int fd, struct timeval *t)
{
CONNECT_LOCK(fd);
Connect[fd].lastCAsendtime = *t;
CONNECT_UNLOCK(fd);
}
ANYTHREAD static void CONNECT_getLastCAsendtime( int fd, struct timeval *t )
{
CONNECT_LOCK(fd);
*t = Connect[fd].lastCAsendtime;
CONNECT_UNLOCK(fd);
}
ANYTHREAD static void CONNECT_setLastCDsendtime( int fd, struct timeval *t )
{
CONNECT_LOCK(fd);
Connect[fd].lastCDsendtime = *t;
CONNECT_UNLOCK(fd);
}
ANYTHREAD static void CONNECT_getLastCDsendtime( int fd, struct timeval *t )
{
CONNECT_LOCK(fd);
*t = Connect[fd].lastCDsendtime;
CONNECT_UNLOCK(fd);
}
ANYTHREAD int CONNECT_getUse_debug( int fd, int i )
{
int a;
CONNECT_LOCK_ARG2(fd,i);
a = Connect[fd].use;
CONNECT_UNLOCK_ARG2(fd,i);
return a;
}
ANYTHREAD int CONNECT_getUse( int fd )
{
int a;
CONNECT_LOCK(fd);
a = Connect[fd].use;
CONNECT_UNLOCK(fd);
return a;
}
void CONNECT_setUse( int fd , int a)
//ANYTHREAD static void CONNECT_setUse( int fd , int a)
{
CONNECT_LOCK(fd);
Connect[fd].use = a;
CONNECT_UNLOCK(fd);
}
ANYTHREAD void CONNECT_checkStatecount( int a )
{
int i;
int count=0;
for( i=0; i < ConnectLen; i++ ){
if( Connect[i].use == FALSE || Connect[i].state != a ) continue;
if( Connect[i].nstatecount <= 0 ){
Connect[i].nstatecount=(int)time(NULL)+60;
}else{
if( Connect[i].nstatecount < (int)time(NULL) ){
CONNECT_endOne_debug(i);
close( i );
count++;
}
}
}
{
memset(StateTable, 0, sizeof(StateTable));
for (i=0; i < ConnectLen; i++)
if (Connect[i].use == TRUE)
StateTable[Connect[i].state]++;
}
}
ANYTHREAD int CONNECT_checkStateSomeOne( int a, int maxcount)
{
char temp[80],buffer[1024];
int i, ret=1;
if( StateTable[a] >= maxcount ) ret =-1;