I tried to use parse_length_delimited_from_reader for parsing a protobuf Message from a TcpStream, but I think it calls read too many times on the underlying TcpStream and winds up blocking unless the other side calls shutdown(Shutdown::Write). I put together a fairly simple example client/server that demonstrates the problem:
https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/client.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/server.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/proto.proto
With the calls to stream.shutdown() commented out, the client hangs waiting for a response from the server, and the server hangs waiting for more data from the client. With those calls uncommented, everything works.
For this simple case, this workaround would be sufficient, but if I want to have the client and server exchange multiple messages this will not work. I wound up having to manually parse the length, call read_exact, and then call parse_from_bytes, which is quite a bit more code:
https://github.com/luser/sccache2/blob/d9e5315b9da649112ad08c5b15016b13c9a4f5ba/src/client.rs#L51
It seems like in the parse_length_delimited case, rust-protobuf ought to be able to do just this and not attempt to read more bytes than it needs, which would make this scenario much simpler to write.
I tried to use
parse_length_delimited_from_readerfor parsing a protobufMessagefrom aTcpStream, but I think it callsreadtoo many times on the underlyingTcpStreamand winds up blocking unless the other side callsshutdown(Shutdown::Write). I put together a fairly simple example client/server that demonstrates the problem:https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/client.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/server.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/proto.proto
With the calls to
stream.shutdown()commented out, the client hangs waiting for a response from the server, and the server hangs waiting for more data from the client. With those calls uncommented, everything works.For this simple case, this workaround would be sufficient, but if I want to have the client and server exchange multiple messages this will not work. I wound up having to manually parse the length, call
read_exact, and then callparse_from_bytes, which is quite a bit more code:https://github.com/luser/sccache2/blob/d9e5315b9da649112ad08c5b15016b13c9a4f5ba/src/client.rs#L51
It seems like in the
parse_length_delimitedcase, rust-protobuf ought to be able to do just this and not attempt to read more bytes than it needs, which would make this scenario much simpler to write.