DataPacket: encode() and decode() are now final, encodePayload() and decodePayload() are now abstract

This commit is contained in:
Dylan K. Taylor 2019-01-03 21:14:54 +00:00
parent 60687d8d6c
commit c559dfccfe
2 changed files with 16 additions and 13 deletions

View File

@ -65,7 +65,7 @@ abstract class DataPacket extends NetworkBinaryStream{
* @throws \OutOfBoundsException * @throws \OutOfBoundsException
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
*/ */
public function decode() : void{ final public function decode() : void{
$this->rewind(); $this->rewind();
$this->decodeHeader(); $this->decodeHeader();
$this->decodePayload(); $this->decodePayload();
@ -83,16 +83,14 @@ abstract class DataPacket extends NetworkBinaryStream{
} }
/** /**
* Note for plugin developers: If you're adding your own packets, you should perform decoding in here. * Decodes the packet body, without the packet ID or other generic header fields.
* *
* @throws \OutOfBoundsException * @throws \OutOfBoundsException
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
*/ */
protected function decodePayload() : void{ abstract protected function decodePayload() : void;
} final public function encode() : void{
public function encode() : void{
$this->reset(); $this->reset();
$this->encodeHeader(); $this->encodeHeader();
$this->encodePayload(); $this->encodePayload();
@ -104,11 +102,9 @@ abstract class DataPacket extends NetworkBinaryStream{
} }
/** /**
* Note for plugin developers: If you're adding your own packets, you should perform encoding in here. * Encodes the packet body, without the packet ID or other generic header fields.
*/ */
protected function encodePayload() : void{ abstract protected function encodePayload() : void;
}
/** /**
* Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for * Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for

View File

@ -44,12 +44,19 @@ class UnknownPacket extends DataPacket{
return "unknown packet"; return "unknown packet";
} }
public function decode() : void{ protected function decodeHeader() : void{
}
protected function decodePayload() : void{
$this->payload = $this->getRemaining(); $this->payload = $this->getRemaining();
} }
public function encode() : void{ protected function encodeHeader() : void{
//Do not reset the buffer, this class does not have a valid NETWORK_ID constant.
}
protected function encodePayload() : void{
$this->put($this->payload); $this->put($this->payload);
} }