#include "tupApi.h" #include "log.h" #include #include "main.h" #include "app.h" void tupParaSet(TUPSEND_DEF *para, T_UINT16 port, LSAPI_SOCK_Callback *callback,T_UINT16 stacksize){ para->port=port; para->tupRecvCallBack=callback; para->stacksize=stacksize; } void trySendTup(T_UINT8 *data, T_UINT16 len, TUP_ENUM tup, TUPSEND_DEF *para){ T_INT32 ret; if(para->fd<=0){ wlog_warn("tup failed,invalid fd"); return; } if(TUP_TCP==para->tupType) ret=LSAPI_SOCK_Send(para->fd,data,len,0); else ret=LSAPI_SOCK_Sendto(para->fd,data,len,0, ¶->sockAddr, sizeof(LSAPI_SOCK_TcpipSocketAddr_t)); if(ret<0) wlog_info("tup[%d]:%d send failed", para->fd,len); else wlog_info("tup[%d]:%d send ok",para->fd,len); } void userCloseSocket(T_INT32 *fd){ if(*fd<=0) return; wlog_info("SockClose:%d",*fd); LSAPI_SOCK_Close(*fd); *fd=0; } void showTupEventInfo(const char *name, T_UINT32 id,TUPSEND_DEF *para){ if(para->ptimer_t!=NULL){//有回调响应后清除定时器 wlog_info("[%s]timerStop",name); LSAPI_OSI_TimerStop(para->ptimer_t); para->ptimer_t=NULL; } switch(id){ case LSAPI_SOCK_TCPIP_REV_DATA_IND: wlog_info("[%d %s]:recv data ok",id,name);break; case LSAPI_SOCK_TCPIP_CLOSE_IND: para->tupStatus=CNNT_CLOSED;userCloseSocket(¶->fd);wlog_info("[%d %s]:server shut",id,name);break; case LSAPI_SOCK_TCPIP_ERR_IND: para->tupStatus=CNNT_ERROR;userCloseSocket(¶->fd);wlog_info("[%d %s]:link err",id,name);break; case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:para->tupStatus=CNNT_OK;wlog_info("[%d %s]:link ok",id,name);break; case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP: para->tupStatus=CNNT_CLOSED;userCloseSocket(¶->fd);wlog_info("[%d %s]:client shut",id,name);break; case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP: wlog_info("[%d %s]:send data ok",id,name);break; default:wlog_info("%d %s]:ignore",id,name);break; } WakeupNow(name); } void sockTimerOutCallback(void *param){ TUPSEND_DEF *para=(TUPSEND_DEF *)param; wlog_info("sockTimerOutCallback enter"); LSAPI_OSI_TimerStop(para->ptimer_t); userCloseSocket(¶->fd); para->failNo=3;//连接超时错误3 threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT,NULL); } void sockEntry(void *param){ TUPSEND_DEF *para=(TUPSEND_DEF *)param; T_INT32 sockfd; T_UINT8 type; int g_keepalive = 0; wlog_info("[NewThread:sockEntry]%x",LSAPI_OSI_ThreadCurrent()); if(TUP_TCP==para->tupType) type=LSAPI_SOCK_TCPIP_SOCK_STREAM; else type=LSAPI_SOCK_TCPIP_SOCK_DGRAM; sockfd = LSAPI_SOCK_Create(LSAPI_SOCK_TCPIP_AF_INET, type, 0, para->tupRecvCallBack,(uint32_t)para); if(sockfd<0){ wlog_info("sockfd create err=%d",LSAPI_SOCK_Error()); para->failNo=1;//错误1 wlog_warn("reset pdp!!!"); app.netWork.resetPDP=1; LSAPI_OSI_ThreadExit(); } para->fd=sockfd; wlog_info("tup info:%d,%s:%d", sockfd,para->saddr, para->port); LSAPI_SOCK_TcpipSocketAddr_t *serveraddr=¶->sockAddr; serveraddr->sin_family = LSAPI_SOCK_TCPIP_AF_INET; serveraddr->sin_port = LSAPI_SOCK_htons(para->port); serveraddr->sin_addr.s_addr = LSAPI_SOCK_inet_addr(para->saddr); LSAPI_SOCK_SetSockOpt(sockfd, LSAPI_SOCK_SOL_SOCKET, LSPAI_SOCK_KEEPALIVE, (void *)&g_keepalive, sizeof(g_keepalive)); if(LSAPI_RET_OK != (LSAPI_SOCK_Connect(sockfd, serveraddr))){ wlog_error("sockfd connect err"); userCloseSocket(&sockfd); para->fd=0; para->failNo=2;//错误2 userExitThread("sockEntry"); return; } para->ptimer_t=LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), sockTimerOutCallback, param); if(NULL != para->ptimer_t) LSAPI_OSI_TimerStart(para->ptimer_t, 10000);//设置为超时10秒 wlog_info("open fd:%d",sockfd); LSAPI_OSI_Event_t event={}; for(;;){ LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event); if(USER_EVENT_EXIT==event.id){ userCloseSocket(¶->fd); userExitThread("sockEntry"); } } } TUP_CONNECT_ENUM tryConnectTup(T_INT8 *seraddr, TUP_ENUM tup,TUPSEND_DEF *para){ //必须使用线程来创建连接再waitevent para->saddr=seraddr; para->tupType=tup; para->failNo=0; para->tupStatus=CNNT_BUSY; para->fd=0; if(NULL==LSAPI_OSI_ThreadCreate("sockCreate", sockEntry,(void *)para,LSAPI_OSI_PRIORITY_NORMAL,para->stacksize,4)){ wlog_error("tryConnectTup thread create error for %s",seraddr); return TUP_STATUS_FALSE; } return TUP_STATUS_WAIT; } void threadPostEvent(LSAPI_OSI_Thread_t *threadId, T_INT32 id, T_INT32 param1){ LSAPI_OSI_Event_t pEventSend; pEventSend.id=id; pEventSend.param1=param1; LSAPI_OSI_EvnetSend(threadId,&pEventSend); } void userExitThread(const char *name){ wlog_info("[DieThread:%x]%s", LSAPI_OSI_ThreadCurrent(),name); LSAPI_OSI_ThreadExit(); }