int main() { //init win32 socket #ifdef __WIN32__ static WSADATA wsa_data; int result = WSAStartup((WORD)(1<<8|1), &wsa_data); //初始化WinSocket动态连接库 if( result != 0 ) // 初始化失败 return -1; #endif #ifndef __WIN32__ signal( SIGPIPE, SIG_IGN ); //ignore send,recv SIGPIPE error #endif NEW( websrv, sizeof(webserver) ); memset( websrv, 0, sizeof(webserver) ); server_create( websrv, "config.xml" ); server_start( websrv ); do_console(); server_stop( websrv ); server_end( websrv ); DEL( websrv ); #ifdef __WIN32__ WSACleanup(); #endif memory_end(); return 0; }
/* * This function is invoked when a SIGCLD is received */ void child_exit(void) { const char *func = "child_exit" ; for ( ;; ) /* Find all children that exited */ { int status ; pid_t pid ; struct server *serp ; #ifdef HAVE_WAITPID pid = waitpid( -1, &status, WNOHANG ) ; #else #if defined( sun ) && defined( lint ) pid = wait3( (union wait *)&status, WNOHANG, RUSAGE_NULL ) ; #else pid = wait3( &status, WNOHANG, RUSAGE_NULL ) ; #endif #endif if ( debug.on ) #ifdef HAVE_WAITPID msg( LOG_DEBUG, func, "waitpid returned = %d", pid ) ; #else msg( LOG_DEBUG, func, "wait3 returned = %d", pid ) ; #endif if ( pid == -1 ) { if ( errno == EINTR ) continue ; else break ; } if ( pid == 0 ) break ; if ( ( serp = server_lookup( pid ) ) != NULL ) { serp->svr_exit_status = status ; server_end( serp ) ; } else msg( LOG_NOTICE, func, "unknown child process %d %s", pid, PROC_STOPPED( status ) ? "stopped" : "died" ) ; } }
//服务器端执行入口函数 int main(int argc, char*argv[]){ struct sigaction new_action, old_action; message_data message_request; new_action.sa_handler=catch_signals; sigemptyset(&new_action.sa_mask); new_action.sa_flags=0; if ((sigaction(SIGINT, &new_action, &old_action) != 0) || (sigaction(SIGHUP, &new_action, &old_action) != 0) || (sigaction(SIGTERM, &new_action, &old_action) != 0)) { fprintf(stderr, "Server startup error, signal catching failed\n"); exit(EXIT_FAILURE); } char* server_host="localhost"; char* sql_username="******"; char* sql_password="******"; char* db_name="railwaysystem"; if(!database_init(server_host, sql_username, sql_password, db_name)){ fprintf(stderr, "Server error: could not init database. \n"); sys_log("数据库连接失败."); exit(EXIT_FAILURE); } sys_log("数据库连接成功."); if(!server_start()){ sys_log("服务器启动失败."); exit(EXIT_FAILURE); } sys_log("服务器启动成功."); while(server_state){ if(read_request_from_client(&message_request)){ exec_request(message_request); }else{ if(server_state){ fprintf(stderr, "Server ended, cannot read request from message queue. \n"); sys_log("Server ended, cannot read request from message queue."); } server_state=0; } } database_close(); server_end(); sys_log("服务器关闭."); exit(EXIT_SUCCESS); }
// Receives from another IP server and sends reply back void* IPTask(void* _IP_data_task) { struct IP_data* IP_data_task = (struct IP_data*) _IP_data_task; struct Context* context = (struct Context*) IP_data_task->ptr_context; unsigned char VPS = IP_data_task->VPS, *dataPtr, msgFlag; char printbuffer[DATASIZE]; /////////////////////////////////////// // task_id needs to be made generic! // //name_id[SENDERNAMESIZE] = {IPTask1_id,UNUSED,UNUSED}; /////////////////////////////////////// unsigned char name_id[SENDERNAMESIZE] = { context->task_id, UNUSED,UNUSED }; int server_port = IP_data_task->port; printf("IPTask%i: Started\n", VPS); // Initialize socket and port int s, client; unsigned int tmp; fd_set fdset; struct timeval timer; struct sockaddr_in c_addr; //unsigned char *pointer; if ((s = server_init(server_port)) < 0) //return(s); FD_ZERO(&fdset); FD_SET(s, &fdset); timer.tv_sec = 30; timer.tv_usec = 0; pthread_mutex_lock(context->mutex); while (communication_needed) { //Wait for IP packet if (select(s + 1, &fdset, NULL, NULL, NULL)> 0 ) { //#ifdef _DEBUG_VERBOSE printf("IPTask%i: SELECT has continued\n",VPS); //#endif tmp = sizeof(c_addr); client = accept(s, (struct sockaddr *)&c_addr, &tmp); if (client < 0) { printf("FAIL1: %s\n", strerror(errno)); } if (PRINT_STATUS) { printf("IPTask%i: client connect from %s\n",VPS, inet_ntoa(c_addr.sin_addr)); } usleep(10000); if ( (dataPtr = receive_cmd(client, VPS)) != NULL ) { //#ifdef _DEBUG_VERBOSE printf("IPTask%i: Got [%s] from receive_cmd (ASCII)\n", VPS,binary_to_hexbinaryString(dataPtr,dataPtr[1]+2,printbuffer)); //#endif dataPtr[1] = strlen((char*)dataPtr+2); // TODO remove warning // Convert hexbinary to binary if first char is a hexbin char else skip conv if( ((0x30 <= dataPtr[2]) && (dataPtr[2] <= 0x39)) || (('a' <= dataPtr[2] ) && (dataPtr[2] <= 'f')) || (('A' <= dataPtr[2]) && (dataPtr[2] <= 'F'))) { dataPtr[1] = hexbinary_to_binary( &dataPtr[2], &dataPtr[2]);} printf("IPTask%i: Receiving from port %i: [",VPS, server_port); printf("%s]\n", binary_to_hexbinaryString(dataPtr,dataPtr[1]+2,printbuffer)); //printf("IPTask%i: Has data after strcat: [%s]\n",VPS,binary_to_hexbinaryString(dataPtr,dataPtr[1]+2,printbuffer)); // Pass message on //#ifdef _DEBUG_VERBOSE printf("broadcast: IPTask%i\n",VPS); //#endif broadcast_msg(name_id, Out1, dataPtr); // DEBUG: Must wait for receiver to pass data on //sleep(1.5); // Begin listening on Condition for reply //#ifdef _DEBUG_VERBOSE printf("IPTask%i: calling receive()\n",VPS); //#endif //msgFlag = receive(IPTask1_id, context->condition, context->mutex); msgFlag = receive(VPS, context->condition, context->mutex); dataPtr = context->holder; printf(" *** IPTask%i: Sending on port %i: [%s] ***\n", VPS, server_port, binary_to_hexbinaryString(dataPtr,dataPtr[1]+2,printbuffer)); //#ifdef _DEBUG_VERBOSE printf("IPTask%i: received from sender: [%s]\n", VPS, binary_to_hexbinaryString(context->sender,SENDERNAMESIZE,printbuffer)); //#endif // Send reply back thru open connection // dataPtr+2 to avoid sending the VPS and length //if(server_port==MANAGEMENTPORT) IP_send_server_String(client, dataPtr+2, dataPtr[1]); //else IP_send_server_hexbinaryString(client, dataPtr+2, dataPtr[1]); IP_send_server_hexbinaryString(client, dataPtr+2, dataPtr[1]); printf("IPTask%i: ip send server func: \n", VPS); } else printf("IPTask%i: no data in client \n", VPS); // Shutdown connection if ((shutdown(client, 0) < 0) || (close(client) < 0)) { printf("FAIL2: %s\n", strerror(errno)); } } // end select } // while-end // Shutdown listening server if (s> 0) server_end(s); printf("IPTask%i: unlocking mutex and stopping\n",VPS); pthread_mutex_unlock(context->mutex); return NULL; }