void CClient::run() { setThreadName("CClient::run"); try { while(!terminate) { CPack *pack = serv->retreivePack(); //get the package from the server if (terminate) { vstd::clear_pointer(pack); break; } handlePack(pack); } } //catch only asio exceptions catch (const boost::system::system_error& e) { logNetwork->errorStream() << "Lost connection to server, ending listening thread!"; logNetwork->errorStream() << e.what(); if(!terminate) //rethrow (-> boom!) only if closing connection was unexpected { logNetwork->errorStream() << "Something wrong, lost connection while game is still ongoing..."; throw; } } }
/*#################################### 函数功能: 监听fd 入参: 无 #####################################*/ int isFdReadable(void) { /* 等待客户端连接 */ int sock_fd; fd_set fds; struct timeval tv; int retVal; if(g_sysParam->udp_fd < 0) { DEBUG("somethig rong with udp fd.\n"); return RET_FAIL; } sock_fd = g_sysParam->udp_fd; DEBUG("Wait for recieving data..\n"); while(1) { /* 监控sock_fd */ FD_ZERO(&fds); FD_SET(sock_fd,&fds); /* 设置等待时间5s */ tv.tv_sec = 5; tv.tv_usec = 0; retVal = select(sock_fd+1,&fds,NULL,NULL,&tv); switch(retVal) { case -1: DEBUG("receive err.\n"); if(g_sysParam->revFlag == 1) { /* 上报错误 */ rptRevErr("receive data err"); DEBUG("receive data error ,clean up received data!!!\n"); /* 清标志 */ g_sysParam->revFlag = 0; /* 清空之前接收的buf等数据 */ delRecvFile(); } break; case 0: DEBUG("receive time out..\n"); /* 添加接收超时判断 */ if(g_sysParam->revFlag == 1) { /* 上报错误 */ rptRevErr("receive data time out"); DEBUG("receive data time out ,clean up received data!!!\n"); /* 清标志 */ g_sysParam->revFlag = 0; /* 清空之前接收的buf等数据 */ delRecvFile(); } break; default: if(FD_ISSET(sock_fd,&fds) == 1) { FD_ZERO(&fds); DEBUG("UDP sock can be read.\n"); handlePack(sock_fd); } break; } } return retVal; }
/*#################################### 函数功能: 监听fd 入参: 无 #####################################*/ int fdIsReadable(void) { /* 等待客户端连接 */ int sock_fd; fd_set fds; struct timeval tv; int retVal; if(g_sysParam->udp_fd < 0) { DEBUG("somethig rong with udp fd.\n"); return RET_FAIL; } sock_fd = g_sysParam->udp_fd; DEBUG("Wait for recieving data..\n"); while(1) { /* 监控sock_fd */ FD_ZERO(&fds); FD_SET(sock_fd,&fds); /* 设置等待时间5s */ tv.tv_sec = 5; tv.tv_usec = 0; retVal = select(sock_fd+1,&fds,NULL,NULL,&tv); switch(retVal) { case -1: DEBUG("select err.\n"); if(g_sysParam->revFlag == 1) { /* 上报错误 */ rptRevErr("receive data err"); DEBUG("receive data error ,clean up received data!!!\n"); /* 清标志 */ g_sysParam->revFlag = 0; /* 清空已接收的buf和文件 */ memset(fileBuf,0,BUF_SIZE); fb_idx = 0; char cmdBuf[256] = {0}; sprintf(cmdBuf,"rm -rf %s%s",REVROOT,g_sysParam->fileHead.name); if(system(cmdBuf) <0) { DEBUG("delete local buffer file fail.\n"); } } break; case 0: DEBUG("select time out..\n"); /* 添加接收超时判断 */ if(g_sysParam->revFlag == 1) { /* 上报错误 */ rptRevErr("receive data time out"); DEBUG("receive data time out ,clean up received data!!!\n"); /* 清标志 */ g_sysParam->revFlag = 0; /* 清空已接收的buf和文件 */ memset(fileBuf,0,BUF_SIZE); fb_idx = 0; char cmdBuf[256] = {0}; sprintf(cmdBuf,"rm -rf %s%s",REVROOT,g_sysParam->fileHead.name); if(system(cmdBuf) <0) { DEBUG("delete local buffer file fail.\n"); } } break; default: if(FD_ISSET(sock_fd,&fds) == 1) { FD_ZERO(&fds); DEBUG("UDP sock can be read.\n"); handlePack(sock_fd); } break; } } return retVal; }