-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocolWritter.go
343 lines (322 loc) · 9.2 KB
/
protocolWritter.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package protocol
import (
"bytes"
"encoding/binary"
"errors"
"reflect"
"unsafe"
)
// ProtocolDataHeaderWritter 序列化写入头
type ProtocolDataHeaderWritter struct {
isValid bool
shortLenMode bool
shortClassId bool
headerLength uint16
startPos int
}
// 序列化写入
type ProtocolWritter struct {
buf []byte
}
// NewProtocolWritter create new ProtocolWritter instance.
// bufSize is the initial cap for the internal buffer in bytes.
func NewProtocolWritter(bufSize int) *ProtocolWritter {
if bufSize <= 0 {
bufSize = 1024
}
return &ProtocolWritter{
buf: make([]byte, 0, bufSize),
}
}
// Bytes returns the number of bytes that have been written into the current buffer.
func (b *ProtocolWritter) Bytes() []byte { return b.buf[:] }
// Len returns how many bytes are unused in the buffer.
func (b *ProtocolWritter) Len() int { return len(b.buf) }
// Cap returns the cap of the buffer
func (b *ProtocolWritter) Cap() int { return cap(b.buf) }
func (b *ProtocolWritter) Reset() {
b.buf = b.buf[:0]
}
func (b *ProtocolWritter) tryGrowByReslice(n int) (int, bool) {
if l := len(b.buf); n <= cap(b.buf)-l {
b.buf = b.buf[:l+n]
return l, true
}
return 0, false
}
const maxInt = int(^uint(0) >> 1)
func (b *ProtocolWritter) grow(n int) int {
m := b.Len()
// Try to grow by means of a reslice.
if i, ok := b.tryGrowByReslice(n); ok {
return i
}
c := cap(b.buf)
if c > maxInt-c-n {
panic(bytes.ErrTooLarge)
} else {
// Not enough space anywhere, we need to allocate.
buf := make([]byte, 2*c+n)
copy(buf, b.buf[:])
b.buf = buf
}
// Restore b.off and len(b.buf).
b.buf = b.buf[:m+n]
return m
}
func (b *ProtocolWritter) WriteByte(c byte) error {
m, ok := b.tryGrowByReslice(1)
if !ok {
m = b.grow(1)
}
b.buf[m] = c
return nil
}
func (b *ProtocolWritter) Write(p []byte) (n int, err error) {
m, ok := b.tryGrowByReslice(len(p))
if !ok {
m = b.grow(len(p))
}
return copy(b.buf[m:], p), nil
}
// writeUint16 write a Uint16
func (b *ProtocolWritter) writeUint16(v uint16) {
m, ok := b.tryGrowByReslice(2)
if !ok {
m = b.grow(2)
}
b.buf[m] = byte(v)
b.buf[m+1] = byte(v >> 8)
}
func (b *ProtocolWritter) writeUint32(v uint32) {
m, ok := b.tryGrowByReslice(4)
if !ok {
m = b.grow(4)
}
b.buf[m] = byte(v)
b.buf[m+1] = byte(v >> 8)
b.buf[m+2] = byte(v >> 16)
b.buf[m+3] = byte(v >> 24)
}
func (b *ProtocolWritter) UpdateDataLength(datlen uint32, head *ProtocolDataHeaderWritter) uint32 {
if !head.isValid {
return 0
}
idx := head.startPos + 2 + 2 // Sign + ClassId
if !head.shortClassId {
idx += 2
}
if head.shortLenMode {
if datlen > 0xFFFF {
head.isValid = false
return 0
}
binary.LittleEndian.PutUint16(b.buf[idx:], uint16(datlen))
return 2
} else {
binary.LittleEndian.PutUint32(b.buf[idx:], datlen)
return 4
}
}
func (b *ProtocolWritter) WriteDataHead(rtti *TRegRttiData, head *ProtocolDataHeaderWritter) {
head.isValid = false
if rtti == nil {
return
}
sign := cSignFlag
cId := rtti.ClassId
head.shortClassId = cId <= 0xFFFF
head.shortLenMode = !rtti.BigData
head.headerLength = 6
head.startPos = b.Len()
lenIdx := 4
if !head.shortClassId {
sign = sign | 1
head.headerLength += 2
lenIdx += 2
}
if !head.shortLenMode {
sign = sign | 2
head.headerLength += 2
}
b.writeUint16(sign)
if head.shortClassId {
b.writeUint16(uint16(cId))
} else {
b.writeUint32(cId)
}
if head.shortLenMode {
b.writeUint16(0)
} else {
b.writeUint32(0)
}
head.isValid = true
}
// WriteString write the length of string into the buffer,
// then it write string data into the buffer.
func (b *ProtocolWritter) writeString(s string) {
l := uint16(len(s))
b.writeUint16(l)
if l > 0 {
b.Write([]byte(s))
}
}
func (b *ProtocolWritter) writeMemory(ptr unsafe.Pointer, len int) {
if len <= 0 {
return
}
sliceHeader := reflect.SliceHeader{Data: uintptr(ptr), Len: len, Cap: len}
b.Write(*(*[]byte)(unsafe.Pointer(&sliceHeader)))
}
func (b *ProtocolWritter) writeAny(obj interface{}) (err error) {
if rtti, ok := GetRegRttiDataFromObj(obj); ok {
_, err = b.writeStruct(PtrOf(obj), rtti)
} else {
err = errors.New("object isn't register")
}
return
}
func (b *ProtocolWritter) WriteEmptyHeader() {
b.writeUint16(cSignFlag)
b.writeUint16(0)
b.writeUint16(6)
}
//writeStruct Write a Strcut to bytes
func (b *ProtocolWritter) writeStruct(ptr unsafe.Pointer, rttiData *TRegRttiData) (n int, err error) {
if rttiData == nil {
return 0, errors.New("rttiData is nil")
}
headWritter := ProtocolDataHeaderWritter{}
b.WriteDataHead(rttiData, &headWritter)
if !headWritter.isValid {
return 0, errors.New("write protocol head error")
}
if ptr == nil {
return 0, nil
}
for idx := 0; idx < len(rttiData.FieldData); {
rttiField := &rttiData.FieldData[idx]
fieldPtr := unsafe.Pointer(uintptr(ptr) + rttiField.offset)
if rttiField.podMergeCount <= 1 { // 未合并的字段
if rttiField.isPod() {
b.writeMemory(fieldPtr, rttiField.podSize)
} else {
switch rttiField.Kind {
case reflect.String:
b.writeString(*(*string)(fieldPtr))
case reflect.Array:
b.writeUint32(rttiField.arrayLen)
fieldPtr = PtrOf((*emptyInterface)(fieldPtr))
if isPod(rttiField.arrayKind) { // 元素是pod类型,可以直接写入
b.writeMemory(fieldPtr, rttiField.podSize)
} else {
switch rttiField.arrayKind {
case reflect.String:
for i := 0; i < int(rttiField.arrayLen); i++ {
b.writeString(*(*string)(unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))))
}
case reflect.Struct:
if filedRttiData, ok := GetRegRttiDataFromType(rttiField.arrayType); ok {
for i := 0; i < int(rttiField.arrayLen); i++ {
b.writeStruct(unsafe.Pointer(uintptr(fieldPtr)+uintptr(i*rttiField.arraySize)), filedRttiData)
}
}
case reflect.Interface:
for i := 0; i < int(rttiField.arrayLen); i++ {
b.writeAny(*(*interface{})(unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))))
}
case reflect.Ptr:
if filedRttiData, ok := GetRegRttiDataFromType(rttiField.arrayType); ok {
for i := 0; i < int(rttiField.arrayLen); i++ {
arrPtr := unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))
arrPtr = *(*unsafe.Pointer)(arrPtr)
if arrPtr == nil {
b.WriteEmptyHeader()
} else {
b.writeStruct(arrPtr, filedRttiData)
}
}
}
}
}
case reflect.Slice:
slicePtr := (*reflect.SliceHeader)(fieldPtr)
b.writeUint32(uint32(slicePtr.Len))
if slicePtr.Len == 0 {
idx++
continue
}
fieldPtr := unsafe.Pointer(slicePtr.Data)
if isPod(rttiField.arrayKind) { // 元素是pod类型,可以直接写入
fieldPtr = PtrOf((*emptyInterface)(fieldPtr))
b.writeMemory(fieldPtr, slicePtr.Len*rttiField.arraySize)
} else {
switch rttiField.arrayKind {
case reflect.String:
for i := 0; i < slicePtr.Len; i++ {
b.writeString(*(*string)(unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))))
}
case reflect.Struct:
if filedRttiData, ok := GetRegRttiDataFromType(rttiField.arrayType); ok {
for i := 0; i < slicePtr.Len; i++ {
b.writeStruct(unsafe.Pointer(uintptr(fieldPtr)+uintptr(i*rttiField.arraySize)), filedRttiData)
}
}
case reflect.Interface:
for i := 0; i < slicePtr.Len; i++ {
b.writeAny(*(*interface{})(unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))))
}
case reflect.Ptr:
if filedRttiData, ok := GetRegRttiDataFromType(rttiField.arrayType); ok {
for i := 0; i < slicePtr.Len; i++ {
arrPtr := unsafe.Pointer(uintptr(fieldPtr) + uintptr(i*rttiField.arraySize))
arrPtr = *(*unsafe.Pointer)(arrPtr)
if arrPtr == nil {
b.WriteEmptyHeader()
} else {
b.writeStruct(arrPtr, filedRttiData)
}
}
}
}
}
case reflect.Struct:
if filedRttiData, ok := G_DataClass[rttiField.typeHash]; ok {
b.writeStruct(fieldPtr, filedRttiData)
}
case reflect.Interface:
b.writeAny(*(*interface{})(fieldPtr))
case reflect.Ptr:
if filedRttiData, ok := G_DataClass[rttiField.typeHash]; ok {
fieldPtr = *(*unsafe.Pointer)(fieldPtr)
if fieldPtr == nil {
b.WriteEmptyHeader()
} else {
b.writeStruct(fieldPtr, filedRttiData)
}
}
}
}
idx++
} else { // 合并的 pod 类型,直接取 podSize
b.writeMemory(fieldPtr, rttiField.podSize)
idx += rttiField.podMergeCount
}
}
b.UpdateDataLength(uint32(b.Len()-headWritter.startPos), &headWritter)
if !headWritter.isValid { // 非bigData长度却超了
if !rttiData.BigData {
rttiData.BigData = true
tmp := make([]byte, b.Len()-headWritter.startPos-int(headWritter.headerLength))
copy(tmp, b.buf[headWritter.startPos+int(headWritter.headerLength):])
b.buf = b.buf[:headWritter.startPos]
b.WriteDataHead(rttiData, &headWritter)
b.buf = append(b.buf[:headWritter.startPos], tmp...)
b.UpdateDataLength(uint32(b.Len()-headWritter.startPos), &headWritter)
} else {
b.buf = b.buf[:headWritter.startPos]
return 0, errors.New("非bigData长度却超了")
}
}
return b.Len(), nil
}