group.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. group.c
  3. 集群功能任务
  4. */
  5. #include "includes.h"
  6. #define GROUP_SER_PORT 25060
  7. typedef enum{
  8. GROUP_IDLE,
  9. GROUP_TCP,
  10. GROUP_TCP_WAIT,
  11. GROUP_SEND_WAIT_ACK,
  12. GROUP_END,
  13. }GROUP_ENUM;
  14. static GROUP_ENUM groupStatus=GROUP_IDLE;
  15. T_INT32 login_status=0;
  16. static TUPSEND_DEF tupsendPara;
  17. static void group_login_req(void);
  18. static void groupRecv(T_INT32 fd);
  19. static void LoginAckHandle(T_UINT8 *data, T_UINT16 len);
  20. static T_INT16 timeout;
  21. /*
  22. groupRecv_cb
  23. 集群访问接收监控IO回调
  24. */
  25. void groupRecv_cb(void *param){
  26. int len_ret;
  27. T_UINT8 buffer[256];
  28. LSAPI_OSI_Event_t *pEvent = (LSAPI_OSI_Event_t *)param;
  29. TUPSEND_DEF *para=(TUPSEND_DEF *)pEvent->param3;
  30. T_BOOL needExitThread=FALSE;
  31. showTupEventInfo("group",pEvent->id,para,1);//服务器主动关闭时,不马上关闭TCP,否则收到不数据了,这里延迟后也不闭TCP了,因为有超时判断
  32. switch(pEvent->id){
  33. case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:
  34. tupsendPara.fd=para->fd;
  35. if(GROUP_TCP_WAIT==groupStatus) groupStatus=GROUP_SEND_WAIT_ACK;
  36. break;
  37. case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP:break;
  38. case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP:
  39. if(GROUP_TCP_WAIT==groupStatus){
  40. if(--timeout){
  41. groupStatus=GROUP_TCP;
  42. wlog_warn("group connect retry");
  43. }else{
  44. wlog_warn("group connect tout");
  45. groupStatus=GROUP_END;
  46. }
  47. needExitThread=TRUE;
  48. }
  49. break;
  50. case LSAPI_SOCK_TCPIP_REV_DATA_IND:
  51. while(1){
  52. len_ret=LSAPI_SOCK_Recv(para->fd, buffer, sizeof(buffer),0);
  53. if(len_ret>=0){
  54. if(len_ret==0) break;
  55. LoginAckHandle(buffer, len_ret);
  56. userCloseSocket(&para->fd);
  57. needExitThread=TRUE;
  58. break;
  59. }else{
  60. wlog_warn("group recv failed");
  61. break;
  62. }
  63. }
  64. break;
  65. case LSAPI_SOCK_TCPIP_CLOSE_IND:break;
  66. case LSAPI_SOCK_TCPIP_ERR_IND:
  67. wlog_error("group server err");
  68. groupStatus=GROUP_END;
  69. needExitThread=TRUE;
  70. break;
  71. default: break;
  72. }
  73. LSAPI_OSI_Free(pEvent);
  74. if(TRUE==needExitThread) threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
  75. }
  76. void isGroupIdle(void){
  77. if(TRUE==talking.groupStart) ticketDeVote(TICKET_PT_GROUP);
  78. else ticketVote(TICKET_PT_GROUP);
  79. }
  80. /*
  81. ptGroupTask
  82. 集群访问任务操作
  83. */
  84. //group handler start...////////////////////////////////
  85. PT_THREAD (ptGroupTask(pt_timer_t *ptPool, struct pt *pt)){
  86. static pt_timer_t ptTimer;
  87. static T_UINT8 index,i;
  88. T_UINT8 ret;
  89. static T_INT8 ipAddr[20];
  90. PT_BEGIN(pt);
  91. while(1){
  92. if(TRUE==talking.groupStart && talking.netWork.netReady!=0){
  93. if(paras.groupServer[0]=='\0'){
  94. wlog_warn("group server null , quit");
  95. talking.groupStart=FALSE;
  96. }else if(GROUP_IDLE==groupStatus){
  97. if(FALSE==setDomainForIp(paras.groupServer, &index)){
  98. wlog_warn("group domain fun busy, retry later");
  99. PTTimerStart(ptPool, &ptTimer,300);//3 seconds later retry
  100. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  101. }else{
  102. i=0;
  103. while(1){
  104. ret=getDomainForIp(index, ipAddr, sizeof(ipAddr)-1);
  105. if(ret==DOMAIN_ERR){
  106. wlog_error("group domain fail , quit");
  107. talking.groupStart=FALSE;
  108. groupStatus=GROUP_END;break;
  109. }else if(DOMAIN_OK==ret){
  110. wlog_info("group domain done:%s", ipAddr);
  111. tupParaSet(&tupsendPara, GROUP_SER_PORT, groupRecv_cb,GROUP_TCP_THREAD_STACK);
  112. timeout=5;
  113. groupStatus=GROUP_TCP;
  114. break;
  115. }else{
  116. PTTimerStart(ptPool, &ptTimer,50);//wait 0.5 per time
  117. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  118. if(++i>=20){
  119. wlog_error("group domain timeout, quit");
  120. talking.groupStart=FALSE;
  121. groupStatus=GROUP_END;break;
  122. }
  123. }
  124. }
  125. }
  126. }else if(GROUP_TCP==groupStatus){
  127. //do tcp for ip now
  128. if(1==talking.netWork.netReady){
  129. switch(tryConnectTup(ipAddr,TUP_TCP,&tupsendPara)){
  130. case TUP_STATUS_TRUE:
  131. wlog_info("group connect server ok");
  132. groupStatus=GROUP_SEND_WAIT_ACK;
  133. break;
  134. case TUP_STATUS_FALSE:
  135. if(--timeout){
  136. wlog_warn("group connect failed, retry later");
  137. PTTimerStart(ptPool, &ptTimer,300);//3 seconds later retry
  138. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  139. }else{
  140. wlog_warn("group connect timeout");
  141. groupStatus=GROUP_END;
  142. }
  143. break;
  144. case TUP_STATUS_WAIT:
  145. wlog_info("group connect wait");
  146. groupStatus=GROUP_TCP_WAIT;
  147. break;
  148. }
  149. }
  150. }else if(GROUP_SEND_WAIT_ACK==groupStatus){
  151. login_status=0;
  152. group_login_req();
  153. wlog_info("group wait ack");
  154. timeout=5;
  155. do{
  156. PTTimerStart(ptPool, &ptTimer,100);
  157. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  158. }while(--timeout && login_status<=0);
  159. if(1==login_status) wlog_info("group login ok");
  160. else if(2==login_status) wlog_info("group login but no right");
  161. else if(3==login_status) wlog_info("group packet error:len");
  162. else if(4==login_status) wlog_info("group packet error:sum");
  163. else if(5==login_status) wlog_info("group packet error:no login");
  164. else wlog_info("group login but server no responsed");
  165. //close tcp
  166. groupStatus=GROUP_END;
  167. }else if(GROUP_END==groupStatus){
  168. wlog_info("group end");
  169. talking.groupStart=FALSE;
  170. groupStatus=GROUP_IDLE;
  171. }
  172. }
  173. isGroupIdle();
  174. PTTimerStart(ptPool, &ptTimer,1);//should be fast
  175. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  176. }
  177. PT_END(pt);
  178. }
  179. #ifdef ACTIVE_LIBEV
  180. /*
  181. groupRecv_cb
  182. 集群访问接收监控IO回调
  183. */
  184. static void groupRecv_cb(struct ev_loop *main_loop,ev_io *io_w, int revents){
  185. T_INT32 len;
  186. T_UINT8 buffer[256];
  187. if(EV_ERROR & revents){
  188. wlog_warn("groupRecv_cb error");
  189. ev_io_stop(main_loop, io_w);
  190. return;
  191. }
  192. len=recv(tupsendPara.fd, buffer, sizeof(buffer),0);
  193. wlog_info("get data len:%d", len);
  194. if(len>0) LoginAckHandle(buffer, len);
  195. userCloseSocket(tupsendPara.fd);
  196. ev_io_stop(main_loop, io_w);
  197. }
  198. #endif
  199. /*
  200. groupRecv
  201. 集群访问接收监控IO创建
  202. */
  203. static void groupRecv(T_INT32 fd){
  204. #ifdef ACTIVE_LIBEV
  205. static ev_io io_recv;
  206. wlog_info("groupRecv call");
  207. ev_io_stop(loop, &io_recv);
  208. ev_init(&io_recv, groupRecv_cb);
  209. ev_io_set(&io_recv, fd,EV_READ);
  210. ev_io_start(loop, &io_recv);
  211. #endif
  212. }
  213. /*
  214. SendLoginFill
  215. 集群访问请求数据包打包
  216. */
  217. static T_UINT16 SendLoginFill(T_UINT8 *info,T_UINT32 psn, T_INT8 *key){
  218. T_UINT8 *p=info;
  219. T_UINT16 i,len;
  220. i=0;
  221. p[i++]=(psn>>24);
  222. p[i++]=(psn>>16);
  223. p[i++]=(psn>>8);
  224. p[i++]=psn;
  225. p[i++]=0;
  226. p[i++]=0;
  227. p[i++]=0;
  228. p[i++]=0;
  229. memcpy(p+i, "groupPoc",8);
  230. i+= 8;
  231. p[i++]=0;
  232. p[i++]=0;
  233. len=strlen(key);
  234. if(len>20) len=20;
  235. memset(p+i, 0, 20);
  236. memcpy(p+i, key,len);
  237. i += 20;
  238. return i;
  239. }
  240. /*
  241. SendDataPack
  242. 集群访问请求数据包发送
  243. */
  244. static T_UINT16 SendDataPack(T_UINT8 *info, T_UINT16 datalen){
  245. T_UINT16 j,i=0;
  246. T_UINT8 *pBuf=info,sum;
  247. pBuf[0]=0x12;
  248. i=datalen+1;
  249. pBuf[1]=i>>8;
  250. pBuf[2]=i;
  251. sum=0;
  252. for(j=0;j<1+2+datalen;j++) sum += pBuf[j];
  253. pBuf[3+datalen]=sum;
  254. return (3+datalen+1);
  255. }
  256. /*
  257. group_login_req
  258. 集群访问请求数据包
  259. */
  260. static void group_login_req(void){
  261. T_UINT8 info[128];
  262. T_UINT16 length;
  263. length=SendLoginFill(info+3,paras.psn,paras.pass);
  264. length=SendDataPack(info,length);
  265. wlog_info("group send login req info");
  266. trySendTup(info, length, TUP_TCP, &tupsendPara);
  267. }
  268. /*
  269. LoginAckHandle
  270. 集群访问请求的响应数据解析
  271. */
  272. #define INVALID_IP_ADDRESS "0.0.0.0"
  273. static void LoginAckHandle(T_UINT8 *data, T_UINT16 len){
  274. T_UINT8 type,sum,right;
  275. T_UINT8 *p=data;
  276. T_UINT16 thisLen,i;
  277. T_INT8 info[IP_SIZE+1];
  278. T_UINT8 *pip,*gip,needsave=0;
  279. type=*p++;
  280. thisLen=*p++;
  281. thisLen <<= 8;
  282. thisLen &= 0xff00;
  283. thisLen |= *p++;
  284. if((1+2+thisLen) != len){
  285. login_status=3;
  286. return;
  287. }
  288. sum=0;
  289. for(i=0;i<thisLen+2;i++) sum += data[i];
  290. if(sum != data[len-1]){
  291. login_status=4;
  292. return;
  293. }
  294. if(type != 0x12){
  295. login_status=5;
  296. return;
  297. }
  298. right=*p++;
  299. if(0xea != right){
  300. login_status=2;
  301. return;
  302. }
  303. pip=data+23;
  304. gip=data+27;
  305. snprintf(talking.npAddr, sizeof(talking.npAddr), "%d.%d.%d.%d", pip[0],pip[1],pip[2],pip[3]);
  306. snprintf(talking.ngAddr, sizeof(talking.ngAddr), "%d.%d.%d.%d", gip[0],gip[1],gip[2],gip[3]);
  307. wlog_info("group:newpip[%s] newgip[%s]", talking.npAddr,talking.ngAddr);
  308. if(0!=strcmp(paras.gps.gpsServer.addr, talking.ngAddr) && 0!=strcmp(INVALID_IP_ADDRESS, talking.ngAddr)){
  309. wlog_warn("fine new gip");
  310. strcpy(paras.gps.gpsServer.addr, talking.ngAddr);
  311. needsave=1;
  312. }
  313. if(0!=strcmp(paras.pocServer.addr, talking.npAddr) && 0!=strcmp(INVALID_IP_ADDRESS,talking.npAddr)){
  314. wlog_warn("fine new pip");
  315. strcpy(paras.pocServer.addr, talking.npAddr);
  316. needsave=1;
  317. closeTalk();
  318. }
  319. if(needsave!=0) SaveNewPara();
  320. login_status=1;
  321. }