Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a special ProtobufError for truncated message #154

Closed
luser opened this issue May 4, 2016 · 4 comments
Closed

Add a special ProtobufError for truncated message #154

luser opened this issue May 4, 2016 · 4 comments

Comments

@luser
Copy link

luser commented May 4, 2016

I'm using rust-protobuf to send and receive protobuf messages over sockets. The server is using mio, and it tries to parse the message with whatever bytes it has received so far by using parse_length_delimited_from_bytes. Unfortunately, if the entire message hasn't been received yet, the only way to tell is to check that the returned error is a ProtobufError::WireError("truncated message"):
https://github.com/luser/sccache2/blob/1ad02689a9829502cd09b2b7381cec431a01e5a7/src/server.rs#L332

If would be nice if there was a separate ProtobufError::MessageTruncated so that I didn't have to do string comparison to catch this fairly likely case.

Maybe I'm missing something, if so I'd appreciate any info.

@stepancheg
Copy link
Owner

Protobuf messages are not self-delimited. Protobuf reads messages until EOF (or until explicitly specified limit).

When transferring messages over network, messages are usually prepended with length (with function like write_length_delimited_to). So when you read a message from network, first you need to read the length. After that you read the message only after buffer contains length bytes, and truncated message error does not occur.

ProtobufError::MessageTruncated should be added, but I think it is not the way you handle input from network.

See the explanation in protobuf manual.

@luser
Copy link
Author

luser commented May 12, 2016

Right, and that's what I'm doing, but it's pretty awkward to manually read the length like that. I'm doing that on the client side of my app currently, since parse_length_delimited_from doesn't work on a TcpStream if the other end doesn't shutdown its write handle after writing data:
https://github.com/luser/sccache2/blob/1ad02689a9829502cd09b2b7381cec431a01e5a7/src/client.rs#L51

It seems like at the very least the parse_length_delimited_ functions could pretty easily return a MessageTruncated error, which would make them a lot easier to use.

@luser
Copy link
Author

luser commented May 12, 2016

Related: #157 (the other issue I mentioned)

@stepancheg
Copy link
Owner

Closing due to old age.

jxs pushed a commit to jxs/rust-protobuf that referenced this issue Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants