示例#1
0
int main(int argc, char** argv) {
    RegisterExecutablePlatform(ExePlatformZone);
    Log.LoadLogSettingsDefaults();

    set_exception_handler();
    QServ = new QueryServ;

    Log.Out(Logs::General, Logs::Zone_Server, "Loading server configuration..");
    if(!ZoneConfig::LoadConfig()) {
        Log.Out(Logs::General, Logs::Error, "Loading server configuration failed.");
        return 1;
    }
    Config = ZoneConfig::get();

    const char *zone_name;
    uint32 instance_id = 0;
    std::string z_name;
    if(argc == 4) {
        instance_id = atoi(argv[3]);
        worldserver.SetLauncherName(argv[2]);
        auto zone_port = SplitString(argv[1], ':');

        if(!zone_port.empty()) {
            z_name = zone_port[0];
        }

        if(zone_port.size() > 1) {
            std::string p_name = zone_port[1];
            Config->SetZonePort(atoi(p_name.c_str()));
        }

        worldserver.SetLaunchedName(z_name.c_str());
        if(strncmp(z_name.c_str(), "dynamic_", 8) == 0) {
            zone_name = ".";
        }
        else {
            zone_name = z_name.c_str();
        }
    } else if(argc == 3) {
        worldserver.SetLauncherName(argv[2]);
        auto zone_port = SplitString(argv[1], ':');

        if(!zone_port.empty()) {
            z_name = zone_port[0];
        }

        if(zone_port.size() > 1) {
            std::string p_name = zone_port[1];
            Config->SetZonePort(atoi(p_name.c_str()));
        }

        worldserver.SetLaunchedName(z_name.c_str());
        if(strncmp(z_name.c_str(), "dynamic_", 8) == 0) {
            zone_name = ".";
        } else {
            zone_name = z_name.c_str();
        }
    } else if (argc == 2) {
        worldserver.SetLauncherName("NONE");
        auto zone_port = SplitString(argv[1], ':');

        if(!zone_port.empty()) {
            z_name = zone_port[0];
        }

        if(zone_port.size() > 1) {
            std::string p_name = zone_port[1];
            Config->SetZonePort(atoi(p_name.c_str()));
        }

        worldserver.SetLaunchedName(z_name.c_str());
        if(strncmp(z_name.c_str(), "dynamic_", 8) == 0) {
            zone_name = ".";
        }
        else {
            zone_name = z_name.c_str();
        }
    } else {
        zone_name = ".";
        worldserver.SetLaunchedName(".");
        worldserver.SetLauncherName("NONE");
    }

    worldserver.SetPassword(Config->SharedKey.c_str());

    Log.Out(Logs::General, Logs::Zone_Server, "Connecting to MySQL...");
    if (!database.Connect(
                Config->DatabaseHost.c_str(),
                Config->DatabaseUsername.c_str(),
                Config->DatabasePassword.c_str(),
                Config->DatabaseDB.c_str(),
                Config->DatabasePort)) {
        Log.Out(Logs::General, Logs::Error, "Cannot continue without a database connection.");
        return 1;
    }

#ifdef BOTS
    if (!botdb.Connect(
                Config->DatabaseHost.c_str(),
                Config->DatabaseUsername.c_str(),
                Config->DatabasePassword.c_str(),
                Config->DatabaseDB.c_str(),
                Config->DatabasePort)) {
        Log.Out(Logs::General, Logs::Error, "Cannot continue without a bots database connection.");
        return 1;
    }
#endif

    /* Register Log System and Settings */
    Log.OnLogHookCallBackZone(&Zone::GMSayHookCallBackProcess);
    database.LoadLogSettings(Log.log_settings);
    Log.StartFileLogs();

    /* Guilds */
    guild_mgr.SetDatabase(&database);
    GuildBanks = nullptr;

#ifdef _EQDEBUG
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif

    Log.Out(Logs::General, Logs::Zone_Server, "CURRENT_VERSION: %s", CURRENT_VERSION);

    /*
    * Setup nice signal handlers
    */
    if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
        Log.Out(Logs::General, Logs::Error, "Could not set signal handler");
        return 1;
    }
    if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
        Log.Out(Logs::General, Logs::Error, "Could not set signal handler");
        return 1;
    }
#ifndef WIN32
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)	{
        Log.Out(Logs::General, Logs::Error, "Could not set signal handler");
        return 1;
    }
#endif

    Log.Out(Logs::General, Logs::Zone_Server, "Mapping Incoming Opcodes");
    MapOpcodes();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading Variables");
    database.LoadVariables();

    std::string hotfix_name;
    if(database.GetVariable("hotfix_name", hotfix_name)) {
        if(!hotfix_name.empty()) {
            Log.Out(Logs::General, Logs::Zone_Server, "Current hotfix in use: '%s'", hotfix_name.c_str());
        }
    }

    Log.Out(Logs::General, Logs::Zone_Server, "Loading zone names");
    database.LoadZoneNames();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading items");
    if(!database.LoadItems(hotfix_name)) {
        Log.Out(Logs::General, Logs::Error, "Loading items FAILED!");
        Log.Out(Logs::General, Logs::Error, "Failed. But ignoring error and going on...");
    }

    Log.Out(Logs::General, Logs::Zone_Server, "Loading npc faction lists");
    if(!database.LoadNPCFactionLists(hotfix_name)) {
        Log.Out(Logs::General, Logs::Error, "Loading npcs faction lists FAILED!");
        return 1;
    }
    Log.Out(Logs::General, Logs::Zone_Server, "Loading loot tables");
    if(!database.LoadLoot(hotfix_name)) {
        Log.Out(Logs::General, Logs::Error, "Loading loot FAILED!");
        return 1;
    }
    Log.Out(Logs::General, Logs::Zone_Server, "Loading skill caps");
    if(!database.LoadSkillCaps(std::string(hotfix_name))) {
        Log.Out(Logs::General, Logs::Error, "Loading skill caps FAILED!");
        return 1;
    }

    Log.Out(Logs::General, Logs::Zone_Server, "Loading spells");
    if(!database.LoadSpells(hotfix_name, &SPDAT_RECORDS, &spells)) {
        Log.Out(Logs::General, Logs::Error, "Loading spells FAILED!");
        return 1;
    }

    Log.Out(Logs::General, Logs::Zone_Server, "Loading base data");
    if(!database.LoadBaseData(hotfix_name)) {
        Log.Out(Logs::General, Logs::Error, "Loading base data FAILED!");
        return 1;
    }

    Log.Out(Logs::General, Logs::Zone_Server, "Loading guilds");
    guild_mgr.LoadGuilds();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading factions");
    database.LoadFactionData();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading titles");
    title_manager.LoadTitles();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading tributes");
    database.LoadTributes();

    Log.Out(Logs::General, Logs::Zone_Server, "Loading corpse timers");
    database.GetDecayTimes(npcCorpseDecayTimes);

    Log.Out(Logs::General, Logs::Zone_Server, "Loading commands");
    int retval=command_init();
    if(retval<0)
        Log.Out(Logs::General, Logs::Error, "Command loading FAILED");
    else
        Log.Out(Logs::General, Logs::Zone_Server, "%d commands loaded", retval);

    //rules:
    {
        std::string tmp;
        if (database.GetVariable("RuleSet", tmp)) {
            Log.Out(Logs::General, Logs::Zone_Server, "Loading rule set '%s'", tmp.c_str());
            if(!RuleManager::Instance()->LoadRules(&database, tmp.c_str())) {
                Log.Out(Logs::General, Logs::Error, "Failed to load ruleset '%s', falling back to defaults.", tmp.c_str());
            }
        } else {
            if(!RuleManager::Instance()->LoadRules(&database, "default")) {
                Log.Out(Logs::General, Logs::Zone_Server, "No rule set configured, using default rules");
            } else {
                Log.Out(Logs::General, Logs::Zone_Server, "Loaded default rule set 'default'", tmp.c_str());
            }
        }
    }

#ifdef BOTS
    Log.Out(Logs::General, Logs::Zone_Server, "Loading bot commands");
    int botretval = bot_command_init();
    if (botretval<0)
        Log.Out(Logs::General, Logs::Error, "Bot command loading FAILED");
    else
        Log.Out(Logs::General, Logs::Zone_Server, "%d bot commands loaded", botretval);
#endif

    if(RuleB(TaskSystem, EnableTaskSystem)) {
        Log.Out(Logs::General, Logs::Tasks, "[INIT] Loading Tasks");
        taskmanager = new TaskManager;
        taskmanager->LoadTasks();
    }

    parse = new QuestParserCollection();
#ifdef LUA_EQEMU
    auto lua_parser = new LuaParser();
    parse->RegisterQuestInterface(lua_parser, "lua");
#endif

#ifdef EMBPERL
    auto perl_parser = new PerlembParser();
    parse->RegisterQuestInterface(perl_parser, "pl");

    /* Load Perl Event Export Settings */
    parse->LoadPerlEventExportSettings(parse->perl_event_export_settings);

#endif

    //now we have our parser, load the quests
    Log.Out(Logs::General, Logs::Zone_Server, "Loading quests");
    parse->ReloadQuests();

    if (!worldserver.Connect()) {
        Log.Out(Logs::General, Logs::Error, "Worldserver Connection Failed :: worldserver.Connect()");
    }

    Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
#ifdef EQPROFILE
#ifdef PROFILE_DUMP_TIME
    Timer profile_dump_timer(PROFILE_DUMP_TIME*1000);
    profile_dump_timer.Start();
#endif
#endif
    if (!strlen(zone_name) || !strcmp(zone_name,".")) {
        Log.Out(Logs::General, Logs::Zone_Server, "Entering sleep mode");
    } else if (!Zone::Bootup(database.GetZoneID(zone_name), instance_id, true)) {
        Log.Out(Logs::General, Logs::Error, "Zone Bootup failed :: Zone::Bootup");
        zone = 0;
    }

    //register all the patches we have avaliable with the stream identifier.
    EQStreamIdentifier stream_identifier;
    RegisterAllPatches(stream_identifier);

#ifndef WIN32
    Log.Out(Logs::Detail, Logs::None,  "Main thread running with thread id %d", pthread_self());
#endif

    Timer quest_timers(100);
    UpdateWindowTitle();
    bool worldwasconnected = worldserver.Connected();
    std::shared_ptr<EQStream> eqss;
    EQStreamInterface *eqsi;
    uint8 IDLEZONEUPDATE = 200;
    uint8 ZONEUPDATE = 10;
    Timer zoneupdate_timer(ZONEUPDATE);
    zoneupdate_timer.Start();
    while(RunLoops) {
        {   //profiler block to omit the sleep from times

            //Advance the timer to our current point in time
            Timer::SetCurrentTime();

            worldserver.Process();

            if (!eqsf.IsOpen() && Config->ZonePort != 0) {
                Log.Out(Logs::General, Logs::Zone_Server, "Starting EQ Network server on port %d", Config->ZonePort);
                if (!eqsf.Open(Config->ZonePort)) {
                    Log.Out(Logs::General, Logs::Error, "Failed to open port %d", Config->ZonePort);
                    ZoneConfig::SetZonePort(0);
                    worldserver.Disconnect();
                    worldwasconnected = false;
                }
            }

            //check the factory for any new incoming streams.
            while ((eqss = eqsf.Pop())) {
                //pull the stream out of the factory and give it to the stream identifier
                //which will figure out what patch they are running, and set up the dynamic
                //structures and opcodes for that patch.
                struct in_addr	in;
                in.s_addr = eqss->GetRemoteIP();
                Log.Out(Logs::Detail, Logs::World_Server, "New connection from %s:%d", inet_ntoa(in), ntohs(eqss->GetRemotePort()));
                stream_identifier.AddStream(eqss);	//takes the stream
            }

            //give the stream identifier a chance to do its work....
            stream_identifier.Process();

            //check the stream identifier for any now-identified streams
            while((eqsi = stream_identifier.PopIdentified())) {
                //now that we know what patch they are running, start up their client object
                struct in_addr	in;
                in.s_addr = eqsi->GetRemoteIP();
                Log.Out(Logs::Detail, Logs::World_Server, "New client from %s:%d", inet_ntoa(in), ntohs(eqsi->GetRemotePort()));
                auto client = new Client(eqsi);
                entity_list.AddClient(client);
            }

            if ( numclients < 1 && zoneupdate_timer.GetDuration() != IDLEZONEUPDATE )
                zoneupdate_timer.SetTimer(IDLEZONEUPDATE);
            else if ( numclients > 0 && zoneupdate_timer.GetDuration() == IDLEZONEUPDATE )
            {
                zoneupdate_timer.SetTimer(ZONEUPDATE);
                zoneupdate_timer.Trigger();
            }

            //check for timeouts in other threads
            timeout_manager.CheckTimeouts();

            if (worldserver.Connected()) {
                worldwasconnected = true;
            }
            else {
                if (worldwasconnected && is_zone_loaded)
                    entity_list.ChannelMessageFromWorld(0, 0, 6, 0, 0, "WARNING: World server connection lost");
                worldwasconnected = false;
            }

            if (is_zone_loaded && zoneupdate_timer.Check()) {
                {
                    if(net.group_timer.Enabled() && net.group_timer.Check())
                        entity_list.GroupProcess();

                    if(net.door_timer.Enabled() && net.door_timer.Check())
                        entity_list.DoorProcess();

                    if(net.object_timer.Enabled() && net.object_timer.Check())
                        entity_list.ObjectProcess();

                    if(net.corpse_timer.Enabled() && net.corpse_timer.Check())
                        entity_list.CorpseProcess();

                    if(net.trap_timer.Enabled() && net.trap_timer.Check())
                        entity_list.TrapProcess();

                    if(net.raid_timer.Enabled() && net.raid_timer.Check())
                        entity_list.RaidProcess();

                    entity_list.Process();
                    entity_list.MobProcess();
                    entity_list.BeaconProcess();
                    entity_list.EncounterProcess();

                    if (zone) {
                        if(!zone->Process()) {
                            Zone::Shutdown();
                        }
                    }

                    if(quest_timers.Check())
                        quest_manager.Process();

                }
            }
            if (InterserverTimer.Check()) {
                InterserverTimer.Start();
                database.ping();
                // AsyncLoadVariables(dbasync, &database);
                entity_list.UpdateWho();
                if (worldserver.TryReconnect() && (!worldserver.Connected()))
                    worldserver.AsyncConnect();
            }

#ifdef EQPROFILE
#ifdef PROFILE_DUMP_TIME
            if(profile_dump_timer.Check()) {
                DumpZoneProfile();
            }
#endif
#endif
        }	//end extra profiler block
        Sleep(ZoneTimerResolution);
    }

    entity_list.Clear();

    parse->ClearInterfaces();

#ifdef EMBPERL
    safe_delete(perl_parser);
#endif

#ifdef LUA_EQEMU
    safe_delete(lua_parser);
#endif

    safe_delete(Config);

    if (zone != 0)
        Zone::Shutdown(true);
    //Fix for Linux world server problem.
    eqsf.Close();
    worldserver.Disconnect();
    safe_delete(taskmanager);
    command_deinit();
#ifdef BOTS
    bot_command_deinit();
#endif
    safe_delete(parse);
    Log.Out(Logs::General, Logs::Zone_Server, "Proper zone shutdown complete.");
    Log.CloseFileLogs();
    return 0;
}
示例#2
0
文件: net.cpp 项目: fizzgig16/Server
int main(int argc, char** argv) {
    RegisterExecutablePlatform(ExePlatformWorld);
    set_exception_handler();

	// Load server configuration
	_log(WORLD__INIT, "Loading server configuration..");
	if (!WorldConfig::LoadConfig()) {
		_log(WORLD__INIT_ERR, "Loading server configuration failed.");
		return(1);
	}
	const WorldConfig *Config=WorldConfig::get();

	if(!load_log_settings(Config->LogSettingsFile.c_str()))
		_log(WORLD__INIT, "Warning: Unable to read %s", Config->LogSettingsFile.c_str());
	else
		_log(WORLD__INIT, "Log settings loaded from %s", Config->LogSettingsFile.c_str());


	_log(WORLD__INIT, "CURRENT_VERSION: %s", CURRENT_VERSION);
	
	#ifdef _DEBUG
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	#endif
	
	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#ifndef WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#endif

	// add login server config to list
	if (Config->LoginCount == 0) {
		if (Config->LoginHost.length()) {
			loginserverlist.Add(Config->LoginHost.c_str(), Config->LoginPort, Config->LoginAccount.c_str(), Config->LoginPassword.c_str());
			_log(WORLD__INIT, "Added loginserver %s:%i", Config->LoginHost.c_str(), Config->LoginPort);
		}
	} else {
		LinkedList<LoginConfig*> loginlist=Config->loginlist;
		LinkedListIterator<LoginConfig*> iterator(loginlist);
		iterator.Reset();
		while(iterator.MoreElements()) {
			loginserverlist.Add(iterator.GetData()->LoginHost.c_str(), iterator.GetData()->LoginPort, iterator.GetData()->LoginAccount.c_str(), iterator.GetData()->LoginPassword.c_str());
			_log(WORLD__INIT, "Added loginserver %s:%i", iterator.GetData()->LoginHost.c_str(), iterator.GetData()->LoginPort);
			iterator.Advance();
		}
	}
	
	_log(WORLD__INIT, "Connecting to MySQL...");
	if (!database.Connect(
		Config->DatabaseHost.c_str(),
		Config->DatabaseUsername.c_str(),
		Config->DatabasePassword.c_str(),
		Config->DatabaseDB.c_str(),
		Config->DatabasePort)) {
		_log(WORLD__INIT_ERR, "Cannot continue without a database connection.");
		return(1);
	}
	dbasync = new DBAsync(&database);
	guild_mgr.SetDatabase(&database);
	
	if (argc >= 2) {
		char tmp[2];
		if (strcasecmp(argv[1], "help") == 0 || strcasecmp(argv[1], "?") == 0 || strcasecmp(argv[1], "/?") == 0 || strcasecmp(argv[1], "-?") == 0 || strcasecmp(argv[1], "-h") == 0 || strcasecmp(argv[1], "-help") == 0) {
			cout << "Worldserver command line commands:" << endl;
			cout << "adduser username password flag    - adds a user account" << endl;
			cout << "flag username flag    - sets GM flag on the account" << endl;
			cout << "startzone zoneshortname    - sets the starting zone" << endl;
			cout << "-holdzones    - reboots lost zones" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "-holdzones") == 0) {
			cout << "Reboot Zones mode ON" << endl;
			holdzones = true;
		}
		else if (database.GetVariable("disablecommandline", tmp, 2)) {
			if (strlen(tmp) == 1) {
				if (tmp[0] == '1') {
					cout << "Command line disabled in database... exiting" << endl;
					return 0;
				}
			}
		}
		else if (strcasecmp(argv[1], "adduser") == 0) {
			if (argc == 5) {
				if (Seperator::IsNumber(argv[4])) {
					if (atoi(argv[4]) >= 0 && atoi(argv[4]) <= 255) {
						if (database.CreateAccount(argv[2], argv[3], atoi(argv[4])) == 0)
							cout << "database.CreateAccount failed." << endl;
						else
							cout << "Account created: Username='******', Password='******', status=" << argv[4] << endl;
						return 0;
					}
				}
			}
			cout << "Usage: world adduser username password flag" << endl;
			cout << "flag = 0, 1 or 2" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "flag") == 0) {
			if (argc == 4) {
				if (Seperator::IsNumber(argv[3])) {

					if (atoi(argv[3]) >= 0 && atoi(argv[3]) <= 255) {
						if (database.SetAccountStatus(argv[2], atoi(argv[3])))
							cout << "Account flagged: Username='******', status=" << argv[3] << endl;
						else
							cout << "database.SetAccountStatus failed." << endl;
						return 0;
					}
				}
			}
			cout << "Usage: world flag username flag" << endl;
			cout << "flag = 0-200" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "startzone") == 0) {
			if (argc == 3) {
				if (strlen(argv[2]) < 3) {
					cout << "Error: zone name too short" << endl;
				}
				else if (strlen(argv[2]) > 15) {
					cout << "Error: zone name too long" << endl;
				}
				else {
					if (database.SetVariable("startzone", argv[2]))
						cout << "Starting zone changed: '" << argv[2] << "'" << endl;
					else
						cout << "database.SetVariable failed." << endl;
				}
				return 0;
			}
			cout << "Usage: world startzone zoneshortname" << endl;
			return 0;
		}
		else {
			cout << "Error, unknown command line option" << endl;
			return 0;
		}
	}
	
	if(Config->WorldHTTPEnabled) {
		_log(WORLD__INIT, "Starting HTTP world service...");
		http_server.Start(Config->WorldHTTPPort, Config->WorldHTTPMimeFile.c_str());
	} else {
		_log(WORLD__INIT, "HTTP world service disabled.");
	}
	
	_log(WORLD__INIT, "Loading variables..");
	database.LoadVariables();
	_log(WORLD__INIT, "Loading zones..");
	database.LoadZoneNames();
	_log(WORLD__INIT, "Clearing groups..");
	database.ClearGroup();
	_log(WORLD__INIT, "Clearing raids..");
	database.ClearRaid();
	database.ClearRaidDetails();
	_log(WORLD__INIT, "Loading items..");
	if (!database.LoadItems()) {
		_log(WORLD__INIT_ERR, "Error: Could not load item data.  But ignoring");
	}
	_log(WORLD__INIT, "Loading guilds..");
	guild_mgr.LoadGuilds();
	//rules:
	{
		char tmp[64];
		if (database.GetVariable("RuleSet", tmp, sizeof(tmp)-1)) {
			_log(WORLD__INIT, "Loading rule set '%s'", tmp);
			if(!RuleManager::Instance()->LoadRules(&database, tmp)) {
				_log(WORLD__INIT_ERR, "Failed to load ruleset '%s', falling back to defaults.", tmp);
			}
		} else {
			if(!RuleManager::Instance()->LoadRules(&database, "default")) {
				_log(WORLD__INIT, "No rule set configured, using default rules");
			} else {
				_log(WORLD__INIT, "Loaded default rule set 'default'", tmp);
			}
		}
	}
	if(RuleB(World, ClearTempMerchantlist)){
        _log(WORLD__INIT, "Clearing temporary merchant lists..");
		database.ClearMerchantTemp();
      }
	_log(WORLD__INIT, "Loading EQ time of day..");
	if (!zoneserver_list.worldclock.loadFile(Config->EQTimeFile.c_str()))
		_log(WORLD__INIT_ERR, "Unable to load %s", Config->EQTimeFile.c_str());
	_log(WORLD__INIT, "Loading launcher list..");
	launcher_list.LoadList();
	
	char tmp[20];
	tmp[0] = '\0';
	database.GetVariable("holdzones",tmp, 20);	
	if ((strcasecmp(tmp, "1") == 0)) {
		holdzones = true;
	}
	_log(WORLD__INIT, "Reboot zone modes %s",holdzones ? "ON" : "OFF");
	
	_log(WORLD__INIT, "Deleted %i stale player corpses from database", database.DeleteStalePlayerCorpses());
	if (RuleB(World, DeleteStaleCorpeBackups) == true) {
	_log(WORLD__INIT, "Deleted %i stale player backups from database", database.DeleteStalePlayerBackups());
	}

	_log(WORLD__INIT, "Loading adventures...");
	if(!adventure_manager.LoadAdventureTemplates())
	{
		_log(WORLD__INIT_ERR, "Unable to load adventure templates.");
	}

	if(!adventure_manager.LoadAdventureEntries())
	{
		_log(WORLD__INIT_ERR, "Unable to load adventure templates.");
	}

	adventure_manager.Load();
	adventure_manager.LoadLeaderboardInfo();

	_log(WORLD__INIT, "Purging expired instances");
	database.PurgeExpiredInstances();
	Timer PurgeInstanceTimer(450000);
	PurgeInstanceTimer.Start(450000);

    _log(WORLD__INIT, "Loading char create info...");
    database.LoadCharacterCreateAllocations();
    database.LoadCharacterCreateCombos();

	char errbuf[TCPConnection_ErrorBufferSize];
	if (tcps.Open(Config->WorldTCPPort, errbuf)) {
		_log(WORLD__INIT,"Zone (TCP) listener started.");
	} else {
		_log(WORLD__INIT_ERR,"Failed to start zone (TCP) listener on port %d:",Config->WorldTCPPort);
		_log(WORLD__INIT_ERR,"        %s",errbuf);
		return 1;
	}
	if (eqsf.Open()) {
		_log(WORLD__INIT,"Client (UDP) listener started.");
	} else {
		_log(WORLD__INIT_ERR,"Failed to start client (UDP) listener (port 9000)");
		return 1;
	}

	//register all the patches we have avaliable with the stream identifier.
	EQStreamIdentifier stream_identifier;
	RegisterAllPatches(stream_identifier);
	zoneserver_list.shutdowntimer = new Timer(60000);
	zoneserver_list.shutdowntimer->Disable();
	zoneserver_list.reminder = new Timer(20000);
	zoneserver_list.reminder->Disable();
	Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
	InterserverTimer.Trigger();
	uint8 ReconnectCounter = 100;
	EQStream* eqs;
	EmuTCPConnection* tcpc;
	EQStreamInterface *eqsi;
	
	while(RunLoops) {
		Timer::SetCurrentTime();
		
		//check the factory for any new incoming streams.
		while ((eqs = eqsf.Pop())) {
			//pull the stream out of the factory and give it to the stream identifier
			//which will figure out what patch they are running, and set up the dynamic
			//structures and opcodes for that patch.
			struct in_addr	in;
			in.s_addr = eqs->GetRemoteIP();
			_log(WORLD__CLIENT, "New connection from %s:%d", inet_ntoa(in),ntohs(eqs->GetRemotePort()));
			stream_identifier.AddStream(eqs);	//takes the stream
		}
		
		//give the stream identifier a chance to do its work....
		stream_identifier.Process();
		
		//check the stream identifier for any now-identified streams
		while((eqsi = stream_identifier.PopIdentified())) {
			//now that we know what patch they are running, start up their client object
			struct in_addr	in;
			in.s_addr = eqsi->GetRemoteIP();
 			if (RuleB(World, UseBannedIPsTable)){ //Lieka:  Check to see if we have the responsibility for blocking IPs.
				_log(WORLD__CLIENT, "Checking inbound connection %s against BannedIPs table", inet_ntoa(in));
 				if (!database.CheckBannedIPs(inet_ntoa(in))){ //Lieka:  Check inbound IP against banned IP table.
 					_log(WORLD__CLIENT, "Connection %s PASSED banned IPs check.  Processing connection.", inet_ntoa(in));
 					Client* client = new Client(eqsi);
 					// @merth: client->zoneattempt=0;
 					client_list.Add(client);
 				} else {
 					_log(WORLD__CLIENT, "Connection from %s FAILED banned IPs check.  Closing connection.", inet_ntoa(in));
 					eqsi->Close(); //Lieka:  If the inbound IP is on the banned table, close the EQStream.
 				}
 			}
 			if (!RuleB(World, UseBannedIPsTable)){
 					_log(WORLD__CLIENT, "New connection from %s:%d, processing connection", inet_ntoa(in), ntohs(eqsi->GetRemotePort()));
 					Client* client = new Client(eqsi);
 					// @merth: client->zoneattempt=0;
 					client_list.Add(client);
 			}
		}
		
		client_list.Process();
		
		while ((tcpc = tcps.NewQueuePop())) {
			struct in_addr in;
			in.s_addr = tcpc->GetrIP();
			_log(WORLD__ZONE, "New TCP connection from %s:%d", inet_ntoa(in),tcpc->GetrPort());
			console_list.Add(new Console(tcpc));
		}

		if(PurgeInstanceTimer.Check())
		{
			database.PurgeExpiredInstances();
		}
		
		//check for timeouts in other threads
		timeout_manager.CheckTimeouts();
		
		loginserverlist.Process();
		
		console_list.Process();
		
		zoneserver_list.Process();
		
		launcher_list.Process();

		UCSLink.Process();
	
		QSLink.Process();

		LFPGroupList.Process();

		adventure_manager.Process();
		
		if (InterserverTimer.Check()) {
			InterserverTimer.Start();
			database.ping();
			AsyncLoadVariables(dbasync, &database);
			ReconnectCounter++;
			if (ReconnectCounter >= 12) { // only create thread to reconnect every 10 minutes. previously we were creating a new thread every 10 seconds
				ReconnectCounter = 0;
				if (loginserverlist.AllConnected() == false) {
#ifdef _WINDOWS
					_beginthread(AutoInitLoginServer, 0, nullptr);
#else
					pthread_t thread;
					pthread_create(&thread, nullptr, &AutoInitLoginServer, nullptr);
#endif
				}
			}
		}
		if (numclients == 0) {
			Sleep(50);
			continue;
		}
		Sleep(20);
	}
	_log(WORLD__SHUTDOWN,"World main loop completed.");
	_log(WORLD__SHUTDOWN,"Shutting down console connections (if any).");
	console_list.KillAll();
	_log(WORLD__SHUTDOWN,"Shutting down zone connections (if any).");
	zoneserver_list.KillAll();
	_log(WORLD__SHUTDOWN,"Zone (TCP) listener stopped.");
	tcps.Close();
	_log(WORLD__SHUTDOWN,"Client (UDP) listener stopped.");
	eqsf.Close();
	_log(WORLD__SHUTDOWN,"Signaling HTTP service to stop...");
	http_server.Stop();

	CheckEQEMuErrorAndPause();
	return 0;
}
示例#3
0
文件: net.cpp 项目: Xackery/provztz
int main(int argc, char** argv) {
	// Load server configuration
	_log(WORLD__INIT, "Loading server configuration..");
	if (!WorldConfig::LoadConfig()) {
		_log(WORLD__INIT_ERR, "Loading server configuration failed.");
		return(1);
	}
	const WorldConfig *Config=WorldConfig::get();

	if(!load_log_settings(Config->LogSettingsFile.c_str()))
		_log(WORLD__INIT, "Warning: Unable to read %s", Config->LogSettingsFile.c_str());
	else
		_log(WORLD__INIT, "Log settings loaded from %s", Config->LogSettingsFile.c_str());


	_log(WORLD__INIT, "CURRENT_WORLD_VERSION:%s", CURRENT_WORLD_VERSION);
	
	#ifdef _DEBUG
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	#endif
	
	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#ifndef WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)	{
		_log(WORLD__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#endif
	
	_log(WORLD__INIT, "Connecting to MySQL...");
	if (!database.Connect(
		Config->DatabaseHost.c_str(),
		Config->DatabaseUsername.c_str(),
		Config->DatabasePassword.c_str(),
		Config->DatabaseDB.c_str(),
		Config->DatabasePort)) {
		_log(WORLD__INIT_ERR, "Cannot continue without a database connection.");
		return(1);
	}
	dbasync = new DBAsync(&database);
	guild_mgr.SetDatabase(&database);
	
	if (argc >= 2) {
		char tmp[2];
		if (strcasecmp(argv[1], "help") == 0 || strcasecmp(argv[1], "?") == 0 || strcasecmp(argv[1], "/?") == 0 || strcasecmp(argv[1], "-?") == 0 || strcasecmp(argv[1], "-h") == 0 || strcasecmp(argv[1], "-help") == 0) {
			cout << "Worldserver command line commands:" << endl;
			cout << "adduser username password flag    - adds a user account" << endl;
			cout << "flag username flag    - sets GM flag on the account" << endl;
			cout << "startzone zoneshortname    - sets the starting zone" << endl;
			cout << "-holdzones    - reboots lost zones" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "-holdzones") == 0) {
			cout << "Reboot Zones mode ON" << endl;
			holdzones = true;
		}
		else if (database.GetVariable("disablecommandline", tmp, 2)) {
			if (strlen(tmp) == 1) {
				if (tmp[0] == '1') {
					cout << "Command line disabled in database... exiting" << endl;
					return 0;
				}
			}
		}
		else if (strcasecmp(argv[1], "adduser") == 0) {
			if (argc == 5) {
				if (Seperator::IsNumber(argv[4])) {
					if (atoi(argv[4]) >= 0 && atoi(argv[4]) <= 255) {
						if (database.CreateAccount(argv[2], argv[3], atoi(argv[4])) == 0)
							cout << "database.CreateAccount failed." << endl;
						else
							cout << "Account created: Username='******', Password='******', status=" << argv[4] << endl;
						return 0;
					}
				}
			}
			cout << "Usage: world adduser username password flag" << endl;
			cout << "flag = 0, 1 or 2" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "flag") == 0) {
			if (argc == 4) {
				if (Seperator::IsNumber(argv[3])) {

					if (atoi(argv[3]) >= 0 && atoi(argv[3]) <= 255) {
						if (database.SetAccountStatus(argv[2], atoi(argv[3])))
							cout << "Account flagged: Username='******', status=" << argv[3] << endl;
						else
							cout << "database.SetAccountStatus failed." << endl;
						return 0;
					}
				}
			}
			cout << "Usage: world flag username flag" << endl;
			cout << "flag = 0-200" << endl;
			return 0;
		}
		else if (strcasecmp(argv[1], "startzone") == 0) {
			if (argc == 3) {
				if (strlen(argv[2]) < 3) {
					cout << "Error: zone name too short" << endl;
				}
				else if (strlen(argv[2]) > 15) {
					cout << "Error: zone name too long" << endl;
				}
				else {
					if (database.SetVariable("startzone", argv[2]))
						cout << "Starting zone changed: '" << argv[2] << "'" << endl;
					else
						cout << "database.SetVariable failed." << endl;
				}
				return 0;
			}
			cout << "Usage: world startzone zoneshortname" << endl;
			return 0;
		}
		else {
			cout << "Error, unknown command line option" << endl;
			return 0;
		}
	}
	srand(time(NULL));
	
	if(Config->WorldHTTPEnabled) {
		_log(WORLD__INIT, "Starting HTTP world service...");
		http_server.Start(Config->WorldHTTPPort, Config->WorldHTTPMimeFile.c_str());
	} else {
		_log(WORLD__INIT, "HTTP world service disabled.");
	}
	
	_log(WORLD__INIT, "Loading variables..");
	database.LoadVariables();
	_log(WORLD__INIT, "Loading zones..");
	database.LoadZoneNames();
	_log(WORLD__INIT, "Clearing groups..");
	database.ClearGroup();
	_log(WORLD__INIT, "Clearing raids..");
	database.ClearRaid();
	database.ClearRaidDetails();
	_log(WORLD__INIT, "Loading items..");
	if (!database.LoadItems()) {
		_log(WORLD__INIT_ERR, "Error: Could not load item data.  But ignoring");
	}
	_log(WORLD__INIT, "Loading guilds..");
	guild_mgr.LoadGuilds();
	//rules:
	{
		char tmp[64];
		if (database.GetVariable("RuleSet", tmp, sizeof(tmp)-1)) {
			_log(WORLD__INIT, "Loading rule set '%s'", tmp);
			if(!rules->LoadRules(&database, tmp)) {
				_log(WORLD__INIT_ERR, "Failed to load ruleset '%s', falling back to defaults.", tmp);
			}
		} else {
			if(!rules->LoadRules(&database, "default")) {
				_log(WORLD__INIT, "No rule set configured, using default rules");
			} else {
				_log(WORLD__INIT, "Loaded default rule set 'default'", tmp);
			}
		}
	}
	if(RuleB(World, ClearTempMerchantlist)){
        _log(WORLD__INIT, "Clearing temporary merchant lists..");
		database.ClearMerchantTemp();
      }
	_log(WORLD__INIT, "Loading EQ time of day..");
	if (!zoneserver_list.worldclock.loadFile(Config->EQTimeFile.c_str()))
		_log(WORLD__INIT_ERR, "Unable to load %s", Config->EQTimeFile.c_str());
	_log(WORLD__INIT, "Loading launcher list..");
	launcher_list.LoadList();
	
	char tmp[20];
	tmp[0] = '\0';
	database.GetVariable("holdzones",tmp, 20);	
	if ((strcasecmp(tmp, "1") == 0)) {
		holdzones = true;
	}
	_log(WORLD__INIT, "Reboot zone modes %s",holdzones ? "ON" : "OFF");
	
	_log(WORLD__INIT, "Deleted %i stale player corpses from database", database.DeleteStalePlayerCorpses());
	_log(WORLD__INIT, "Deleted %i stale player backups from database", database.DeleteStalePlayerBackups());

	_log(WORLD__INIT, "Purging expired instances");
	database.PurgeExpiredInstances();
	Timer PurgeInstanceTimer(450000);
	PurgeInstanceTimer.Start(450000);

	char errbuf[TCPConnection_ErrorBufferSize];
	if (tcps.Open(Config->WorldTCPPort, errbuf)) {
		_log(WORLD__INIT,"Zone (TCP) listener started.");
	} else {
		_log(WORLD__INIT_ERR,"Failed to start zone (TCP) listener on port %d:",Config->WorldTCPPort);
		_log(WORLD__INIT_ERR,"        %s",errbuf);
		return 1;
	}
	if (eqsf.Open()) {
		_log(WORLD__INIT,"Client (UDP) listener started.");
	} else {
		_log(WORLD__INIT_ERR,"Failed to start client (UDP) listener (port 9000)");
		return 1;
	}

	//ItemInst a((SharedDatabase *)&database,(uint32)17354,0);
	//ItemInst b((SharedDatabase *)&database,(uint32)26885,0);
	//ItemInst c((SharedDatabase *)&database,(uint32)41075,0);
	//ItemInst d((SharedDatabase *)&database,(uint32)29041,0);
	//d.PutAugment(&database,0,41006);
	//a.PutItem(0,b);
	//a.PutItem(1,c);
	//a.PutItem(2,d);
	//_log(WORLD__INIT,"%s\n",Client62::SerializeItem(&a,30,0));
	
	//register all the patches we have avaliable with the stream identifier.
	EQStreamIdentifier stream_identifier;
	RegisterAllPatches(stream_identifier);
	
	
	zoneserver_list.shutdowntimer=new Timer(60000);
	zoneserver_list.shutdowntimer->Disable();
	zoneserver_list.reminder = new Timer(20000);
	zoneserver_list.reminder->Disable();
	Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
	InterserverTimer.Trigger();
	EQStream* eqs;
	EmuTCPConnection* tcpc;
	EQStreamInterface *eqsi;
	
	while(RunLoops) {
		Timer::SetCurrentTime();
		
		//check the factory for any new incoming streams.
		while ((eqs = eqsf.Pop())) {
			//pull the stream out of the factory and give it to the stream identifier
			//which will figure out what patch they are running, and set up the dynamic
			//structures and opcodes for that patch.
			struct in_addr	in;
			in.s_addr = eqs->GetRemoteIP();
			_log(WORLD__CLIENT, "New connection from %s:%d", inet_ntoa(in),ntohs(eqs->GetRemotePort()));
			stream_identifier.AddStream(eqs);	//takes the stream
		}
		
		//give the stream identifier a chance to do its work....
		stream_identifier.Process();
		
		//check the stream identifier for any now-identified streams
		while((eqsi = stream_identifier.PopIdentified())) {
			//now that we know what patch they are running, start up their client object
			struct in_addr	in;
			in.s_addr = eqsi->GetRemoteIP();
 			if (RuleB(World, UseBannedIPsTable)){ //Lieka:  Check to see if we have the responsibility for blocking IPs.
				_log(WORLD__CLIENT, "Checking inbound connection %s against BannedIPs table", inet_ntoa(in));
 				if (!database.CheckBannedIPs(inet_ntoa(in))){ //Lieka:  Check inbound IP against banned IP table.
 					_log(WORLD__CLIENT, "Connection %s PASSED banned IPs check.  Processing connection.", inet_ntoa(in));
 					Client* client = new Client(eqsi);
 					// @merth: client->zoneattempt=0;
 					client_list.Add(client);
 				} else {
 					_log(WORLD__CLIENT, "Connection from %s FAILED banned IPs check.  Closing connection.", inet_ntoa(in));
 					eqsi->Close(); //Lieka:  If the inbound IP is on the banned table, close the EQStream.
 				}
 			}
 			if (!RuleB(World, UseBannedIPsTable)){
 					_log(WORLD__CLIENT, "New connection from %s:%d, processing connection", inet_ntoa(in), ntohs(eqsi->GetRemotePort()));
 					Client* client = new Client(eqsi);
 					// @merth: client->zoneattempt=0;
 					client_list.Add(client);
 			}
		}
		
		client_list.Process();
		
		while ((tcpc = tcps.NewQueuePop())) {
			struct in_addr in;
			in.s_addr = tcpc->GetrIP();
			_log(WORLD__ZONE, "New TCP connection from %s:%d", inet_ntoa(in),tcpc->GetrPort());
			console_list.Add(new Console(tcpc));
		}

		if(PurgeInstanceTimer.Check())
		{
			database.PurgeExpiredInstances();
		}
		
		//check for timeouts in other threads
		timeout_manager.CheckTimeouts();
		
		loginserver.Process();
		
		console_list.Process();
		
		zoneserver_list.Process();
		
		launcher_list.Process();

		LFPGroupList.Process();
		
		if (InterserverTimer.Check()) {
			InterserverTimer.Start();
			database.ping();
			AsyncLoadVariables(dbasync, &database);
			if (Config->LoginHost.length() && loginserver.Connected() == false) {
#ifdef WIN32
				_beginthread(AutoInitLoginServer, 0, NULL);
#else
				pthread_t thread;
				pthread_create(&thread, NULL, &AutoInitLoginServer, NULL);
#endif
			}
		}
		if (numclients == 0) {
			Sleep(50);
			continue;
		}
		Sleep(20);
	}
	_log(WORLD__SHUTDOWN,"World main loop completed.");
	_log(WORLD__SHUTDOWN,"Shutting down console connections (if any).");
	console_list.KillAll();
	_log(WORLD__SHUTDOWN,"Shutting down zone connections (if any).");
	zoneserver_list.KillAll();
	_log(WORLD__SHUTDOWN,"Zone (TCP) listener stopped.");
	tcps.Close();
	_log(WORLD__SHUTDOWN,"Client (UDP) listener stopped.");
	eqsf.Close();
	_log(WORLD__SHUTDOWN,"Signaling HTTP service to stop...");
	http_server.Stop();
#if 0
#if defined(SHAREMEM) && !defined(WIN32)
		for (int ipc_files = 0; ipc_files <= 4; ipc_files++) {
			key_t share_key;
			switch (ipc_files) {
				// Item
				case 0: share_key = ftok(".", 'I'); break;
				// Npctype
				case 1: share_key = ftok(".", 'N'); break;
				// Door
				case 2: share_key = ftok(".", 'D'); break;
				// Spell
				case 3: share_key = ftok(".", 'S'); break;
				// Faction
				case 4: share_key = ftok(".", 'F'); break;
				// ERROR Fatal
				default: cerr<<"Opps!"<<endl; share_key = 0xFF; break;
			}
			
			int share_id = shmget(share_key, 0, IPC_NOWAIT|0400);
			if (share_id <= 0) {
				cerr<<"exiting could not check user count on shared memory ipcs mem leak!!!!!!!! id="<<share_id<<" key:"<<share_key<<endl;
				exit(1);
			}
			struct shmid_ds mem_users;
			if ((shmctl(share_id, IPC_STAT, &mem_users)) != 0) {
				cerr<<"exiting error checking user count on shared memory, marking for deletion!!!!!id="<<share_id<<" key:"<<share_key<<endl;
				shmctl(share_id, IPC_RMID, 0);
				exit(1);
			}
			if (mem_users.shm_nattch == 0) {
				//cerr<<"exiting stale share marked for deletion!id="<<share_id<<" key:"<<share_key<<endl;
				shmctl(share_id, IPC_RMID, 0);
			}
			else if (mem_users.shm_nattch == 1) {
				//cerr<<"mem_users = 1"<<endl;
				// Detatch and delete shared mem here
				EMuShareMemDLL.Unload();
				shmctl(share_id, IPC_RMID, 0);
			}

		}
#endif
#endif

	CheckEQEMuErrorAndPause();
	return 0;
}
示例#4
0
int main() {
	RegisterExecutablePlatform(ExePlatformWebInterface);
	Log.LoadLogSettingsDefaults();
	
	set_exception_handler();
	register_methods();
	Timer InterserverTimer(INTERSERVER_TIMER); // does auto-reconnect
	Log.Out(Logs::General, Logs::WebInterface_Server, "Starting EQEmu Web Server.");
	
	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		Log.Out(Logs::General, Logs::Error, "Could not set signal handler");
		return 1;
	}
	
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		Log.Out(Logs::General, Logs::Error, "Could not set signal handler");
		return 1;
	}

	Timer writable_socket_timer;
	config = EQEmuConfig::get();
	lws_context_creation_info info;
	memset(&info, 0, sizeof info);
	info.port = config->WebInterfacePort;
	info.protocols = protocols;
	info.extensions = nullptr;
	info.gid = -1;
	info.uid = -1;

	context = libwebsocket_create_context(&info);
	if (context == NULL) {
		Log.Out(Logs::General, Logs::Error, "Could not create websocket handler.");
		return 1;
	}

	db = new SharedDatabase();
	Log.Out(Logs::General, Logs::WebInterface_Server, "Connecting to database...");
	if(!db->Connect(config->DatabaseHost.c_str(), config->DatabaseUsername.c_str(),
		config->DatabasePassword.c_str(), config->DatabaseDB.c_str(), config->DatabasePort)) {
		Log.Out(Logs::General, Logs::WebInterface_Server, "Unable to connect to the database, cannot continue without a database connection");
		return 1;
	}
	db->LoadLogSettings(Log.log_settings);
	worldserver = new WorldServer(config->SharedKey);
	worldserver->Connect();
	writable_socket_timer.Start(10);

	while(run) { 
		Timer::SetCurrentTime(); 
		
		if (InterserverTimer.Check()) {
			if (worldserver->TryReconnect() && (!worldserver->Connected()))
				worldserver->AsyncConnect();
		}
		worldserver->Process(); 

		timeout_manager.CheckTimeouts();

		if (writable_socket_timer.Check(true)) {
			libwebsocket_callback_on_writable_all_protocol(&protocols[1]);
		}

		libwebsocket_service(context, 5);
		Sleep(1);
	}

	safe_delete(worldserver);
	safe_delete(db);
	libwebsocket_context_destroy(context);

	return 0;
}
示例#5
0
文件: net.cpp 项目: Zamthos/Server
int main(int argc, char** argv) {
    RegisterExecutablePlatform(ExePlatformZone);
    set_exception_handler();

	const char *zone_name;
	
	if(argc == 3) {
		worldserver.SetLauncherName(argv[2]);
		worldserver.SetLaunchedName(argv[1]);
		if(strncmp(argv[1], "dynamic_", 8) == 0) {
			//dynamic zone with a launcher name correlation
			zone_name = ".";
		} else {
			zone_name = argv[1];
			worldserver.SetLaunchedName(zone_name);
		}
	} else if (argc == 2) {
		worldserver.SetLauncherName("NONE");
		worldserver.SetLaunchedName(argv[1]);
		if(strncmp(argv[1], "dynamic_", 8) == 0) {
			//dynamic zone with a launcher name correlation
			zone_name = ".";
		} else {
			zone_name = argv[1];
			worldserver.SetLaunchedName(zone_name);
		}
	} else {
		zone_name = ".";
		worldserver.SetLaunchedName(".");
		worldserver.SetLauncherName("NONE");
	}

	_log(ZONE__INIT, "Loading server configuration..");
	if (!ZoneConfig::LoadConfig()) {
		_log(ZONE__INIT_ERR, "Loading server configuration failed.");
		return(1);
	}
	const ZoneConfig *Config=ZoneConfig::get();

	if(!load_log_settings(Config->LogSettingsFile.c_str()))
		_log(ZONE__INIT, "Warning: Unable to read %s", Config->LogSettingsFile.c_str());
	else
		_log(ZONE__INIT, "Log settings loaded from %s", Config->LogSettingsFile.c_str());
	
	worldserver.SetPassword(Config->SharedKey.c_str());

	_log(ZONE__INIT, "Connecting to MySQL...");
	if (!database.Connect(
		Config->DatabaseHost.c_str(),
		Config->DatabaseUsername.c_str(),
		Config->DatabasePassword.c_str(),
		Config->DatabaseDB.c_str(),
		Config->DatabasePort)) {
		_log(ZONE__INIT_ERR, "Cannot continue without a database connection.");
		return(1);
	}
	dbasync = new DBAsync(&database);
	dbasync->AddFQ(&MTdbafq);
	guild_mgr.SetDatabase(&database);

	GuildBanks = NULL;

#ifdef _EQDEBUG
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
	
	_log(ZONE__INIT, "CURRENT_VERSION: %s", CURRENT_VERSION);
	
	/*
	 * Setup nice signal handlers
	 */
	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		_log(ZONE__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		_log(ZONE__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#ifndef WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)	{
		_log(ZONE__INIT_ERR, "Could not set signal handler");
		return 0;
	}
	#endif

	const char *log_ini_file = "./log.ini";
	if(!load_log_settings(log_ini_file))
		_log(ZONE__INIT, "Warning: Unable to read %s", log_ini_file);
	else
		_log(ZONE__INIT, "Log settings loaded from %s", log_ini_file);
	
	_log(ZONE__INIT, "Mapping Incoming Opcodes");
	MapOpcodes();
	_log(ZONE__INIT, "Loading Variables");
	database.LoadVariables();
	_log(ZONE__INIT, "Loading zone names");
	database.LoadZoneNames();
	_log(ZONE__INIT, "Loading items");
	if (!database.LoadItems()) {
		_log(ZONE__INIT_ERR, "Loading items FAILED!");
		_log(ZONE__INIT, "Failed.  But ignoring error and going on...");
	}

	_log(ZONE__INIT, "Loading npc faction lists");
	if (!database.LoadNPCFactionLists()) {
		_log(ZONE__INIT_ERR, "Loading npcs faction lists FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
	}
	_log(ZONE__INIT, "Loading loot tables");
	if (!database.LoadLoot()) {
		_log(ZONE__INIT_ERR, "Loading loot FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
    }
	_log(ZONE__INIT, "Loading skill caps");
	if (!database.LoadSkillCaps()) {
		_log(ZONE__INIT_ERR, "Loading skill caps FAILED!");
		CheckEQEMuErrorAndPause();
		return 0;
	}
	
    _log(ZONE__INIT, "Loading spells");
    EQEmu::MemoryMappedFile *mmf = NULL;
	LoadSpells(&mmf);

	_log(ZONE__INIT, "Loading guilds");
	guild_mgr.LoadGuilds();
	_log(ZONE__INIT, "Loading factions");
	database.LoadFactionData();
	_log(ZONE__INIT, "Loading titles");
	title_manager.LoadTitles();
	_log(ZONE__INIT, "Loading AA effects");
	database.LoadAAEffects();
	_log(ZONE__INIT, "Loading tributes");
	database.LoadTributes();
	_log(ZONE__INIT, "Loading corpse timers");
	database.GetDecayTimes(npcCorpseDecayTimes);
	_log(ZONE__INIT, "Loading commands");
	int retval=command_init();
	if(retval<0)
		_log(ZONE__INIT_ERR, "Command loading FAILED");
	else
		_log(ZONE__INIT, "%d commands loaded", retval);

	//rules:
	{
		char tmp[64];
		if (database.GetVariable("RuleSet", tmp, sizeof(tmp)-1)) {
			_log(ZONE__INIT, "Loading rule set '%s'", tmp);
			if(!RuleManager::Instance()->LoadRules(&database, tmp)) {
				_log(ZONE__INIT_ERR, "Failed to load ruleset '%s', falling back to defaults.", tmp);
			}
		} else {
			if(!RuleManager::Instance()->LoadRules(&database, "default")) {
				_log(ZONE__INIT, "No rule set configured, using default rules");
			} else {
				_log(ZONE__INIT, "Loaded default rule set 'default'", tmp);
			}
		}
	}

	if(RuleB(TaskSystem, EnableTaskSystem)) {
		_log(ZONE__INIT, "Loading Tasks");
		taskmanager = new TaskManager;
		taskmanager->LoadTasks();
	}

    parse = new QuestParserCollection();
    PerlXSParser *pxs = new PerlXSParser();
    Parser *ps = new Parser();
    parse->RegisterQuestInterface(pxs, "pl");
    //parse->RegisterQuestInterface(ps, "qst");


	//now we have our parser, load the quests
	_log(ZONE__INIT, "Loading quests");
	parse->ReloadQuests();
	

#ifdef CLIENT_LOGS
	LogFile->SetAllCallbacks(ClientLogs::EQEmuIO_buf);
	LogFile->SetAllCallbacks(ClientLogs::EQEmuIO_fmt);
	LogFile->SetAllCallbacks(ClientLogs::EQEmuIO_pva);
#endif
	if (!worldserver.Connect()) {
		_log(ZONE__INIT_ERR, "worldserver.Connect() FAILED!");
	}
	
	Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
#ifdef EQPROFILE
#ifdef PROFILE_DUMP_TIME
	Timer profile_dump_timer(PROFILE_DUMP_TIME*1000);
	profile_dump_timer.Start();
#endif
#endif
	if (!strlen(zone_name) || !strcmp(zone_name,".")) {
		_log(ZONE__INIT, "Entering sleep mode");
	} else if (!Zone::Bootup(database.GetZoneID(zone_name), 0, true)) { //todo: go above and fix this to allow cmd line instance
		_log(ZONE__INIT_ERR, "Zone bootup FAILED!");
		zone = 0;
	}
	
	//register all the patches we have avaliable with the stream identifier.
	EQStreamIdentifier stream_identifier;
	RegisterAllPatches(stream_identifier);
	
#ifndef WIN32
	_log(COMMON__THREADS, "Main thread running with thread id %d", pthread_self());
#endif
	
	Timer quest_timers(100);
	UpdateWindowTitle();
	bool worldwasconnected = worldserver.Connected();
	EQStream* eqss;
	EQStreamInterface *eqsi;
	Timer temp_timer(10);
	temp_timer.Start();
	while(RunLoops) {
		{	//profiler block to omit the sleep from times
		_ZP(net_main);
		
		//Advance the timer to our current point in time
		Timer::SetCurrentTime();
		
		//process stuff from world
		worldserver.Process();

		if (!eqsf.IsOpen() && Config->ZonePort!=0) {
			_log(ZONE__INIT, "Starting EQ Network server on port %d",Config->ZonePort);
			if (!eqsf.Open(Config->ZonePort)) {
				_log(ZONE__INIT_ERR, "Failed to open port %d",Config->ZonePort);
				ZoneConfig::SetZonePort(0);
				worldserver.Disconnect();
				worldwasconnected = false;
			}
		}
		
		//check the factory for any new incoming streams.
		while ((eqss = eqsf.Pop())) {
			//pull the stream out of the factory and give it to the stream identifier
			//which will figure out what patch they are running, and set up the dynamic
			//structures and opcodes for that patch.
			struct in_addr	in;
			in.s_addr = eqss->GetRemoteIP();
			_log(WORLD__CLIENT, "New connection from %s:%d", inet_ntoa(in),ntohs(eqss->GetRemotePort()));
			stream_identifier.AddStream(eqss);	//takes the stream
		}
		
		//give the stream identifier a chance to do its work....
		stream_identifier.Process();
		
		//check the stream identifier for any now-identified streams
		while((eqsi = stream_identifier.PopIdentified())) {
			//now that we know what patch they are running, start up their client object
			struct in_addr	in;
			in.s_addr = eqsi->GetRemoteIP();
			_log(WORLD__CLIENT, "New client from %s:%d", inet_ntoa(in), ntohs(eqsi->GetRemotePort()));
			Client* client = new Client(eqsi);
			entity_list.AddClient(client);
		}
		
		
		//check for timeouts in other threads
		timeout_manager.CheckTimeouts();
		
		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 && temp_timer.Check()) {
			{
				if(net.group_timer.Enabled() && net.group_timer.Check())
					entity_list.GroupProcess();

				if(net.door_timer.Enabled() && net.door_timer.Check())
					entity_list.DoorProcess();

				if(net.object_timer.Enabled() && net.object_timer.Check())
					entity_list.ObjectProcess();

				if(net.corpse_timer.Enabled() && net.corpse_timer.Check())
					entity_list.CorpseProcess();

				if(net.trap_timer.Enabled() && net.trap_timer.Check())
					entity_list.TrapProcess();

				if(net.raid_timer.Enabled() && net.raid_timer.Check())
					entity_list.RaidProcess();

				entity_list.Process();

				entity_list.MobProcess();

				entity_list.BeaconProcess();

				if (zone) {
					if(!zone->Process()) {
						Zone::Shutdown();
					}
				}

				if(quest_timers.Check())
					quest_manager.Process();

			}
		}
		DBAsyncWork* dbaw = 0;
		while ((dbaw = MTdbafq.Pop())) {
			DispatchFinishedDBAsync(dbaw);
		}
		if (InterserverTimer.Check()) {
			InterserverTimer.Start();
			database.ping();
			AsyncLoadVariables(dbasync, &database);
			entity_list.UpdateWho();
			if (worldserver.TryReconnect() && (!worldserver.Connected()))
				worldserver.AsyncConnect();
		}

#if defined(_EQDEBUG) && 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
#ifdef EQPROFILE
#ifdef PROFILE_DUMP_TIME
		if(profile_dump_timer.Check()) {
			DumpZoneProfile();
		}
#endif
#endif
		}	//end extra profiler block
		Sleep(ZoneTimerResolution);
	}

    safe_delete(parse);
    safe_delete(pxs);
    safe_delete(ps);
    safe_delete(mmf);
	
	entity_list.Clear();
	if (zone != 0)
		Zone::Shutdown(true);
	//Fix for Linux world server problem.
	eqsf.Close();
	worldserver.Disconnect();
	dbasync->CommitWrites();
	dbasync->StopThread();
	safe_delete(taskmanager);
	command_deinit();
	
	CheckEQEMuErrorAndPause();
	_log(ZONE__INIT, "Proper zone shutdown complete.");
	return 0;
}
示例#6
0
int main() {
	RegisterExecutablePlatform(ExePlatformQueryServ);
	set_exception_handler();

	Timer LFGuildExpireTimer(60000);

	Timer InterserverTimer(INTERSERVER_TIMER); // does auto-reconnect

	_log(QUERYSERV__INIT, "Starting EQEmu QueryServ.");

	if (!queryservconfig::LoadConfig()) {

		_log(QUERYSERV__INIT, "Loading server configuration failed.");

		return 1;
	}

	Config = queryservconfig::get();

	if(!load_log_settings(Config->LogSettingsFile.c_str()))
		_log(QUERYSERV__INIT, "Warning: Unable to read %s", Config->LogSettingsFile.c_str());
	else
		_log(QUERYSERV__INIT, "Log settings loaded from %s", Config->LogSettingsFile.c_str());

	WorldShortName = Config->ShortName;

	_log(QUERYSERV__INIT, "Connecting to MySQL...");

	if (!database.Connect(
		Config->QSDatabaseHost.c_str(),
		Config->QSDatabaseUsername.c_str(),
		Config->QSDatabasePassword.c_str(),
		Config->QSDatabaseDB.c_str(),
		Config->QSDatabasePort)) {
		_log(WORLD__INIT_ERR, "Cannot continue without a database connection.");
		return 1;
	}

	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		_log(QUERYSERV__ERROR, "Could not set signal handler");
		return 1;
	}
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		_log(QUERYSERV__ERROR, "Could not set signal handler");
		return 1;
	}

	worldserver = new WorldServer;

	worldserver->Connect();

	lfguildmanager.LoadDatabase();

	while(RunLoops) {

		Timer::SetCurrentTime();

		if(LFGuildExpireTimer.Check())
			lfguildmanager.ExpireEntries();

		if (InterserverTimer.Check()) {
			if (worldserver->TryReconnect() && (!worldserver->Connected()))
				worldserver->AsyncConnect();
		}
		worldserver->Process();

		timeout_manager.CheckTimeouts();

		Sleep(100);
	}
}
示例#7
0
文件: ucs.cpp 项目: Leere/Server
int main() {
	RegisterExecutablePlatform(ExePlatformUCS);
	set_exception_handler();

	// Check every minute for unused channels we can delete
	//
	Timer ChannelListProcessTimer(60000);

	Timer InterserverTimer(INTERSERVER_TIMER); // does auto-reconnect

	_log(UCS__INIT, "Starting EQEmu Universal Chat Server.");

	if (!ucsconfig::LoadConfig()) {

		_log(UCS__INIT, "Loading server configuration failed.");

		return 1;
	}

	Config = ucsconfig::get();

	if(!load_log_settings(Config->LogSettingsFile.c_str()))
		_log(UCS__INIT, "Warning: Unable to read %s", Config->LogSettingsFile.c_str());
	else
		_log(UCS__INIT, "Log settings loaded from %s", Config->LogSettingsFile.c_str());

	WorldShortName = Config->ShortName;

	_log(UCS__INIT, "Connecting to MySQL...");

	if (!database.Connect(
		Config->DatabaseHost.c_str(),
		Config->DatabaseUsername.c_str(),
		Config->DatabasePassword.c_str(),
		Config->DatabaseDB.c_str(),
		Config->DatabasePort)) {
		_log(WORLD__INIT_ERR, "Cannot continue without a database connection.");
		return 1;
	}

	char tmp[64];

	if (database.GetVariable("RuleSet", tmp, sizeof(tmp)-1)) {
		_log(WORLD__INIT, "Loading rule set '%s'", tmp);
		if(!RuleManager::Instance()->LoadRules(&database, tmp)) {
			_log(UCS__ERROR, "Failed to load ruleset '%s', falling back to defaults.", tmp);
		}
	} else {
		if(!RuleManager::Instance()->LoadRules(&database, "default")) {
			_log(UCS__INIT, "No rule set configured, using default rules");
		} else {
			_log(UCS__INIT, "Loaded default rule set 'default'", tmp);
		}
	}

	database.ExpireMail();

	if(Config->ChatPort != Config->MailPort)
	{
		_log(UCS__ERROR, "MailPort and CharPort must be the same in eqemu_config.xml for UCS.");
		exit(1);
	}

	CL = new Clientlist(Config->ChatPort);

	ChannelList = new ChatChannelList();

	database.LoadChatChannels();

	if (signal(SIGINT, CatchSignal) == SIG_ERR)	{
		_log(UCS__ERROR, "Could not set signal handler");
		return 1;
	}
	if (signal(SIGTERM, CatchSignal) == SIG_ERR)	{
		_log(UCS__ERROR, "Could not set signal handler");
		return 1;
	}

	worldserver = new WorldServer;

	worldserver->Connect();

	while(RunLoops) {

		Timer::SetCurrentTime();

		CL->Process();

		if(ChannelListProcessTimer.Check())
			ChannelList->Process();

		if (InterserverTimer.Check()) {
			if (worldserver->TryReconnect() && (!worldserver->Connected()))
				worldserver->AsyncConnect();
		}
		worldserver->Process();

		timeout_manager.CheckTimeouts();

		Sleep(100);
	}

	ChannelList->RemoveAllChannels();

	CL->CloseAllConnections();

}