New network reliability layer

This commit is contained in:
Shoghi Cervantes Pueyo 2013-04-14 13:04:57 +02:00
parent a92518ef9e
commit 633f7233b4
3 changed files with 69 additions and 114 deletions

View File

@ -237,92 +237,77 @@ class Protocol{
0x80 => array(
"itriad",
"ubyte",
"customData",
),
0x81 => array(
"itriad",
"ubyte",
"customData",
),
0x82 => array(
"itriad",
"ubyte",
"customData",
),
0x83 => array(
"itriad",
"ubyte",
"customData",
),
0x84 => array(
"itriad",
"ubyte",
"customData",
),
0x85 => array(
"itriad",
"ubyte",
"customData",
),
0x86 => array(
"itriad",
"ubyte",
"customData",
),
0x87 => array(
"itriad",
"ubyte",
"customData",
),
0x88 => array(
"itriad",
"ubyte",
"customData",
),
0x89 => array(
"itriad",
"ubyte",
"customData",
),
0x8a => array(
"itriad",
"ubyte",
"customData",
),
0x8b => array(
"itriad",
"ubyte",
"customData",
),
0x8c => array(
"itriad",
"ubyte",
"customData",
),
0x8d => array(
"itriad",
"ubyte",
"customData",
),
0x8e => array(
"itriad",
"ubyte",
"customData",
),

View File

@ -98,23 +98,24 @@ class Packet{
}
break;
case "customData":
$this->addRaw(chr($this->data[1]));
switch($this->data[1]){
case 0x40:
$reply = new CustomPacketHandler($this->data[$field]["id"], "", $this->data[$field], true);
$reply = new CustomPacketHandler($this->data[2]["id"], "", $this->data[2], true);
$this->addRaw(Utils::writeShort((strlen($reply->raw) + 1) << 3));
$this->addRaw(Utils::writeTriad(strrev($this->data[$field]["count"])));
$this->addRaw(chr($this->data[$field]["id"]));
$this->addRaw(Utils::writeTriad(strrev($this->data[2]["count"])));
$this->addRaw(chr($this->data[2]["id"]));
$this->addRaw($reply->raw);
break;
case 0x00:
if($this->data[$field]["id"] !== false){
$raw = new CustomPacketHandler($this->data[$field]["id"], "", $this->data[$field], true);
if($this->data[2]["id"] !== false){
$raw = new CustomPacketHandler($this->data[2]["id"], "", $this->data[2], true);
$raw = $raw->raw;
$this->addRaw(Utils::writeShort((strlen($raw) + 1) << 3));
$this->addRaw(chr($this->data[$field]["id"]));
$this->addRaw(chr($this->data[2]["id"]));
$this->addRaw($raw);
}else{
$this->addRaw($this->data[$field]["raw"]);
$this->addRaw($this->data[2]["raw"]);
}
break;
}
@ -209,11 +210,67 @@ class Packet{
}
break;
case "customData":
$d = new SerializedPacketHandler($this->data[1], $this->get(true));
if(isset($d->data["packets"])){
$this->data["packets"] = $d->data["packets"];
}else{
$this->data[] = $d->data;
$raw = $this->get(true);
$len = strlen($raw);
$offset = 0;
$this->data["packets"] = array();
while($len > $offset){
$pid = ord($raw{$offset});
++$offset;
$reliability = ($pid & 0b11100000) >> 5;
$hasSplit = ($pid & 0b00010000) >> 4;
$length = Utils::readShort(substr($raw, $offset, 2), false);
$offset += 2;
if($reliability === 2
or $reliability === 3
or $reliability === 4){
$messageNumber = Utils::readTriad(strrev(substr($raw, $offset, 3)));
$offset += 3;
}else{
$messageNumber = 0;
}
if($reliability === 1
or $reliability === 3
or $reliability === 4){
$orderIndex = Utils::readInt(substr($raw, $offset, 4));
$offset += 4;
$orderChannel = ord($raw{$offset}); //5 bits, 32 values
++$offset;
}else{
$orderIndex = 0;
$orderChannel = 0;
}
if($hasSplit === 1){
$splitCount = Utils::readInt(substr($raw, $offset, 4));
$offset += 4;
$splitID = Utils::readShort(substr($raw, $offset, 2));
$offset += 2;
$splitIndex = Utils::readInt(substr($raw, $offset, 4));
$offset += 4;
}else{
$splitCount = 0;
$splitID = 0;
$splitIndex = 0;
}
if($length == 0
or $orderingChannel >= 32
or ($hasSplit === 1 and $splitIndex >= $splitCount)){
continue;
}
$ln = ceil($length / 8);
$id = ord($raw{$offset});
++$offset;
$pak = substr($raw, $offset, $ln - 1);
$offset += $ln - 1;
$pk = new CustomPacketHandler($id, $pak);
$pk->data["length"] = $ln;
$pk->data["id"] = $id;
$pk->data["counter"] = $messageNumber;
$pk->data["packetName"] = $pk->name;
$this->data["packets"][] = array($pid, $pk->data, $pak);
}
break;
case "magic":

View File

@ -1,87 +0,0 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
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.
*/
class SerializedPacketHandler{
var $offset, $raw, $c, $data, $name = "";
private function get($len = true, $check = true){
if($len === true){
$data = substr($this->raw, $this->offset);
if($check === true){
$this->offset = strlen($this->raw);
}
return $data;
}
$data = substr($this->raw, $this->offset, $len);
if($check === true){
$this->offset += $len;
}
return $data;
}
public function __construct($pid, $raw = "", $data = array(), $create = false){
$this->raw = $raw;
$this->data = $data;
$this->offset = 0;
$this->c = (bool) $create;
switch($pid){
case 0x60:
case 0x40:
case 0x00:
if($this->c === false){
$this->data["packets"] = array();
$i = 0;
while($this->offset < strlen($this->raw)){
if($i > 0){
$pid = ord($this->get(1));
}
$len = ceil(Utils::readShort($this->get(2), false) / 8); //Utils::readShort($this->get(2), false) >> 3;
if($pid !== 0x00){
$c = Utils::readTriad(strrev($this->get(3)));
}
if($pid === 0x60 and $i === 0){
$this->data["unknown1"] = $this->get(4);
}
$id = ord($this->get(1));
$raw = $this->get($len - 1);
$pk = new CustomPacketHandler($id, $raw);
$pk->data["length"] = $len;
$pk->data["id"] = $id;
if($pid !== 0x00){
$pk->data["counter"] = $c;
}
$pk->data["packetName"] = $pk->name;
$this->data["packets"][] = array($pid, $pk->data, $raw);
++$i;
}
}
break;
}
}
}