1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Application;
13:
14: use DedicatedApi\Connection;
15: use ManiaLive\Config\Loader;
16: use ManiaLive\Event\Dispatcher;
17:
18: abstract class AbstractApplication extends \ManiaLib\Utils\Singleton
19: {
20: const CYCLES_PER_SECOND = 60;
21:
22: static $startTime;
23:
24: protected $running = true;
25:
26: protected $connection;
27:
28: protected function __construct()
29: {
30: set_error_handler('\ManiaLive\Application\ErrorHandling::createExceptionFromError');
31: if(extension_loaded('pcntl'))
32: {
33: pcntl_signal(SIGTERM, array($this, 'kill'));
34: pcntl_signal(SIGINT, array($this, 'kill'));
35: declare(ticks = 1);
36: }
37:
38: try
39: {
40: $configFile = CommandLineInterpreter::preConfigLoad();
41:
42:
43: $loader = Loader::getInstance();
44: $loader->setConfigFilename(APP_ROOT.'config'.DIRECTORY_SEPARATOR.$configFile);
45: $loader->run();
46:
47:
48: CommandLineInterpreter::postConfigLoad();
49:
50:
51: $manialiveConfig = \ManiaLive\Config\Config::getInstance();
52: $serverConfig = \ManiaLive\DedicatedApi\Config::getInstance();
53: if($manialiveConfig->logsPrefix != null)
54: {
55: $manialiveConfig->logsPrefix = str_replace('%ip%', str_replace('.', '-', $serverConfig->host), $manialiveConfig->logsPrefix);
56: $manialiveConfig->logsPrefix = str_replace('%port%', $serverConfig->port, $manialiveConfig->logsPrefix);
57: }
58:
59:
60: if(!$manialiveConfig->runtimeLog)
61: \ManiaLive\Utilities\Logger::getLog('runtime')->disableLog();
62: }
63: catch(\Exception $e)
64: {
65:
66: ErrorHandling::processStartupException($e);
67: }
68: }
69:
70: protected function init()
71: {
72: new \ManiaLive\Features\Tick\Ticker();
73: $config = \ManiaLive\DedicatedApi\Config::getInstance();
74: $this->connection = Connection::factory(
75: $config->host,
76: $config->port,
77: $config->timeout,
78: $config->user,
79: $config->password
80: );
81: $this->connection->enableCallbacks(true);
82: \ManiaLive\Data\Storage::getInstance();
83: \ManiaLive\Features\ChatCommand\Interpreter::getInstance();
84: \ManiaLive\Features\EchoHandler::getInstance();
85: \ManiaLive\Gui\GuiHandler::getInstance();
86: \ManiaLive\PluginHandler\PluginHandler::getInstance();
87: \ManiaLive\Threading\ThreadHandler::getInstance();
88:
89: Dispatcher::dispatch(new Event(Event::ON_INIT));
90: }
91:
92: final function run()
93: {
94: try
95: {
96: $this->init();
97:
98: Dispatcher::dispatch(new Event(Event::ON_RUN));
99: self::$startTime = microtime(true);
100: $nextCycleStart = self::$startTime;
101: $cycleTime = 1 / static::CYCLES_PER_SECOND;
102:
103: while($this->running)
104: {
105: Dispatcher::dispatch(new Event(Event::ON_PRE_LOOP));
106: $calls = $this->connection->executeCallbacks();
107: if(!empty($calls))
108: {
109: foreach($calls as $call)
110: {
111: $method = preg_replace('/^[[:alpha:]]+\./', '', $call[0]);
112: $params = (array) $call[1];
113: Dispatcher::dispatch(new \ManiaLive\DedicatedApi\Callback\Event($method, $params));
114: }
115: }
116: $this->connection->executeMulticall();
117: Dispatcher::dispatch(new Event(Event::ON_POST_LOOP));
118:
119: $endCycleTime = microtime(true) + $cycleTime / 10;
120: do
121: {
122: $nextCycleStart += $cycleTime;
123: }
124: while($nextCycleStart < $endCycleTime);
125: @time_sleep_until($nextCycleStart);
126: }
127: }
128: catch(\Exception $e)
129: {
130: ErrorHandling::processRuntimeException($e);
131: }
132:
133: Dispatcher::dispatch(new Event(Event::ON_TERMINATE));
134: }
135:
136: function kill()
137: {
138: if($this->connection) $this->connection->manualFlowControlEnable(false);
139: $this->running = false;
140: }
141:
142: }
143:
144: ?>