PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
DataPacket.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\network\protocol;
23 
24 #include <rules/DataPacket.h>
25 
26 #ifndef COMPILE
28 #endif
29 
31 
32 
33 abstract class DataPacket extends \stdClass{
34 
35  private $offset = 0;
36  public $buffer = "";
37  public $isEncoded = false;
38 
39  abstract public function pid();
40 
41  abstract public function encode();
42 
43  abstract public function decode();
44 
45  protected function reset(){
46  $this->buffer = chr($this->pid());
47  $this->offset = 0;
48  }
49 
50  public function setBuffer($buffer = ""){
51  $this->buffer = $buffer;
52  $this->offset = 0;
53  }
54 
55  public function getBuffer(){
56  return $this->buffer;
57  }
58 
59  protected function get($len){
60  if($len < 0){
61  $this->offset = strlen($this->buffer) - 1;
62  return "";
63  }elseif($len === true){
64  return substr($this->buffer, $this->offset);
65  }
66 
67  return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len);
68  }
69 
70  protected function put($str){
71  $this->buffer .= $str;
72  }
73 
74  protected function getLong(){
75  return Binary::readLong($this->get(8));
76  }
77 
78  protected function putLong($v){
79  $this->buffer .= Binary::writeLong($v);
80  }
81 
82  protected function getInt(){
83  return Binary::readInt($this->get(4));
84  }
85 
86  protected function putInt($v){
87  $this->buffer .= Binary::writeInt($v);
88  }
89 
90  protected function getShort($signed = true){
91  return $signed ? Binary::readSignedShort($this->get(2)) : Binary::readShort($this->get(2));
92  }
93 
94  protected function putShort($v){
95  $this->buffer .= Binary::writeShort($v);
96  }
97 
98  protected function getFloat(){
99  return Binary::readFloat($this->get(4));
100  }
101 
102  protected function putFloat($v){
103  $this->buffer .= Binary::writeFloat($v);
104  }
105 
106  protected function getTriad(){
107  return Binary::readTriad($this->get(3));
108  }
109 
110  protected function putTriad($v){
111  $this->buffer .= Binary::writeTriad($v);
112  }
113 
114 
115  protected function getLTriad(){
116  return Binary::readLTriad($this->get(3));
117  }
118 
119  protected function putLTriad($v){
120  $this->buffer .= Binary::writeLTriad($v);
121  }
122 
123  protected function getByte(){
124  return ord($this->buffer{$this->offset++});
125  }
126 
127  protected function putByte($v){
128  $this->buffer .= chr($v);
129  }
130 
131  protected function getDataArray($len = 10){
132  $data = [];
133  for($i = 1; $i <= $len and !$this->feof(); ++$i){
134  $data[] = $this->get($this->getTriad());
135  }
136 
137  return $data;
138  }
139 
140  protected function putDataArray(array $data = []){
141  foreach($data as $v){
142  $this->putTriad(strlen($v));
143  $this->put($v);
144  }
145  }
146 
147  protected function getSlot(){
148  $id = $this->getShort();
149  $cnt = $this->getByte();
150 
151  return Item::get(
152  $id,
153  $this->getShort(),
154  $cnt
155  );
156  }
157 
158  protected function putSlot(Item $item){
159  $this->putShort($item->getId());
160  $this->putByte($item->getCount());
161  $this->putShort($item->getDamage());
162  }
163 
164  protected function getString(){
165  return $this->get($this->getShort());
166  }
167 
168  protected function putString($v){
169  $this->putShort(strlen($v));
170  $this->put($v);
171  }
172 
173  protected function feof(){
174  return !isset($this->buffer{$this->offset});
175  }
176 
177  public function clean(){
178  $this->buffer = null;
179  $this->isEncoded = false;
180  $this->offset = 0;
181  return $this;
182  }
183 }
static writeTriad($value)
Definition: Binary.php:54
static readLTriad($str)
Definition: Binary.php:65
static readSignedShort($str)
Definition: Binary.php:270
static writeShort($value)
Definition: Binary.php:285
static readShort($str)
Definition: Binary.php:259
static readTriad($str)
Definition: Binary.php:43
static writeLTriad($value)
Definition: Binary.php:76