matlab-msgpack icon indicating copy to clipboard operation
matlab-msgpack copied to clipboard

Reading binary file containing mutliple messages

Open dikouz opened this issue 7 years ago • 4 comments

When using the packer in C++ to write multiple messages in one binary file and reading this binary file in matlab, only the first message is read. Ideally, the parser would return a cell array of messages, or as a simpler solution, the indx that is calculated inside the parser should be returned to the user, in order to call again the parser with intput bytes(indx:end) and so on..

dikouz avatar May 23 '19 11:05 dikouz

I added the extra output to the parser and then did something like:

    messages = {};
    indx = 1;
    
    while indx < numel(bytes)
        [messages{end+1}, currIndx] = parsemsgpack(bytes(indx:end));  %#ok<AGROW>
        indx = indx + currIndx - 1;
    end

dikouz avatar May 23 '19 11:05 dikouz

Interesting. In principle, I'd argue that the right way to encode multiple values in msgpack is to use an array, instead of using multiple msgpack blocks.

However, the main function currently reads

function obj = parsemsgpack(bytes)
    [obj, ~] = parse(uint8(bytes(:)), 1);
end

If we changed that to

function [obj, idx] = parsemsgpack(bytes)
    [obj, idx] = parse(uint8(bytes(:)), 1);
end

it would return the number of bytes consumed by parsemsgpack, and you could continue from there if you wanted to. I see this is what you did in your code snippet as well.

Would you like to package this change up as a pull request?

bastibe avatar May 24 '19 06:05 bastibe

Thanks for the quick answer. Yes that's exactly what I did, I will send you a PR. I am not very familiar with msgpack yet and picked up an example that was using the packer and unpacker functionality in C++. So I was trying to get the same functionality in matlab. Is there a major disadvantage of this approach over creating an array? The thing is I want to collect binary data every time a function is called in the program and as thisdata may grow very large quickly, I thought it makes more sense to push the message to a file on each call not to have memory issues.

dikouz avatar May 24 '19 07:05 dikouz

Is there a major disadvantage of this approach over creating an array?

No significant downside, no, especially if you're trying to push out data in a streaming fashion. But if you had all the data available up front, using msgpack's built-in functionality would be preferable on principle--if only because then you'd be using a documented data format instead of an ad-hoc one.

bastibe avatar May 24 '19 11:05 bastibe