Code Monkey home page Code Monkey logo

Comments (2)

rybakit avatar rybakit commented on May 23, 2024

A non-empty result of the $unpacker->tryUnpack() call indicates that the entire msgpack message (or several) has been received and unpacked. If you expect only one message from a stream, you can just do:

if ($unpackedBlocks = $unpacker->tryUnpack()) {
    return $unpackedBlocks[0];
}

If you receive an unknown number of msgpack messages from the stream, then I guess feof() is the right way to break the loop.

from msgpack.php.

decebal avatar decebal commented on May 23, 2024

that was so simple, I can't believe I didn't figure that out. I was trying to compare the lengths somehow..
Here's my snippet now:

         stream_set_blocking($io, 0);
         $unpackedBlocks = null;
         $unpacker = new BufferUnpacker();

        while (!feof($io)) {
            $r = array($io);
            $n = null;
            stream_select($r, $n, $n, null);
            $read = fread($io, $size);
            if ($read === false) {
                throw new MessagePackRPCNetworkException(error_get_last());
            }
            $unpacker->append($read);
            $unpackedBlocks = $unpacker->tryUnpack();
            if ($unpackedBlocks) {
                  return $unpackedBlocks;
            }
        }

        throw new RPCNetworkException("Could not read the server response!");

Also this allows me to process multiple messages at once!

from msgpack.php.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.