forked from simps/mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
591c304
commit a224d07
Showing
1 changed file
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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); | ||
} | ||
} |