Skip to content

Commit 63dd06d

Browse files
authored
Merge pull request #214 from jakubkulhan/0.6.x-make-connection-take-clientinterface
[0.6.x] Make connection take ClientInterface
2 parents 181cf20 + a8f5c31 commit 63dd06d

5 files changed

Lines changed: 28 additions & 6 deletions

File tree

spec/generate.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function amqpTypeToLength(string $type, string $e): array
230230
$connectionContent .= "use Bunny\\Protocol\\MethodFrame;\n";
231231
$connectionContent .= "use Bunny\\Protocol\\ProtocolReader;\n";
232232
$connectionContent .= "use Bunny\\Protocol\\ProtocolWriter;\n";
233+
$connectionContent .= "use Closure;\n";
233234
$connectionContent .= "use Evenement\\EventEmitterInterface;\n";
234235
$connectionContent .= "use Evenement\\EventEmitterTrait;\n";
235236
$connectionContent .= "use React\\EventLoop\\Loop;\n";
@@ -270,15 +271,17 @@ function amqpTypeToLength(string $type, string $e): array
270271
$connectionContent .= " /** @var array<array{filter: (callable(\Bunny\Protocol\AbstractFrame): bool), promise: \React\Promise\Deferred<\Bunny\Protocol\AbstractFrame>}> */\n";
271272
$connectionContent .= " private array \$awaitList = [];\n";
272273
$connectionContent .= "\n";
274+
$connectionContent .= " /** @param (Closure(): int) \$frameMax */\n";
273275
$connectionContent .= " public function __construct(\n";
274-
$connectionContent .= " private readonly Client \$client,\n";
276+
$connectionContent .= " private readonly ClientInterface \$client,\n";
275277
$connectionContent .= " private readonly ConnectionInterface \$connection,\n";
276278
$connectionContent .= " private readonly Buffer \$readBuffer,\n";
277279
$connectionContent .= " private readonly Buffer \$writeBuffer,\n";
278280
$connectionContent .= " private readonly ProtocolReader \$reader,\n";
279281
$connectionContent .= " private readonly ProtocolWriter \$writer,\n";
280282
$connectionContent .= " private readonly Channels \$channels,\n";
281283
$connectionContent .= " private readonly Configuration \$configuration,\n";
284+
$connectionContent .= " private readonly Closure \$frameMax,\n";
282285
$connectionContent .= " ) {\n";
283286
$connectionContent .= " \$this->connection->on('data', function (string \$data): void {\n";
284287
$connectionContent .= " \$this->readBuffer->append(\$data);\n";
@@ -899,7 +902,7 @@ function amqpTypeToLength(string $type, string $e): array
899902
$connectionContent .= " }\n\n";
900903
}
901904

902-
$connectionContent .= " for (\$payloadMax = \$this->client->frameMax - 8 /* frame preface and frame end */, \$i = 0, \$l = strlen(\$body); \$i < \$l; \$i += \$payloadMax) {\n";
905+
$connectionContent .= " for (\$payloadMax = (\$this->frameMax)() - 8 /* frame preface and frame end */, \$i = 0, \$l = strlen(\$body); \$i < \$l; \$i += \$payloadMax) {\n";
903906
$connectionContent .= " \$payloadSize = \$l - \$i;\n";
904907
$connectionContent .= " if (\$payloadSize > \$payloadMax) {\n";
905908
$connectionContent .= " \$payloadSize = \$payloadMax;\n";

src/Client.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class Client implements ClientInterface, EventEmitterInterface
5656

5757
private Channels $channels;
5858

59-
public int $frameMax = 0xFFFF;
59+
public int $frameMax = Constants::FRAME_MAX;
6060

6161
private int $nextChannelId = 1;
6262

63-
private int $channelMax = 0xFFFF;
63+
private int $channelMax = Constants::CHANNEL_MAX;
6464

6565
/**
6666
* @var list<\React\Promise\Deferred<null>>
@@ -209,6 +209,9 @@ public function connect(): self
209209
new ProtocolWriter(),
210210
$this->channels,
211211
$this->configuration,
212+
function (): int {
213+
return $this->channelMax;
214+
},
212215
);
213216
$this->connection->on('error', function (Throwable $error): void {
214217
$this->emit('error', [$error]);

src/Connection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Bunny\Protocol\MethodFrame;
1515
use Bunny\Protocol\ProtocolReader;
1616
use Bunny\Protocol\ProtocolWriter;
17+
use Closure;
1718
use Evenement\EventEmitterInterface;
1819
use Evenement\EventEmitterTrait;
1920
use React\EventLoop\Loop;
@@ -54,15 +55,17 @@ final class Connection implements EventEmitterInterface
5455
/** @var array<array{filter: (callable(\Bunny\Protocol\AbstractFrame): bool), promise: \React\Promise\Deferred<\Bunny\Protocol\AbstractFrame>}> */
5556
private array $awaitList = [];
5657

58+
/** @param (Closure(): int) $frameMax */
5759
public function __construct(
58-
private readonly Client $client,
60+
private readonly ClientInterface $client,
5961
private readonly ConnectionInterface $connection,
6062
private readonly Buffer $readBuffer,
6163
private readonly Buffer $writeBuffer,
6264
private readonly ProtocolReader $reader,
6365
private readonly ProtocolWriter $writer,
6466
private readonly Channels $channels,
6567
private readonly Configuration $configuration,
68+
private readonly Closure $frameMax,
6669
) {
6770
$this->connection->on('data', function (string $data): void {
6871
$this->readBuffer->append($data);
@@ -1699,7 +1702,7 @@ public function publish(int $channel, string $body, array $headers = [], string
16991702
}
17001703
}
17011704

1702-
for ($payloadMax = $this->client->frameMax - 8 /* frame preface and frame end */, $i = 0, $l = strlen($body); $i < $l; $i += $payloadMax) {
1705+
for ($payloadMax = ($this->frameMax)() - 8 /* frame preface and frame end */, $i = 0, $l = strlen($body); $i < $l; $i += $payloadMax) {
17031706
$payloadSize = $l - $i;
17041707
if ($payloadSize > $payloadMax) {
17051708
$payloadSize = $payloadMax;

src/Constants.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ final class Constants
2424

2525
public const FRAME_END = 0xCE;
2626

27+
public const FRAME_MAX = 0xFFFF;
28+
29+
public const CHANNEL_MAX = 0xFFFF;
30+
2731
// connection channel
2832
public const CONNECTION_CHANNEL = 0;
2933

test/ConnectionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function testThrowOn(): void
6666
new ProtocolWriter(),
6767
new Channels(),
6868
new Configuration(),
69+
static function (): int {
70+
return Constants::FRAME_MAX;
71+
},
6972
);
7073
$deferred = new Deferred();
7174
Loop::addTimer(0.1, async(static function () use ($deferred, $connection): void {
@@ -108,6 +111,9 @@ public function testNoMethodConnectionCloseOkFrameIsWrittenWhenConnectionIsClose
108111
new ProtocolWriter(),
109112
new Channels(),
110113
new Configuration(),
114+
static function (): int {
115+
return Constants::FRAME_MAX;
116+
},
111117
);
112118
$baseBuffer = $mockConnection->getWrittenData();
113119
self::assertSame('', $baseBuffer);
@@ -152,6 +158,9 @@ public function testNoMethodConnectionCloseOkFrameIsWrittenWhenConnectionIsClose
152158
new ProtocolWriter(),
153159
new Channels(),
154160
new Configuration(),
161+
static function (): int {
162+
return Constants::FRAME_MAX;
163+
},
155164
);
156165
$baseBuffer = $mockConnection->getWrittenData();
157166
self::assertSame('', $baseBuffer);

0 commit comments

Comments
 (0)