Skip to content

Commit 61f3bb1

Browse files
committed
Refactor protocol writer
1 parent 4c89150 commit 61f3bb1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1460
-843
lines changed

src/Channel.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -981,110 +981,110 @@ public function doPublish
981981
->appendUint8(206)
982982
;
983983

984-
$s = 14;
984+
$size = 14;
985985

986986
if (isset($headers['content-type'])) {
987987
$flags |= 32768;
988988
$contentType = $headers['content-type'];
989-
$s += 1 + \strlen($contentType);
989+
$size += 1 + \strlen($contentType);
990990
unset($headers['content-type']);
991991
}
992992

993993
if (isset($headers['content-encoding'])) {
994994
$flags |= 16384;
995995
$contentEncoding = $headers['content-encoding'];
996-
$s += 1 + \strlen($contentEncoding);
996+
$size += 1 + \strlen($contentEncoding);
997997
unset($headers['content-encoding']);
998998
}
999999

10001000
if (isset($headers['delivery-mode'])) {
10011001
$flags |= 4096;
10021002
$deliveryMode = (int) $headers['delivery-mode'];
1003-
$s += 1;
1003+
$size += 1;
10041004
unset($headers['delivery-mode']);
10051005
}
10061006

10071007
if (isset($headers['priority'])) {
10081008
$flags |= 2048;
10091009
$priority = (int) $headers['priority'];
1010-
$s += 1;
1010+
$size += 1;
10111011
unset($headers['priority']);
10121012
}
10131013

10141014
if (isset($headers['correlation-id'])) {
10151015
$flags |= 1024;
10161016
$correlationId = $headers['correlation-id'];
1017-
$s += 1 + \strlen($correlationId);
1017+
$size += 1 + \strlen($correlationId);
10181018
unset($headers['correlation-id']);
10191019
}
10201020

10211021
if (isset($headers['reply-to'])) {
10221022
$flags |= 512;
10231023
$replyTo = $headers['reply-to'];
1024-
$s += 1 + \strlen($replyTo);
1024+
$size += 1 + \strlen($replyTo);
10251025
unset($headers['reply-to']);
10261026
}
10271027

10281028
if (isset($headers['expiration'])) {
10291029
$flags |= 256;
10301030
$expiration = $headers['expiration'];
1031-
$s += 1 + \strlen($expiration);
1031+
$size += 1 + \strlen($expiration);
10321032
unset($headers['expiration']);
10331033
}
10341034

10351035
if (isset($headers['message-id'])) {
10361036
$flags |= 128;
10371037
$messageId = $headers['message-id'];
1038-
$s += 1 + \strlen($messageId);
1038+
$size += 1 + \strlen($messageId);
10391039
unset($headers['message-id']);
10401040
}
10411041

10421042
if (isset($headers['timestamp'])) {
10431043
$flags |= 64;
10441044
$timestamp = $headers['timestamp'];
1045-
$s += 8;
1045+
$size += 8;
10461046
unset($headers['timestamp']);
10471047
}
10481048

10491049
if (isset($headers['type'])) {
10501050
$flags |= 32;
10511051
$type = $headers['type'];
1052-
$s += 1 + \strlen($type);
1052+
$size += 1 + \strlen($type);
10531053
unset($headers['type']);
10541054
}
10551055

10561056
if (isset($headers['user-id'])) {
10571057
$flags |= 16;
10581058
$userId = $headers['user-id'];
1059-
$s += 1 + \strlen($userId);
1059+
$size += 1 + \strlen($userId);
10601060
unset($headers['user-id']);
10611061
}
10621062

10631063
if (isset($headers['app-id'])) {
10641064
$flags |= 8;
10651065
$appId = $headers['app-id'];
1066-
$s += 1 + \strlen($appId);
1066+
$size += 1 + \strlen($appId);
10671067
unset($headers['app-id']);
10681068
}
10691069

10701070
if (isset($headers['cluster-id'])) {
10711071
$flags |= 4;
10721072
$clusterId = $headers['cluster-id'];
1073-
$s += 1 + \strlen($clusterId);
1073+
$size += 1 + \strlen($clusterId);
10741074
unset($headers['cluster-id']);
10751075
}
10761076

10771077
if (!empty($headers)) {
10781078
$flags |= 8192;
10791079
$headersBuffer = new Buffer;
10801080
$headersBuffer->appendTable($headers);
1081-
$s += $headersBuffer->size();
1081+
$size += $headersBuffer->size();
10821082
}
10831083

10841084
$buffer
10851085
->appendUint8(2)
10861086
->appendUint16($this->id)
1087-
->appendUint32($s)
1087+
->appendUint32($size)
10881088
->appendUint16(60)
10891089
->appendUint16(0)
10901090
->appendUint64(\strlen($body))
@@ -1165,6 +1165,16 @@ public function doPublish
11651165

11661166
return $this->connection->write($buffer);
11671167
}
1168+
1169+
/**
1170+
* @param string $frame
1171+
*
1172+
* @return Promise<Protocol\AbstractFrame>
1173+
*/
1174+
private function await(string $frame): Promise
1175+
{
1176+
return $this->connection->await($this->id, $frame);
1177+
}
11681178

11691179
/**
11701180
* @return void
@@ -1179,30 +1189,20 @@ private function startConsuming(): void
11791189

11801190
asyncCall(function () {
11811191
while ($this->state === self::STATE_OPEN) {
1182-
/** @var Protocol\BasicDeliverFrame $deliver */
1183-
$deliver = yield $this->await(Protocol\BasicDeliverFrame::class);
1192+
/** @var Protocol\BasicDeliverFrame $frame */
1193+
$frame = yield $this->await(Protocol\BasicDeliverFrame::class);
11841194

1185-
if (!isset($this->callbacks[$deliver->consumerTag])) {
1195+
if (!isset($this->callbacks[$frame->consumerTag])) {
11861196
continue;
11871197
}
11881198

1189-
$message = yield $this->consumeMessage($deliver, $deliver->consumerTag);
1199+
$message = yield $this->consumeMessage($frame, $frame->consumerTag);
11901200

1191-
asyncCall($this->callbacks[$deliver->consumerTag], $message, $this);
1201+
asyncCall($this->callbacks[$frame->consumerTag], $message, $this);
11921202
}
11931203
});
11941204
}
11951205

1196-
/**
1197-
* @param string $frame
1198-
*
1199-
* @return Promise<Protocol\AbstractFrame>
1200-
*/
1201-
private function await(string $frame): Promise
1202-
{
1203-
return $this->connection->await($this->id, $frame);
1204-
}
1205-
12061206
/**
12071207
* @param Protocol\MessageFrame $frame
12081208
* @param string $consumerTag

src/Connection.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
use function Amp\asyncCall, Amp\call, Amp\Socket\connect;
1616
use Amp\Deferred;
17+
use Amp\Emitter;
18+
use Amp\Iterator;
1719
use Amp\Loop;
1820
use Amp\Promise;
1921
use Amp\Socket\ClientConnectContext;
2022
use Amp\Socket\Socket;
23+
use PHPinnacle\Ridge\Protocol\ContentHeaderFrame;
24+
use PHPinnacle\Ridge\Protocol\MethodFrame;
2125

2226
final class Connection
2327
{
@@ -27,14 +31,9 @@ final class Connection
2731
private $uri;
2832

2933
/**
30-
* @var ProtocolWriter
34+
* @var Parser
3135
*/
32-
private $writer;
33-
34-
/**
35-
* @var ProtocolReader
36-
*/
37-
private $reader;
36+
private $parser;
3837

3938
/**
4039
* @var Socket
@@ -62,8 +61,7 @@ final class Connection
6261
public function __construct(string $uri)
6362
{
6463
$this->uri = $uri;
65-
$this->writer = new ProtocolWriter;
66-
$this->reader = new ProtocolReader;
64+
$this->parser = new Parser;
6765
}
6866

6967
/**
@@ -88,7 +86,28 @@ public function write(Buffer $payload): Promise
8886
*/
8987
public function send(Protocol\AbstractFrame $frame): Promise
9088
{
91-
return $this->write($this->writer->buffer($frame));
89+
if ($frame instanceof MethodFrame && $frame->payload !== null) {
90+
// payload already supplied
91+
} elseif ($frame instanceof MethodFrame || $frame instanceof ContentHeaderFrame) {
92+
$buffer = $frame->pack();
93+
94+
$frame->size = $buffer->size();
95+
$frame->payload = $buffer;
96+
} elseif ($frame instanceof Protocol\ContentBodyFrame) {
97+
// body frame's payload is already loaded
98+
} elseif ($frame instanceof Protocol\HeartbeatFrame) {
99+
// heartbeat frame is empty
100+
} else {
101+
throw Exception\ProtocolException::unknownFrameClass($frame);
102+
}
103+
104+
return $this->write((new Buffer)
105+
->appendUint8($frame->type)
106+
->appendUint16($frame->channel)
107+
->appendUint32($frame->size)
108+
->append($frame->payload)
109+
->appendUint8(206)
110+
);
92111
}
93112

94113
/**
@@ -222,15 +241,15 @@ public function close(): void
222241
*/
223242
private function consume(string $chunk): void
224243
{
225-
$this->reader->append($chunk);
244+
$this->parser->append($chunk);
226245

227-
while ($frame = $this->reader->frame()) {
246+
while ($frame = $this->parser->parse()) {
228247
$class = \get_class($frame);
229248
$defers = $this->await[$frame->channel][$class] ?? [];
230-
249+
231250
foreach ($defers as $i => $defer) {
232251
$defer->resolve($frame);
233-
252+
234253
unset($this->await[$frame->channel][$class][$i]);
235254
}
236255
}

0 commit comments

Comments
 (0)