From 38e871822487ddc3b5eebecd03213a151497d9c3 Mon Sep 17 00:00:00 2001 From: yufewell Date: Mon, 18 Nov 2019 14:53:21 +0800 Subject: [PATCH 1/5] readBytes fix-read-endless-loop --- src/Engine/AbstractSocketIO.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Engine/AbstractSocketIO.php b/src/Engine/AbstractSocketIO.php index 3a09b19..70310f4 100644 --- a/src/Engine/AbstractSocketIO.php +++ b/src/Engine/AbstractSocketIO.php @@ -113,6 +113,15 @@ protected function readBytes($bytes) $data = ''; $chunk = null; while ($bytes > 0 && false !== ($chunk = \fread($this->stream, $bytes))) { + // if endless loop, break + if (strlen($chunk) == 0) { + $stream = stream_get_meta_data($this->stream); + + if (isset($stream['timed_out']) && $stream['timed_out'] == true) { + break; + } + } + $bytes -= \strlen($chunk); $data .= $chunk; } From 8e12a6e4a7c75a0cae32b2df5e63ca0e7454c262 Mon Sep 17 00:00:00 2001 From: yufewell Date: Tue, 19 Nov 2019 12:05:57 +0800 Subject: [PATCH 2/5] Update src/Engine/AbstractSocketIO.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Baptiste ClaviƩ --- src/Engine/AbstractSocketIO.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/AbstractSocketIO.php b/src/Engine/AbstractSocketIO.php index 70310f4..94c9617 100644 --- a/src/Engine/AbstractSocketIO.php +++ b/src/Engine/AbstractSocketIO.php @@ -114,7 +114,7 @@ protected function readBytes($bytes) $chunk = null; while ($bytes > 0 && false !== ($chunk = \fread($this->stream, $bytes))) { // if endless loop, break - if (strlen($chunk) == 0) { + if (\strlen($chunk) === 0) { $stream = stream_get_meta_data($this->stream); if (isset($stream['timed_out']) && $stream['timed_out'] == true) { From d2b0b9d99e48bba721b3960bb86f0eea07090c70 Mon Sep 17 00:00:00 2001 From: yufewell Date: Tue, 19 Nov 2019 12:06:18 +0800 Subject: [PATCH 3/5] Update src/Engine/AbstractSocketIO.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Baptiste ClaviƩ --- src/Engine/AbstractSocketIO.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/AbstractSocketIO.php b/src/Engine/AbstractSocketIO.php index 94c9617..0b98daf 100644 --- a/src/Engine/AbstractSocketIO.php +++ b/src/Engine/AbstractSocketIO.php @@ -117,7 +117,7 @@ protected function readBytes($bytes) if (\strlen($chunk) === 0) { $stream = stream_get_meta_data($this->stream); - if (isset($stream['timed_out']) && $stream['timed_out'] == true) { + if (isset($stream['timed_out']) && $stream['timed_out']) { break; } } From 851b2cc782803e2c002c646a80a8e107ccbe6199 Mon Sep 17 00:00:00 2001 From: yufewell Date: Mon, 2 Dec 2019 20:36:44 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9Version1X,AbstractSocketI?= =?UTF-8?q?O?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Engine/AbstractSocketIO.php | 15 ++++++++------- src/Engine/SocketIO/Version1X.php | 9 +++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Engine/AbstractSocketIO.php b/src/Engine/AbstractSocketIO.php index 70310f4..aa17f65 100644 --- a/src/Engine/AbstractSocketIO.php +++ b/src/Engine/AbstractSocketIO.php @@ -21,6 +21,7 @@ use ElephantIO\Payload\Decoder; use ElephantIO\Exception\UnsupportedActionException; use ElephantIO\Exception\MalformedUrlException; +use ElephantIO\Exception\ServerConnectionFailureException; abstract class AbstractSocketIO implements EngineInterface { @@ -112,18 +113,15 @@ protected function readBytes($bytes) { $data = ''; $chunk = null; + $i = 0; while ($bytes > 0 && false !== ($chunk = \fread($this->stream, $bytes))) { - // if endless loop, break - if (strlen($chunk) == 0) { - $stream = stream_get_meta_data($this->stream); - - if (isset($stream['timed_out']) && $stream['timed_out'] == true) { - break; - } + if ($i > 2) { + throw new ServerConnectionFailureException('fread times error'); } $bytes -= \strlen($chunk); $data .= $chunk; + $i++; } if (false === $chunk) { @@ -141,10 +139,12 @@ protected function readBytes($bytes) public function read() { if (!\is_resource($this->stream)) { + \Log::error('stream not resource'); return; } $this->keepAlive(); + /* * The first byte contains the FIN bit, the reserved bits, and the * opcode... We're not interested in them. Yet. @@ -154,6 +154,7 @@ public function read() $bytes = \unpack('C*', $data); if (empty($bytes[2])){ + \Log::error('bytes[2] empty'); return; } diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index ea2d7ef..5c40dd6 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -126,6 +126,7 @@ public function write($code, $message = null) } $payload = new Encoder($code . $message, Encoder::OPCODE_TEXT, true); + $bytes = @\fwrite($this->stream, (string) $payload); if ($bytes === false){ @@ -312,4 +313,12 @@ public function keepAlive() $this->write(static::PING); } } + + /** + * @return resource + */ + public function getResource() + { + return $this->stream; + } } From 1b71b1bb9942165238a0d413a777e0e0c44481ca Mon Sep 17 00:00:00 2001 From: yufewell Date: Tue, 3 Dec 2019 12:31:18 +0800 Subject: [PATCH 5/5] delete unused code --- src/Engine/AbstractSocketIO.php | 2 -- src/Engine/SocketIO/Version1X.php | 8 -------- 2 files changed, 10 deletions(-) diff --git a/src/Engine/AbstractSocketIO.php b/src/Engine/AbstractSocketIO.php index aa17f65..548df6c 100644 --- a/src/Engine/AbstractSocketIO.php +++ b/src/Engine/AbstractSocketIO.php @@ -139,7 +139,6 @@ protected function readBytes($bytes) public function read() { if (!\is_resource($this->stream)) { - \Log::error('stream not resource'); return; } @@ -154,7 +153,6 @@ public function read() $bytes = \unpack('C*', $data); if (empty($bytes[2])){ - \Log::error('bytes[2] empty'); return; } diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index 5c40dd6..3c48b45 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -313,12 +313,4 @@ public function keepAlive() $this->write(static::PING); } } - - /** - * @return resource - */ - public function getResource() - { - return $this->stream; - } }