int main(int argc, char*argv[]) { char*input_path = 0; char*symbol_name = 0; int opt; while ( (opt = getopt(argc, argv, "i:s:")) != -1 ) { switch (opt) { case 'i': input_path = optarg; break; case 's': symbol_name = optarg; break; default: fprintf(stderr, "Usage: %s -i <input path> -s <symbol name>\n", argv[0]); return EXIT_FAILURE; } } if (input_path == 0) { fprintf(stderr, "Input path required. Use -s <input path>\n"); return EXIT_FAILURE; } if (symbol_name == 0) { fprintf(stderr, "Symbol name required. Use 'i <symbol_name>\n"); return EXIT_FAILURE; } FILE*input_fd = fopen(input_path, "rb"); if (input_fd == 0) { fprintf(stderr, "Unable to open %s for read.\n", input_path); return EXIT_FAILURE; } printf("/* Auto-generated by bin2carray.c from %s. */\n", input_path); printf("unsigned char %s[] = {\n", symbol_name); unsigned char buf[1024]; size_t nbuf = fread(buf, 1, sizeof buf, input_fd); printf_bytes(nbuf, buf); while (nbuf == sizeof buf) { printf(",\n"); nbuf = fread(buf, 1, sizeof buf, input_fd); printf_bytes(nbuf, buf); } printf("\n};\n"); fclose(input_fd); return 0; }
// 消息线程处理:先接收网络协议的头,然后判断,如果头正确,则处理命令或继续接收命令参数后再处理 int TcpMsgRecvThread() { int ret = -1; char *recvBuf = NULL; NET_HEAD *netHead = NULL; char *pData = NULL; NET_HEAD keepalive; CLIENT_INFO *clientInfo = NULL; MSG_HEAD msgHead; int nRight = 0; fd_set fset; struct timeval to; pthread_detach(pthread_self()); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // 填充Keepalive数据 keepalive.nFlag = HDVS_FLAG; keepalive.nCommand = NETCMD_KEEP_ALIVE; keepalive.nErrorCode = 0; keepalive.nBufSize = 0; keepalive.nReserve = 0; // 分配发送recvBuf recvBuf = (char *)malloc(NETCMD_MAX_SIZE); // 100K if (recvBuf == NULL) { net_debug(); restartSystem(); return -1; } netHead = (NET_HEAD*)recvBuf; pData = recvBuf + sizeof(NET_HEAD); g_server_info.bServerExit = 0; while (!g_server_info.bServerExit) { pthread_mutex_lock(&g_server_info.msgThreadMutex); //printf("%s %d\n", __func__, __LINE__); // 等待用户登陆,加入命令线程池 while (g_server_info.msgWaitThreadNum == 0) { pthread_cond_wait(&g_server_info.msgThreadCond, &g_server_info.msgThreadMutex); if (g_server_info.bServerExit) { break; } } // 判断用户是否退出 if (g_server_info.bServerExit) { pthread_mutex_unlock(&g_server_info.msgThreadMutex); continue; } // 查找已登陆的用户,但还未加入线程池 clientInfo = FindWaitProccessClient(); if ((clientInfo == NULL)||(clientInfo->nflag == UDP_FLAG)) { pthread_mutex_unlock(&g_server_info.msgThreadMutex); printf("udp message quit!\n"); break; } if (clientInfo->hMsgSocket <= 0) { continue; } // 更新用户线程池计数 g_server_info.msgProcessThreadNum++; g_server_info.msgWaitThreadNum--; if (g_server_info.msgWaitThreadNum < 0) { g_server_info.msgWaitThreadNum = 0; } pthread_mutex_unlock(&g_server_info.msgThreadMutex); // FD_ZERO(&fset); memset(&to, 0, sizeof(to)); clientInfo->nKeepAlive = 0; clientInfo->status = RUN; nRight = clientInfo->level; while (STOP != clientInfo->status) { // 网络心跳 clientInfo->nKeepAlive ++; if (clientInfo->nKeepAlive >= 10) { net_debug(); printf("TCP_CMD_ERROR(%d): NOT KEEPALIVE!\n", clientInfo->hMsgSocket); break; } //接收网络协议的头 ret = 0; ret = TcpReceive(clientInfo->hMsgSocket, (char *)netHead, sizeof(NET_HEAD)); //ret = recv(clientInfo->hMsgSocket, (char *)netHead, sizeof(NET_HEAD), 0); if (ret < 0) { net_debug(); printf("TCP_CMD_ERROR(%d): RECEIVE ERROR(NET_HEAD)!\n", clientInfo->hMsgSocket); break; } if (ret == 0) { printf("TCP_CMD_ERROR(%d): RECEIVE TIMEOUT(NET_HEAD)!\n", clientInfo->hMsgSocket); continue; } // 判断网络帧头是否正确 if (netHead->nFlag != HDVS_FLAG) { printf("TCP_CMD_ERROR(%d): NET FLAG ERROR!\n", clientInfo->hMsgSocket); printf_bytes(recvBuf, 32); continue; } // 判断是否是心跳,如果是的话,则回复 if (netHead->nCommand == NETCMD_KEEP_ALIVE) { //printf("NETCMD_KEEP_ALIVE(%d): %d\n", clientInfo->hMsgSocket, netHead->nReserve); clientInfo->nKeepAlive = 0; ret = send(clientInfo->hMsgSocket, &keepalive, sizeof(NET_HEAD), 0); if (ret < 0) { printf("TCP_CMD_ERROR(%d): SEND KEEPALIVE ERROR!\n", clientInfo->hMsgSocket); net_debug(); } continue; } //printf("NetHead BufferSize: %d\n", netHead->nBufSize); // 接收网络命令参数 if (netHead->nBufSize>0 && netHead->nBufSize<NETCMD_MAX_SIZE-sizeof(NET_HEAD)) { ret = TcpReceive(clientInfo->hMsgSocket, pData, netHead->nBufSize); //ret = recv(clientInfo->hMsgSocket, pData, netHead->nBufSize, 0); if (ret < 0) { net_debug(); printf("TCP_CMD_ERROR(%d): RECEIVE ERROR(CONTEXT)!\n", clientInfo->hMsgSocket); break; } if (ret == 0) { net_debug(); printf("TCP_CMD_ERROR(%d): RECEIVE TIMEOUT(CONTEXT)!\n", clientInfo->hMsgSocket); continue; } } // 如果没有心跳,但有参数传过来,也可当心跳 clientInfo->nKeepAlive = 0; // 解析命令 msgHead.nSock = clientInfo->hMsgSocket; msgHead.nCmd = netHead->nCommand; msgHead.nRight = nRight; msgHead.nErrorCode = 0; msgHead.nBufSize = netHead->nBufSize; msgHead.nflag = TCP_FLAG; g_server_info.nupdate_flag = TCP_FLAG; printf("%s %d %x\n", __func__, __LINE__, netHead->nCommand); ParseCommand(&msgHead, pData); } clientInfo->status = STOP; // 释放命令线程池的节点 //printf("Cmd Exit(%d)!\n", clientInfo->hMsgSocket); StopUdpNode(clientInfo->hMsgSocket); StopTcpNode(clientInfo->hMsgSocket); pthread_cond_signal(&g_server_info.dataQuitThreadCond); WakeupStreamWait(); usleep(1); // 刷新线程池的计数 pthread_mutex_lock(&g_server_info.msgQuitThreadMutex); g_server_info.msgQuitThreadNum ++; g_server_info.msgProcessThreadNum --; if (g_server_info.msgProcessThreadNum < 0) { g_server_info.msgProcessThreadNum = 0; } pthread_mutex_unlock(&g_server_info.msgQuitThreadMutex); pthread_cond_signal(&g_server_info.msgQuitThreadCond); } g_server_info.nMsgThreadCount--; // 释放接收BUFFER if (recvBuf) { free(recvBuf); recvBuf = NULL; } return 0; }