From a4e309b0a1df57a8e45af40bbf90a04e9dc5995c Mon Sep 17 00:00:00 2001 From: "Lem()nTea_" Date: Sat, 29 Nov 2014 13:15:22 +0900 Subject: [PATCH] Session manager(WIP). Related to #16 --- .../src/ifteam/CustomPacket/CustomSocket.php | 1 + plugin/src/ifteam/CustomPacket/MainLoader.php | 13 ++++--- .../ifteam/CustomPacket/SessionManager.php | 38 +++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 plugin/src/ifteam/CustomPacket/SessionManager.php diff --git a/plugin/src/ifteam/CustomPacket/CustomSocket.php b/plugin/src/ifteam/CustomPacket/CustomSocket.php index b3d863a..182a2dd 100644 --- a/plugin/src/ifteam/CustomPacket/CustomSocket.php +++ b/plugin/src/ifteam/CustomPacket/CustomSocket.php @@ -47,6 +47,7 @@ public function run(){ socket_set_nonblock ( $this->socket ); $plugin->getServer()->getLogger()->info("CustomSocket: Done loading. Enthering the loop..."); while(1){ + $plugin->getServer()->getLogger()->debug('Wating for packet(s)...'); $buffer = $address = $port = NULL; if($this->recvPacket($buffer, $address, $port) !== false){ echo 'GOTCHA!! from: '.$address.':'.$port.', data: '.$buffer.PHP_EOL; diff --git a/plugin/src/ifteam/CustomPacket/MainLoader.php b/plugin/src/ifteam/CustomPacket/MainLoader.php index 0698dba..381533f 100644 --- a/plugin/src/ifteam/CustomPacket/MainLoader.php +++ b/plugin/src/ifteam/CustomPacket/MainLoader.php @@ -2,27 +2,30 @@ namespace ifteam\CustomPacket; -use pocketmine\plugin\PluginBase; use pocketmine\utils\Config; use pocketmine\event\Listener; use pocketmine\Server; -use ifteam\CustomPacket\event\ReceivePacketEvent; -use ifteam\CustomPacket\event\SendPacketEvent; +use ifteam\CustomPacket\Event\ReceivePacketEvent; +use ifteam\CustomPacket\Event\SendPacketEvent; use ifteam\CustomPacket\Packet\CustomPacket; +use ifteam\CustomPacket\SessionManager; use pocketmine\scheduler\CallbackTask; -class MainLoader extends PluginBase implements Listener { +class MainLoader extends CPBase implements Listener { /** @var CustomSocket */ private $socket; private $option; - private $socketManager; + private $sessionManager; + private $dbg = true; // debug/test mode public $stream = null; private static $instance = null; + private $registeredCallbacks; public function onEnable() { + $this->registeredCallbacks = array(); if(self::$instance == null) self::$instance = $this; $defaultOption = [ diff --git a/plugin/src/ifteam/CustomPacket/SessionManager.php b/plugin/src/ifteam/CustomPacket/SessionManager.php new file mode 100644 index 0000000..de2e04a --- /dev/null +++ b/plugin/src/ifteam/CustomPacket/SessionManager.php @@ -0,0 +1,38 @@ +getLogger()->debug'; + + public function __construct(){ + self::$instance = $this; + } + + public static function getInstance(){ + return self::$instance; + } + + public function add(Player $player, $ip, $port){ + if($player->getAddress() !== $ip): Server::getInstance()->getLogger()->warning('Player '.$player->getName().' attempted to connect to CustomPacket server with invalid IP!'); return; endif; + $this->session[$player->getName()]['ip'] = $ip; + $this->session[$player->getName()]['port'] = $port; + $this->log($player->getName().' ('.$ip.')connected to CustomPacket server on port '. $port); + } + + public function close($key){ + if(isset($this->session[$key])): $this->session[$key] = NULL; $this->log('Player '. $key. 'disconnected from CustomPacket Server'); endif; //faster freeing memory without garbage collector + } + + public function getSessionData($key){ + return isset($this->session[$key]) ? $this->session[$key] : false; + } + +} +?>