Skip to content

Commit

Permalink
Update unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Oct 22, 2020
1 parent 591c304 commit a224d07
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion src/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact luffy <[email protected]>
* @contact lufei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
Expand All @@ -13,6 +13,12 @@

namespace Simps\MQTT;

use Simps\MQTT\Exception\MQTTException;
use Simps\MQTT\Exception\MQTTLengthException;
use Simps\MQTT\Packet\UnPack;
use Throwable;
use TypeError;

class Protocol
{
public static function pack(array $array)
Expand All @@ -21,5 +27,81 @@ public static function pack(array $array)

public static function unpack(string $data)
{
try {
$type = static::getType($data);
$remaining = static::getRemaining($data);
switch ($type) {
case Types::CONNECT:
$package = UnPack::connect($remaining);
break;
case Types::CONNACK:
$package = UnPack::connAck($remaining);
break;
case Types::PUBLISH:
$dup = ord($data[0]) >> 3 & 0x1;
$qos = ord($data[0]) >> 1 & 0x3;
$retain = ord($data[0]) & 0x1;
$package = UnPack::publish($dup, $qos, $retain, $remaining);
break;
case Types::PUBACK:
case Types::PUBREC:
case Types::PUBREL:
case Types::PUBCOMP:
case Types::UNSUBACK:
$package = ['type' => $type, 'message_id' => unpack('n', $remaining)[1]];
break;
case Types::PINGREQ:
case Types::PINGRESP:
case Types::DISCONNECT:
$package = ['type' => $type];
break;
case Types::SUBSCRIBE:
$package = UnPack::subscribe($remaining);
break;
case Types::SUBACK:
$package = UnPack::subAck($remaining);
break;
case Types::UNSUBSCRIBE:
$package = UnPack::unSubscribe($remaining);
break;
default:
$package = [];
}
} catch (TypeError $e) {
throw new MQTTException($e->getMessage(), $e->getCode());
} catch (Throwable $e) {
throw new MQTTException($e->getMessage(), $e->getCode());
}

return $package;
}

public static function getType(string $data)
{
return ord($data[0]) >> 4;
}

public static function getRemainingLength(string $data, &$headBytes)
{
$headBytes = $multiplier = 1;
$value = 0;
do {
if (!isset($data[$headBytes])) {
throw new MQTTLengthException('Malformed Remaining Length');
}
$digit = ord($data[$headBytes]);
$value += ($digit & 127) * $multiplier;
$multiplier *= 128;
++$headBytes;
} while (($digit & 128) != 0);

return $value;
}

public static function getRemaining(string $data)
{
$remainingLength = static::getRemainingLength($data, $headBytes);

return substr($data, $headBytes, $remainingLength);
}
}

0 comments on commit a224d07

Please sign in to comment.