123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- #include "includes.h"
- //poc login condition ctl handler start...////////////////////////////////
- static T_BOOL loginEnable(void){
- static T_BOOL lastLoginStatus=FALSE;
- T_BOOL needClose=FALSE;
- if(FALSE==talking.remoteEnable){
- if(TCP_INIT != talking.tcpLoginStatus) closeTalk();
- talking.functionEnable=FALSE;
- return FALSE;
- }
- if(lastLoginStatus != talking.keepLogin){
- if(FALSE==talking.keepLogin) needClose=TRUE;
- lastLoginStatus=talking.keepLogin;
- }
- if(TRUE==talking.reLogin) needClose=TRUE;
- if(TRUE==needClose) closeTalk();
- if(TRUE==talking.reLogin){
- talking.reLogin=FALSE;
- talking.keepLogin=TRUE;
- }
- talking.functionEnable=talking.keepLogin;
- return talking.keepLogin;
- }
- static void pocTcpDataHandler(T_UINT8 *msg, T_UINT16 len){
- static T_INT32 length=0;
- static T_UINT8 pocbuf[GU_NAME_SIZE+50];
- T_UINT8 *tempBuf=msg,head, tail;
- T_UINT16 tempLen=len;
- T_INT8 ret;
- #if 1
- T_UINT16 i;
- for(i=0;i<len;i++){
- if(length<sizeof(pocbuf)) pocbuf[length++]=msg[i];
- else{
- wlog_warn("poc tcp buf over,rst");
- length=0;
- continue;
- }
- if(pocbuf[0] != 0x7e){
- length=0;
- continue;
- }
- if(length >1 && msg[i]==0x7e){
- pocTcpRx(pocbuf,length);
- length=0;
- }
- }
- #else
- //fill packet
- head=tempBuf[0];
- tail=tempBuf[tempLen-1];
- wlog_info("reclen=%d,head=%02x,tail=%02x",tempLen,head,tail);
- if(head==0x7e && tail==0x7e){
- //completed packet
- if(tempLen > sizeof(pocbuf)) ret=1;//buffer full
- else{
- length=tempLen;
- memcpy(pocbuf, tempBuf, length);
- ret=0;//good or full packet
- }
- }else if(head==0x7e && tail != 0x7e){
- //first fench packet
- length=tempLen;
- if(tempLen > sizeof(pocbuf)) ret=1;//buffer full
- else{
- length=tempLen;
- memcpy(pocbuf, tempBuf, length);
- ret=2;//need second packet
- }
- }else if(head!=0x7e && tail == 0x7e){
- //last fench packet
- if((tempLen+length) > sizeof(pocbuf)) ret=1;//buffer full
- else{
- memcpy(pocbuf+length, tempBuf, tempLen);
- length += tempLen;
- ret=0;//good or full packet
- }
- }else{
- //middle packet or bad packet
- if((tempLen+length) > sizeof(pocbuf)) ret=1;//buffer full
- else{
- if(length==0) ret=4;
- else{
- memcpy(pocbuf+length, tempBuf, tempLen);
- length += tempLen;
- ret=3;//middle packet
- }
- }
- }
- if(ret ==0){
- pocTcpRx(pocbuf,length);
- length=0;
- }else if(ret == 1){
- wlog_warn("buffer full");
- length=0;
- }else if(ret==4){
- wlog_warn("bad packet");
- length=0;
- }
- #endif
- }
- static void pocBeingClose(void){
- T_UINT32 subtime;
- T_INT8 info[50];
- wlog_warn("pocClose, updateData");
- talking.pttReqStatus=FALSE;//连接断掉后,如果当前在请麦
- talking.hasVoicePri=FALSE;
- pocStopPlayer();
- pocStopRecord();
- talking.netWork.tcp=0;
- if(TCP_ONLINE_IDLE==talking.tcpLoginStatus){
- snprintf(info, sizeof(info), "+POC:8200%08x\r\n", get_uid());
- msgToOutter(info);
- }
- talking.tcpLoginStatus=TCP_INIT;
- if(talking.loginAckAlready==FALSE)updateLineStatus(LINES_ACCOUNT_ERR, S_CLEAR);
- else talking.loginAckAlready=FALSE;
- subtime=getSysTick() - talking.conFailType.time;
- if(subtime <100){
- if(++talking.conFailType.count>=3){
- updateLineStatus(LINES_UN_CONNECT_IP, S_SET);
- talking.conFailType.count=0;
- }
- }
- else if(subtime > 500){//连接花很长时间才响应失败的连接,IP认为不可连接
- updateLineStatus(LINES_UN_CONNECT_IP, S_SET);
- }
- }
- static TUPSEND_DEF tupsendPara;
- void pocRecv_cb(void *param){
- // T_UINT8 pocTcpBuf[1024];
- unsigned char *pdata;
- int len_ret;
- nwy_osiEvent_t *pEvent = (nwy_osiEvent_t *)param;
- TUPSEND_DEF *para=(TUPSEND_DEF *)pEvent->param3;
- bool needtoclose=false;
-
- showTupEventInfo("poc",pEvent->id,para,1);
- switch(pEvent->id){
- case TUP_EVENT_SOCK_LINK_OK:
- wlog_info("poc connect server ok:%d",para->fd);
- talking.tcpSocket=para->fd;
- talking.netWork.tcp=1;
- break;
- case TUP_EVENT_SOCK_LINK_CLIENT_SHUT:
- wlog_warn("poc client close done");
- *para->defPara=TUP_STATUS_FALSE;//触发重新连接
- needtoclose=true;
- break;
- case TUP_EVENT_SOCK_LINK_RECV:
- pdata=(unsigned char *)pEvent->param1;
- len_ret=pEvent->param2;
- pocTcpDataHandler(pdata, len_ret);
- if(talking.accountError==TRUE){
- talking.accountError=FALSE;
- wlog_info("poc dly close for account error");
- userCloseSocket(¶->fd);//账号信息错误时,服务器会主动关掉终端,这里作了延迟关闭fd)
- }
- break;
- case TUP_EVENT_SOCK_LINK_SERVER_SHUT:
- wlog_warn("poc server close done");
- *para->defPara=TUP_STATUS_FALSE;//非定向
- needtoclose=true;
- break;
- case TUP_EVENT_SOCK_LINK_ERR:
- wlog_error("poc server err,retry");
- *para->defPara=TUP_STATUS_FALSE;//连接错误/失败
- needtoclose=true;
- break;
- default: break;
- }
-
- if(needtoclose==true){
- if(talking.tcpSocket!=0) talking.tcpSocket=0;
- pocBeingClose();
- }
- // LSAPI_OSI_Free(pEvent);
- // if(needtoclose==true) threadPostEvent(nwy_get_current_thread(),USER_EVENT_EXIT);
- }
- PT_THREAD (ptPocLoginConditionTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- static T_UINT8 index;
- static T_UINT8 printfCnt=0;
- static TUP_CONNECT_ENUM tupCStatus=TUP_STATUS_FALSE;
- T_UINT8 ret;
- PT_BEGIN(pt);
- while(1){
- if(1==talking.netWork.reg){
- //loginEnable();
- //if(0){
- if(TRUE==loginEnable()){//login is enable , is avaliable to pull tcp up
- if(0==talking.netWork.tcp){//tcp is off , need to pull up
- ticketDeVote(TICKET_PT_LOGINCON);
- #if 0
- if(LINES_ACCOUNT_ERR==getLineStatus()){//we meet account err ack, we can slow down the pull interval
- if(++talking.netWork.tcpAccountErrReCnt>=5) talking.netWork.tcpAccountErrReCnt=0;
- }else talking.netWork.tcpAccountErrReCnt=0;
- #else
- //只要是新连接,我们都不要那么快
- if(++talking.netWork.tcpAccountErrReCnt>=3) talking.netWork.tcpAccountErrReCnt=0;
- #endif
- if(talking.netWork.tcpAccountErrReCnt==0 && TUP_STATUS_WAIT != tupCStatus){//ready to try to pull up tcp
- tupsendPara.defPara=&tupCStatus;
- if(TRUE==setDomainForIp(paras.pocServer.addr, &index)){//if set ok , we wait for result
- while(1){
- ret=getDomainForIp(index, paras.pocServer.ip, sizeof(paras.pocServer.ip));
- if(DOMAIN_ERR==ret){
- wlog_warn("domain failed");
- break;
- }else if(DOMAIN_OK==ret){
- tupParaSet(&tupsendPara, DEFAULT_POC_PORT, pocRecv_cb,POC_TCP_THREAD_STACK);
- break;
- }else{
- PTTimerStart(ptPool, &ptTimer,1);//can be set fast
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- }
- }
- if(ret==DOMAIN_OK){
- if(TUP_STATUS_WAIT != tupCStatus) tupCStatus=tryConnectTup(paras.pocServer.ip,TUP_TCP,&tupsendPara);
- switch(tupCStatus){
- case TUP_STATUS_TRUE:
- wlog_info("poc connect server ok");
- talking.tcpSocket=tupsendPara.fd;
- talking.conFailType.time=getSysTick();
- talking.netWork.tcp=1;
- break;
- case TUP_STATUS_FALSE:
- wlog_warn("poc connect server failed, retry");
- if(++talking.conFailType.count>=3) updateLineStatus(LINES_UN_CONNECT_IP, S_SET);
- break;
- case TUP_STATUS_WAIT:
- break;
- }
- }
- }else if(TUP_STATUS_WAIT==tupCStatus){
- if(tupsendPara.errno!=0){
- tupCStatus=TUP_STATUS_FALSE;
- }
- }
- }else{
- tupCStatus=TUP_STATUS_ONLINE;
- ticketVote(TICKET_PT_LOGINCON);
- }
- }else{
- ticketVote(TICKET_PT_LOGINCON);
- if(++printfCnt>=5){
- printfCnt=0;
- wlog_info("poc login is invalid");
- }
- }
- }
- PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- //poc login logic handler start...////////////////////////////////
- static T_BOOL pocLoginTimeOut(void){
- static T_UINT8 tick=0;
- if(++tick < 5) return FALSE;
- tick=0;
- return TRUE;
- }
- static void pocKeepAliveUdp(void){
- T_UINT8 value;
- if(talking.workIn_4G==TRUE) value=paras.udpHeartTick_4G;
- else value=paras.udpHeartTick_none_4G;
- if(talking.pocTupCnt % value != 0) return;
- wlog_info("udp keep alive:%d", value);
- udpAliveConnect();
- msgToOutter("+POC:WAKEUP\r\n");
- }
- static void pocKeepAliveTcp(void){
- T_UINT8 value;
- if(talking.pocTupCnt != 0) return;
- if(talking.workIn_4G==TRUE) value=talking.maxValue_4G;
- else value=talking.maxValue_none_4G;
- wlog_info("tcp keep alive:%d,%d", value,talking.pocTupCnt);
- pocTryTcpHeart(talking.tcpSocket);
- talking.tcpHeartCtl.counter=0;
- talking.tcpHeartCtl.hadSendOnePacket=1;
- }
- void pocTupCntCheck(void){
- T_UINT8 value;
- if(talking.workIn_4G==TRUE) value=talking.maxValue_4G;
- else value=talking.maxValue_none_4G;
- if(++talking.pocTupCnt >= value) talking.pocTupCnt=0;
- }
- //检查登陆任务是否空闲
- void isLoginIdle(void){
- if(0==talking.netWork.tcp) ticketDeVote(TICKET_PT_LOGIN);
- else{
- //如果已登陆,投票
- if(TCP_ONLINE_IDLE==talking.tcpLoginStatus || talking.pocTupCnt!=0) ticketVote(TICKET_PT_LOGIN);//pocTupCnt为0时不能休眠,因为要发心跳或建立链接
- else ticketDeVote(TICKET_PT_LOGIN);
- }
- }
- PT_THREAD (ptPocLoginTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- PT_BEGIN(pt);
- while(1){
- if(1==talking.netWork.tcp){
- switch(talking.tcpLoginStatus){
- case TCP_INIT:
- wlog_info("Try login");
- talking.accountError=FALSE;
- pocTryLogin(talking.tcpSocket,paras.psn, paras.pass);
- talking.tcpLoginStatus=TCP_LOGINING;
- break;
- case TCP_LOGIN_SUCC:
- wlog_info("Try query group");
- pocTryQueryGroup(talking.tcpSocket,G_NUM_MAX);
- talking.tcpLoginStatus=TCP_LOGINING;
- break;
- case TCP_QUERY_GRP_SUCC:
- wlog_info("Try join group");
- talking.loginAckAlready=FALSE;
- pocTryJoinGroup(talking.tcpSocket);
- talking.tcpLoginStatus=TCP_LOGINING;
- break;
- case TCP_JOIN_GRP_SUCC:
- wlog_info("----->Poc Online<-----");
- talking.tcpLoginStatus=TCP_ONLINE_IDLE;
- talking.outputInfoInterval=30;
- talking.pocTupCnt=0;//登陆成功后马上发心跳,尽快建立UDP通道
- break;
- case TCP_LOGINING:
- if(TRUE==pocLoginTimeOut()){
- wlog_warn("Login timeout, reset");
- talking.tcpLoginStatus=TCP_INIT;
- }
- break;
- case TCP_COMM_FAIL:
- wlog_error("Login status failed, retry");
- closeTalk();
- break;
- case TCP_ONLINE_IDLE:
- pocKeepAliveUdp();
- pocKeepAliveTcp();
- pocTupCntCheck();
- break;
- default:break;
- }
- }
- isLoginIdle();
- PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- void closeTalk(void){
- T_INT8 info[50];
- wlog_warn("close talk");
- updateLineStatus(LINES_ONLINE, S_CLEAR);
- talking.pttReqStatus=FALSE;
- talking.hasVoicePri=FALSE;
- // if(isPlayerIsOff()==FALSE || isRecordIsOff()==FALSE) pocSpkMicNotify(C_OFF, C_OFF);//off line will output info new add
- pocStopPlayer();
- pocStopRecord();
-
- if(TCP_ONLINE_IDLE==talking.tcpLoginStatus){
- pocTryLogout(talking.tcpSocket);
- snprintf(info, sizeof(info), "+POC:8200%08x\r\n", get_uid());
- msgToOutter(info);
- }
- talking.tcpLoginStatus=TCP_INIT;
-
- if(talking.tcpSocket!=0){
- userCloseSocket(&talking.tcpSocket);
- talking.tcpSocket=0;
- }
- talking.netWork.tcp=0;
- if(talking.udpSocket!=0){
- userCloseSocket(&talking.udpSocket);
- talking.udpSocket=0;
- }
-
- talking.udpHeartAckCnt=0;
- talking.pocTupCnt=0;
- talking.tcpHeartCtl.counter=0;
- talking.tcpHeartCtl.hadSendOnePacket=0;
- GroupListClear();
- UserListClear();
- msgQueueClear(Q_VOICEP);
- msgQueueClear(Q_VOICER);
- }
- void pocStartPlayer(void){
- PlyOpenNowApi();
- //sem_post(talking.semPlyRec);
- }
- void pocStartRecord(void){
- talking.recordStatus=RECORD_START;
- RecOpenNowApi();
- //sem_post(talking.semPlyRec);
- }
- void pocStopPlayer(void){
- PlyCloseNowApi();
- }
- void pocStopRecord(void){
- RecCloseNowApi();
- talking.recordStatus=RECORD_END;
- }
- void GroupListClear(void){
- T_UINT16 i;
- for(i=0;i<G_NUM_MAX;i++){
- talking.gInfo.gInfo[i].id=0;
- talking.gInfo.gInfo[i].memNum=0;
- memset(talking.gInfo.gInfo[i].name,0,sizeof(talking.gInfo.gInfo[i].name));
- }
- talking.gInfo.gIndex=0;
- talking.gInfo.g_numbers=0;
- }
- void UserListClear(void){
- T_UINT16 i;
- for(i=0;i<U_NUM_MAX;i++){
- talking.uInfo.uInfo[i].id=0;
- memset(talking.uInfo.uInfo[i].name,0,sizeof(talking.uInfo.uInfo[i].name));
- }
- talking.uInfo.u_numbers=0;
- }
- T_INT32 getPocMinCnt(void){
- T_INT32 value;
- if(talking.workIn_4G==TRUE) value=paras.udpHeartTick_4G;
- else value=paras.udpHeartTick_none_4G;
- value -= talking.pocTupCnt%30;
- return value;
- }
- void pocCntUpdate(T_INT32 value){
- talking.pocTupCnt += value;//下次起来就可以直接发心跳了
- if(talking.workIn_4G==TRUE) value=talking.maxValue_4G;
- else value=talking.maxValue_none_4G;
- if(talking.pocTupCnt>=value) talking.pocTupCnt=0;//要判断一下,否则会发两次UDP包
- }
- T_UID get_uid(void){return talking.uid;}
- T_GID get_gid(void){return talking.gid;}
|