-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffer.go
180 lines (166 loc) · 4.59 KB
/
buffer.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
/**
* Golang socket.io
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package socket
import (
"encoding/json"
"errors"
"reflect"
)
var errNotPlaceHolder = errors.New("socket.io: require `{ _placeholder: true, num: <number> }` in order to unmarshal socket.Buffer")
type Buffer struct {
B []byte
num uint
}
var (
_ json.Marshaler = Buffer{}
_ json.Unmarshaler = (*Buffer)(nil)
)
type placeholder struct {
Placeholder bool `json:"_placeholder"`
Num uint `json:"num"`
}
func (b Buffer) MarshalJSON() ([]byte, error) {
return json.Marshal(placeholder{
Placeholder: true,
Num: b.num - 1,
})
}
func (b *Buffer) UnmarshalJSON(data []byte) (err error) {
var p placeholder
if err = json.Unmarshal(data, &p); err != nil {
return errNotPlaceHolder
}
if !p.Placeholder { // is null
b.B = nil
b.num = 0
} else {
b.num = p.Num + 1
}
return
}
var bufferTyp = reflect.TypeOf((*Buffer)(nil)).Elem()
func needCheckForBufferType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Pointer:
return needCheckForBufferType(t.Elem())
case reflect.Struct, reflect.Interface:
return true
}
return false
}
func (p *Packet) encodeAttachs(v []any) {
p._encodeAttachs(reflect.ValueOf(v), false, make(map[*Buffer]struct{}))
}
func (p *Packet) _encodeAttachs(v reflect.Value, recurse bool, encoded map[*Buffer]struct{}) {
typ := v.Type()
switch typ.Kind() {
case reflect.Array:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
l := typ.Len()
for i := 0; i < l; i++ {
p._encodeAttachs(v.Index(i), recurse, encoded)
}
}
case reflect.Slice:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
l := v.Len()
for i := 0; i < l; i++ {
p._encodeAttachs(v.Index(i), recurse, encoded)
}
}
case reflect.Map:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
iter := v.MapRange()
for iter.Next() {
p._encodeAttachs(iter.Value(), recurse, encoded)
}
}
case reflect.Pointer:
if et := typ.Elem(); (recurse || needCheckForBufferType(et)) && !v.IsNil() {
recurse = et.Kind() != reflect.Interface
p._encodeAttachs(v.Elem(), recurse, encoded)
}
case reflect.Struct:
if typ == bufferTyp {
b := v.Addr().Interface().(*Buffer)
if _, ok := encoded[b]; !ok {
encoded[b] = struct{}{}
p.attachs = append(p.attachs, b.B)
b.num = (uint)(len(p.attachs))
}
return
}
l := typ.NumField()
for i := 0; i < l; i++ {
p._encodeAttachs(v.Field(i), false, encoded)
}
}
}
func (p *Packet) decodeAttachs(ptr any) {
p._decodeAttachs(reflect.ValueOf(ptr), false)
}
func (p *Packet) _decodeAttachs(v reflect.Value, recurse bool) {
typ := v.Type()
switch typ.Kind() {
case reflect.Array:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
l := typ.Len()
for i := 0; i < l; i++ {
p._decodeAttachs(v.Index(i), recurse)
}
}
case reflect.Slice:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
l := v.Len()
for i := 0; i < l; i++ {
p._decodeAttachs(v.Index(i), recurse)
}
}
case reflect.Map:
if et := typ.Elem(); recurse || needCheckForBufferType(et) {
recurse = et.Kind() != reflect.Interface
iter := v.MapRange()
for iter.Next() {
p._decodeAttachs(iter.Value(), recurse)
}
}
case reflect.Pointer:
if et := typ.Elem(); (recurse || needCheckForBufferType(et)) && !v.IsNil() {
recurse = et.Kind() != reflect.Interface
p._decodeAttachs(v.Elem(), recurse)
}
case reflect.Struct:
if typ == bufferTyp {
b := v.Addr().Interface().(*Buffer)
if b.num > 0 {
b.B = p.attachs[b.num-1]
}
return
}
l := typ.NumField()
for i := 0; i < l; i++ {
p._decodeAttachs(v.Field(i), false)
}
}
}