/
sio.go
239 lines (205 loc) · 4.71 KB
/
sio.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sio
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"reflect"
)
// Reader is the interface that wraps the basic io.Reader interface
// and adds SIO pointer tagging capabilities.
type Reader interface {
io.Reader
Versioner
Tag(ptr interface{}) error
Pointer(ptr interface{}) error
}
// Writer is the interface that wraps the basic io.Writer interface
// and adds SIO pointer tagging capabilities.
type Writer interface {
io.Writer
Versioner
Tag(ptr interface{}) error
Pointer(ptr interface{}) error
}
// Marshaler is the interface implemented by an object that can marshal
// itself into a binary, sio-compatible, form.
type Marshaler interface {
MarshalSio(w Writer) error
}
// Unmarshaler is the interface implemented by an object that can
// unmarshal a binary, sio-compatible, representation of itself.
type Unmarshaler interface {
UnmarshalSio(r Reader) error
}
// Code is the interface implemented by an object that can
// unmarshal and marshal itself from and to a binary, sio-compatible, form.
type Codec interface {
Marshaler
Unmarshaler
}
// Linker is the interface implemented by an object that
// needs to recompute (internal) pointers, after the sio layer
// had performed pointer tagging/chasing relocation.
type Linker interface {
LinkSio(v uint32) error
}
// Versioner is the interface implemented by an object that
// tells which version of SIO serialization/deserialization it supports.
type Versioner interface {
VersionSio() uint32
}
type reader struct {
buf *bytes.Buffer
ver uint32
ptr map[interface{}]uint32
tag map[interface{}]uint32
}
func newReader(data []byte) *reader {
return &reader{
buf: bytes.NewBuffer(data),
ptr: make(map[interface{}]uint32),
tag: make(map[interface{}]uint32),
}
}
func (r *reader) Read(data []byte) (int, error) {
return r.buf.Read(data)
}
func (r *reader) Bytes() []byte {
return r.buf.Bytes()
}
func (r *reader) Len() int {
return r.buf.Len()
}
func (r *reader) Next(n int) []byte {
return r.buf.Next(n)
}
func (r *reader) VersionSio() uint32 {
min := r.ver & uint32(0x0000ffff)
maj := (r.ver & uint32(0xffff0000)) >> 16
return maj*1000 + min
}
func (r *reader) Tag(ptr interface{}) error {
var pid uint32
err := binary.Read(r.buf, binary.BigEndian, &pid)
if err != nil {
return err
}
if pid == ptagMarker {
return nil
}
r.tag[ptr] = pid
return nil
}
func (r *reader) Pointer(ptr interface{}) error {
rptr := reflect.ValueOf(ptr)
if !(rptr.Kind() == reflect.Ptr && (rptr.Elem().Kind() == reflect.Ptr || rptr.Elem().Kind() == reflect.Interface)) {
panic(fmt.Errorf("sio: Reader.Pointer expects a pointer to pointer"))
}
var pid uint32
err := binary.Read(r, binary.BigEndian, &pid)
if err != nil {
return err
}
if pid == pntrMarker {
return nil
}
r.ptr[ptr] = pid
return nil
}
func (r *reader) relocate() {
ptrloop:
for ptr, pid := range r.ptr {
rptr := reflect.ValueOf(ptr)
for tag, tid := range r.tag {
if tid == pid {
rtag := reflect.ValueOf(tag)
rptr.Elem().Set(rtag)
continue ptrloop
}
}
}
}
type writer struct {
buf *bytes.Buffer
ver uint32
ids uint32
ptr map[uint32]interface{}
tag map[interface{}]uint32
}
func newWriter() *writer {
return &writer{
buf: new(bytes.Buffer),
ptr: make(map[uint32]interface{}),
tag: make(map[interface{}]uint32),
}
}
func newWriterFrom(w *writer) *writer {
return &writer{
buf: new(bytes.Buffer),
ver: w.ver,
ids: w.ids,
ptr: w.ptr,
tag: w.tag,
}
}
func (w *writer) Write(data []byte) (int, error) {
return w.buf.Write(data)
}
func (w *writer) Bytes() []byte {
return w.buf.Bytes()
}
func (w *writer) Len() int {
return w.buf.Len()
}
func (w *writer) VersionSio() uint32 {
min := w.ver & uint32(0x0000ffff)
maj := (w.ver & uint32(0xffff0000)) >> 16
return maj*1000 + min
}
func (w *writer) Tag(ptr interface{}) error {
var id uint32 = ptagMarker
if _, ok := w.tag[ptr]; !ok {
err := w.genID()
if err != nil {
return err
}
w.tag[ptr] = w.ids
}
id = w.tag[ptr]
err := binary.Write(w.buf, binary.BigEndian, &id)
if err != nil {
return err
}
return nil
}
func (w *writer) Pointer(ptr interface{}) error {
ptr = reflect.ValueOf(ptr).Elem().Interface()
var id uint32 = pntrMarker
if _, ok := w.tag[ptr]; !ok {
err := w.genID()
if err != nil {
return err
}
w.tag[ptr] = w.ids
}
id = w.tag[ptr]
err := binary.Write(w.buf, binary.BigEndian, &id)
if err != nil {
return err
}
return nil
}
func (w *writer) genID() error {
if w.ids+1 == math.MaxUint32 {
return errPointerIDOverflow
}
w.ids++
return nil
}
var _ Reader = (*reader)(nil)
var _ Writer = (*writer)(nil)