clrghouz/app/Classes/Protocol/BinkpMessage.php

43 lines
724 B
PHP

<?php
namespace App\Classes\Protocol;
final class BinkpMessage
{
public const BLK_HDR_SIZE = 2; /* header size */
private int $id;
private string $body;
public function __construct(int $id,string $body)
{
$this->id = $id;
$this->body = $body;
}
public function __get($key)
{
switch ($key) {
case 'len':
return strlen($this->body)+1+self::BLK_HDR_SIZE;
case 'msg':
$buf = self::mkheader((strlen($this->body)+1) | 0x8000);
$buf .= chr($this->id);
$buf .= $this->body;
return $buf;
default:
throw new \Exception('Unknown key :'.$key);
}
}
public static function mkheader(int $l): string
{
$buf = chr(($l>>8)&0xff);
$buf .= chr($l&0xff);
return $buf;
}
}