From 47db66f68004a3d0fbcf4cde3f8680900c0956a9 Mon Sep 17 00:00:00 2001 From: ubuntu20 Date: Tue, 29 Sep 2020 16:48:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPack=E7=B1=BB=E5=9B=A0?= =?UTF-8?q?=E5=8C=85=E4=BD=93=E8=BF=87=E5=B0=8F=E4=BA=A7=E7=9A=84=E8=AD=A6?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/coroutine.php | 46 ++++++++++++++++++++++++++++++++++++++++++ src/Pack.php | 4 ++-- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100755 examples/coroutine.php diff --git a/examples/coroutine.php b/examples/coroutine.php new file mode 100755 index 0000000..e38f4a1 --- /dev/null +++ b/examples/coroutine.php @@ -0,0 +1,46 @@ +set(array( + 'open_length_check' => true, + 'package_max_length' => 81920, + 'package_length_type' => 'N', + 'package_length_offset' => 9, + 'package_body_offset' => 13, + 'connect_timeout' => 1.0, + 'write_timeout' => 60.0, + 'read_timeout' => 60.0 + )); + + if (!$client->connect($host, $port)) { + echo "connect failed.\n"; + return false; + } + + + @$client->send($pack); + $pack = @$client->recv(); + if ($pack === false) { + echo $client->errCode . "\n"; + return false; + } else if (empty($pack)) { + echo "close by peer.\n"; + return false; + } + + $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); + $client->close(); + return $ret['data']; +}); + + +print_r($ret); \ No newline at end of file diff --git a/src/Pack.php b/src/Pack.php index 10957cc..0b6f156 100644 --- a/src/Pack.php +++ b/src/Pack.php @@ -132,8 +132,8 @@ class Pack { */ public function unpack(string $pack) { try { - $ret = unpack($this->makeFormat(true), $pack); - if ($ret['type'] === self::DATA_TYPE_SAMPLE_DATA && $ret['length'] == strlen($ret['data']) && (int) $ret['length'] <= self::$packageMaxLength) { + $ret = @unpack($this->makeFormat(true), $pack); + if (is_array($ret) && $ret['type'] === self::DATA_TYPE_SAMPLE_DATA && $ret['length'] == strlen($ret['data']) && (int) $ret['length'] <= self::$packageMaxLength) { return $ret; } } catch (\Exception $e) {