This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
protobuf.go
120 lines (108 loc) · 2.81 KB
/
protobuf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package encoding
import (
"encoding/binary"
"fmt"
"io"
"github.com/golang/protobuf/proto"
)
type Buffer struct {
*proto.Buffer
}
// Centralise proto.Buffer constructor. gogo protobuf freaks out about deterministic
// marshalling on objects with custom marshallers so we use google protobuf here
func NewBuffer(bs []byte) *Buffer {
buf := proto.NewBuffer(bs)
buf.SetDeterministic(true)
return &Buffer{Buffer: buf}
}
// Single shot encoding
func Encode(msg proto.Message) ([]byte, error) {
buf := NewBuffer(nil)
err := buf.Marshal(msg)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Single shot decoding
func Decode(bs []byte, msg proto.Message) error {
return NewBuffer(bs).Unmarshal(msg)
}
// Write messages with length-prefix framing to the provider Writer. Returns the number of bytes written.
func WriteMessage(w io.Writer, pb proto.Message) (int, error) {
const errHeader = "WriteMessage()"
buf := NewBuffer(nil)
err := buf.Marshal(pb)
if err != nil {
return 0, fmt.Errorf("%s: %v", errHeader, err)
}
// Write length prefix
bs := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(bs, int64(len(buf.Bytes())))
written, err := w.Write(bs[:n])
if err != nil {
return written, fmt.Errorf("%s: %v", errHeader, err)
}
// Write message
n, err = w.Write(buf.Bytes())
written += n
return written, nil
}
// Read messages with length-prefix framing from the provided Reader. Returns the number of bytes read and io.EOF if
// ReadMessage is called exactly on the end of a stream.
func ReadMessage(r io.Reader, pb proto.Message) (int, error) {
const errHeader = "ReadMessage()"
// Read varint
br := newByteReader(r)
msgLength, err := binary.ReadVarint(br)
if err != nil {
// Only return EOF if called precisely at the end of stream
if err == io.EOF && br.read == 0 {
return 0, io.EOF
}
return br.read, fmt.Errorf("%s: %v", errHeader, err)
}
read := br.read
// Use any message bytes at end of buffer
bs := make([]byte, msgLength)
n, err := r.Read(bs)
read += n
if err != nil {
return read, fmt.Errorf("%s: %v", errHeader, err)
}
if len(bs) != n {
return read, fmt.Errorf("%s: expected protobuf message of %d bytes but could only read %d bytes",
errHeader, msgLength, n)
}
err = proto.NewBuffer(bs).Unmarshal(pb)
if err != nil {
return read, fmt.Errorf("%s: %v", errHeader, err)
}
return read, nil
}
type byteReader struct {
io.Reader
byte []byte
read int
}
func newByteReader(r io.Reader) *byteReader {
return &byteReader{
Reader: r,
byte: make([]byte, 1),
}
}
func (br *byteReader) ReadByte() (byte, error) {
br.byte[0] = 0
n, err := br.Read(br.byte)
if err != nil {
return 0, err
}
if n == 0 {
return 0, io.EOF
}
br.read++
return br.byte[0], nil
}
func (tm TestMessage) String() string {
return fmt.Sprintf("{Type: %d, Amount: %d}", tm.Type, tm.Amount)
}