THREADRET tunet_thread(THREAD *self) { BOOL hasErr = FALSE; fd_set rset; fd_set wset; fd_set eset; struct timeval tv; time_t keepalive_watchDog=0; tv.tv_sec=2; tv.tv_usec=0; int ret,flags; int maxsock; int main_retry=0; char flag,connected=0; logs_append(g_logs, "TUNET_THREAD_STARTING", NULL, NULL, 0); while(os_thread_is_running(self) || os_thread_is_paused(self)) { // printf("main_socket=%d\n",main_socket); if (keepalive_watchDog) { if (time(NULL)-keepalive_watchDog>=36) { if(tunet_keepalive()==ERR) break; } } if (tunet_state==TUNET_STATE_RECV_REMAINING_DATA || tunet_state==TUNET_STATE_RECV_WELCOME) { keepalive_watchDog=time(NULL); } hasErr=FALSE; if(tunet_get_state() == TUNET_STATE_NONE) { break; } if(tunet_get_state() == TUNET_STATE_ERROR) { //an server-side error occurs when trying to login/logout logs_append(g_logs, "TUNET_ERROR", NULL, NULL, 0); break; } if (tunet_state ==TUNET_STATE_LOGOUT) { if (!connected) { break; } tunet_connect_logout_server(); } if (tunet_state==TUNET_STATE_LOGOUT||tunet_state==TUNET_STATE_LOGOUT_RECV_LOGOUT) { hasErr |= (tunet_logout_send_logout() == ERR); hasErr |= (tunet_logout_recv_logout() == ERR); if(hasErr) { //if an error occurs when LOGOUT, we can just omit it, and exit the thread. break; } os_thread_test_paused(self); os_sleep(20); }else { if(tunet_state == TUNET_STATE_LOGIN){ tunet_connect_main_server(); } if(tunet_connect_keepalive_server()==ERR){ break; } if (tunet_state == TUNET_STATE_KEEPALIVE||tunet_state==TUNET_STATE_RECV_REMAINING_DATA) { connected=1; } FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset); maxsock=0; flag=0; if (main_socket) { FD_SET(main_socket, &rset); FD_SET(main_socket, &wset); FD_SET(main_socket, &eset); if (main_socket>=maxsock) { maxsock=main_socket; } flag=1; } if (keepalive_socket) { FD_SET(keepalive_socket,&rset); FD_SET(keepalive_socket,&eset); if (keepalive_socket>=maxsock) { maxsock=keepalive_socket; } } ret=select(maxsock+1,&rset,&wset,&eset,&tv); if (ret<0) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "UNKNOWN", NULL, 0); break; }else if (ret==0) { if (tunet_state==TUNET_STATE_LOGIN) { if (main_retry>=3) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "MAIN", NULL, 0); break; } main_retry++; } os_thread_test_paused(self); continue; } if (flag) { if (FD_ISSET(main_socket,&wset)) { tunet_logon_send_tunet_user(); tunet_logon_reply_welcome(); } if (FD_ISSET(main_socket,&rset)) { if(tunet_logon_recv_welcome()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "RECV_WELCOME", NULL, 0); break; } if(tunet_logon_recv_remaining_data()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "RECV_REMAINING_DATA", NULL, 0); break; } } if (FD_ISSET(main_socket, &eset)) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "MAIN", NULL, 0); break; } } if (FD_ISSET(keepalive_socket,&rset)) { if(tunet_keepalive()==ERR){ logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); break; } keepalive_watchDog=time(NULL); } if (FD_ISSET(keepalive_socket, &eset)) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); break; } os_thread_test_paused(self); } } main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); logout_socket = os_socket_tcp_close(logout_socket); main_socket_buffer = buffer_clear(main_socket_buffer); keepalive_socket_buffer = buffer_clear(keepalive_socket_buffer); logout_socket_buffer = buffer_clear(logout_socket_buffer); tunet_state = TUNET_STATE_NONE; logs_append(g_logs, "TUNET_THREAD_EXITING", NULL, NULL, 0); thread_tunet = os_thread_free(thread_tunet); return 0; }
THREADRET tunet_thread(THREAD *self) { BOOL hasErr = FALSE; logs_append(g_logs, "TUNET_THREAD_STARTING", NULL, NULL, 0); while(os_thread_is_running(self) || os_thread_is_paused(self)) { if(tunet_state == TUNET_STATE_LOGIN) tunet_connect_main_server(); hasErr = FALSE; hasErr |= (tunet_logon_send_tunet_user() == ERR); hasErr |= (tunet_logon_recv_welcome() == ERR); hasErr |= (tunet_logon_reply_welcome() == ERR); hasErr |= (tunet_logon_recv_remaining_data() == ERR); hasErr |= (tunet_connect_keepalive_server() == ERR); hasErr |= (tunet_keepalive() == ERR); if(hasErr) { /* COMMENT: We won't help the user to retry for some network error shoud be dealt by the users. //a network error occurs when try to LOGIN main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); tunet_state = TUNET_STATE_LOGIN; continue; */ break; } hasErr = FALSE; if(tunet_state == TUNET_STATE_LOGOUT) tunet_connect_logout_server(); hasErr |= (tunet_logout_send_logout() == ERR); hasErr |= (tunet_logout_recv_logout() == ERR); if(hasErr) { //if an error occurs when LOGOUT, we can just omit it, and exit the thread. break; } if(tunet_get_state() == TUNET_STATE_NONE) { //nothing to do , exit the thread break; } if(tunet_get_state() == TUNET_STATE_ERROR) { //an server-side error occurs when trying to login/logout logs_append(g_logs, "TUNET_ERROR", NULL, NULL, 0); break; } os_thread_test_paused(self); os_sleep(20); } main_socket = os_socket_tcp_close(main_socket); keepalive_socket = os_socket_tcp_close(keepalive_socket); logout_socket = os_socket_tcp_close(logout_socket); main_socket_buffer = buffer_clear(main_socket_buffer); keepalive_socket_buffer = buffer_clear(keepalive_socket_buffer); logout_socket_buffer = buffer_clear(logout_socket_buffer); tunet_state = TUNET_STATE_NONE; logs_append(g_logs, "TUNET_THREAD_EXITING", NULL, NULL, 0); thread_tunet = os_thread_free(thread_tunet); return 0; }