示例#1
0
文件: main.c 项目: borlak/acmud
// MAIN - you can check out any time you like, but you can never leave
int main(int argc, char **argv)
{
	struct timeval time;
	time_t time_slept=0;
	time_t processing_time=0, total_processing_time=0;
	long hotreboot = 0;

	if( argc > 1 )
	{
		if( !is_number(argv[1]) )
		{
			mudlog("Invalid port number!");
			exit(1);
		}
		else if( (port = atoi(argv[1])) <= 1024 )
		{
			mudlog("Port must be above 1024!");
			exit(1);
		}
		if(argc > 2 && !strcmp(argv[2], "hotreboot"))
			hotreboot = 1;
	}

// test stuff
	{
	}


	get_time(&time);
	current_time = time.tv_sec;

	if(!hotreboot)
		create_host(port);
	init_mud(hotreboot);

	while(!mud_shutdown)
	{
		// we want the mud to loop no more than (LOOPS) times a second.. but not to loop
		// (LOOPS) times in 1/10th of a second and then wait for 9/10ths... try to make it even
		if( tick % LOOPS_PER_SECOND == 0 )
		{
			if( time_slept > 1000000 )
			{
				mudlog("time_slept is over a second (%li) and total processing_time this tick (%li)", 
					time_slept, total_processing_time);
//				exit(1);
			}
			else
				mudsleep(1000000 - time_slept);

			time_slept = 0;
			total_processing_time = 0;
		}

		get_time( &time );
		processing_time = time.tv_usec;

		// do all the mud stuff here....
		check_connections();
		mud_update();
		// end mud stuff

		get_time( &time );
		if(time.tv_usec > processing_time)
			processing_time = time.tv_usec - processing_time;
		else
			processing_time = processing_time - time.tv_usec;
		total_processing_time += processing_time;
		current_time = time.tv_sec;
		time_slept += (1000000 / LOOPS_PER_SECOND)-(processing_time);
		tick++;
		mudsleep((1000000 / LOOPS_PER_SECOND)-(processing_time));
	}

	close(host);
	free_mud();
	mudlog("Mud terminated normally.");
	return 1;
}
示例#2
0
int main( int argc, char **argv )
#endif
{
   struct timeval now_time;
   int temp = -1, temp2 = -1;
   bool fCopyOver = false;

#if !defined(WIN32)
   moron_check(  );  // Debatable weather or not this is true in WIN32 anyway :)
#endif

   DONT_UPPER = false;
   num_descriptors = 0;
   num_logins = 0;
   dlist.clear(  );
   mudstrlcpy( lastplayercmd, "No commands issued yet", MIL * 2 );

   // Init time.
   tzset(  );
   gettimeofday( &now_time, NULL );
   current_time = now_time.tv_sec;
   mudstrlcpy( str_boot_time, c_time( current_time, -1 ), MIL );  /* Records when the mud was last rebooted */

   new_pfile_time_t = current_time + 86400;
   mud_start_time = current_time;

   // Get the port number.
   mud_port = 9500;
   if( argc > 1 )
   {
      if( !is_number( argv[1] ) )
      {
         fprintf( stderr, "Usage: %s [port #]\n", argv[0] );
         exit( 1 );
      }
      else if( ( mud_port = atoi( argv[1] ) ) <= 1024 )
      {
         fprintf( stderr, "%s", "Port number must be above 1024.\n" );
         exit( 1 );
      }

      if( argv[2] && argv[2][0] )
      {
         fCopyOver = true;
         control = atoi( argv[3] );
#ifdef IMC
         temp2 = atoi( argv[4] );
#endif
      }
      else
         fCopyOver = false;
   }

#if defined(WIN32)
   {
      /*
       * Initialise Windows sockets library 
       */
      unsigned short wVersionRequested = MAKEWORD( 1, 1 );
      WSADATA wsadata;
      int err;

      /*
       * Need to include library: wsock32.lib for Windows Sockets 
       */
      err = WSAStartup( wVersionRequested, &wsadata );
      if( err )
      {
         fprintf( stderr, "Error %i on WSAStartup\n", err );
         exit( 1 );
      }

      /*
       * standard termination signals 
       */
      signal( SIGINT, bailout );
      signal( SIGTERM, bailout );
   }
#endif /* WIN32 */

   // Initialize all startup functions of the mud. 
   init_mud( fCopyOver, mud_port, temp, temp2 );

#if !defined(WIN32)
   /*
    * Set various signal traps, waiting until after completing all bootup operations
    * before doing so because crashes during bootup should not be intercepted. Samson 3-11-04
    */
   signal( SIGTERM, SigTerm );   /* Catch kill signals */
   signal( SIGPIPE, SIG_IGN );
   signal( SIGALRM, caught_alarm );
   signal( SIGUSR1, SigUser1 );  /* Catch user defined signals */
   signal( SIGUSR2, SigUser2 );
#endif
#ifdef MULTIPORT
   signal( SIGCHLD, SigChld );
#endif

   /*
    * If this setting is active, intercept SIGSEGV and keep the mud running.
    * Doing so sets a flag variable which if true will cause SegVio to abort()
    * If game_loop is restarted and makes it through once without crashing again,
    * then the flag is unset and SIGSEGV will continue to be intercepted. Samson 3-11-04
    */
   if( sysdata->crashhandler == true )
      set_chandler(  );

   log_string( "No people online yet. Suspending autonomous update handlers." );

   // Sick isn't it? The whole game being run inside of one little statement..... :P 
   game_loop(  );

   // Clean up the loose ends. 
   close_mud(  );

   // That's all, folks.
   log_string( "Normal termination of game." );
   log_string( "Cleaning up Memory.&d" );
   cleanup_memory(  );
   exit( 0 );
}