Repository navigation
Expand file tree
/
Copy pathvu32io.go
More file actions
192 lines (173 loc) · 3.32 KB
/
Copy pathvu32io.go
File metadata and controls
192 lines (173 loc) · 3.32 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2016 The Gini Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package crisp
import (
"fmt"
"github.com/irifrance/gini/z"
"io"
)
// This file implements varint encoding/decoding + buffered io
// for uint32s. We don't use encoding/binary varint because
//
// 1. encoding/binary is 64bit
// 2. encoding/binary keeps a separate buffer for each value, we code directly
// to the underlying buffer of the buffered io.
// 3. we are not safe for multiple goroutines (unliked buffered io)
// 4. encoding/binary's buffered io needs a separate buffer for reading and
// writing, we don't in this context.
type vu32io struct {
rw io.ReadWriter
buf []byte
r, w int
}
const (
VarUintMask = uint32((1 << 7) - 1)
)
func newVu32Io(rw io.ReadWriter) *vu32io {
return &vu32io{
rw: rw,
r: 0,
w: 0,
buf: make([]byte, 1024)}
}
func (io *vu32io) readu32() (uint32, error) {
res := uint32(0)
s := uint32(0)
var b byte
var e error
for i := 0; i < 5; i++ {
b, e = io.readByte()
if e != nil {
return 0, e
}
z := uint32(b) & VarUintMask
res = res | (z << s)
if b&(1<<7) == 0 {
return res, nil
}
s += 7
}
return 0, ErrVarUint
}
func (io *vu32io) readu32Data() (uint32, error) {
n, e := io.readu32()
if e != nil {
return 0, e
}
if n >= uint32(MinProto) {
return n, ErrNotLit
}
return n, nil
}
func (io *vu32io) writeu32(d uint32) error {
var b byte
for {
b = byte(d & VarUintMask)
d = d >> 7
if d > 0 {
b |= (1 << 7)
if e := io.writeByte(b); e != nil {
return e
}
continue
}
if e := io.writeByte(b); e != nil {
return e
}
return nil
}
}
func (io *vu32io) send(ms []z.Lit) error {
for _, m := range ms {
if e := io.writeu32(uint32(m)); e != nil {
return e
}
}
return io.writeu32(uint32(0))
}
func (io *vu32io) recv(dst []z.Lit) ([]z.Lit, error) {
for {
d, e := io.readu32()
if e != nil {
return dst, e
}
if d == 0 {
return dst, nil
}
if d >= uint32(MinProto) {
return dst, ErrNotLit
}
dst = append(dst, z.Lit(d))
}
}
func (io *vu32io) writeflush(u uint32) error {
if e := io.writeu32(u); e != nil {
return e
}
return io.flush()
}
func (io *vu32io) readByte() (byte, error) {
if io.r >= io.w {
if e := io.fill(); e != nil {
return 0, e
}
}
b := io.buf[io.r]
io.r++
return b, nil
}
func (io *vu32io) writeByte(b byte) error {
if io.r >= len(io.buf) {
if e := io.flush(); e != nil {
return e
}
}
io.buf[io.r] = b
io.r++
return nil
}
func (vio *vu32io) fill() error {
if vio.r > vio.w {
return fmt.Errorf("fill without write flush")
}
if vio.r == vio.w {
vio.r = 0
vio.w = 0
}
// shouldn't give both zero length read and nil error
// but just in case we try a few times
for i := 0; i < 10; i++ {
nn, e := vio.rw.Read(vio.buf[vio.w:])
vio.w += nn
if nn > 0 {
return nil
}
if e != nil {
return e
}
//log.Printf("vu32io read without data %d/%d\n", vio.r, vio.w)
}
return fmt.Errorf("vu32io: too many reads without data\n")
}
func (io *vu32io) flush() error {
n := io.r
m := io.w
k := 0
for m < n {
o, e := io.rw.Write(io.buf[m:n])
m += o
if o > 0 {
k = 0
}
if e != nil {
return e
}
k++
if k >= 10 {
return fmt.Errorf("repeated zero length no-error writes.")
}
}
io.r = 0
io.w = 0
return nil
}