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 \UnexpectedValueException
*/
public function decode() : void{
final public function decode() : void{
$this->rewind();
$this->decodeHeader();
$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 \UnexpectedValueException
*/
protected function decodePayload() : void{
abstract protected function decodePayload() : void;
}
public function encode() : void{
final public function encode() : void{
$this->reset();
$this->encodeHeader();
$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

View File

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