tupApi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "tupApi.h"
  2. #include "log.h"
  3. #include <string.h>
  4. void threadPostEvent(LSAPI_OSI_Thread_t *threadId, T_INT32 id){
  5. LSAPI_OSI_Event_t pEventSend;
  6. pEventSend.id=id;
  7. //LSAPI_OSI_EvnetSend(threadId,&pEventSend);
  8. osiEventTrySend(threadId,&pEventSend,0);
  9. }
  10. void userExitThread(const char *name){
  11. wlog_info("[DieThread:%x]%s", LSAPI_OSI_ThreadCurrent(),name);
  12. LSAPI_OSI_ThreadExit();
  13. }
  14. void tupParaSet(TUPSEND_DEF *para, T_UINT16 port, LSAPI_SOCK_Callback *callback,T_UINT16 stacksize){
  15. para->port=port;
  16. para->tupRecvCallBack=callback;
  17. para->stacksize=stacksize;
  18. }
  19. void trySendTup(T_UINT8 *data, T_UINT16 len, TUP_ENUM tup, TUPSEND_DEF *para){
  20. T_INT32 ret;
  21. if(para->fd<=0) return;
  22. if(TUP_TCP==para->tupType) ret=LSAPI_SOCK_Send(para->fd,data,len,0);
  23. else ret=LSAPI_SOCK_Sendto(para->fd,data,len,0, &para->sockAddr, sizeof(LSAPI_SOCK_TcpipSocketAddr_t));
  24. if(ret<0) wlog_info("tup[%d]:%d send failed", para->fd,len);
  25. else wlog_info("tup[%d]:%d send ok",para->fd,len);
  26. }
  27. void userCloseSocket(T_INT32 *fd){
  28. if(*fd<=0) return;
  29. wlog_info("SockClose:%d",*fd);
  30. LSAPI_SOCK_Close(*fd);
  31. *fd=0;
  32. }
  33. void showTupEventInfo(const char *name, T_UINT32 id,TUPSEND_DEF *para, char needShutWhenSerShut){
  34. if(para->ptimer_t!=NULL){//有回调�应�清除定时�
  35. wlog_info("[%s]timerStop",name);
  36. LSAPI_OSI_TimerStop(para->ptimer_t);
  37. if(NULL != para->ptimer_t) LSAPI_OSI_TimerDelete(para->ptimer_t);
  38. para->ptimer_t=NULL;
  39. }
  40. switch(id){
  41. case LSAPI_SOCK_TCPIP_REV_DATA_IND: wlog_info("[%d %s]:recv data ok",id,name);break;
  42. case LSAPI_SOCK_TCPIP_CLOSE_IND:
  43. para->tupStatus=CNNT_CLOSED;
  44. if(needShutWhenSerShut==0) userCloseSocket(&para->fd);
  45. else wlog_info("[%d %s dly shut fd",id,name);
  46. wlog_info("[%d %s]:server shut",id,name);
  47. break;
  48. case LSAPI_SOCK_TCPIP_ERR_IND: para->tupStatus=CNNT_ERROR;userCloseSocket(&para->fd);wlog_info("[%d %s]:link err",id,name);break;
  49. case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:para->tupStatus=CNNT_OK;wlog_info("[%d %s]:link ok",id,name);break;
  50. case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP: para->tupStatus=CNNT_CLOSED;userCloseSocket(&para->fd);wlog_info("[%d %s]:client shut",id,name);break;
  51. case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP: wlog_info("[%d %s]:send data ok",id,name);break;
  52. default:wlog_info("%d %s]:ignore",id,name);break;
  53. }
  54. }
  55. void sockTimerOutCallback(void *param){
  56. TUPSEND_DEF *para=(TUPSEND_DEF *)param;
  57. wlog_info("sockTimerOutCallback enter");
  58. LSAPI_OSI_TimerStop(para->ptimer_t);
  59. userCloseSocket(&para->fd);
  60. para->errno=3;//连接超时错误3
  61. threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
  62. }
  63. void AppsockEntry(void *param){
  64. TUPSEND_DEF *para=(TUPSEND_DEF *)param;
  65. T_INT32 sockfd;
  66. T_UINT8 type;
  67. int g_keepalive = 0;
  68. wlog_info("[NewThread:sockEntry]%x",LSAPI_OSI_ThreadCurrent());
  69. if(TUP_TCP==para->tupType) type=LSAPI_SOCK_TCPIP_SOCK_STREAM;
  70. else type=LSAPI_SOCK_TCPIP_SOCK_DGRAM;
  71. sockfd = LSAPI_SOCK_Create(LSAPI_SOCK_TCPIP_AF_INET, type, 0, para->tupRecvCallBack,(uint32_t)para);
  72. if(sockfd<0){
  73. wlog_info("sockfd create err=%d",LSAPI_SOCK_Error());
  74. para->errno=1;//错误1
  75. LSAPI_OSI_ThreadExit();
  76. }
  77. para->fd=sockfd;
  78. wlog_info("tup info:%d,%s:%d", sockfd,para->saddr, para->port);
  79. LSAPI_SOCK_TcpipSocketAddr_t *serveraddr=&para->sockAddr;
  80. serveraddr->sin_family = LSAPI_SOCK_TCPIP_AF_INET;
  81. serveraddr->sin_port = LSAPI_SOCK_htons(para->port);
  82. serveraddr->sin_addr.s_addr = LSAPI_SOCK_inet_addr(para->saddr);
  83. wlog_info("tup info222:%d,%s:%d", sockfd,para->saddr, para->port);
  84. LSAPI_SOCK_SetSockOpt(sockfd, LSAPI_SOCK_SOL_SOCKET, LSPAI_SOCK_KEEPALIVE, (void *)&g_keepalive, sizeof(g_keepalive));
  85. if(LSAPI_RET_OK != LSAPI_SOCK_Connect(sockfd, serveraddr)){
  86. wlog_info("sockfd connect err");
  87. userCloseSocket(&sockfd);
  88. para->fd=0;
  89. para->errno=2;//错误2
  90. para->tupStatus=CNNT_ERROR;
  91. userExitThread("sockEntry");
  92. return;
  93. }
  94. para->ptimer_t=LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), sockTimerOutCallback, param);
  95. if(NULL != para->ptimer_t) LSAPI_OSI_TimerStart(para->ptimer_t, 10000);//设置为超�0�10000
  96. wlog_info("open fd:%d",sockfd);
  97. LSAPI_OSI_Event_t event={};
  98. for(;;){
  99. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  100. //LSAPI_OSI_Free(pEvent);
  101. if(USER_EVENT_EXIT==event.id){
  102. userCloseSocket(&para->fd);
  103. if(NULL != para->ptimer_t) LSAPI_OSI_TimerDelete(para->ptimer_t);
  104. userExitThread("AppsockEntry");
  105. }
  106. }
  107. }
  108. TUP_CONNECT_ENUM tryConnectTup(T_INT8 *seraddr, TUP_ENUM tup,TUPSEND_DEF *para){
  109. //必须使用线程�创建连接�waitevent
  110. para->saddr=seraddr;
  111. para->tupType=tup;
  112. para->errno=0;
  113. para->tupStatus=CNNT_BUSY;
  114. para->fd=0;
  115. if(NULL==LSAPI_OSI_ThreadCreate("AppsockCreate", AppsockEntry,(void *)para,LSAPI_OSI_PRIORITY_NORMAL,para->stacksize,4)){
  116. wlog_info("tryConnectTup thread create error for %s",seraddr);
  117. return TUP_STATUS_FALSE;
  118. }
  119. return TUP_STATUS_WAIT;
  120. }