function test($type, $variable, $object, $result = null) { $msgpack = new MessagePack(); $serialized = $msgpack->pack($variable); $unserialized = $msgpack->unpack($serialized, $object); var_dump($unserialized); if ($result) { echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL; } else { echo 'SKIP', PHP_EOL; } }
function test($type, $variable, $test = null) { $msgpack = new MessagePack(); $serialized = $msgpack->pack($variable); $unserialized = $msgpack->unpack($serialized); var_dump($unserialized); if (!is_bool($test)) { echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL; } else { echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL; } }
function test($type, $variable, $test = null) { $msgpack = new MessagePack(); if (version_compare(PHP_VERSION, '5.1.0') < 0) { $msgpack->setOption(MESSAGEPACK_OPT_PHPONLY, false); } else { $msgpack->setOption(MessagePack::OPT_PHPONLY, false); } $serialized = $msgpack->pack($variable); $unpacker = new MessagePackUnpacker(); if (version_compare(PHP_VERSION, '5.1.0') < 0) { $unpacker->setOption(MESSAGEPACK_OPT_PHPONLY, false); } else { $unpacker->setOption(MessagePack::OPT_PHPONLY, false); } $length = strlen($serialized); if (rand(0, 1)) { for ($i = 0; $i < $length;) { $len = rand(1, 10); $str = substr($serialized, $i, $len); $unpacker->feed($str); if ($unpacker->execute()) { $unserialized = $unpacker->data(); var_dump($unserialized); $unpacker->reset(); } $i += $len; } } else { $str = ""; $offset = 0; for ($i = 0; $i < $length;) { $len = rand(1, 10); $str .= substr($serialized, $i, $len); if ($unpacker->execute($str, $offset)) { $unserialized = $unpacker->data(); var_dump($unserialized); $unpacker->reset(); $str = ""; $offset = 0; } $i += $len; } } if (!is_bool($test)) { echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL; } else { echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL; } }
function test($type, $variable, $object, $result = null) { $msgpack = new MessagePack(); if (version_compare(PHP_VERSION, '5.1.0') <= 0) { $msgpack->setOption(MESSAGEPACK_OPT_PHPONLY, false); } else { $msgpack->setOption(MessagePack::OPT_PHPONLY, false); } $serialized = $msgpack->pack($variable); $unserialized = $msgpack->unpack($serialized, $object); var_dump($unserialized); if ($result) { echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL; } else { echo 'SKIP', PHP_EOL; } }
function test($type, $variable, $object, $result = null) { $msgpack = new MessagePack(); $msgpack->setOption(MessagePack::OPT_PHPONLY, false); $serialized = $msgpack->pack($variable); $unserialized = null; $unpacker = $msgpack->unpacker(); if ($unpacker->execute($serialized)) { $unserialized = $unpacker->data($object); } var_dump($unserialized); if ($result) { echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL; } else { echo 'SKIP', PHP_EOL; } }
function test($type, $variable, $test = null) { $msgpack = new MessagePack(); if (version_compare(PHP_VERSION, '5.1.0') < 0) { $msgpack->setOption(MESSAGEPACK_OPT_PHPONLY, false); } else { $msgpack->setOption(MessagePack::OPT_PHPONLY, false); } $serialized = $msgpack->pack($variable); $unserialized = $msgpack->unpack($serialized); var_dump($unserialized); if (!is_bool($test)) { echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL; } else { echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL; } }
public function tryMsgPackSend($sendmg = null, $sizerp = 1024) { // TODO: Event Loop Implementation // TODO: Socket Stream while (true) { $read = null; $write = array($this->cltf); $except = null; $w = stream_select($read, $write, $except, 0, 100 * self::USECONDS); if ($w === false) { // TODO: error throw new RuntimeException('write stream'); } if ($w === 0) { continue; } // TODO: stream write $this->fwrite($sendmg); break; } $unpacker = new MessagePack(); $unpacker->initialize(); $buffer = ''; while (true) { $read = array($this->cltf); $write = null; $except = null; $r = stream_select($read, $write, $except, 0, 20000); if ($r === false) { // TODO: error throw new RuntimeException('read stream'); } if ($r === 0) { continue; } $buffer .= fread($this->cltf, $sizerp); $nread = $unpacker->execute($buffer, $nread); if ($unpacker->finished()) { break; } } $this->tryConnClosing(); $data = $unpacker->data(); $this->cbMsgsReceived($data); }
function test() { $serialized = msgpack_serialize(null); $serialized = substr($serialized, 0, -1); $length = mt_rand(1, 255); for ($i = 0; $i < $length; ++$i) { $serialized .= chr(mt_rand(0, 255)); } // if returned null everything is OK $msgpack = new MessagePack(); if (($unserialized = $msgpack->unpack($serialized)) === null) { return true; } // whole data is read? if ($serialized !== msgpack_serialize($unserialized)) { return true; } echo bin2hex($serialized), "\n"; var_dump($unserialized); return false; }
function test($type, $variable, $test = null) { $msgpack = new MessagePack(); $serialized = $msgpack->pack($variable); $unpacker = new MessagePackUnpacker(); $length = strlen($serialized); if (rand(0, 1)) { for ($i = 0; $i < $length;) { $len = rand(1, 10); $str = substr($serialized, $i, $len); $unpacker->feed($str); if ($unpacker->execute()) { $unserialized = $unpacker->data(); var_dump($unserialized); $unpacker->reset(); } $i += $len; } } else { $str = ""; $offset = 0; for ($i = 0; $i < $length;) { $len = rand(1, 10); $str .= substr($serialized, $i, $len); if ($unpacker->execute($str, $offset)) { $unserialized = $unpacker->data(); var_dump($unserialized); $unpacker->reset(); $str = ""; $offset = 0; } $i += $len; } } if (!is_bool($test)) { echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL; } else { echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL; } }
<?php // serialized data $msgs = array(pack("C*", 0x93, 0x1, 0x2, 0x3, 0x92), pack("C*", 0x3, 0x9, 0x4)); // streaming deserialize $unpacker = new MessagePack(); $unpacker->initialize(); $buffer = ""; $nread = 0; foreach ($msgs as $msg) { $buffer = $buffer . $msg; while (true) { $nread = $unpacker->execute($buffer, $nread); if ($unpacker->finished()) { $msg = $unpacker->data(); var_dump($msg); $unpacker->initialize(); $buffer = substr($buffer, $nread); $nread = 0; if (!empty($buffer)) { continue; } } break; } } ?>