Skip to content

Commit dc70675

Browse files
author
prophet777
committed
Recover when stream is reseted by peers
1 parent 0d69ad9 commit dc70675

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

Wamp/Client.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class Client
4444
/** @var bool */
4545
protected $secured;
4646

47+
/** @var string */
48+
protected $target;
49+
4750
/**
4851
* @param string $host
4952
* @param int|string $port
@@ -97,17 +100,19 @@ public function setAuthenticationToken()
97100
*/
98101
public function connect($target = '/')
99102
{
103+
$this->target = $target;
104+
100105
if ($this->connected) {
101106
return $this->sessionId;
102107
}
103108

104-
$this->socket = stream_socket_client($this->endpoint, $errno, $errstr);
109+
$this->socket = @stream_socket_client($this->endpoint, $errno, $errstr);
105110

106111
if (!$this->socket) {
107112
throw new BadResponseException('Could not open socket. Reason: ' . $errstr);
108113
}
109114

110-
$response = $this->upgradeProtocol($target);
115+
$response = $this->upgradeProtocol($this->target);
111116

112117
$this->verifyResponse($response);
113118

@@ -196,7 +201,7 @@ protected function read()
196201

197202
switch ($payloadLength) {
198203
case 126:
199-
$payloadLength = unpack("n", fread($this->socket, 2));
204+
$payloadLength = unpack('n', fread($this->socket, 2));
200205
$payloadLength = $payloadLength[1];
201206
break;
202207
case 127:
@@ -210,7 +215,7 @@ protected function read()
210215
/**
211216
* Disconnect.
212217
*
213-
* @return boolean
218+
* @return bool
214219
*/
215220
public function disconnect()
216221
{
@@ -225,15 +230,15 @@ public function disconnect()
225230
$payloadLength = ord(fread($this->socket, 1));
226231
$payload = fread($this->socket, $payloadLength);
227232

228-
if ($payloadLength >= 2) {
229-
$bin = $payload[0] . $payload[1];
230-
$status = bindec(sprintf("%08b%08b", ord($payload[0]), ord($payload[1])));
231-
}
232-
233233
if ($this->closing) {
234234
$this->closing = false;
235235
} else {
236-
$this->send($bin . 'Close acknowledged: ' . $status, WebsocketPayload::OPCODE_CLOSE);
236+
if ($payloadLength >= 2) {
237+
$bin = $payload[0] . $payload[1];
238+
$status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1])));
239+
240+
$this->send($bin . 'Close acknowledged: ' . $status, WebsocketPayload::OPCODE_CLOSE);
241+
}
237242
}
238243

239244
fclose($this->socket);
@@ -248,7 +253,6 @@ public function disconnect()
248253
/**
249254
* Send message to the websocket.
250255
*
251-
* @access private
252256
*
253257
* @param array $data
254258
*
@@ -264,7 +268,13 @@ protected function send($data, $opcode = WebsocketPayload::OPCODE_TEXT, $masked
264268
->setPayload($rawMessage);
265269

266270
$encoded = $payload->encodePayload();
267-
fwrite($this->socket, $encoded);
271+
272+
if (0 === @fwrite($this->socket, $encoded)) { //connection reseted by peers, just reconnect.
273+
$this->connected = false;
274+
$this->connect($this->target);
275+
276+
fwrite($this->socket, $encoded);
277+
}
268278

269279
return $this;
270280
}
@@ -297,7 +307,7 @@ public function call($procUri, $arguments = [])
297307
$args = func_get_args();
298308
array_shift($args);
299309
$type = Protocol::MSG_CALL;
300-
$callId = uniqid("", $moreEntropy = true);
310+
$callId = uniqid('', $moreEntropy = true);
301311
$data = array_merge(array($type, $callId, $procUri), $args);
302312

303313
$this->send($data);

Wamp/WebsocketPayload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function maskData($data, $key)
195195
{
196196
$masked = '';
197197

198-
for ($i = 0; $i < strlen($data); $i++) {
198+
for ($i = 0; $i < strlen($data); ++$i) {
199199
$masked .= $data[$i] ^ $key[$i % 4];
200200
}
201201

0 commit comments

Comments
 (0)