New packet writing done

This commit is contained in:
Shoghi Cervantes 2014-02-06 23:30:09 +01:00
parent 415c9f16f4
commit 0af197a0f7
4 changed files with 53 additions and 32 deletions

View File

@ -62,21 +62,16 @@ class MinecraftInterface{
if(RakNetInfo::isValid($pid)){ if(RakNetInfo::isValid($pid)){
$parser = new RakNetParser($buffer); $parser = new RakNetParser($buffer);
if($parser->packet !== false){ if($parser->packet !== false){
$this->packets[] = array( $parser->packet->ip = $source;
"pid" => $pid, $parser->packet->port = $port;
"packet" => $packet, $this->packets[] = $parser->packet;
"ip" => $source,
"port" => $port
);
} }
}else{ }else{
if(ServerAPI::request()->api->dhandle("server.unknownpacket", array( $packet = new Packet();
"pid" => $pid, $packet->ip = $source;
"data" => array(), $packet->port = $port;
"raw" => $buffer, $packet->buffer = $buffer;
"ip" => $source, if(ServerAPI::request()->api->dhandle("server.unknownpacket.$pid", $packet) !== true){
"port" => $port
)) !== true){
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2); console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
} }
return false; return false;
@ -93,18 +88,12 @@ class MinecraftInterface{
return false; return false;
} }
public function writePacket($pid, $data = array(), $raw = false, $dest = false, $port = false, $force = false){ public function writePacket(Packet $packet){
$CID = PocketMinecraftServer::clientID($dest, $port); if($packet instanceof RakNetPacket){
if($raw === false){ $codec = new RakNetCodec($packet);
$packet = new Packet($pid, $this->getStruct($pid));
$packet->data = $data;
@$packet->create();
$write = $this->socket->write($packet->raw, $dest, $port);
$this->bandwidth[1] += $write;
}else{
$write = $this->socket->write($data, $dest, $port);
$this->bandwidth[1] += $write;
} }
$write = $this->socket->write($packet->buffer, $packet->ip, $packet->port);
$this->bandwidth[1] += $write;
return $write; return $write;
} }

27
src/network/Packet.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class Packet extends stdClass{
public $ip;
public $port;
public $buffer;
}

View File

@ -19,11 +19,15 @@
* *
*/ */
class RakNetPacket extends stdClass{ class RakNetPacket extends Packet{
public $id; private $packetID;
public function __construct($packetID){ public function __construct($packetID){
$this->id = (int) $packetID; $this->packetID = (int) $packetID;
}
public function pid(){
return $this->packetID;
} }
public function __destruct(){} public function __destruct(){}

View File

@ -23,10 +23,10 @@ class RakNetParser{
private $id = -1; private $id = -1;
private $buffer; private $buffer;
private $offset; private $offset;
private $packet; public $packet;
public function __construct($buffer){ public function __construct(&$buffer){
$this->buffer = $buffer; $this->buffer =& $buffer;
$this->offset = 0; $this->offset = 0;
if(strlen($this->buffer) > 0){ if(strlen($this->buffer) > 0){
$this->id = ord($this->get(1)); $this->id = ord($this->get(1));
@ -78,6 +78,7 @@ class RakNetParser{
private function parse(){ private function parse(){
$this->packet = new RakNetPacket($this->pid()); $this->packet = new RakNetPacket($this->pid());
$this->packet->buffer =& $this->buffer;
$this->packet->length = strlen($this->buffer); $this->packet->length = strlen($this->buffer);
switch($this->pid()){ switch($this->pid()){
case RakNetInfo::UNCONNECTED_PING: case RakNetInfo::UNCONNECTED_PING: