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) {