Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large payload #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/packet/ControlPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public function __construct(Version $version)

/**
* @param Version $version
* @param string $rawInput
* @param string $rawInput
* @param int $topicStart
* @return static
*/
public static function parse(Version $version, $rawInput)
public static function parse(Version $version, $rawInput, $topicStart = 2)
{
static::checkRawInputValidControlPackageType($rawInput);

Expand Down
16 changes: 11 additions & 5 deletions src/packet/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ class Factory
public static function getNextPacket(Version $version, $remainingData)
{
while(isset($remainingData{1})) {
$remainingLength = ord($remainingData{1});
$packetLength = 2 + $remainingLength;
$byte = 1;
$packetLength = 0;
do {
$digit = ord($remainingData{$byte});
$packetLength += $digit;
$byte++;
} while (($digit & 128) != 0);
$packetLength += 2;
$nextPacketData = substr($remainingData, 0, $packetLength);
$remainingData = substr($remainingData, $packetLength);

yield self::getByMessage($version, $nextPacketData);
yield self::getByMessage($version, $nextPacketData, $byte);
}
}

private static function getByMessage(Version $version, $input)
private static function getByMessage(Version $version, $input, $topicStart = 2)
{
$controlPacketType = ord($input{0}) >> 4;

Expand All @@ -45,7 +51,7 @@ private static function getByMessage(Version $version, $input)
return SubscribeAck::parse($version, $input);

case Publish::getControlPacketType():
return Publish::parse($version, $input);
return Publish::parse($version, $input, $topicStart);

case PublishComplete::getControlPacketType():
return PublishComplete::parse($version, $input);
Expand Down
6 changes: 3 additions & 3 deletions src/packet/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static function getControlPacketType()
return ControlPacketType::PUBLISH;
}

public static function parse(Version $version, $rawInput)
public static function parse(Version $version, $rawInput, $topicStart = 2)
{
/** @var Publish $packet */
$packet = parent::parse($version, $rawInput);

//TODO 3.3.2.2 Packet Identifier not yet supported
$topic = static::getPayloadLengthPrefixFieldInRawInput(2, $rawInput);
$topic = static::getPayloadLengthPrefixFieldInRawInput($topicStart, $rawInput);
$packet->setTopic($topic);

$byte1 = $rawInput{0};
Expand All @@ -53,7 +53,7 @@ public static function parse(Version $version, $rawInput)
}
$packet->payload = substr(
$rawInput,
4 + strlen($topic)
$topicStart + 2 + strlen($topic)
);

return $packet;
Expand Down
2 changes: 2 additions & 0 deletions src/packet/PublishComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class PublishComplete extends ControlPacket {

const EVENT = 'PUBLISH_COMPLETE';

public static function getControlPacketType()
{
return ControlPacketType::PUBCOMP;
Expand Down