Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chalkpe committed Nov 28, 2014
2 parents 9c31bc8 + c65fbb3 commit 5dac4a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
18 changes: 10 additions & 8 deletions plugin/src/ifteam/CustomPacket/CustomSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use pocketmine\Thread;
use pocketmine\Server;
use ifteam\CustomPacket\Event\ReceivePacketEvent;

class CustomSocket extends Thread{

protected $caller, $logger, $interface, $port, $socket;
protected $caller, $interface, $port, $socket;

public function __construct($caller, $interface = '0.0.0.0', $port = 19131){
$this->caller = $caller;
$this->logger = $caller->getLogger();
global $plugin;
$plugin = $caller;
$this->interface = filter_var($interface, FILTER_VALIDATE_IP)? $interface : '0.0.0.0';
$this->port = $port;
$this->start();
$this->start(PTHREADS_INHERIT_ALL | PTHREADS_ALLOW_GLOBALS);
}

public function sendPacket($buffer, $address, $port, $scream = false){
Expand All @@ -33,22 +34,23 @@ public function close(){
}

public function run(){
global $plugin;
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (socket_bind ( $this->socket, $this->interface, $this->port ) === true) {
@socket_set_option ( $this->socket, SOL_SOCKET, SO_REUSEADDR, 0 );
@socket_set_option ( $this->socket, SOL_SOCKET, SO_RCVBUF, 1024 * 1024 );
@socket_set_option ( $this->socket, SOL_SOCKET, SO_SNDBUF, 1024 * 1024 * 8 );
} else {
$this->logger->critical ("*** FAILED TO BIND TO " . $this->interface . ":" . $this->port . "!", true, true, 0 );
$this->logger->critical ("*** Perhaps a server is already running on that port?", \true, \true, 0);
$plugin->getServer()->getLogger()->critical ("*** FAILED TO BIND TO " . $this->interface . ":" . $this->port . "!", true, true, 0 );
$plugin->getServer()->getLogger()->critical ("*** Perhaps a server is already running on that port?", \true, \true, 0);
}
socket_set_nonblock ( $this->socket );
//$this->logger->info("CustomSocket: Done loading. Enthering the loop...");
$plugin->getServer()->getLogger()->info("CustomSocket: Done loading. Enthering the loop...");
while(1){
$buffer = $address = $port = NULL;
if($this->recvPacket($buffer, $address, $port) !== false){
echo 'GOTCHA! from: '.$address.':'.$port.', data: '.$buffer.PHP_EOL;
// $this->caller->getPluginManager()->callEvent(new ReceivePacketEvent(new CustomPacket($buffer), $address, $port)); //THIS CAUSES SEGFAULT!
$plugin->callEvent('rcv', array('rawstring' => $buffer, 'ip' => $address, 'port' => $port));
$this->sendPacket('You sent '. $buffer .' !!! success! WOW!', $address, $port, true); //Test code. should be remove in official release.
}
}
Expand Down
5 changes: 3 additions & 2 deletions plugin/src/ifteam/CustomPacket/Event/SendPacketEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace ifteam\CustomPacket\event;

use pocketmine\event\Cancellable;
use ifteam\CustomPacket\Packet\CustomPacket;

class SendPacketEvent extends CustomPacketEvent implements Cancellable{
public function __construct(CustomSocket $plugin, $packet, $ip, $port) {
parent::__construct ( $plugin, $packet, $ip, $port );
public function __construct(CustomPacket $packet, $ip, $port) {
parent::__construct ($packet, $ip, $port );
}

public function getPacket() {
Expand Down
18 changes: 17 additions & 1 deletion plugin/src/ifteam/CustomPacket/MainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use pocketmine\Server;
use ifteam\CustomPacket\event\ReceivePacketEvent;
use ifteam\CustomPacket\event\SendPacketEvent;
use ifteam\CustomPacket\Packet\CustomPacket;
use pocketmine\scheduler\CallbackTask;

class MainLoader extends PluginBase implements Listener {
Expand All @@ -29,7 +30,7 @@ public function onEnable() {
"port" => 19131
];
$this->option = (new Config($this->getDataFolder() . "SocketOption.yml", Config::YAML, $defaultOption))->getAll();
$this->socket = new CustomSocket($this->getServer(), $this->option["interface"], $this->option["port"]);
$this->socket = new CustomSocket($this, $this->option["interface"], $this->option["port"]);
//$this->socketManager = new SocketManager($this->socket, $this);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
Expand Down Expand Up @@ -93,5 +94,20 @@ public static function sendPacket($packet, $ip, $port) {
}
}
}

public function callEvent($type, array $data){
switch($type){
case 'recv':
$this->getServer()->getPluginManager()->callEvent(new ReceivePacketEvent(new CustomPacket($data['rawstring']), $data['ip'], $data['port']));
break;
case 'send':
$this->getServer()->getPluginManager()->callEvent(new SendPacketEvent($data['pk'], $data['ip'], $data['port']));
break;
}
ob_start();
var_dump($data);
$dump = ob_get_clean();
$this->getServer()->getLogger()->debug("[CustomPacket Event debug message]\n".$dump);
}
}
?>

0 comments on commit 5dac4a2

Please sign in to comment.