group.c 8.5 KB

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