Browse Source

修复Pack类因包体过小产的警告

master
ubuntu20 4 years ago
parent
commit
47db66f680
  1. 46
      examples/coroutine.php
  2. 4
      src/Pack.php

46
examples/coroutine.php

@ -0,0 +1,46 @@
<?php
$host = '127.0.0.1';
$port = 5188;
$data = '{"controller":"site","methor":"test","params":{"id":1}}';
$length = strlen($data);
$pack = pack('CNNNa*', 1, time(), 0, $length, $data);
$ret = Swoole\Coroutine::create(function()use($host, $port, $pack) {
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
$client->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);

4
src/Pack.php

@ -132,8 +132,8 @@ class Pack {
*/ */
public function unpack(string $pack) { public function unpack(string $pack) {
try { 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; return $ret;
} }
} catch (\Exception $e) { } catch (\Exception $e) {

Loading…
Cancel
Save