示例#1
0
文件: net.cpp 项目: aceoyame/OpenEQC
int main(int argc, char** argv) {
#ifdef _DEBUG
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//	_crtBreakAlloc = 2025;
#endif
	srand(time(NULL));
  
    for (int logs = 0; logs < EQEMuLog::MaxLogID; logs++) {
      LogFile->write((EQEMuLog::LogIDs)logs, "CURRENT_ZONE_VERSION: %s", CURRENT_ZONE_VERSION);
    }

	if (argc != 5)
	{
		cerr << "Usage: zone zone_name address port worldaddress" << endl;
		exit(0);
	}
	char* filename = argv[0];
	char* zone_name = argv[1];
	char* address = argv[2];
	int32 port = atoi(argv[3]);
	char* worldaddress = argv[4];
	
	if (strlen(address) <= 0) {
		cerr << "Invalid address" << endl;

		exit(0);
	}
	if (port <= 0) {
		cerr << "Bad port specified" << endl;
		exit(0);
	}
	if (strlen(worldaddress) <= 0) {
		cerr << "Invalid worldaddress" << endl;
		exit(0);
	}
	if (signal(SIGINT, CatchSignal) == SIG_ERR) {
		cerr << "Could not set signal handler" << endl;
		return 0;
	}
#ifndef WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
		cerr << "Could not set signal handler" << endl;
		return 0;
	}
#endif
	net.SaveInfo(address, port, worldaddress,filename);
	
	LogFile->write(EQEMuLog::Status, "Loading Variables");
	database.LoadVariables();
	LogFile->write(EQEMuLog::Status, "Loading zone names");
	database.LoadZoneNames();
	LogFile->write(EQEMuLog::Status, "Loading items");
	if (!database.LoadItems()) {
		LogFile->write(EQEMuLog::Error, "Loading items FAILED!");
		cout << "Failed.  But ignoring error and going on..." << endl;
	}
	LogFile->write(EQEMuLog::Status, "Loading npcs");
	if (!database.LoadNPCTypes()) {
		LogFile->write(EQEMuLog::Error, "Loading npcs FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
	}
#ifdef SHAREMEM
	LogFile->write(EQEMuLog::Status, "Loading npc faction lists");
	if (!database.LoadNPCFactionLists()) {
		LogFile->write(EQEMuLog::Error, "Loading npcs faction lists FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
	}
#endif
	LogFile->write(EQEMuLog::Status, "Loading loot tables");
	if (!database.LoadLoot()) {
		LogFile->write(EQEMuLog::Error, "Loading loot FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
	}
#ifdef SHAREMEM
	LogFile->write(EQEMuLog::Status, "Loading doors");
	database.LoadDoors();
#endif
	LoadSPDat();

#ifdef GUILDWARS
	LogFile->write(EQEMuLog::Status, "Loading guild alliances");
	//database.LoadGuildAlliances();
#endif

	// New Load function.  keeping it commented till I figure out why its not working correctly in linux. Trump.
	// NewLoadSPDat();
	LogFile->write(EQEMuLog::Status, "Loading guilds");
	database.LoadGuilds(guilds);
	LogFile->write(EQEMuLog::Status, "Loading factions");
	database.LoadFactionData();
	LogFile->write(EQEMuLog::Status, "Loading corpse timers");
	database.GetDecayTimes(npcCorpseDecayTimes);
	LogFile->write(EQEMuLog::Status, "Loading what ever is left");
	database.ExtraOptions();
	AutoDelete<Parser> ADparse(&parse, new Parser);
#ifdef ADDONCMD	
	LogFile->write(EQEMuLog::Status, "Looding addon commands from dll");
	if ( !addonCmd.openLib() ) {
		LogFile->write(EQEMuLog::Error, "Loading addons failed =(");
	}
#endif	
	if (!worldserver.Connect()) {
		LogFile->write(EQEMuLog::Error, "worldserver.Connect() FAILED!");
	}
	
	if (strcmp(zone_name, ".") == 0 || strcasecmp(zone_name, "sleep") == 0) {
		LogFile->write(EQEMuLog::Status, "Entering sleep mode");
	} else if (!Zone::Bootup(database.GetZoneID(zone_name), true)) {
		LogFile->write(EQEMuLog::Error, "Zone bootup FAILED!");
		zone = 0;
	}
	
	Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
	UpdateWindowTitle();
	bool worldwasconnected = worldserver.Connected();
	EQNetworkConnection* eqnc;
	while(RunLoops) {
		Timer::SetCurrentTime();
		while ((eqnc = eqns.NewQueuePop())) {
			struct in_addr	in;
			in.s_addr = eqnc->GetrIP();
			LogFile->write(EQEMuLog::Status, "%i New client from ip:%s port:%i", Timer::GetCurrentTime(), inet_ntoa(in), ntohs(eqnc->GetrPort()));
			Client* client = new Client(eqnc);
			entity_list.AddClient(client);
		}
#ifdef CATCH_CRASH
		try{
#endif
			worldserver.Process();
#ifdef CATCH_CRASH
		}
		catch(...){
			error = 1;
			adverrornum = worldserver.GetErrorNumber();
			worldserver.Disconnect();
			worldwasconnected = false;
		}
#endif			
		if (worldserver.Connected()) {
			worldwasconnected = true;
		}
		else {
			if (worldwasconnected && ZoneLoaded)
				entity_list.ChannelMessageFromWorld(0, 0, 6, 0, 0, "WARNING: World server connection lost");
			worldwasconnected = false;
		}
		if (ZoneLoaded) {
			{	
#ifdef CATCH_CRASH
				try{
#endif
					entity_list.Process();
#ifdef CATCH_CRASH
				}
				catch(...){
					error = 4;
				}
				try{
#endif
					zoneprocess= zone->Process();
					if (!zoneprocess) {
						Zone::Shutdown();
					}
#ifdef CATCH_CRASH
				}
				catch(...){
					error = 2;
				}
#endif
			}
		}
		DBAsyncWork* dbaw = 0;
		while ((dbaw = MTdbafq->Pop())) {
			DispatchFinishedDBAsync(dbaw);
		}
		if (InterserverTimer.Check()
#ifdef CATCH_CRASH
			&& !error
#endif
			) {
#ifdef CATCH_CRASH
			try{
#endif
				InterserverTimer.Start();
				database.ping();
				AsyncLoadVariables();
//				NPC::GetAILevel(true);
				entity_list.UpdateWho();
				if (worldserver.TryReconnect() && (!worldserver.Connected()))
					worldserver.AsyncConnect();
#ifdef CATCH_CRASH
			}
			catch(...)
			{
				error = 16;
				RunLoops = false;
			}
#endif
		}
#ifdef CATCH_CRASH
		if (error){
			RunLoops = false;
		}
#endif
#if defined(_DEBUG) && defined(DEBUG_PC)
QueryPerformanceCounter(&tmp3);
mainloop_time += tmp3.QuadPart - tmp2.QuadPart;
if (!--tmp0) {
	tmp0 = 200;
	printf("Elapsed Tics  : %9.0f (%1.4f sec)\n", (double)mainloop_time, ((double)mainloop_time/tmp.QuadPart));
	printf("NPCAI Tics    : %9.0f (%1.2f%%)\n", (double)npcai_time, ((double)npcai_time/mainloop_time)*100);
	printf("FindSpell Tics: %9.0f (%1.2f%%)\n", (double)findspell_time, ((double)findspell_time/mainloop_time)*100);
	printf("AtkAllowd Tics: %9.0f (%1.2f%%)\n", (double)IsAttackAllowed_time, ((double)IsAttackAllowed_time/mainloop_time)*100);
	printf("ClientPro Tics: %9.0f (%1.2f%%)\n", (double)clientprocess_time, ((double)clientprocess_time/mainloop_time)*100);
	printf("ClientAtk Tics: %9.0f (%1.2f%%)\n", (double)clientattack_time, ((double)clientattack_time/mainloop_time)*100);
mainloop_time = 0;
npcai_time = 0;
findspell_time = 0;
IsAttackAllowed_time = 0;
clientprocess_time = 0;
clientattack_time = 0;
}
#endif
		Sleep(1);
	}
	
#ifdef CATCH_CRASH
	if (error)
		FilePrint("eqemudebug.log",true,true,"Zone %i crashed. Errorcode: %i/%i. Current zone loaded:%s. Current clients:%i. Caused by: %s",net.GetZonePort(), error,adverrornum, zone->GetShortName(), numclients,errorname);
	try{
		entity_list.Message(0, 15, "ZONEWIDE_MESSAGE: This zone caused a fatal error and will shut down now. Your character will be restored to the last saved status. We are sorry for any inconvenience!");
	}
	catch(...){}
	if (error){
#ifdef WIN32		
		ExitProcess(error);
#else	
		entity_list.Clear();
		safe_delete(zone);
#endif
	}
#endif

	entity_list.Clear();
	if (zone != 0
#ifdef CATCH_CRASH
		& !error
#endif
		)
		Zone::Shutdown(true);
	//Fix for Linux world server problem.
	eqns.Close();
	worldserver.Disconnect();
	dbasync->CommitWrites();
	dbasync->StopThread();
#ifdef NEW_LoadSPDat
	safe_delete(spells_delete);
#endif

	CheckEQEMuErrorAndPause();
	return 0;
}