void run_udp_server(server_type* server) { if(server) { unsigned int cycle=0; while (1) { cycle++; run_events(server); } } }
int main(int argc, char** argv) { struct sigaction sig = {}; /* Ensuring that we are in a separate group and session if possible prevents * us from being killed by infantcidal hosts. */ setpgid(0,0); setsid(); unlink_at_exit = argv[1]; atexit(cleanup); event_queue = NULL; directories_traversed = hs_create(); hs_defunct_at(directories_traversed, 65536); directories_read = hs_create(); hs_defunct_at(directories_read, 65536); /* If a profile is set, read it if possible, then open for writing. */ profile_open(); /* Don't die on SIGIO or SIGALRM */ sig.sa_handler = signal_ignore; sigaction(SIGIO, &sig, NULL); sigaction(SIGALRM, &sig, NULL); /* Close stdin on SIGTERM, causing the program to exit normally. */ sig.sa_handler = close_stdin; sigaction(SIGTERM, &sig, NULL); /* Reconfigure stdin to be ASYNC and NONBLOCK */ if (-1 == fcntl(STDIN_FILENO, F_SETFL, O_ASYNC|O_NONBLOCK)) perror("fcntl(F_SETFL,O_ASYNC|O_NONBLOCK)"); if (-1 == fcntl(STDIN_FILENO, F_SETOWN, getpid())) perror("fcntl(F_SETOWN)"); /* Run until we exit due to an unexpected error or the input stream closes. */ while (1) { read_input(); run_events(); pause(); } return 0; }
void game_loop( void ) { struct timeval last_time; gettimeofday( &last_time, NULL ); current_time = last_time.tv_sec; // Main loop while( !mud_down ) { accept_new( control ); // Primative, yet effective infinite loop catcher. At least that's the idea. set_alarm( 30 ); alarm_section = "game_loop"; // If no descriptors are present, why bother processing input for them? if( dlist.size( ) > 0 ) process_input( ); #if !defined(__CYGWIN__) #ifdef MULTIPORT mud_recv_message( ); #endif #endif #ifdef IMC imc_loop( ); #endif // Autonomous game motion. Stops processing when there are no people at all online. if( dlist.size( ) > 0 ) update_handler( ); // Event handling. Will continue to process even with nobody around. Keeps areas fresh this way. run_events( current_time ); // If no descriptors are present, why bother processing output for them? if( dlist.size( ) > 0 ) process_output( ); /* * Synchronize to a clock. ( Would have moved this to its own function, but the code REALLY hated that plan.... ) * Sleep( last_time + 1/PULSE_PER_SECOND - now ). * Careful here of signed versus unsigned arithmetic. */ { struct timeval now_time; long secDelta; long usecDelta; gettimeofday( &now_time, NULL ); usecDelta = ( last_time.tv_usec ) - ( now_time.tv_usec ) + 1000000 / sysdata->pulsepersec; secDelta = ( last_time.tv_sec ) - ( now_time.tv_sec ); while( usecDelta < 0 ) { usecDelta += 1000000; secDelta -= 1; } while( usecDelta >= 1000000 ) { usecDelta -= 1000000; secDelta += 1; } if( secDelta > 0 || ( secDelta == 0 && usecDelta > 0 ) ) { struct timeval stall_time; stall_time.tv_usec = usecDelta; stall_time.tv_sec = secDelta; if( select( 0, NULL, NULL, NULL, &stall_time ) < 0 && errno != EINTR ) { perror( "game_loop: select: stall" ); exit( 1 ); } } } gettimeofday( &last_time, NULL ); current_time = last_time.tv_sec; // Dunno if it needs to be reset, but I'll do it anyway. End of the loop here. set_alarm( 0 ); /* * This will be the very last thing done here, because if you can't make it through * one lousy loop without crashing a second time..... */ sigsegv = false; } // End of main game loop // Returns back to 'main', and will result in mud shutdown }