コード例 #1
0
ファイル: util.c プロジェクト: athanos/Forgotten-Legacy
/////////////////////
// LOCAL FUNCTIONS //
/////////////////////
// pauses the mud for # of microseconds.....
// strangely enough.. windows _requires_ you have at least one fd_set
// with at least one socket in it, to use select.. linux does not
void mudsleep(long microseconds)
{
	static unsigned long sleep_socket = 0;
	fd_set fd_sleep;
	struct timeval wait_time;

	if( tick % (LOOPS_PER_SECOND*60*15) == 0 )
	{
		CREATURE *crit;
		OBJECT *obj;
		long players = 0, creatures = 0, objects = 0;

		for(crit = creature_list; crit; crit = crit->next)
		{
			if(IsPlayer(crit))	players++;
			else			creatures++;
		}
		for(obj = object_list; obj; obj = obj->next)
			objects++;

// not a true representation of a tick.. it gets called twice in a row
//		mudlog("Tick %d -- Players:%li  Creatures:%li  Objects:%li",
//			tick,
//			players, creatures, objects);
	}

	if( !sleep_socket ) // initialize
	{
		if( (sleep_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
		{
			mudlog("mudsleep: Error creating sleep_socket");
			mud_exit();
			return;
		}
	}

	if( microseconds < 0 )
		microseconds = 0;

	wait_time.tv_usec = microseconds;
	wait_time.tv_sec  = 0;
	FD_ZERO(&fd_sleep);
	FD_SET(sleep_socket,&fd_sleep);

	if(select(0, 0, 0, &fd_sleep, &wait_time) < 0)
	{
#ifdef WIN32
		mudlog("select: error #%d",WSAGetLastError());
#else
		perror("mudsleep: select");
#endif
//		mud_exit();
	}
}
コード例 #2
0
ファイル: main.c プロジェクト: borlak/acmud
/////////////////////
// LOCAL FUNCTIONS //
/////////////////////
// catches process signals and deals with them.. checks for
// infinite looping/hanging as well.
void signal_handler(int type)
{
	static struct timeval last_signal_time;
	struct timeval time;

	get_time(&time);

	switch(type)
	{
	case SIGINT:
	case SIGILL:
	case SIGFPE:
	case SIGSEGV:
#ifndef WIN32
	case SIGTRAP:
#endif
		mudlog("Mud received bad signal(%d), aborting...",type);
		mud_exit();
		abort();
		break;
#ifndef WIN32
	case SIGVTALRM:
		// check if the mud is looping
		// this type of signal only comes every 5 seconds of _process_ usage, not real
		// time.. so if the process is using the CPU constantly for 5 seconds, we
		// assume it's looping
		if( last_signal_time.tv_sec > 0 && (time.tv_sec-last_signal_time.tv_sec) < 10 )
		{
			mudlog("Mud in infinite loop, aborting!");
			abort();
		}
		break;
#endif
	}
	get_time(&last_signal_time);
}