Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(115)

Unified Diff: proto/decode.go

Issue 9102043: Support streaming record length-encoded messages.
Patch Set: Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « proto/all_test.go ('k') | proto/encode.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: proto/decode.go
===================================================================
--- a/proto/decode.go
+++ b/proto/decode.go
@@ -699,3 +699,34 @@
return err
}
+
+// ReadDelimited decodes a message from the provided length-delimited stream,
+// where the length is encoded as 64-bit varint prefix to the message body.
+// It returns the total number of bytes read and any applicable error.
+func ReadDelimited(r io.Reader, m Message) (n int, err error) {
+ buffer := make([]byte, maxVarintBytes)
+
+ headerLength, err := r.Read(buffer)
+ if err != nil {
+ return headerLength, err
+ }
+ if headerLength == 0 || int(buffer[0]) == -1 {
+ return headerLength, io.EOF
+ }
+
+ messageLength, syncLength := DecodeVarint(buffer)
+ buffer = buffer[syncLength:]
+
+ remainder := make([]byte, int(messageLength)-len(buffer))
+ remainderLength, err := r.Read(remainder)
+ if err != nil {
+ return headerLength + remainderLength, err
+ }
+ if len(buffer)+len(remainder) != int(messageLength) {
+ return headerLength + remainderLength, fmt.Errorf("truncated message")
+ }
+
+ buffer = append(buffer, remainder...)
+
+ return headerLength + remainderLength, Unmarshal(buffer, m)
+}
« no previous file with comments | « proto/all_test.go ('k') | proto/encode.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b