cyphesis/server/server.cpp

688 lines
25 KiB
C++
Raw Permalink Normal View History

2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000-2004 Alistair Riddoch
2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2014-01-29 13:08:52 +01:00
#include "CommHttpClient.h"
#include "CommPythonClient.h"
#include "CommPSQLSocket.h"
#include "CommMetaClient.h"
#include "CommMDNSPublisher.h"
#include "CommAsioListener_impl.h"
#include "Connection.h"
#include "ServerRouting.h"
#include "EntityBuilder.h"
#include "ArithmeticBuilder.h"
2009-03-24 03:08:45 +00:00
#include "Persistence.h"
#include "WorldRouter.h"
#include "Ruleset.h"
#include "StorageManager.h"
#include "IdleConnector.h"
#include "Admin.h"
#include "PossessionAuthenticator.h"
#include "TrustedConnection.h"
#include "HttpCache.h"
#include "rulesets/Python_API.h"
#include "rulesets/LocatedEntity.h"
#if POSTGRES_FOUND
2018-05-16 21:48:18 +02:00
#include "common/DatabasePostgres.h"
#endif
#include "common/id.h"
#include "common/const.h"
#include "common/Inheritance.h"
#include "common/system.h"
#include "common/sockets.h"
2011-10-31 11:19:33 +01:00
#include "common/SystemTime.h"
2015-04-06 21:47:55 +02:00
#include "common/Monitors.h"
#include <varconf/config.h>
#include <thread>
#include <fstream>
#include <boost/filesystem/operations.hpp>
#include <common/FileSystemObserver.h>
2018-04-26 19:28:20 +02:00
#include <common/AssetsManager.h>
#include <common/DatabaseSQLite.h>
#include <common/RepeatedTask.h>
using String::compose;
2014-01-29 09:05:07 +01:00
using namespace boost::asio;
class TrustedConnection;
class Peer;
static const bool debug_flag = false;
INT_OPTION(http_port_num, 6780, CYPHESIS, "httpport",
"Network listen port for http connection to the server")
;
BOOL_OPTION(useMetaserver, true, CYPHESIS, "usemetaserver",
"Flag to control registration with the metaserver")
;
STRING_OPTION(mserver, "metaserver.worldforge.org", CYPHESIS, "metaserver",
"Hostname to use as the metaserver")
;
INT_OPTION(ai_clients, 1, CYPHESIS, "aiclients",
"Number of AI clients to spawn.")
;
void interactiveSignalsHandler(boost::asio::signal_set& this_, boost::system::error_code error, int signal_number) {
if (!error) {
switch (signal_number) {
case SIGINT:
case SIGTERM:
case SIGHUP:
//If we've already received one call to shut down softly we should elevate
//it to a hard shutdown.
//This also happens if "soft" exit isn't enabled.
if (exit_flag_soft || !exit_soft_enabled) {
exit_flag = true;
} else {
exit_flag_soft = true;
}
break;
case SIGQUIT:
exit_flag = true;
break;
default:
break;
}
this_.async_wait(std::bind(interactiveSignalsHandler, std::ref(this_), std::placeholders::_1, std::placeholders::_2));
}
}
void daemonSignalsHandler(boost::asio::signal_set& this_, boost::system::error_code error, int signal_number) {
if (!error) {
switch (signal_number) {
case SIGTERM:
//If we've already received one call to shut down softly we should elevate
//it to a hard shutdown.
//This also happens if "soft" exit isn't enabled.
if (exit_flag_soft || !exit_soft_enabled) {
exit_flag = true;
} else {
exit_flag_soft = true;
}
break;
default:
break;
}
this_.async_wait(std::bind(daemonSignalsHandler, std::ref(this_), std::placeholders::_1, std::placeholders::_2));
}
}
int main(int argc, char ** argv)
{
if (security_init() != 0) {
log(CRITICAL, "Security initialization Error. Exiting.");
return EXIT_SECURITY_ERROR;
}
if (security_check() != SECURITY_OKAY) {
log(CRITICAL, "Security check error. Exiting.");
return EXIT_SECURITY_ERROR;
}
//Turn on soft exits so we get a chance to persist external clients' thoughts.
exit_soft_enabled = true;
interactive_signals();
int config_status = loadConfig(argc, argv, USAGE_SERVER);
if (config_status < 0) {
if (config_status == CONFIG_VERSION) {
std::cout << argv[0] << " (cyphesis) " << consts::version
<< " (Cyphesis build: " << consts::buildId << ")"
<< std::endl << std::flush;
return 0;
} else if (config_status == CONFIG_HELP) {
showUsage(argv[0], USAGE_SERVER);
return 0;
} else if (config_status != CONFIG_ERROR) {
log(ERROR, "Unknown error reading configuration.");
}
// Fatal error loading config file.
return EXIT_CONFIG_ERROR;
}
if (daemon_flag) {
int pid = daemonise();
if (pid == -1) {
return EXIT_FORK_ERROR;
} else if (pid > 0) {
return EXIT_SUCCESS;
}
}
//Check if we should spawn AI clients.
if (ai_clients) {
log(INFO, compose("Spawning %1 AI client processes.", ai_clients));
for (int i = 0; i < ai_clients; ++i) {
auto pid = fork();
if (pid == 0) {
2018-01-30 16:54:49 +01:00
execl((bin_directory + "/cyaiclient").c_str(), (bin_directory + "/cyaiclient").c_str(), nullptr);
return EXIT_FORK_ERROR;
} else if (pid == -1) {
log(WARNING, "Could not spawn AI client process.");
}
}
}
// If we are a daemon logging to syslog, we need to set it up.
initLogger();
//Check that there's a valid assets directory, and warn if not.
if (!boost::filesystem::is_directory(assets_directory)) {
log(ERROR, String::compose("Could not find any valid assets directory at '%1'.", assets_directory));
log(ERROR, "If you've built Cyphesis yourself make sure you've run the 'make assets-download' command.");
}
io_service* io_service = new boost::asio::io_service();
// Initialise the persistence subsystem.
std::string databaseBackend;
readConfigItem(instance, "database", databaseBackend);
Database* database;
CommPSQLSocket * dbsocket = nullptr;
RepeatedTask* dbvacuumTask = nullptr;
if (databaseBackend == "postgres") {
#if POSTGRES_FOUND
database = new DatabasePostgres();
dbsocket = new CommPSQLSocket(*io_service, *database);
#else
log(ERROR, "Database specified as 'postgres', but this server is not built with Postgres SQL support.");
return -1;
#endif
} else {
auto sqliteDatabase = new DatabaseSQLite();
database = sqliteDatabase;
dbvacuumTask = new RepeatedTask(*io_service, boost::posix_time::seconds(25 * 60), [=]() {sqliteDatabase->runMaintainance();});
}
auto persistence = new Persistence(*database);
int dbstatus = persistence->init();
if (dbstatus < 0) {
log(ERROR, "Error opening database.");
return -1;
}
// If the restricted flag is set in the config file, then we
// don't allow connecting users to create accounts. Accounts must
// be created manually by the server administrator.
if (restricted_flag) {
log(INFO, "Setting restricted mode.");
}
readConfigItem(instance, "inittime", timeoffset);
2001-06-04 11:22:27 +00:00
std::string server_name;
if (readConfigItem(instance, "servername", server_name) != 0) {
if (instance == CYPHESIS) {
server_name = get_hostname();
} else {
server_name = instance;
}
}
int nice = 1;
readConfigItem(instance, "nice", nice);
boost::asio::signal_set* signalSet = new boost::asio::signal_set(*io_service);
//If we're not running as a daemon we should use the interactive signal handler.
if (!daemon_flag) {
signalSet->add(SIGINT);
signalSet->add(SIGTERM);
signalSet->add(SIGHUP);
signalSet->add(SIGQUIT);
signalSet->async_wait(std::bind(interactiveSignalsHandler, std::ref(*signalSet), std::placeholders::_1, std::placeholders::_2));
} else {
signalSet->add(SIGTERM);
signalSet->async_wait(std::bind(daemonSignalsHandler, std::ref(*signalSet), std::placeholders::_1, std::placeholders::_2));
}
2014-01-25 20:29:45 +01:00
FileSystemObserver* file_system_observer = new FileSystemObserver(*io_service);
2018-04-26 19:28:20 +02:00
AssetsManager* assets_manager = new AssetsManager(*file_system_observer);
assets_manager->init();
// Start up the Python subsystem.
2018-05-04 19:17:55 +02:00
init_python_api(ruleset_name);
observe_python_directories(*io_service, *assets_manager);
Inheritance* inheritance = new Inheritance();
SystemTime time{};
2011-10-31 11:19:33 +01:00
time.update();
EntityBuilder::init();
2018-05-10 00:21:30 +02:00
Ruleset* ruleset = new Ruleset(EntityBuilder::instance(), *io_service);
ruleset->loadRules(ruleset_name);
WorldRouter * world = new WorldRouter(time);
PossessionAuthenticator::init();
StorageManager * store = new StorageManager(*world);
// This ID is currently generated every time, but should perhaps be
// persistent in future.
std::string server_id, lobby_id;
long int_id, lobby_int_id;
if (((int_id = newId(server_id)) < 0)
|| ((lobby_int_id = newId(lobby_id)) < 0)) {
log(CRITICAL, "Unable to get server IDs from Database");
return EXIT_DATABASE_ERROR;
}
2012-02-06 20:52:39 +00:00
// Create the core server object, which stores central data,
// and track objects
ServerRouting * server = new ServerRouting(*world, ruleset_name,
server_name, server_id, int_id, lobby_id, lobby_int_id);
std::function<void(CommAsioClient<ip::tcp>&)> tcpAtlasStarter = [&](CommAsioClient<ip::tcp>& client) {
std::string connection_id;
long c_iid = newId(connection_id);
//Turn off Nagle's algorithm to increase responsiveness.
client.getSocket().set_option(ip::tcp::no_delay(true));
2017-08-08 13:59:53 +02:00
//Listen to both ipv4 and ipv6
//client.getSocket().set_option(boost::asio::ip::v6_only(false));
client.startAccept(new Connection(client, *server, "", connection_id, c_iid));
};
std::list<CommAsioListener<ip::tcp, CommAsioClient<ip::tcp>>> tcp_atlas_clients;
if (client_port_num < 0) {
client_port_num = dynamic_port_start;
for (; client_port_num <= dynamic_port_end; client_port_num++) {
try {
tcp_atlas_clients.emplace_back(tcpAtlasStarter, server->getName(), *io_service,
2017-08-08 13:59:53 +02:00
ip::tcp::endpoint(ip::tcp::v6(), client_port_num));
} catch (const std::exception& e) {
break;
}
}
if (client_port_num < dynamic_port_end) {
log(ERROR,
String::compose("Could not find free client listen "
"socket in range %1-%2. Init failed.",
dynamic_port_start, dynamic_port_end));
log(INFO,
String::compose("To allocate 8 more ports please run:"
"\n\n cyconfig "
"--cyphesis:dynamic_port_end=%1\n\n",
dynamic_port_end + 8));
return EXIT_PORT_ERROR;
}
log(INFO, String::compose("Auto configuring new instance \"%1\" "
"to use port %2.", instance, client_port_num));
global_conf->setItem(instance, "tcpport", client_port_num,
varconf::USER);
global_conf->setItem(CYPHESIS, "dynamic_port_start",
client_port_num + 1, varconf::USER);
} else {
try {
2017-08-08 13:59:53 +02:00
tcp_atlas_clients.emplace_back(tcpAtlasStarter, server->getName(), *io_service, ip::tcp::endpoint(ip::tcp::v6(), client_port_num));
} catch (const std::exception& e) {
log(ERROR, String::compose("Could not create client listen socket "
2014-10-01 19:45:42 +02:00
"on port %1. Init failed. The most common reason for this "
"is that you're already running an instance of Cyphesis.",
client_port_num));
return EXIT_SOCKET_ERROR;
}
}
remove(python_socket_name.c_str());
std::function<void(CommPythonClient&)> pythonStarter =
[&](CommPythonClient& client) {
client.startAccept();
};
auto pythonListener = new CommAsioListener<local::stream_protocol, CommPythonClient>(pythonStarter, server->getName(), *io_service,
local::stream_protocol::endpoint(python_socket_name));
remove(client_socket_name.c_str());
std::function<void(CommAsioClient<local::stream_protocol>&)> localStarter = [&](CommAsioClient<local::stream_protocol>& client) {
std::string connection_id;
long c_iid = newId(connection_id);
client.startAccept(new TrustedConnection(client, *server, "", connection_id, c_iid));
};
auto localListener = new CommAsioListener<local::stream_protocol, CommAsioClient<local::stream_protocol>>(localStarter, server->getName(), *io_service,
local::stream_protocol::endpoint(client_socket_name));
//Instantiate at startup
HttpCache::instance();
std::function<void(CommHttpClient&)> httpStarter = [&](CommHttpClient& client) {
2017-08-08 13:59:53 +02:00
//Listen to both ipv4 and ipv6
//client.getSocket().set_option(boost::asio::ip::v6_only(false));
client.serveRequest();
};
2014-01-29 13:08:52 +01:00
auto httpListener = new CommAsioListener<ip::tcp, CommHttpClient>(httpStarter, server->getName(), *io_service,
2017-08-08 13:59:53 +02:00
ip::tcp::endpoint(ip::tcp::v6(), http_port_num));
2015-04-10 21:58:31 +02:00
log(INFO, compose("Http service. The following endpoints are available over port %1.", http_port_num));
log(INFO, " /config : shows server configuration");
log(INFO, " /monitors : various monitored values, suitable for time series systems");
log(INFO, " /monitors/numerics : only numerical values, suitable for time series system that only operates on numerical data");
CommMetaClient * cmc(nullptr);
if (useMetaserver) {
cmc = new CommMetaClient(*io_service);
if (cmc->setup(mserver) != 0) {
log(ERROR, "Error creating metaserver comm channel.");
delete cmc;
cmc = nullptr;
2001-06-04 11:22:27 +00:00
}
}
2002-12-28 21:52:32 +00:00
CommMDNSPublisher * cmdns = nullptr;
#if defined(HAVE_AVAHI)
cmdns = new CommMDNSPublisher(*io_service, *server);
2014-01-31 13:36:03 +01:00
if (cmdns->setup() != 0) {
log(ERROR, "Unable to register service with MDNS daemon.");
delete cmdns;
}
#endif // defined(HAVE_AVAHI)
// Configuration is now complete, and verified as somewhat sane, so
// we save the updated user config.
IdleConnector* storage_idle = nullptr;
log(INFO, "Restoring world from database...");
store->restoreWorld();
// Read the world entity if any from the database, or set it up.
// If it was there, make sure it did not get any of the wrong
// position or orientation data.
store->initWorld();
log(INFO, "Restored world.");
storage_idle = new IdleConnector(*io_service);
storage_idle->idling.connect(
sigc::mem_fun(store, &StorageManager::tick));
updateUserConfiguration();
log(INFO, "Running");
logEvent(START, "- - - Standalone server startup");
// Inform things that want to know that we are running.
running();
// Reduce our system priority to make it easier to debug a runaway
// server.
if (nice != 0) {
reduce_priority(nice);
}
//Check if the world is populated; if it's not we should perhaps import some entities into it.
//Note that we only check the top level; we don't perform a full hierarchical
//traversal. Mainly because we never need to.
auto autoImport = global_conf->getItem(CYPHESIS, "autoimport");
if (autoImport.is_string() && !autoImport.as_string().empty()) {
std::string importPath = autoImport.as_string();
std::ifstream file(importPath);
if (file.good()) {
file.close();
//We should only try to import if the world isn't populated.
bool isPopulated = false;
if (world->getRootEntity().m_contains) {
for (auto entity : *world->getRootEntity().m_contains) {
//if there's any entity that's not transient we consider it populated
if (!entity->hasAttr("transient")) {
isPopulated = true;
break;
}
}
}
if (!isPopulated) {
//Populate the server through separate process (mainly because it's easier as we've
//already written the importer tool; we might also do it in-process, but that would
//require some rewriting of code).
2013-11-24 23:10:04 +01:00
log(INFO,
compose("Trying to import world from %1.", importPath));
std::stringstream ss;
ss << bin_directory <<
"/cyimport --resume \"" << importPath + "\"";
std::string command = ss.str();
std::thread importer([=]() {
int result = std::system(command.c_str());
if (result == 0) {
log(INFO, "Imported world into empty server.");
} else {
log(INFO, "No world imported.");
}
});
importer.detach();
}
} else {
log(NOTICE,
2013-11-24 23:10:04 +01:00
compose("Not importing as \"%1\" could not be found",
importPath));
file.close();
}
}
bool soft_exit_in_progress = false;
{
//Make sure that the io_service never runs out of work.
boost::asio::io_service::work work(*io_service);
//This timer is used to wake the io_service when next op needs to be handled.
boost::asio::deadline_timer nextOpTimer(*io_service);
//This timer will set a deadline for any mind persistence during soft exits.
boost::asio::deadline_timer softExitTimer(*io_service);
// Loop until the exit flag is set. The exit flag can be set anywhere in
// the code easily.
while (!exit_flag) {
try {
time.update();
bool busy = world->idle();
world->markQueueAsClean();
//If the world is busy we should just poll.
if (busy) {
io_service->poll();
} else {
//If it's not busy however we should run until we get a task.
//We will either get an io task, or we will be triggered by the timer
//which is set to expire when the next op should be dispatched.
double secondsUntilNextOp = world->secondsUntilNextOp();
if (secondsUntilNextOp <= 0.0) {
io_service->poll();
} else {
bool nextOpTimeExpired = false;
2017-05-27 20:27:48 +02:00
boost::posix_time::microseconds waitTime((int64_t)(secondsUntilNextOp * 1000000L));
nextOpTimer.expires_from_now(waitTime);
nextOpTimer.async_wait([&](boost::system::error_code ec){
if (ec != boost::asio::error::operation_aborted) {
nextOpTimeExpired = true;
}
});
//Keep on running IO handlers until either the queue is dirty (i.e. we need to handle
//any new operation) or the timer has expired.
do {
io_service->run_one();
} while (!world->isQueueDirty() && !nextOpTimeExpired &&
!exit_flag_soft && !exit_flag && !soft_exit_in_progress);
nextOpTimer.cancel();
}
}
if (soft_exit_in_progress) {
//If we're in soft exit mode and either the deadline has been exceeded
//or we've persisted all minds we should shut down normally.
if (store->numberOfOutstandingThoughtRequests() == 0) {
log(NOTICE, "All entity thoughts were persisted.");
exit_flag = true;
softExitTimer.cancel();
}
} else if (exit_flag_soft) {
exit_flag_soft = false;
soft_exit_in_progress = true;
size_t requestNumber = store->requestMinds(
world->getEntities());
log(INFO,
String::compose(
"Soft exit requested, persisting %1 minds.",
requestNumber));
//Set a deadline for five seconds.
softExitTimer.expires_from_now(boost::posix_time::seconds(5));
softExitTimer.async_wait([&](boost::system::error_code ec){
if (!ec) {
log(WARNING,
String::compose("Waiting for persisting thoughts timed out. This might "
"lead to lost entity thoughts. %1 thoughts outstanding.",
store->numberOfOutstandingThoughtRequests()));
exit_flag = true;
}
});
log(NOTICE,
String::compose(
"Deadline for mind persistence set to %1 seconds.",
5));
}
// It is hoped that commonly thrown exception, particularly
// exceptions that can be caused by external influences
// should be caught close to where they are thrown. If
// an exception makes it here then it should be debugged.
} catch (const std::exception& e) {
log(ERROR,
String::compose("Exception caught in main(): %1",
e.what()));
} catch (...) {
log(ERROR, "Exception caught in main()");
}
}
// exit flag has been set so we close down the databases, and indicate
// to the metaserver (if we are using one) that this server is going down.
// It is assumed that any preparation for the shutdown that is required
// by the game has been done before exit flag was set.
log(NOTICE, "Performing clean shutdown...");
//Actually, there's no way for the world to know that it's shutting down,
//as the shutdown signal most probably comes from a sighandler. We need to
//tell it it's shutting down so it can do some housekeeping.
try {
exit_flag = false;
if (store->shutdown(exit_flag, world->getEntities()) != 0) {
//Ignore this error and carry on with shutting down.
log(ERROR, "Error when shutting down");
}
} catch (const std::exception& e) {
log(ERROR,
String::compose("Exception caught when shutting down: %1",
e.what()));
} catch (...) {
//Ignore this error and carry on with shutting down.
log(ERROR, "Exception caught when shutting down");
}
}
delete dbvacuumTask;
2018-04-26 19:28:20 +02:00
delete assets_manager;
delete file_system_observer;
signalSet->clear();
delete signalSet;
delete cmdns;
if (cmc) {
cmc->metaserverTerminate();
delete cmc;
}
2014-02-17 23:22:17 +01:00
delete localListener;
delete httpListener;
HttpCache::del();
2014-02-17 23:22:17 +01:00
delete pythonListener;
tcp_atlas_clients.clear();
delete storage_idle;
delete dbsocket;
//Run any outstanding tasks before shutting down service.
io_service->run();
delete io_service;
delete server;
delete store;
delete world;
2018-05-10 00:21:30 +02:00
delete ruleset;
2014-02-17 23:22:17 +01:00
delete persistence;
delete database;
EntityBuilder::instance()->flushFactories();
EntityBuilder::del();
ArithmeticBuilder::del();
PossessionAuthenticator::del();
delete inheritance;
2013-08-22 10:47:08 +02:00
// Shutdown the Python interpreter. This frees lots of memory, and if
// the malloc heap is in any way corrupt, a segfault is likely to
2013-08-22 10:47:08 +02:00
// occur at this point. Previous occasions where pointers have been
// deleted twice elsewhere in the code, have resulted in a segfault
// at this point. AlRiddoch 10th November 2001
* aiclient/ClientConnection.cpp, aiclient/client.cpp, client/ClientConnection.cpp, common/database.h, common/globals.cpp, common/persistance.cpp, common/stringstream.h, server/CommClient.cpp, server/CommServer.cpp, server/Persistance.cpp, tools/cyctrl.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cyisoload.cpp, tools/cypasswd.cpp: Removed unused config.h. * client/ObserverClient.cpp, common/const.h, tools/cycmd.cpp: Use default password if the admin account cannot be fetched from the database. * client/Py_CreatorClient.cpp, common/BaseEntity.cpp, common/BaseEntity.h, rulesets/Character.cpp, rulesets/Creator.cpp, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityFactory.cpp, rulesets/MemMap.cpp, rulesets/Py_Mind.cpp, rulesets/Py_Thing.cpp, rulesets/Py_World.cpp, rulesets/Thing.cpp, server/Account.cpp, server/Admin.cpp, server/Connection.cpp, tools/cywatchdog.cpp: Make many of the member variables of Entity protected, and add the required accessors. Changed the name of the id attribute to m_id. * common/BaseWorld.h, common/database.cpp, common/persistance.cpp: Removed unused includes. * common/Chop.h, common/Cut.h, common/Eat.h, common/Fire.h, common/Generic.h, common/Load.h, common/Nourish.h, common/Save.h, common/Setup.h, common/Tick.h: Moved definition of virtual functions into cpp file, to reduce wasted code. * common/Makefile.am: Create cyphesis library, which can be used by other applications to access cyphesis databases. * common/config.h, common/database.h, server/Persistance.cpp, tools/cydbload.cpp, tools/cyisoload.cpp, tools/cypasswd.cpp: Remove conditional compilation of db code. db3 is now required. * server/WorldRouter.h: Fixed getNewId() so it take const string. * server/server.cpp: Removed python shutdown, so it does not mess up the db if it causes a segfault. Al Riddoch <alriddoch@zepler.org>
2001-10-17 14:09:42 +00:00
shutdown_python_api();
delete global_conf;
2015-04-06 21:47:55 +02:00
Monitors::cleanup();
log(INFO, "Clean shutdown complete.");
logEvent(STOP, "- - - Standalone server shutdown");
return 0;
}