1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Connection; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Transport\Protocol, |
8
|
|
|
Transport\Frame, |
9
|
|
|
Transport\Frame\Method, |
10
|
|
|
Transport\Frame\Type, |
11
|
|
|
Transport\Frame\Channel, |
12
|
|
|
Transport\Frame\Value\UnsignedOctet, |
13
|
|
|
Transport\Frame\Value\UnsignedShortInteger, |
14
|
|
|
Transport\Frame\Value\UnsignedLongInteger, |
15
|
|
|
Exception\ReceivedFrameNotDelimitedCorrectly, |
16
|
|
|
Exception\PayloadTooShort |
17
|
|
|
}; |
18
|
|
|
use Innmind\Stream\Readable; |
19
|
|
|
|
20
|
|
|
final class FrameReader |
21
|
|
|
{ |
22
|
|
|
public function __invoke(Readable $stream, Protocol $protocol): Frame |
23
|
|
|
{ |
24
|
|
|
$type = Type::fromInt( |
25
|
|
|
UnsignedOctet::fromString($stream->read(1))->original()->value() |
26
|
|
|
); |
27
|
|
|
$channel = new Channel( |
28
|
|
|
UnsignedShortInteger::fromString($stream->read(2))->original()->value() |
29
|
|
|
); |
30
|
|
|
$payload = $stream |
31
|
|
|
->read(UnsignedLongInteger::fromString($stream->read(4))->original()->value()) |
32
|
|
|
->toEncoding('ASCII'); |
33
|
|
|
|
34
|
|
|
if ($payload->length() < 4) { |
35
|
|
|
throw new PayloadTooShort; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$end = $stream->read(1)->toEncoding('ASCII'); |
39
|
|
|
|
40
|
|
|
if ($end->length() !== 1) { |
41
|
|
|
throw new ReceivedFrameNotDelimitedCorrectly; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$end = UnsignedOctet::fromString($end)->original()->value(); |
45
|
|
|
|
46
|
|
|
if ($end !== 0xCE) { |
47
|
|
|
throw new ReceivedFrameNotDelimitedCorrectly; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$method = $payload->substring(0, 4); |
51
|
|
|
$method = new Method( |
52
|
|
|
UnsignedShortInteger::fromString($method->substring(0, 2))->original()->value(), |
53
|
|
|
UnsignedShortInteger::fromString($method->substring(2, 4))->original()->value() |
54
|
|
|
); |
55
|
|
|
$arguments = $payload->substring(4); |
56
|
|
|
|
57
|
|
|
return new Frame( |
58
|
|
|
$type, |
59
|
|
|
$channel, |
60
|
|
|
$method, |
61
|
|
|
...$protocol->read($method, $arguments) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|