Repository navigation
Expand file tree
/
Copy pathctrlprotocol.c
More file actions
2517 lines (2216 loc) · 59.8 KB
/
Copy pathctrlprotocol.c
File metadata and controls
2517 lines (2216 loc) · 59.8 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 <stdio.h>
#include <string.h>
typedef char s8;
#include "ctrlprotocol.h"
#include "common_basetypes.h"
#include "common.h"
#include "mod_config.h"
#include "mod_preview.h"
#ifdef WIN32
typedef HANDLE MutexHandle;
typedef HANDLE SemHandle;
#else
typedef pthread_mutex_t MutexHandle;
typedef sem_t SemHandle;
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#include <netinet/tcp.h>
#endif
static char g_upnpid[16] = {0};
//csp modify 20130405
static char g_upnppw[16] = {0};
//csp modify 20130406
//#define P2P_SERVER
BOOL login_p2p_server();
//pw 2010/9/10
//ns : 超时时间单位:秒,必须大于0
//时间控制上不精确,因为select 成功返回没有计算时间
int loopRcvTimeoutNS(SOCKET s, char * buf, unsigned int rcvsize, u32 ns)
{
int ret = 0;
int time_out_count = 0;
u32 remian = rcvsize;
u32 recvlen = 0;
struct timeval timeout;
fd_set set;
if (s < 0)
{
return SOCKET_ERROR;
}
if (NULL == buf)
{
return SOCKET_ERROR;
}
if (ns <= 0)
{
return SOCKET_ERROR;
}
while(1)
{
FD_ZERO(&set);
FD_SET(s, &set);
//linux平台下timeout会被修改以表示剩余时间,故每次都要重新赋值
timeout.tv_sec = 1;
timeout.tv_usec = 0;
ret = select(s+1, &set, NULL, NULL, &timeout);
if(ret == 0)
{
if(++time_out_count >= ns)//整体超时
{
return SOCKET_TIMEOUT;
}
else //单次超时
{
continue;
}
}
else if(ret < 0)
{
return SOCKET_ERROR;
}
if (!FD_ISSET(s, &set))
{
return SOCKET_ERROR;
}
ret = recv(s, buf+recvlen, remian, 0);
if(ret <= 0)
{
return SOCKET_ERROR;
}
recvlen += ret;
remian -= ret;
if (remian == 0)
{
break;
}
}
return SOCKET_SUCCESS;//0
}
int looprecv(SOCKET s, char * buf, unsigned int rcvsize)
{
u32 remian = rcvsize;
u32 recvlen = 0;
int ret = 0;
while(remian > 0)
{
ret=recv(s,buf+recvlen,remian,0);
if(ret <= 0)
{
return ret;
}
recvlen += ret;
remian -= ret;
}
return recvlen;
}
//pw 2010/9/10
int loopsend(SOCKET s, char *buf, unsigned int sndsize)
{
int remian = sndsize;
int sendlen = 0;
int ret = 0;
while(remian > 0)
{
ret = send(s,buf+sendlen,remian,0);
if(ret <= 0)
{
return ret;
}
sendlen += ret;
remian -= ret;
}
return sndsize;
}
const u8 gc_protocolGUID[16] = {0x61,0x78,0xDA,0xB5,0xD3,0x8E,0x43,0xdb,0x9E,0xD7,0xF2,0x20,0x78,0x36,0x18,0x79};
//linux下接收标志应该设置成MSG_NOSIGNAL?
#define CTRL_PROTOCOL_TYPE SOCK_STREAM//SOCK_STREAM TCP方式
// #ifdef WIN32
struct TCP_KEEPALIVE {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
// #endif
static BOOL SockClose(SOCKHANDLE hSock)
{
//printf("SockClose sockfd=%d\n",hSock);
if(hSock != INVALID_SOCKET)
{
#ifdef WIN32
closesocket(hSock);
#else
close(hSock);
#endif
}
return TRUE;
}
u16 CleanCPHandle(CPHandle cph)
{
if(cph->sockfd != INVALID_SOCKET)
{
SockClose(cph->sockfd);
cph->sockfd = INVALID_SOCKET;
}
cph->ip = 0;
cph->port = 0;
cph->conntype = CTRL_CONNECTION_NULL;
cph->newmsgcome = 0;
cph->nolnkcount = 0;
memset(&cph->guid,0,sizeof(cph->guid));
return CTRL_SUCCESS;
}
SOCKHANDLE ConnectWithTimeout(u32 dwHostIp, u16 wHostPort, u32 dwTimeOut, u16 *pwErrorCode)
{
SOCKHANDLE hClient = INVALID_SOCKET;
struct sockaddr_in remote;
fd_set ConnectSet;
struct timeval timeVal;
int ret;
#ifdef WIN32
LPVOID lpMsgBuf;
unsigned long arg;
#else
int arg;
int nErrCode;
socklen_t errorlen;
#endif
if(pwErrorCode) *pwErrorCode = CTRL_SUCCESS;
hClient = socket(AF_INET,CTRL_PROTOCOL_TYPE,0);
if(hClient == INVALID_SOCKET)
{
#ifdef WIN32
printf("ConnectWithTimeout:create socket failed,errcode=%d\n",WSAGetLastError());
#else
printf("ConnectWithTimeout:create socket failed,errcode=%d,errmsg=%s\n",errno,strerror(errno));
#endif
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_CREATESOCKET;
return INVALID_SOCKET;
}
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = dwHostIp;
remote.sin_port = htons(wHostPort);
if(dwTimeOut == CTRL_PROTOCOL_CONNECT_BLOCK)
{
ret = connect(hClient,(struct sockaddr *)&remote,sizeof(remote));
if(ret == SOCKET_ERROR)
{
#ifdef WIN32
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
printf("ConnectWithTimeout:connect failed,errcode=%d,errmsg=%s\n",WSAGetLastError(),(LPCTSTR)lpMsgBuf);
closesocket(hClient);
#else
printf("ConnectWithTimeout:connect failed,errcode=%d,errmsg=%s\n",errno,strerror(errno));
close(hClient);
#endif
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_CONNECT;
return INVALID_SOCKET;
}
}
else
{
arg = 1;
//设置为非阻塞模式, 防止connect阻塞
#ifdef WIN32
ret = ioctlsocket(hClient,FIONBIO,&arg);
#else
ret = ioctl(hClient,FIONBIO,(int)&arg);
#endif //WIN32
if(ret == SOCKET_ERROR)
{
SockClose(hClient);
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_CONNECT;
return INVALID_SOCKET;
}
ret = connect(hClient, (struct sockaddr *)&remote, sizeof(remote));
if(ret == SOCKET_ERROR)
{
FD_ZERO(&ConnectSet);
timeVal.tv_sec = (dwTimeOut/1000);
timeVal.tv_usec = (dwTimeOut%1000)*1000;
FD_SET(hClient, &ConnectSet);
ret = select(hClient+1, NULL, &ConnectSet, NULL, &timeVal);
//printf("%s sockfd: %d, ret: %d\n", __func__, hClient, ret);
if(ret > 0 && FD_ISSET(hClient, &ConnectSet))
{
//printf("%s ret > 0 && FD_ISSET(hClient, &ConnectSet)\n", __func__);
#ifndef WIN32
nErrCode = 1;
errorlen = sizeof(nErrCode);
ret = getsockopt(hClient, SOL_SOCKET, SO_ERROR, &nErrCode, &errorlen);
if(SOCKET_ERROR == ret || nErrCode != 0)
{
printf("%s SOCKET_ERROR == ret || nErrCode != 0\n", __func__);
close(hClient);
if(pwErrorCode)
{
*pwErrorCode = CTRL_FAILED_CONNECT;
}
return INVALID_SOCKET;
}
#endif
}
else
{
SockClose(hClient);
if(ret == 0)
{
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_TIMEOUT;
return INVALID_SOCKET;
}
else
{
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_CONNECT;
return INVALID_SOCKET;
}
}
}
//设置为阻塞模式
arg = 0;
#ifdef WIN32
ret = ioctlsocket(hClient,FIONBIO,&arg);
#else
ret = ioctl(hClient,FIONBIO,(int)&arg);
#endif //WIN32
if(ret == SOCKET_ERROR)
{
SockClose(hClient);
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_CONNECT;
return INVALID_SOCKET;
}
}
return hClient;
}
u16 SetSockLinkOpt(SOCKHANDLE hSock)
{
int ret = 0;
//设置NODELAY选项
#ifdef WIN32
char optval = 1;
#else
int optval = 1;
#endif
ret = setsockopt( hSock, IPPROTO_TCP/*SOL_SOCKET*/, TCP_NODELAY, (char *)&optval, sizeof(optval) );
if( SOCKET_ERROR == ret )
{
printf("SetSockLinkOpt:set socket nodelay error!\n");
return CTRL_FAILED_UNKNOWN;
}
//设置SO_LINGER为零(亦即linger结构中的l_onoff域设为非零,但l_linger为0),便
//不用担心closesocket调用进入“锁定”状态(等待完成),不论是否有排队数据未发
//送或未被确认。这种关闭方式称为“强行关闭”,因为套接字的虚电路立即被复位,尚
//未发出的所有数据都会丢失。在远端的recv()调用都会失败,并返回WSAECONNRESET错误。
{
struct linger m_sLinger;
m_sLinger.l_onoff = 1; //(在closesocket()调用,但是还有数据没发送完毕的时候容许逗留)
m_sLinger.l_linger = 1; //(容许逗留的时间为0秒) //dong 100424 m_sLinger.l_linger = 0;
ret = setsockopt(hSock,SOL_SOCKET,SO_LINGER,(char*)&m_sLinger,sizeof(struct linger));
if( SOCKET_ERROR == ret )
{
printf("SetSockLinkOpt:set socket linger error!\n");
return CTRL_FAILED_UNKNOWN;
}
}
#ifdef WIN32
{
struct TCP_KEEPALIVE klive;
struct TCP_KEEPALIVE outKeepAlive = {0,0,0};//输出参数
unsigned long ulBytesReturn = 0;
int ret2 = 0;
klive.onoff = 1;//启用保活
klive.keepalivetime = 5000;
klive.keepaliveinterval = 1000 * 5;//重试间隔为5秒 Resend if No-Reply
ret2 = WSAIoctl(
hSock,
SIO_KEEPALIVE_VALS,
&klive,
sizeof(klive),
&outKeepAlive,
sizeof(outKeepAlive),
(unsigned long *)&ulBytesReturn,
0,
NULL
);
}
#endif
return CTRL_SUCCESS;
}
static pAddStreamTCPLink pAddStreamLinkCB = NULL;
static void* pAddStreamLinkCBContext = NULL;
static pFindDeivce_CB pFindCB = NULL;
static void* pFindCBContext = NULL;
static u16 g_wCliAckPort = ACKSEARCHPORT;
static pMESSAGE_CB pfuncMsgCB = NULL;
static void* pCallBackContext = NULL;
static BOOL bCPInit = FALSE;
static THREADHANDLE hCPTask = (THREADHANDLE)NULL;
static THREADHANDLE hCPCheckTask = (THREADHANDLE)NULL;
static THREADHANDLE hCPSearchTask = (THREADHANDLE)NULL;
static SOCKHANDLE hServerSock = INVALID_SOCKET;//服务器套接字
static ifly_cp_t hCPLink[CTRL_PROTOCOL_MAXLINKNUM];
static SOCKHANDLE hInterSock = INVALID_SOCKET;//内部消息套接字
static u16 wInterPort = 0;//内部消息套接字绑定端口
static u8 g_byAckBuf[4096];
static int g_dwAckLen;
static u8 g_bySyncAckBuf[4096];
static int g_dwSyncAckLen;
static MutexHandle g_hSyncMutex;
static SemHandle g_hSyncSem;
static u16 g_wSyncNumber = 0;//等待同步的消息的流水号
static SemHandle g_hFindSem;
static BOOL CreateMutexHandle(MutexHandle *pMutex)
{
BOOL bRet = TRUE;
#ifdef WIN32
*pMutex = CreateMutex(NULL, FALSE, NULL);
if(*pMutex == NULL) bRet = FALSE;
#else
bRet = ( 0 == pthread_mutex_init(pMutex,NULL));
#endif
return bRet;
}
static BOOL CloseMutexHandle(MutexHandle hMutex)
{
#ifdef WIN32
return CloseHandle(hMutex);
#else
return ( 0 == pthread_mutex_destroy(&hMutex) );
#endif
}
static BOOL LockMutex(MutexHandle hMutex)
{
#ifdef WIN32
return ( WAIT_FAILED != WaitForSingleObject(hMutex,INFINITE) );
#else
return ( 0 == pthread_mutex_lock(&hMutex) );
#endif
}
static BOOL UnlockMutex(MutexHandle hMutex)
{
#ifdef WIN32
return ReleaseMutex(hMutex);
#else
return ( 0 == pthread_mutex_unlock(&hMutex) );
#endif
}
static BOOL SemBCraete(SemHandle *pSem)
{
BOOL bRet = TRUE;
#ifdef WIN32
*pSem = CreateSemaphore(NULL, 1, 1, NULL);
if(*pSem == NULL) bRet = FALSE;
#else
bRet = ( 0 == sem_init(pSem,0,1));
#endif
return bRet;
}
static BOOL SemDelete(SemHandle hSem)
{
#ifdef WIN32
return CloseHandle(hSem);
#else
return ( 0 == sem_destroy(&hSem) );
#endif
}
static BOOL SemTake(SemHandle hSem)
{
#ifdef WIN32
return ( WAIT_FAILED != WaitForSingleObject(hSem,INFINITE) );
#else
return ( 0 == sem_wait(&hSem) );
#endif
}
static BOOL SemTakeByTime(SemHandle hSem,u32 dwTimeout)
{
#ifdef WIN32
return ( WAIT_OBJECT_0 == WaitForSingleObject(hSem,dwTimeout) );
#else
if(dwTimeout == 0)
{
return ( 0 == sem_trywait(&hSem) );
}
else
{
//csp modify 20140501
//return ( 0 == sem_wait(&hSem) );
u32 nloop = (dwTimeout + 9) / 10;
while(nloop)
{
if(0 == sem_trywait(&hSem))
{
return TRUE;
}
nloop--;
}
return FALSE;
}
#endif
}
static BOOL SemGive(SemHandle hSem)
{
#ifdef WIN32
LONG previous;
return ReleaseSemaphore(hSem, 1, (LPLONG)&previous);
#else
return ( 0 == sem_post(&hSem) );
#endif
}
void SetMsgCallBack(pMESSAGE_CB pCB,void *pContext)
{
pfuncMsgCB = pCB;
pCallBackContext = pContext;
}
int SendTcpConnHead(SOCKHANDLE hSock, u8 type)
{
ifly_ProtocolHead_t Protocolhdr;
int ret = 0;
memset(&Protocolhdr, 0, sizeof(Protocolhdr));
memcpy(Protocolhdr.safeguid, gc_protocolGUID, 16);
Protocolhdr.protocolver = 1;
Protocolhdr.byConnType = type;
ret = send(hSock,(char *)&Protocolhdr,sizeof(Protocolhdr),0);
return ret;
}
int CheckTcpConnType(SOCKHANDLE hSock)
{
ifly_ProtocolHead_t Protocolhdr;
int remian = sizeof(ifly_ProtocolHead_t);
int recvlen = 0;
int ret = 0;
u8 buf[256] = {0};
while(remian > 0)
{
//ret = recv(hSock,buf+recvlen,remian,0);//yqluo 20110301
struct timeval timeout = {3,0};
struct timeval timeout0 = {0,0};
socklen_t optlen = sizeof(struct timeval);
getsockopt(hSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout0, &optlen);
//printf("timeout0 = %d\n", timeout0.tv_sec);
setsockopt(hSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
ret = recv(hSock,buf+recvlen,remian,0);
setsockopt(hSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout0, sizeof(struct timeval));
if(ret < 0)
{
break;
}
if(ret == 0)
{
break;
}
recvlen += ret;
remian -= ret;
//pw 2010/9/10
if(memcmp(buf, gc_protocolGUID, min(recvlen,16)))
{
return -1;
}
}
if(recvlen != sizeof(ifly_ProtocolHead_t))
{
return -1;
}
memcpy(&Protocolhdr, buf, sizeof(ifly_ProtocolHead_t));
if(memcmp(Protocolhdr.safeguid, gc_protocolGUID, 16))
{
return -1;
}
return Protocolhdr.byConnType;
}
void SetKeepAlive(SOCKHANDLE hSock) //zlb20111110 bugfix
{
////KeepAlive实现,单位秒 //zlb201109
int keepAlive = 1;//设定KeepAlive
int keepIdle = 5;//开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5;//两次KeepAlive探测间的时间间隔
int keepCount = 3;//判定断开前的KeepAlive探测次数
if (setsockopt(hSock,SOL_SOCKET,SO_KEEPALIVE,(void*)&keepAlive,sizeof(keepAlive)) == -1)
{
printf("hSock=%d, set SO_KEEPALIVE failed, errno=%d, errstr=%s\n", hSock, errno, strerror(errno));
return;
}
if (setsockopt(hSock,SOL_TCP,TCP_KEEPIDLE,(void *)&keepIdle,sizeof(keepIdle)) == -1)
{
printf("hSock=%d, set TCP_KEEPIDLE failed\n", hSock);
return;
}
if (setsockopt(hSock,SOL_TCP,TCP_KEEPINTVL,(void *)&keepInterval,sizeof(keepInterval)) == -1)
{
printf("hSock=%d, set TCP_KEEPINTVL failed\n", hSock);
return;
}
if (setsockopt(hSock,SOL_TCP,TCP_KEEPCNT,(void *)&keepCount,sizeof(keepCount)) == -1)
{
printf("hSock=%d, set TCP_KEEPCNT failed\n", hSock);
return;
}
}
CPHandle AddCPLink(SOCKHANDLE hSock,u8 conntype,u32 ip,u16 port,u16 *pwErrorCode)
{
int i;
for(i=0;i<CTRL_PROTOCOL_MAXLINKNUM;i++)
{
if(hCPLink[i].sockfd == INVALID_SOCKET)
{
SetKeepAlive(hSock); //zlb20111110 bugfix
hCPLink[i].sockfd = hSock;
hCPLink[i].conntype = conntype;
hCPLink[i].ip = ip;
hCPLink[i].port = port;
hCPLink[i].newmsgcome = 0;
hCPLink[i].nolnkcount = 0;
memset(&hCPLink[i].guid,0,sizeof(hCPLink[i].guid));
if(pwErrorCode) *pwErrorCode = CTRL_SUCCESS;
return &hCPLink[i];
}
}
if(pwErrorCode) *pwErrorCode = CTRL_FAILED_LINKLIMIT;
return NULL;
}
void *CheckConnTaskProc(void *pParam)
{
int i = 0;
//u8 count = 0;
#ifndef HISI_3515
#ifndef WIN32
printf("$$$$$$$$$$$$$$$$$$CheckConnTaskProc id:%d\n",gettid());
Dump_Thread_Info("CPCheck",gettid());
#endif
#endif
printf("yg CheckConnTaskProc id\n");
while(1)
{
#ifndef WIN32
pthread_testcancel();
sleep(20);
#else
Sleep(20000);
#endif
//printf("CheckConnTaskProc running\n");
//if(++count >= 3)
{
//count = 0;
//检测断链:如果超过3次没有收到客户端的任何消息则认为断链
for(i=0;i<CTRL_PROTOCOL_MAXLINKNUM;i++)
{
if(hCPLink[i].sockfd != INVALID_SOCKET)
{
if(hCPLink[i].newmsgcome)
{
hCPLink[i].newmsgcome = 0;
hCPLink[i].nolnkcount = 0;
}
else
{
if(++hCPLink[i].nolnkcount >= 3)
{
if(pfuncMsgCB != NULL)
{
//MessageBox(NULL, "CNNLOST:CheckConnTaskProc",NULL,MB_OK);
pfuncMsgCB(&hCPLink[i],CTRL_NOTIFY_CONNLOST,NULL,0,NULL,NULL,pCallBackContext);
}
CleanCPHandle(&hCPLink[i]);
continue;
}
}
}
}
}
//发送链路检测心跳消息
for(i=0;i<CTRL_PROTOCOL_MAXLINKNUM;i++)
{
if(hCPLink[i].sockfd != INVALID_SOCKET && hCPLink[i].conntype == CTRL_CONNECTION_TCPCLIENT)
{
ifly_cp_header_t cpsndhead;
cpsndhead.length = htonl(sizeof(ifly_cp_header_t));
cpsndhead.type = htons(CTRL_NOTIFY);
cpsndhead.event = htons(CTRL_NOTIFY_HEARTBEAT_REQ);
cpsndhead.number = htons(GetTransactionNum());
cpsndhead.version = htons(CTRL_VERSION);
send(hCPLink[i].sockfd,(char *)&cpsndhead,ntohl(cpsndhead.length),0);
}
}
}
return 0;
}
void *CPTaskProc(void *pParam)
{
fd_set set;
struct timeval timeout;
int ret;
int i;
SOCKHANDLE hTmp;
struct sockaddr_in clientaddr;
socklen_t addrlen;
u8 abyBuf[4096];
ifly_cp_header_t cpsndhead;
ifly_cp_header_t cprcvhead;
u16 wRet;
#ifndef HISI_3515
#ifndef WIN32
printf("$$$$$$$$$$$$$$$$$$CPTaskProc id:%d\n",gettid());
Dump_Thread_Info("CPTask",gettid());
#endif
#else
#ifndef WIN32
printf("$$$$$$$$$$$$$$$$$$CPTaskProc id:%d\n",getpid());
#endif
#endif
while(1)
{
#ifndef WIN32
pthread_testcancel();
#endif
FD_ZERO(&set);
if(hServerSock != INVALID_SOCKET)
FD_SET(hServerSock,&set);
for(i=0;i<CTRL_PROTOCOL_MAXLINKNUM;i++)
{
if(hCPLink[i].sockfd != INVALID_SOCKET)
{
FD_SET(hCPLink[i].sockfd,&set);
}
}
if(hInterSock != INVALID_SOCKET)
FD_SET(hInterSock,&set);
//linux平台下timeout会被修改以表示剩余时间,故每次都要重新赋值
timeout.tv_sec = 20;
timeout.tv_usec = 0;
ret = select(FD_SETSIZE,&set,NULL,NULL,&timeout);
if(ret == 0)
{
//printf("CPTaskProc:select sock time out\n");
continue;
}
if(ret == SOCKET_ERROR)
{
printf("CPTaskProc:select sock error:[%s]!\n",strerror(errno));
#ifndef WIN32
//应延时一段时间
sleep(1);
#else
Sleep(1000);
#endif
continue;
}
for(i=0;i<CTRL_PROTOCOL_MAXLINKNUM;i++)
{
//客户端消息:回调函数处理
if(hCPLink[i].sockfd != INVALID_SOCKET && FD_ISSET(hCPLink[i].sockfd,&set))
{
//printf("recv msg start\n");
//MessageBox(NULL, "RECV 1", NULL,MB_OK);
{
int remian = sizeof(ifly_cp_header_t);
int recvlen = 0;
while(remian > 0)
{
ret=recv(hCPLink[i].sockfd,(char *)abyBuf+recvlen,remian,0);
if(ret<=0)
{
break;
}
recvlen += ret;
remian -= ret;
}
}
//MessageBox(NULL, "RECV 2", NULL,MB_OK);
//printf("recv msg end\n");
if(ret<=0)
{
//printf("recv failed from(0x%08x,%d)\n",hCPLink[i].ip,hCPLink[i].port);
if(pfuncMsgCB != NULL)
{
printf("CPTaskProc CTRL_NOTIFY_CONNLOST\n");
pfuncMsgCB(&hCPLink[i],CTRL_NOTIFY_CONNLOST,NULL,0,NULL,NULL,pCallBackContext);
}
//MessageBox(NULL, "CONTINUE 1", NULL,MB_OK);
CleanCPHandle(&hCPLink[i]);
continue;
}
hCPLink[i].newmsgcome = 1;
if(ret >= sizeof(ifly_cp_header_t))
{
int ret2 = 0;
int msglen = 0;
memcpy(&cprcvhead,abyBuf,sizeof(cprcvhead));
cprcvhead.length = ntohl(cprcvhead.length);
cprcvhead.type = ntohs(cprcvhead.type);
cprcvhead.version = ntohs(cprcvhead.version);
cprcvhead.number = ntohs(cprcvhead.number);
cprcvhead.event = ntohs(cprcvhead.event);
if(cprcvhead.length > sizeof(ifly_cp_header_t))
{
msglen = cprcvhead.length - sizeof(ifly_cp_header_t);
{
int remian = msglen;
int recvlen = 0;
while(remian > 0)
{
ret2=recv(hCPLink[i].sockfd,(char *)abyBuf+sizeof(ifly_cp_header_t)+recvlen,remian,0);
if(ret2<=0)
{
cprcvhead.event = CTRL_FAILED_NETRCV;
memcpy(abyBuf, &cprcvhead, sizeof(ifly_cp_header_t));
break;
}
recvlen += ret2;
remian -= ret2;
}
}
}
if(cprcvhead.length < sizeof(ifly_cp_header_t) || cprcvhead.type > CTRL_ACK || cprcvhead.version != CTRL_VERSION)
{
//MessageBox(NULL, "CONTINUE 2", NULL,MB_OK);
continue;
}
printf("yg CPTaskProc recv: \n");
printf("\ttype: %d\n", cprcvhead.type);
printf("\tevent: %d\n", cprcvhead.event);
printf("\tlength: %d\n", cprcvhead.length);
if(cprcvhead.type == CTRL_ACK)
{
if(cprcvhead.number == g_wSyncNumber)
{
memcpy(g_bySyncAckBuf,abyBuf,cprcvhead.length);
g_dwSyncAckLen = cprcvhead.length;
SemGive(g_hSyncSem);
//printf("CPSend ack msg%d\n",cprcvhead.number);
}
//MessageBox(NULL, "CONTINUE 3", NULL,MB_OK);
continue;
}
if(cprcvhead.event == CTRL_NOTIFY_HEARTBEAT_REQ)
{
printf("recv heartbeat req msg from (0x%08x,%d)\n",hCPLink[i].ip,hCPLink[i].port);
cpsndhead.length = htonl(sizeof(ifly_cp_header_t));
cpsndhead.type = htons(CTRL_NOTIFY);
cpsndhead.event = htons(CTRL_NOTIFY_HEARTBEAT_RESP);
cpsndhead.number = htons(cprcvhead.number);
cpsndhead.version = htons(cprcvhead.version);
#if 0
send(hCPLink[i].sockfd,(char *)&cpsndhead,ntohl(cpsndhead.length),0);
#endif
//MessageBox(NULL, "CONTINUE 4", NULL,MB_OK);
continue;
}
if(cprcvhead.event == CTRL_NOTIFY_HEARTBEAT_RESP)
{
printf("recv heartbeat resp msg from (0x%08x,%d)\n",hCPLink[i].ip,hCPLink[i].port);
//MessageBox(NULL, "CONTINUE 5", NULL,MB_OK);
continue;
}
g_dwAckLen = 0;
wRet = CTRL_FAILED_UNKNOWN;
if(pfuncMsgCB != NULL)
{
g_dwAckLen = sizeof(g_byAckBuf)-sizeof(ifly_cp_header_t);
//printf("recv msg,call back start\n");
//MessageBox(NULL, "recv msg,call back start\n", NULL,MB_OK);
//printf("event %d \n", cprcvhead.event);
wRet = pfuncMsgCB(&hCPLink[i],cprcvhead.event,abyBuf+sizeof(ifly_cp_header_t),cprcvhead.length-sizeof(ifly_cp_header_t),g_byAckBuf+sizeof(ifly_cp_header_t),&g_dwAckLen,pCallBackContext);
printf("ret %d event %d \n", wRet, cprcvhead.event);
//printf("recv msg,call back end\n");
}
else
{
//MessageBox(NULL, "call back start null\n", NULL,MB_OK);
}
if(cprcvhead.type == CTRL_COMMAND)
{
//printf("cmd %d err %d \n", cprcvhead.type, wRet);
cpsndhead.length = ntohl(sizeof(ifly_cp_header_t)+g_dwAckLen);
cpsndhead.type = ntohs(CTRL_ACK);
cpsndhead.event = ntohs(wRet);
cpsndhead.number = ntohs(cprcvhead.number);
cpsndhead.version = ntohs(cprcvhead.version);
memcpy(g_byAckBuf,&cpsndhead,sizeof(ifly_cp_header_t));
//printf("send ack msg start\n");
//pw 2010/9/10
loopsend(hCPLink[i].sockfd,(char *)g_byAckBuf,ntohl(cpsndhead.length));
//printf("send ack msg end\n");
}
}
}
}
//连接消息:接受连接
if(hServerSock != INVALID_SOCKET && FD_ISSET(hServerSock,&set))
{
addrlen = sizeof(struct sockaddr_in);
hTmp = accept(hServerSock,(struct sockaddr *)&clientaddr,&addrlen);
if(hTmp != INVALID_SOCKET)
{
SetSockLinkOpt(hTmp);
int Conntype = 0;
Conntype = CheckTcpConnType(hTmp);
printf("%s, Conntype: %d\n", __func__, Conntype);
if(0x1 == Conntype)
{
//printf("cpconnect\n");
if(NULL == AddCPLink(hTmp,CTRL_CONNECTION_TCPSERVER,clientaddr.sin_addr.s_addr,ntohs(clientaddr.sin_port),NULL))
{
SockClose(hTmp);
}
else
{
printf("accept cmdlink success,connectin from(%s,%d),sockfd=%d\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port),hTmp);
}
}
else if(0x2 == Conntype)
{
printf("accept medialink success,connectin from(%s,%d),sockfd=%d\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port),hTmp);
ifly_TCP_Stream_Req reqhdr;
int remian = sizeof(ifly_TCP_Stream_Req);
int recvlen = 0;
int ret = 0;
u8 buf[256] = {0};
while(remian > 0)
{
ret=recv(hTmp,buf+recvlen,remian,0);
if(ret < 0)
{
break;
}
if(ret == 0)
{
break;
}
recvlen += ret;