-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
94 lines (81 loc) · 1.55 KB
/
encoding.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
package emux
import (
"encoding/binary"
"io"
)
type Encode struct {
io.Writer
buf [binary.MaxVarintLen64 + 1]byte
}
func NewEncode(w io.Writer) *Encode {
return &Encode{
Writer: w,
}
}
func (e *Encode) WriteUvarint(v uint64) error {
n := binary.PutUvarint(e.buf[:], v)
_, err := e.Write(e.buf[:n])
return err
}
func (e *Encode) WriteBytes(b []byte) error {
err := e.WriteUvarint(uint64(len(b)))
if err != nil {
return err
}
if len(b) > 0 {
_, err = e.Write(b)
}
return err
}
func (e *Encode) WriteByte(b byte) error {
e.buf[0] = b
_, err := e.Write(e.buf[:1])
return err
}
func (e *Encode) WriteCmd(cmd uint8, sid uint64) error {
e.buf[0] = cmd
n := binary.PutUvarint(e.buf[1:], sid)
_, err := e.Write(e.buf[:n+1])
return err
}
type Decode struct {
io.Reader
buf [1]byte
}
func NewDecode(r io.Reader) *Decode {
return &Decode{
Reader: r,
}
}
func (d *Decode) ReadUvarint() (uint64, error) {
return binary.ReadUvarint(d)
}
func (d *Decode) ReadByte() (byte, error) {
if r, ok := d.Reader.(ByteReader); ok {
return r.ReadByte()
}
_, err := d.Read(d.buf[:1])
if err != nil {
return 0, err
}
return d.buf[0], nil
}
func (d *Decode) ReadBytes() ([]byte, error) {
i, err := d.ReadUvarint()
if err != nil {
return nil, err
}
if i == 0 {
return []byte{}, nil
}
buf := make([]byte, i)
_, err = io.ReadFull(d, buf)
return buf, err
}
func (d *Decode) WriteTo(w io.Writer, buf []byte) (int64, error) {
i, err := d.ReadUvarint()
if err != nil {
return 0, err
}
return io.CopyBuffer(w, io.LimitReader(d, int64(i)), buf)
}