forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
189 lines (168 loc) · 3.45 KB
/
encoder.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
package parser
import (
"bufio"
"encoding/json"
"errors"
"io"
"reflect"
engineio "github.com/googollee/go-engine.io"
)
type FrameWriter interface {
NextWriter(ft engineio.FrameType) (io.WriteCloser, error)
}
type Encoder struct {
w FrameWriter
}
func NewEncoder(w FrameWriter) *Encoder {
return &Encoder{
w: w,
}
}
func (e *Encoder) Encode(h Header, args []interface{}) (err error) {
var w io.WriteCloser
w, err = e.w.NextWriter(engineio.TEXT)
if err != nil {
return
}
var buffers [][]byte
buffers, err = e.writePacket(w, h, args)
if err != nil {
return
}
for _, b := range buffers {
w, err = e.w.NextWriter(engineio.BINARY)
if err != nil {
return
}
err = e.writeBuffer(w, b)
if err != nil {
return
}
}
return
}
type byteWriter interface {
io.Writer
WriteByte(byte) error
}
type flusher interface {
Flush() error
}
func (e *Encoder) writePacket(w io.WriteCloser, h Header, args []interface{}) ([][]byte, error) {
defer w.Close()
bw, ok := w.(byteWriter)
if !ok {
bw = bufio.NewWriter(w)
}
max := uint64(0)
buffers, err := e.attachBuffer(reflect.ValueOf(args), &max)
if err != nil {
return nil, err
}
if len(buffers) > 0 && (h.Type == Event || h.Type == Ack) {
h.Type += 3
}
if err := bw.WriteByte(byte(h.Type + '0')); err != nil {
return nil, err
}
if h.Type == binaryAck || h.Type == binaryEvent {
if err := e.writeUint64(bw, max); err != nil {
return nil, err
}
if err := bw.WriteByte('-'); err != nil {
return nil, err
}
}
if h.Namespace != "" {
if _, err := bw.Write([]byte(h.Namespace)); err != nil {
return nil, err
}
if h.ID != 0 || args != nil {
if err := bw.WriteByte(','); err != nil {
return nil, err
}
}
}
if h.NeedAck {
if err := e.writeUint64(bw, h.ID); err != nil {
return nil, err
}
}
if args != nil {
if err := json.NewEncoder(bw).Encode(args); err != nil {
return nil, err
}
}
if f, ok := bw.(flusher); ok {
if err := f.Flush(); err != nil {
return nil, err
}
}
return buffers, nil
}
func (e *Encoder) writeUint64(w byteWriter, i uint64) error {
base := uint64(1)
for i/base >= 10 {
base *= 10
}
for base > 0 {
p := i / base
if err := w.WriteByte(byte(p) + '0'); err != nil {
return err
}
i -= p * base
base /= 10
}
return nil
}
func (e *Encoder) attachBuffer(v reflect.Value, index *uint64) ([][]byte, error) {
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
var ret [][]byte
switch v.Kind() {
case reflect.Struct:
if v.Type().Name() == "Buffer" {
if !v.CanAddr() {
return nil, errors.New("can't get Buffer address")
}
buffer := v.Addr().Interface().(*Buffer)
buffer.num = *index
buffer.isBinary = true
ret = append(ret, buffer.Data)
*index++
} else {
for i := 0; i < v.NumField(); i++ {
b, err := e.attachBuffer(v.Field(i), index)
if err != nil {
return nil, err
}
ret = append(ret, b...)
}
}
case reflect.Array:
fallthrough
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
b, err := e.attachBuffer(v.Index(i), index)
if err != nil {
return nil, err
}
ret = append(ret, b...)
}
case reflect.Map:
for _, key := range v.MapKeys() {
b, err := e.attachBuffer(v.MapIndex(key), index)
if err != nil {
return nil, err
}
ret = append(ret, b...)
}
}
return ret, nil
}
func (e *Encoder) writeBuffer(w io.WriteCloser, buffer []byte) error {
defer w.Close()
_, err := w.Write(buffer)
return err
}