renamed SocketManager to CommunicationManager
This commit is contained in:
parent
8a58035a57
commit
687bb7be1b
@ -211,7 +211,7 @@ class CallbackManager {
|
|||||||
$this->maniaControl->getTimerManager()->manageTimings();
|
$this->maniaControl->getTimerManager()->manageTimings();
|
||||||
|
|
||||||
// Manage Socket Tickets
|
// Manage Socket Tickets
|
||||||
$this->maniaControl->getSocketManager()->tick();
|
$this->maniaControl->getCommunicationManager()->tick();
|
||||||
|
|
||||||
// Server Callbacks
|
// Server Callbacks
|
||||||
if (!$this->maniaControl->getClient()) {
|
if (!$this->maniaControl->getClient()) {
|
||||||
|
@ -5,9 +5,9 @@ namespace ManiaControl;
|
|||||||
use ManiaControl\Admin\AuthenticationManager;
|
use ManiaControl\Admin\AuthenticationManager;
|
||||||
use ManiaControl\Callbacks\CallbackListener;
|
use ManiaControl\Callbacks\CallbackListener;
|
||||||
use ManiaControl\Callbacks\CallbackManager;
|
use ManiaControl\Callbacks\CallbackManager;
|
||||||
|
use ManiaControl\Communication\CommunicationListener;
|
||||||
|
use ManiaControl\Communication\CommunicationMethods;
|
||||||
use ManiaControl\Players\Player;
|
use ManiaControl\Players\Player;
|
||||||
use ManiaControl\Sockets\SocketListener;
|
|
||||||
use ManiaControl\Sockets\SocketMethods;
|
|
||||||
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
|
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,7 +17,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
|
|||||||
* @copyright 2014-2015 ManiaControl Team
|
* @copyright 2014-2015 ManiaControl Team
|
||||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
*/
|
*/
|
||||||
class Chat implements CallbackListener, SocketListener {
|
class Chat implements CallbackListener, CommunicationListener {
|
||||||
/*
|
/*
|
||||||
* Constants
|
* Constants
|
||||||
*/
|
*/
|
||||||
@ -53,7 +53,7 @@ class Chat implements CallbackListener, SocketListener {
|
|||||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'onPlayerChat');
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'onPlayerChat');
|
||||||
|
|
||||||
//Socket Listenings
|
//Socket Listenings
|
||||||
$this->maniaControl->getSocketManager()->registerSocketListener(SocketMethods::GET_SERVER_CHAT, $this, function ($data) {
|
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_SERVER_CHAT, $this, function ($data) {
|
||||||
return $this->chatBuffer;
|
return $this->chatBuffer;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace ManiaControl\Sockets;
|
namespace ManiaControl\Communication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for SocketListener
|
* Interface for SocketListener
|
||||||
@ -9,5 +9,5 @@ namespace ManiaControl\Sockets;
|
|||||||
* @copyright 2014-2015 ManiaControl Team
|
* @copyright 2014-2015 ManiaControl Team
|
||||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
*/
|
*/
|
||||||
interface SocketListener {
|
interface CommunicationListener {
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ManiaControl\Sockets;
|
namespace ManiaControl\Communication;
|
||||||
|
|
||||||
use ManiaControl\Callbacks\CallbackListener;
|
use ManiaControl\Callbacks\CallbackListener;
|
||||||
use ManiaControl\Callbacks\Callbacks;
|
use ManiaControl\Callbacks\Callbacks;
|
||||||
@ -21,7 +21,7 @@ use React\Socket\Server;
|
|||||||
* @copyright 2014-2015 ManiaControl Team
|
* @copyright 2014-2015 ManiaControl Team
|
||||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
*/
|
*/
|
||||||
class SocketManager implements CallbackListener {
|
class CommunicationManager implements CallbackListener {
|
||||||
|
|
||||||
/** @var ManiaControl $maniaControl */
|
/** @var ManiaControl $maniaControl */
|
||||||
private $maniaControl = null;
|
private $maniaControl = null;
|
||||||
@ -29,8 +29,8 @@ class SocketManager implements CallbackListener {
|
|||||||
/** @var LoopInterface $loop */
|
/** @var LoopInterface $loop */
|
||||||
private $loop = null;
|
private $loop = null;
|
||||||
|
|
||||||
/** @var Listening[] $socketListenings */
|
/** @var Listening[] $communicationListenings */
|
||||||
private $socketListenings = array();
|
private $communicationListenings = array();
|
||||||
|
|
||||||
/** @var Server $socket */
|
/** @var Server $socket */
|
||||||
private $socket = null;
|
private $socket = null;
|
||||||
@ -40,7 +40,7 @@ class SocketManager implements CallbackListener {
|
|||||||
const SETTING_SOCKET_PORT = "Socket Port for Server ";
|
const SETTING_SOCKET_PORT = "Socket Port for Server ";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Socket Handler Instance
|
* Create a new Communication Handler Instance
|
||||||
*
|
*
|
||||||
* @param ManiaControl $maniaControl
|
* @param ManiaControl $maniaControl
|
||||||
*/
|
*/
|
||||||
@ -48,30 +48,30 @@ class SocketManager implements CallbackListener {
|
|||||||
$this->maniaControl = $maniaControl;
|
$this->maniaControl = $maniaControl;
|
||||||
|
|
||||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings');
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings');
|
||||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'initSocketManager');
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'initCommunicationManager');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a new Socket Listener
|
* Register a new Communication Listener
|
||||||
*
|
*
|
||||||
* @param string $callbackName
|
* @param string $callbackName
|
||||||
* @param SocketListener $listener
|
* @param CommunicationListener $listener
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function registerSocketListener($echoName, SocketListener $listener, $method) {
|
public function registerCommunicationListener($echoName, CommunicationListener $listener, $method) {
|
||||||
if (!Listening::checkValidCallback($listener, $method)) {
|
if (!Listening::checkValidCallback($listener, $method)) {
|
||||||
$listenerClass = get_class($listener);
|
$listenerClass = get_class($listener);
|
||||||
trigger_error("Given Listener '{$listenerClass}' can't handle Callback '{$echoName}': No callable Method '{$method}'!");
|
trigger_error("Given Listener '{$listenerClass}' can't handle Callback '{$echoName}': No callable Method '{$method}'!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!array_key_exists($echoName, $this->socketListenings)) {
|
if (!array_key_exists($echoName, $this->communicationListenings)) {
|
||||||
$this->socketListenings[$echoName] = new Listening($listener, $method);
|
$this->communicationListenings[$echoName] = new Listening($listener, $method);
|
||||||
} else {
|
} else {
|
||||||
//TODO say which is already listening and other stuff
|
//TODO say which is already listening and other stuff
|
||||||
trigger_error("Only one Listener can listen on a specific Socket Message");
|
trigger_error("Only one Listener can listen on a specific Communication Message");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -82,39 +82,39 @@ class SocketManager implements CallbackListener {
|
|||||||
*
|
*
|
||||||
* @param mixed $callback
|
* @param mixed $callback
|
||||||
*/
|
*/
|
||||||
public function triggerSocketCallback($callbackName) {
|
public function triggerCommuncationCallback($callbackName) {
|
||||||
if (!array_key_exists($callbackName, $this->socketListenings)) {
|
if (!array_key_exists($callbackName, $this->communicationListenings)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = func_get_args();
|
$params = func_get_args();
|
||||||
$params = array_slice($params, 1, null, true);
|
$params = array_slice($params, 1, null, true);
|
||||||
|
|
||||||
$listening = $this->socketListenings[$callbackName];
|
$listening = $this->communicationListenings[$callbackName];
|
||||||
/** @var Listening $listening */
|
/** @var Listening $listening */
|
||||||
return $listening->triggerCallbackWithParams($params);
|
return $listening->triggerCallbackWithParams($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregister a Socket Listener
|
* Unregister a Communication Listener
|
||||||
*
|
*
|
||||||
* @param SocketListener $listener
|
* @param CommunicationListener $listener
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function unregisterEchoListener(SocketListener $listener) {
|
public function unregisterCommunicationListener(CommunicationListener $listener) {
|
||||||
return $this->removeSocketListener($this->socketListenings, $listener);
|
return $this->removeCommunicationListener($this->communicationListenings, $listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the Socket Listener from the given Listeners Array
|
* Remove the Communication Listener from the given Listeners Array
|
||||||
*
|
*
|
||||||
* @param Listening[] $listeningsArray
|
* @param Listening[] $listeningsArray
|
||||||
* @param SocketListener $listener
|
* @param CommunicationListener $listener
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function removeSocketListener(array &$listeningsArray, SocketListener $listener) {
|
private function removeCommunicationListener(array &$listeningsArray, CommunicationListener $listener) {
|
||||||
$removed = false;
|
$removed = false;
|
||||||
foreach ($listeningsArray as &$listening) {
|
foreach ($listeningsArray as &$listening) {
|
||||||
if ($listening->listener === $listener) {
|
if ($listening->listener === $listener) {
|
||||||
@ -126,9 +126,9 @@ class SocketManager implements CallbackListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inits the Socket Manager after ManiaControl Startup
|
* Inits the Communication Manager after ManiaControl Startup
|
||||||
*/
|
*/
|
||||||
public function initSocketManager() {
|
public function initCommunicationManager() {
|
||||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_ENABLED, false);
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_ENABLED, false);
|
||||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PASSWORD, "");
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PASSWORD, "");
|
||||||
|
|
||||||
@ -169,10 +169,10 @@ class SocketManager implements CallbackListener {
|
|||||||
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
|
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
|
||||||
if ($socketEnabled) {
|
if ($socketEnabled) {
|
||||||
|
|
||||||
Logger::log("[SocketManager] Trying to create Socket");
|
Logger::log("[CommunicationManager] Trying to create Socket");
|
||||||
|
|
||||||
// Check for MySQLi
|
// Check for MySQLi
|
||||||
$message = '[SocketManager] Checking for installed openssl ... ';
|
$message = '[CommunicationManager] Checking for installed openssl ... ';
|
||||||
if (!extension_loaded('openssl')) {
|
if (!extension_loaded('openssl')) {
|
||||||
Logger::log($message . 'NOT FOUND!');
|
Logger::log($message . 'NOT FOUND!');
|
||||||
Logger::log(" -- You don't have openssl installed! Check: http://www.php.net/manual/en/openssl.installation.php");
|
Logger::log(" -- You don't have openssl installed! Check: http://www.php.net/manual/en/openssl.installation.php");
|
||||||
@ -183,18 +183,19 @@ class SocketManager implements CallbackListener {
|
|||||||
|
|
||||||
$serverLogin = $this->maniaControl->getServer()->login;
|
$serverLogin = $this->maniaControl->getServer()->login;
|
||||||
$socketPort = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PORT . $serverLogin);
|
$socketPort = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PORT . $serverLogin);
|
||||||
|
$password = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PASSWORD . $serverLogin);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->loop = Factory::create();
|
$this->loop = Factory::create();
|
||||||
$this->socket = new Server($this->loop);
|
$this->socket = new Server($this->loop);
|
||||||
|
|
||||||
$this->socket->on('error', function ($e) {
|
$this->socket->on('error', function ($e) {
|
||||||
Logger::log("[SocketManager] Socket Error" . $e);
|
Logger::log("[CommunicationManager] Socket Error" . $e);
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->socket->on('connection', function (Connection $connection) {
|
$this->socket->on('connection', function (Connection $connection) use ($password) {
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
$connection->on('data', function ($data) use (&$buffer, &$connection) {
|
$connection->on('data', function ($data) use (&$buffer, &$connection, $password) {
|
||||||
$buffer .= $data;
|
$buffer .= $data;
|
||||||
$arr = explode("\n", $buffer, 2);
|
$arr = explode("\n", $buffer, 2);
|
||||||
while (count($arr) == 2 && strlen($arr[1]) >= (int) $arr[0]) {
|
while (count($arr) == 2 && strlen($arr[1]) >= (int) $arr[0]) {
|
||||||
@ -204,7 +205,7 @@ class SocketManager implements CallbackListener {
|
|||||||
$buffer = substr($buffer, strlen((string) $len) + 1 /* newline */ + $len); // clip buffer
|
$buffer = substr($buffer, strlen((string) $len) + 1 /* newline */ + $len); // clip buffer
|
||||||
|
|
||||||
// Decode Message
|
// Decode Message
|
||||||
$data = openssl_decrypt($msg, 'aes-192-cbc', 'testpass123', OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX');
|
$data = openssl_decrypt($msg, 'aes-192-cbc', $password, OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX');
|
||||||
$data = json_decode($data);
|
$data = json_decode($data);
|
||||||
|
|
||||||
if ($data == null) {
|
if ($data == null) {
|
||||||
@ -212,7 +213,7 @@ class SocketManager implements CallbackListener {
|
|||||||
} else if (!property_exists($data, "method") || !property_exists($data, "data")) {
|
} else if (!property_exists($data, "method") || !property_exists($data, "data")) {
|
||||||
$data = array("error" => true, "data" => "Invalid Message");
|
$data = array("error" => true, "data" => "Invalid Message");
|
||||||
} else {
|
} else {
|
||||||
$answer = $this->triggerSocketCallback($data->method, $data->data);
|
$answer = $this->triggerCommuncationCallback($data->method, $data->data);
|
||||||
//Prepare Response
|
//Prepare Response
|
||||||
if (!$answer) {
|
if (!$answer) {
|
||||||
$data = array("error" => true, "data" => "No listener or response on the given Message");
|
$data = array("error" => true, "data" => "No listener or response on the given Message");
|
||||||
@ -223,7 +224,7 @@ class SocketManager implements CallbackListener {
|
|||||||
|
|
||||||
//Encode, Encrypt and Send Response
|
//Encode, Encrypt and Send Response
|
||||||
$data = json_encode($data);
|
$data = json_encode($data);
|
||||||
$data = openssl_encrypt($data, 'aes-192-cbc', 'testpass123', OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX');
|
$data = openssl_encrypt($data, 'aes-192-cbc', $password, OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX');
|
||||||
$connection->write(strlen($data) . "\n" . $data);
|
$connection->write(strlen($data) . "\n" . $data);
|
||||||
|
|
||||||
// next msg
|
// next msg
|
||||||
@ -234,9 +235,9 @@ class SocketManager implements CallbackListener {
|
|||||||
//TODO check if port is closed
|
//TODO check if port is closed
|
||||||
$this->socket->listen($socketPort, getHostByName(getHostName()));
|
$this->socket->listen($socketPort, getHostByName(getHostName()));
|
||||||
|
|
||||||
Logger::log("[SocketManager] Socket " . getHostByName(getHostName()) . ":" . $this->socket->getPort() . " Successfully created!");
|
Logger::log("[CommunicationManager] Socket " . getHostByName(getHostName()) . ":" . $this->socket->getPort() . " Successfully created!");
|
||||||
} catch (ConnectionException $e) {
|
} catch (ConnectionException $e) {
|
||||||
Logger::log("[SocketManager] Exception: " . $e->getMessage());
|
Logger::log("[CommunicationManager] Exception: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace ManiaControl\Sockets;
|
namespace ManiaControl\Communication;
|
||||||
|
|
||||||
|
|
||||||
interface SocketMethods {
|
interface CommunicationMethods {
|
||||||
/** Returns the last 200 lines of the chat (inclusive player logins and nicknames) */
|
/** Returns the last 200 lines of the chat (inclusive player logins and nicknames) */
|
||||||
const GET_SERVER_CHAT = "Chat.GetServerChat";
|
const GET_SERVER_CHAT = "Chat.GetServerChat";
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
Ingame you can activate the Socket Manager and set Password and Port of it.
|
Ingame you can activate the Communication Manager and set Password and Port of it.
|
||||||
|
|
||||||
Sample Web Implementation
|
Sample Web Implementation
|
||||||
<?php
|
<?php
|
@ -24,7 +24,7 @@ use ManiaControl\Players\PlayerManager;
|
|||||||
use ManiaControl\Plugins\PluginManager;
|
use ManiaControl\Plugins\PluginManager;
|
||||||
use ManiaControl\Server\Server;
|
use ManiaControl\Server\Server;
|
||||||
use ManiaControl\Settings\SettingManager;
|
use ManiaControl\Settings\SettingManager;
|
||||||
use ManiaControl\Sockets\SocketManager;
|
use ManiaControl\Communication\CommunicationManager;
|
||||||
use ManiaControl\Statistics\StatisticManager;
|
use ManiaControl\Statistics\StatisticManager;
|
||||||
use ManiaControl\Update\UpdateManager;
|
use ManiaControl\Update\UpdateManager;
|
||||||
use ManiaControl\Utils\CommandLineHelper;
|
use ManiaControl\Utils\CommandLineHelper;
|
||||||
@ -168,7 +168,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
|
|||||||
|
|
||||||
/** @var EchoManager $echoManager */
|
/** @var EchoManager $echoManager */
|
||||||
private $echoManager = null;
|
private $echoManager = null;
|
||||||
private $socketManager = null;
|
private $communicationManager = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new ManiaControl instance
|
* Construct a new ManiaControl instance
|
||||||
@ -183,7 +183,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
|
|||||||
// Load ManiaControl Modules
|
// Load ManiaControl Modules
|
||||||
$this->callbackManager = new CallbackManager($this);
|
$this->callbackManager = new CallbackManager($this);
|
||||||
$this->echoManager = new EchoManager($this);
|
$this->echoManager = new EchoManager($this);
|
||||||
$this->socketManager = new SocketManager($this);
|
$this->communicationManager = new CommunicationManager($this);
|
||||||
$this->timerManager = new TimerManager($this);
|
$this->timerManager = new TimerManager($this);
|
||||||
$this->database = new Database($this);
|
$this->database = new Database($this);
|
||||||
$this->fileReader = new AsynchronousFileReader($this);
|
$this->fileReader = new AsynchronousFileReader($this);
|
||||||
@ -299,10 +299,10 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
|
|||||||
/**
|
/**
|
||||||
* Return the socket manager
|
* Return the socket manager
|
||||||
*
|
*
|
||||||
* @return SocketManager
|
* @return CommunicationManager
|
||||||
*/
|
*/
|
||||||
public function getSocketManager() {
|
public function getCommunicationManager() {
|
||||||
return $this->socketManager;
|
return $this->communicationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user