Skip to content

Commit ad127f0

Browse files
committed
Refactor request writing
1 parent 4181d01 commit ad127f0

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/Rfc6455Connector.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Amp\CancellationToken;
66
use Amp\CancelledException;
7-
use Amp\Deferred;
87
use Amp\Http;
98
use Amp\Http\Rfc7230;
109
use Amp\Http\Status;
@@ -18,7 +17,6 @@
1817
use Amp\Websocket\Rfc6455Client;
1918
use Amp\Websocket\Rfc7692CompressionFactory;
2019
use League\Uri;
21-
use function Amp\asyncCall;
2220
use function Amp\call;
2321

2422
class Rfc6455Connector implements Connector
@@ -78,48 +76,39 @@ public function connect(
7876
throw new ConnectionException('Connecting to the websocket failed', 0, $exception);
7977
}
8078

81-
$deferred = new Deferred;
82-
$id = $cancellationToken->subscribe([$deferred, 'fail']);
79+
$id = $cancellationToken->subscribe([$socket, 'close']);
8380

84-
asyncCall(function () use ($socket, $handshake, $deferred) {
85-
try {
86-
$key = Websocket\generateKey();
87-
yield $socket->write($this->generateRequest($handshake, $key));
81+
try {
82+
$key = Websocket\generateKey();
83+
yield $socket->write($this->generateRequest($handshake, $key));
8884

89-
$buffer = '';
85+
$buffer = '';
9086

91-
while (($chunk = yield $socket->read()) !== null) {
92-
$buffer .= $chunk;
87+
while (($chunk = yield $socket->read()) !== null) {
88+
$buffer .= $chunk;
9389

94-
if ($position = \strpos($buffer, "\r\n\r\n")) {
95-
$headerBuffer = \substr($buffer, 0, $position + 4);
96-
$buffer = \substr($buffer, $position + 4);
90+
if ($position = \strpos($buffer, "\r\n\r\n")) {
91+
$headerBuffer = \substr($buffer, 0, $position + 4);
92+
$buffer = \substr($buffer, $position + 4);
9793

98-
$headers = $this->handleResponse($headerBuffer, $key);
94+
$headers = $this->handleResponse($headerBuffer, $key);
9995

100-
if ($buffer !== '') {
101-
$socket = new ClientSocket($socket, $buffer);
102-
}
96+
$socket = new ClientSocket($socket, $buffer);
10397

104-
$deferred->resolve($this->createConnection($socket, $handshake->getOptions(), $headers));
105-
return;
106-
}
98+
return $this->createConnection($socket, $handshake->getOptions(), $headers);
10799
}
108-
} catch (ConnectionException $exception) {
109-
$deferred->fail($exception);
110-
} catch (\Throwable $exception) {
111-
$deferred->fail(new ConnectionException('Performing the websocket handshake failed', 0, $exception));
112100
}
113-
});
114-
115-
try {
116-
return yield $deferred->promise();
117-
} catch (\Throwable $exception) {
118-
$socket->close(); // Close socket in case operation did not fail but was cancelled.
101+
} catch (ConnectionException $exception) {
119102
throw $exception;
103+
} catch (\Throwable $exception) {
104+
throw new ConnectionException('The websocket handshake failed', 0, $exception);
120105
} finally {
121106
$cancellationToken->unsubscribe($id);
122107
}
108+
109+
$cancellationToken->throwIfRequested(); // Connection may have closed due to cancellation request.
110+
111+
throw new ConnectionException('The socket closed without a response');
123112
});
124113
}
125114

0 commit comments

Comments
 (0)