Skip to content

Commit

Permalink
Session manager(WIP). Related to #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Lem()nTea_ committed Nov 29, 2014
1 parent a2512dc commit a4e309b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions plugin/src/ifteam/CustomPacket/CustomSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions plugin/src/ifteam/CustomPacket/MainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
38 changes: 38 additions & 0 deletions plugin/src/ifteam/CustomPacket/SessionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ifteam\CustomPacket;

use pocketmine\Player;
use pocketmine\Server;

class SessionManager {

private static $instance;
private $session = array();
private $log = 'Server::getInstance()->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;
}

}
?>

0 comments on commit a4e309b

Please sign in to comment.