forked from sei-protocol/sei-tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
134 lines (122 loc) · 3.93 KB
/
writer.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Modified from original GoGo Protobuf to return number of bytes written.
package protoio
import (
"bytes"
"encoding/binary"
"io"
"sync"
"github.com/gogo/protobuf/proto"
)
// NewDelimitedWriter writes a varint-delimited Protobuf message to a writer. It is
// equivalent to the gogoproto NewDelimitedWriter, except WriteMsg() also returns the
// number of bytes written, which is necessary in the p2p package.
func NewDelimitedWriter(w io.Writer) WriteCloser {
return &varintWriter{w, make([]byte, binary.MaxVarintLen64), nil}
}
type varintWriter struct {
w io.Writer
lenBuf []byte
buffer []byte
}
func (w *varintWriter) WriteMsg(msg proto.Message) (int, error) {
if m, ok := msg.(marshaler); ok {
n, ok := getSize(m)
if ok {
if n+binary.MaxVarintLen64 >= len(w.buffer) {
w.buffer = make([]byte, n+binary.MaxVarintLen64)
}
lenOff := binary.PutUvarint(w.buffer, uint64(n))
_, err := m.MarshalTo(w.buffer[lenOff:])
if err != nil {
return 0, err
}
_, err = w.w.Write(w.buffer[:lenOff+n])
return lenOff + n, err
}
}
// fallback
data, err := proto.Marshal(msg)
if err != nil {
return 0, err
}
length := uint64(len(data))
n := binary.PutUvarint(w.lenBuf, length)
_, err = w.w.Write(w.lenBuf[:n])
if err != nil {
return 0, err
}
_, err = w.w.Write(data)
return len(data) + n, err
}
func (w *varintWriter) Close() error {
if closer, ok := w.w.(io.Closer); ok {
return closer.Close()
}
return nil
}
func varintWrittenBytes(m marshaler, size int) ([]byte, error) {
buf := make([]byte, size+binary.MaxVarintLen64)
n := binary.PutUvarint(buf, uint64(size))
nw, err := m.MarshalTo(buf[n:])
if err != nil {
return nil, err
}
return buf[:n+nw], nil
}
var bufPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func MarshalDelimited(msg proto.Message) ([]byte, error) {
// The goal here is to write proto message as is knowning already if
// the exact size can be retrieved and if so just use that.
if m, ok := msg.(marshaler); ok {
size, ok := getSize(msg)
if ok {
return varintWrittenBytes(m, size)
}
}
// Otherwise, go down the route of using proto.Marshal,
// and use the buffer pool to retrieve a writer.
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
buf.Reset()
_, err := NewDelimitedWriter(buf).WriteMsg(msg)
if err != nil {
return nil, err
}
// Given that we are reusing buffers, we should
// make a copy of the returned bytes.
bytesCopy := make([]byte, buf.Len())
copy(bytesCopy, buf.Bytes())
return bytesCopy, nil
}