mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-04 09:10:00 +00:00
Data Packet initial parsing
This commit is contained in:
parent
e6aeb03ec5
commit
de5a3175bc
115
src/network/protocol/DataPacket.php
Normal file
115
src/network/protocol/DataPacket.php
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
<?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 DataPacket extends stdClass{
|
||||||
|
public $id, $raw;
|
||||||
|
private $encoded = false;
|
||||||
|
private $decoded = false;
|
||||||
|
private $offset;
|
||||||
|
|
||||||
|
public function encode(){
|
||||||
|
|
||||||
|
$this->decoded = true;
|
||||||
|
$this->encoded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get($len){
|
||||||
|
if($len <= 0){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if($len === true){
|
||||||
|
return substr($this->raw, $this->offset);
|
||||||
|
}
|
||||||
|
$this->offset += $len;
|
||||||
|
return substr($this->raw, $this->offset - $len, $len);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLong($unsigned = false){
|
||||||
|
return Utils::readLong($this->get(8), $unsigned);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getInt($unsigned = false){
|
||||||
|
return Utils::readInt($this->get(4), $unsigned);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getShort($unsigned = false){
|
||||||
|
return Utils::readShort($this->get(2), $unsigned);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLTriad(){
|
||||||
|
return Utils::readTriad($this->get(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLTriad(){
|
||||||
|
return Utils::readTriad(strrev($this->get(3)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getByte(){
|
||||||
|
return ord($this->get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getDataArray($len = 10){
|
||||||
|
$data = array();
|
||||||
|
for($i = 1; $i <= $len and !$this->feof(); ++$i){
|
||||||
|
$data[] = $this->get($this->getTriad());
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function feof(){
|
||||||
|
return !isset($this->raw{$this->offset});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function decode(){
|
||||||
|
if(!isset($this->raw{0})){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->offset = 0;
|
||||||
|
switch($this->id){
|
||||||
|
case MC_PING:
|
||||||
|
$this->timestamp = $this->getLong();
|
||||||
|
break;
|
||||||
|
case MC_PONG:
|
||||||
|
$this->originalTimestamp = $this->getLong();
|
||||||
|
$this->timestamp = $this->getLong();
|
||||||
|
break;
|
||||||
|
case MC_CLIENT_CONNECT:
|
||||||
|
$this->clientID = $this->getLong();
|
||||||
|
$this->session = $this->getLong();
|
||||||
|
$this->unknown0 = $this->get(1);
|
||||||
|
break;
|
||||||
|
case MC_CLIENT_HANDSHAKE:
|
||||||
|
$this->cookie = $this->get(4);
|
||||||
|
$this->security = $this->get(1);
|
||||||
|
$this->port = $this->getShort(true);
|
||||||
|
$this->dataArray0 = $this->get($this->getByte());
|
||||||
|
$this->dataArray = $this->getDataArray(9);
|
||||||
|
$this->timestamp = $this->get(2);
|
||||||
|
$this->session2 = $this->getLong();
|
||||||
|
$this->session = $this->getLong();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->encoded = true;
|
||||||
|
$this->decoded = true;
|
||||||
|
}
|
||||||
|
}
|
@ -121,7 +121,7 @@ class RakNetParser{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function parseDataPacket(){
|
private function parseDataPacket(){
|
||||||
$data = new stdClass;
|
$data = new DataPacket;
|
||||||
$data->pid = $this->getByte();
|
$data->pid = $this->getByte();
|
||||||
$data->reliability = ($data->pid & 0b11100000) >> 5;
|
$data->reliability = ($data->pid & 0b11100000) >> 5;
|
||||||
$data->hasSplit = ($data->pid & 0b00010000) > 0;
|
$data->hasSplit = ($data->pid & 0b00010000) > 0;
|
||||||
@ -152,7 +152,7 @@ class RakNetParser{
|
|||||||
$data->splitID = $this->getShort();
|
$data->splitID = $this->getShort();
|
||||||
$data->splitIndex = $this->getInt();
|
$data->splitIndex = $this->getInt();
|
||||||
//error! no split packets allowed!
|
//error! no split packets allowed!
|
||||||
return;
|
return false;
|
||||||
}else{
|
}else{
|
||||||
$data->splitCount = 0;
|
$data->splitCount = 0;
|
||||||
$data->splitID = 0;
|
$data->splitID = 0;
|
||||||
@ -162,12 +162,12 @@ class RakNetParser{
|
|||||||
if($data->length <= 0
|
if($data->length <= 0
|
||||||
or $this->orderChannel >= 32
|
or $this->orderChannel >= 32
|
||||||
or ($hasSplit === 1 and $splitIndex >= $splitCount)){
|
or ($hasSplit === 1 and $splitIndex >= $splitCount)){
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data->id = $this->getByte();
|
$data->id = $this->getByte();
|
||||||
$data->payload = $this->get($len - 1);
|
$data->raw = $this->get($len - 1);
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user