improved transport exception handling

This commit is contained in:
Steffen Schröder 2014-08-28 16:25:04 +02:00
parent e56c4dccb5
commit 769015c8d8

View File

@ -211,26 +211,24 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
* @param bool $errorPrefix * @param bool $errorPrefix
*/ */
public function quit($message = null, $errorPrefix = false) { public function quit($message = null, $errorPrefix = false) {
if ($this->getCallbackManager()) {
// OnShutdown callback
$this->getCallbackManager()->triggerCallback(Callbacks::ONSHUTDOWN);
}
if ($this->getChat()) {
// Announce quit
$this->getChat()->sendInformation('ManiaControl shutting down.');
}
if ($this->getClient()) { if ($this->getClient()) {
try { if ($this->getCallbackManager()) {
$this->getClient()->sendHideManialinkPage(); // OnShutdown callback
} catch (TransportException $e) { $this->getCallbackManager()->triggerCallback(Callbacks::ONSHUTDOWN);
$this->getErrorHandler()->handleException($e, false);
} }
}
Connection::delete($this->getClient()); if ($this->getChat()) {
$this->client = null; // Announce quit
$this->getChat()->sendInformation('ManiaControl shutting down.');
}
// Hide UI
$this->getClient()->sendHideManialinkPage();
// Delete client
Connection::delete($this->getClient());
$this->client = null;
}
SystemUtil::quit($message, $errorPrefix); SystemUtil::quit($message, $errorPrefix);
} }
@ -244,6 +242,24 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
return $this->client; return $this->client;
} }
/**
* Return the callback manager
*
* @return CallbackManager
*/
public function getCallbackManager() {
return $this->callbackManager;
}
/**
* Return the chat
*
* @return Chat
*/
public function getChat() {
return $this->chat;
}
/** /**
* Return the error handler * Return the error handler
* *
@ -386,8 +402,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
* Check connection * Check connection
*/ */
public function checkConnection() { public function checkConnection() {
if ($this->getClient()->getIdleTime() > 180 if ($this->getClient()->getIdleTime() > 180) {
) {
$this->getClient()->getServerName(); $this->getClient()->getServerName();
} }
} }
@ -403,15 +418,6 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
$this->getChat()->sendInformation($message, $player); $this->getChat()->sendInformation($message, $player);
} }
/**
* Return the chat
*
* @return Chat
*/
public function getChat() {
return $this->chat;
}
/** /**
* Handle Restart AdminCommand * Handle Restart AdminCommand
* *
@ -419,8 +425,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
* @param Player $player * @param Player $player
*/ */
public function commandRestart(array $chatCallback, Player $player) { public function commandRestart(array $chatCallback, Player $player) {
if (!$this->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_RESTART) if (!$this->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_RESTART)) {
) {
$this->getAuthenticationManager()->sendNotAllowed($player); $this->getAuthenticationManager()->sendNotAllowed($player);
return; return;
} }
@ -433,19 +438,9 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
* @param string $message * @param string $message
*/ */
public function restart($message = null) { public function restart($message = null) {
// Shutdown callback
$this->getCallbackManager()->triggerCallback(Callbacks::ONSHUTDOWN);
// Announce restart // Announce restart
if ($message) { $this->getChat()->sendInformation('Restarting ManiaControl...');
Logger::log($message); Logger::log('Restarting ManiaControl... ' . $message);
}
try {
$this->getChat()->sendInformation('Restarting ManiaControl...');
$this->getClient()->sendHideManialinkPage();
} catch (TransportException $exception) {
}
Logger::log('Restarting ManiaControl!');
// Start new instance // Start new instance
SystemUtil::restart(); SystemUtil::restart();
@ -454,15 +449,6 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
$this->quit('Quitting ManiaControl to restart.'); $this->quit('Quitting ManiaControl to restart.');
} }
/**
* Return the callback manager
*
* @return CallbackManager
*/
public function getCallbackManager() {
return $this->callbackManager;
}
/** /**
* Handle Shutdown Command * Handle Shutdown Command
* *
@ -492,44 +478,47 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
public function run() { public function run() {
Logger::log('Starting ManiaControl v' . self::VERSION . '!'); Logger::log('Starting ManiaControl v' . self::VERSION . '!');
// Connect to server
try { try {
// Connect to server
$this->connect(); $this->connect();
} catch (TransportException $e) {
$this->quit($e->getMessage(), true); // Check if the version of the server is high enough
$version = $this->getClient()->getVersion();
if ($version->build < self::MIN_DEDIVERSION) {
$this->quit("The Server has Version '{$version->build}', while at least '" . self::MIN_DEDIVERSION . "' is required!", true);
}
// Listen for shutdown
$this->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_SERVERSTOP, $this, 'handleServerStopCallback');
// OnInit callback
$this->getCallbackManager()->triggerCallback(Callbacks::ONINIT);
// Load plugins
$this->getPluginManager()->loadPlugins();
$this->getUpdateManager()->getPluginUpdateManager()->checkPluginsUpdate();
// AfterInit callback
$this->getCallbackManager()->triggerCallback(Callbacks::AFTERINIT);
// Loading finished
Logger::log('Loading completed!');
Logger::log('Link: ' . $this->getServer()->getJoinLink());
$this->getChat()->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
// Main loop
while (!$this->requestQuitMessage) {
$this->loop();
}
// Shutdown
$this->quit($this->requestQuitMessage);
} catch (TransportException $exception) {
Logger::logError('Connection interrupted!');
$this->getErrorHandler()->handleException($exception);
SystemUtil::quit($exception->getMessage(), true);
} }
// Check if the version of the server is high enough
$version = $this->getClient()->getVersion();
if ($version->build < self::MIN_DEDIVERSION) {
$this->quit("The Server has Version '{$version->build}', while at least '" . self::MIN_DEDIVERSION . "' is required!", true);
}
// Listen for shutdown
$this->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_SERVERSTOP, $this, 'handleServerStopCallback');
// OnInit callback
$this->getCallbackManager()->triggerCallback(Callbacks::ONINIT);
// Load plugins
$this->getPluginManager()->loadPlugins();
$this->getUpdateManager()->getPluginUpdateManager()->checkPluginsUpdate();
// AfterInit callback
$this->getCallbackManager()->triggerCallback(Callbacks::AFTERINIT);
// Loading finished
Logger::log('Loading completed!');
Logger::log('Link: ' . $this->getServer()->getJoinLink());
$this->getChat()->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
// Main loop
while (!$this->requestQuitMessage) {
$this->loop();
}
// Shutdown
$this->quit($this->requestQuitMessage);
} }
/** /**
@ -555,8 +544,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
$this->getClient()->enableCallbacks(true); $this->getClient()->enableCallbacks(true);
// Wait for server to be ready // Wait for server to be ready
if (!$this->getServer()->waitForStatus(4) if (!$this->getServer()->waitForStatus(4)) {
) {
$this->quit("Server couldn't get ready!"); $this->quit("Server couldn't get ready!");
} }
@ -606,14 +594,10 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
// Extend script timeout // Extend script timeout
set_time_limit(self::SCRIPT_TIMEOUT); set_time_limit(self::SCRIPT_TIMEOUT);
try { // Manage callbacks
$this->getCallbackManager()->manageCallbacks(); $this->getCallbackManager()->manageCallbacks();
} catch (TransportException $e) {
Logger::logError('Connection interrupted!');
$this->quit($e->getMessage(), true);
}
// Manage FileReader // Manage async file reader
$this->getFileReader()->appendData(); $this->getFileReader()->appendData();
// Yield for next tick // Yield for next tick