-
Notifications
You must be signed in to change notification settings - Fork 41
/
messagepackhubprotocol.go
423 lines (411 loc) · 11.4 KB
/
messagepackhubprotocol.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package signalr
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/go-kit/log"
"github.com/vmihailenco/msgpack/v5"
)
type messagePackHubProtocol struct {
dbg log.Logger
}
func (m *messagePackHubProtocol) ParseMessages(reader io.Reader, remainBuf *bytes.Buffer) ([]interface{}, error) {
frames, err := m.readFrames(reader, remainBuf)
if err != nil {
return nil, err
}
messages := make([]interface{}, 0)
for _, frame := range frames {
message, err := m.parseMessage(bytes.NewBuffer(frame))
if err != nil {
return nil, err
}
messages = append(messages, message)
}
return messages, nil
}
func (m *messagePackHubProtocol) readFrames(reader io.Reader, remainBuf *bytes.Buffer) ([][]byte, error) {
frames := make([][]byte, 0)
for {
// Try to get the frame length
frameLenBuf := make([]byte, binary.MaxVarintLen32)
n1, err := remainBuf.Read(frameLenBuf)
if err != nil && !errors.Is(err, io.EOF) {
// Some weird other error
return nil, err
}
n2, err := reader.Read(frameLenBuf[n1:])
if err != nil && !errors.Is(err, io.EOF) {
// Some weird other error
return nil, err
}
frameLen, lenLen := binary.Uvarint(frameLenBuf[:n1+n2])
if lenLen == 0 {
// reader could not supply enough bytes to decode the Uvarint
// Store the already read bytes in the remainBuf for next iteration
_, _ = remainBuf.Write(frameLenBuf[:n1+n2])
return frames, nil
}
if lenLen < 0 {
return nil, fmt.Errorf("messagepack frame length to large")
}
// Still wondering why this happens, but it happens!
if frameLen == 0 {
// Store the overread bytes for the next iteration
_, _ = remainBuf.Write(frameLenBuf[lenLen:])
continue
}
// Try getting data until at least one frame is available
readBuf := make([]byte, frameLen)
frameBuf := &bytes.Buffer{}
// Did we read too many bytes when detecting the frameLen?
_, _ = frameBuf.Write(frameLenBuf[lenLen:])
// Read the rest of the bytes from the last iteration
_, _ = frameBuf.ReadFrom(remainBuf)
for {
n, err := reader.Read(readBuf)
if errors.Is(err, io.EOF) {
// Less than frameLen. Let the caller parse the already read frames and come here again later
_, _ = remainBuf.ReadFrom(frameBuf)
return frames, nil
}
if err != nil {
return nil, err
}
_, _ = frameBuf.Write(readBuf[:n])
if frameBuf.Len() == int(frameLen) {
// Frame completely read. Return it to the caller
frames = append(frames, frameBuf.Next(int(frameLen)))
return frames, nil
}
if frameBuf.Len() > int(frameLen) {
// More than frameLen. Append the current frame to the result and start reading the next frame
frames = append(frames, frameBuf.Next(int(frameLen)))
_, _ = remainBuf.ReadFrom(frameBuf)
break
}
}
}
}
func (m *messagePackHubProtocol) parseMessage(buf *bytes.Buffer) (interface{}, error) {
decoder := msgpack.NewDecoder(buf)
// Default map decoding expects all maps to have string keys
decoder.SetMapDecoder(func(decoder *msgpack.Decoder) (interface{}, error) {
return decoder.DecodeUntypedMap()
})
msgLen, err := decoder.DecodeArrayLen()
if err != nil {
return nil, err
}
msgType, err := decoder.DecodeInt()
if err != nil {
return nil, err
}
// Ignore Header for all messages, except ping message that has no header
// see message spec at https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md#message-headers
if msgType != 6 {
_, err = decoder.DecodeMap()
if err != nil {
return nil, err
}
}
switch msgType {
case 1, 4:
if msgLen < 5 {
return nil, fmt.Errorf("invalid invocationMessage length %v", msgLen)
}
invocationID, err := m.decodeInvocationID(decoder)
if err != nil {
return nil, err
}
invocationMessage := invocationMessage{
Type: msgType,
InvocationID: invocationID,
}
invocationMessage.Target, err = decoder.DecodeString()
if err != nil {
return nil, err
}
argLen, err := decoder.DecodeArrayLen()
if err != nil {
return nil, err
}
for i := 0; i < argLen; i++ {
argument, err := decoder.DecodeRaw()
if err != nil {
return nil, err
}
invocationMessage.Arguments = append(invocationMessage.Arguments, argument)
}
// StreamIds seem to be optional
if msgLen == 6 {
streamIDLen, err := decoder.DecodeArrayLen()
if err != nil {
return nil, err
}
for i := 0; i < streamIDLen; i++ {
streamID, err := decoder.DecodeString()
if err != nil {
return nil, err
}
invocationMessage.StreamIds = append(invocationMessage.StreamIds, streamID)
}
}
return invocationMessage, nil
case 2:
if msgLen != 4 {
return nil, fmt.Errorf("invalid streamItemMessage length %v", msgLen)
}
streamItemMessage := streamItemMessage{Type: 2}
streamItemMessage.InvocationID, err = decoder.DecodeString()
if err != nil {
return nil, err
}
streamItemMessage.Item, err = decoder.DecodeRaw()
if err != nil {
return nil, err
}
return streamItemMessage, nil
case 3:
if msgLen < 4 {
return nil, fmt.Errorf("invalid completionMessage length %v", msgLen)
}
completionMessage := completionMessage{Type: 3}
completionMessage.InvocationID, err = decoder.DecodeString()
if err != nil {
return nil, err
}
resultKind, err := decoder.DecodeInt8()
if err != nil {
return nil, err
}
switch resultKind {
case 1: // Error result
if msgLen < 5 {
return nil, fmt.Errorf("invalid completionMessage length %v", msgLen)
}
completionMessage.Error, err = decoder.DecodeString()
if err != nil {
return nil, err
}
case 2: // Void result
case 3: // Non-void result
if msgLen < 5 {
return nil, fmt.Errorf("invalid completionMessage length %v", msgLen)
}
completionMessage.Result, err = decoder.DecodeRaw()
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid resultKind %v", resultKind)
}
return completionMessage, nil
case 5:
if msgLen != 3 {
return nil, fmt.Errorf("invalid cancelInvocationMessage length %v", msgLen)
}
cancelInvocationMessage := cancelInvocationMessage{Type: 5}
cancelInvocationMessage.InvocationID, err = decoder.DecodeString()
if err != nil {
return nil, err
}
return cancelInvocationMessage, nil
case 6:
if msgLen != 1 {
return nil, fmt.Errorf("invalid pingMessage length %v", msgLen)
}
return hubMessage{Type: 6}, nil
case 7:
if msgLen < 2 {
return nil, fmt.Errorf("invalid closeMessage length %v", msgLen)
}
closeMessage := closeMessage{Type: 7}
closeMessage.Error, err = decoder.DecodeString()
if err != nil {
return nil, err
}
if msgLen > 2 {
closeMessage.AllowReconnect, err = decoder.DecodeBool()
if err != nil {
return nil, err
}
}
return closeMessage, nil
}
return msg, nil
}
func (m *messagePackHubProtocol) decodeInvocationID(decoder *msgpack.Decoder) (string, error) {
rawID, err := decoder.DecodeInterface()
if err != nil {
return "", err
}
// nil is ok
if rawID == nil {
return "", nil
}
// Otherwise, it must be string
invocationID, ok := rawID.(string)
if !ok {
return "", fmt.Errorf("invalid InvocationID %#v", rawID)
}
return invocationID, nil
}
func (m *messagePackHubProtocol) WriteMessage(message interface{}, writer io.Writer) error {
// Encode message body
buf := &bytes.Buffer{}
encoder := msgpack.NewEncoder(buf)
// Ensure uppercase/lowercase mapping for struct member names
encoder.SetCustomStructTag("json")
switch msg := message.(type) {
case invocationMessage:
if err := encodeMsgHeader(encoder, 6, msg.Type); err != nil {
return err
}
if msg.InvocationID == "" {
if err := encoder.EncodeNil(); err != nil {
return err
}
} else {
if err := encoder.EncodeString(msg.InvocationID); err != nil {
return err
}
}
if err := encoder.EncodeString(msg.Target); err != nil {
return err
}
if err := encoder.EncodeArrayLen(len(msg.Arguments)); err != nil {
return err
}
for _, arg := range msg.Arguments {
if err := encoder.Encode(arg); err != nil {
return err
}
}
if err := encoder.EncodeArrayLen(len(msg.StreamIds)); err != nil {
return err
}
for _, id := range msg.StreamIds {
if err := encoder.EncodeString(id); err != nil {
return err
}
}
case streamItemMessage:
if err := encodeMsgHeader(encoder, 4, msg.Type); err != nil {
return err
}
if err := encoder.EncodeString(msg.InvocationID); err != nil {
return err
}
if err := encoder.Encode(msg.Item); err != nil {
return err
}
case completionMessage:
msgLen := 4
if msg.Result != nil || msg.Error != "" {
msgLen = 5
}
if err := encodeMsgHeader(encoder, msgLen, msg.Type); err != nil {
return err
}
if err := encoder.EncodeString(msg.InvocationID); err != nil {
return err
}
var resultKind int8 = 2
if msg.Error != "" {
resultKind = 1
} else if msg.Result != nil {
resultKind = 3
}
if err := encoder.EncodeInt8(resultKind); err != nil {
return err
}
switch resultKind {
case 1:
if err := encoder.EncodeString(msg.Error); err != nil {
return err
}
case 3:
if err := encoder.Encode(msg.Result); err != nil {
return err
}
}
case cancelInvocationMessage:
if err := encodeMsgHeader(encoder, 3, msg.Type); err != nil {
return err
}
if err := encoder.EncodeString(msg.InvocationID); err != nil {
return err
}
case hubMessage:
if err := encoder.EncodeArrayLen(1); err != nil {
return err
}
if err := encoder.EncodeInt(6); err != nil {
return err
}
case closeMessage:
if err := encodeMsgHeader(encoder, 3, msg.Type); err != nil {
return err
}
if err := encoder.EncodeString(msg.Error); err != nil {
return err
}
if err := encoder.EncodeBool(msg.AllowReconnect); err != nil {
return err
}
}
// Build frame with length information
frameBuf := &bytes.Buffer{}
lenBuf := make([]byte, binary.MaxVarintLen32)
lenLen := binary.PutUvarint(lenBuf, uint64(buf.Len()))
if _, err := frameBuf.Write(lenBuf[:lenLen]); err != nil {
return err
}
_ = m.dbg.Log(evt, "Write", msg, fmt.Sprintf("%#v", message))
_, _ = frameBuf.ReadFrom(buf)
_, err := frameBuf.WriteTo(writer)
return err
}
func encodeMsgHeader(e *msgpack.Encoder, msgLen int, msgType int) (err error) {
if err = e.EncodeArrayLen(msgLen); err != nil {
return err
}
if err = e.EncodeInt(int64(msgType)); err != nil {
return err
}
headers := make(map[string]interface{})
if err = e.EncodeMap(headers); err != nil {
return err
}
return nil
}
func (m *messagePackHubProtocol) transferMode() TransferMode {
return BinaryTransferMode
}
func (m *messagePackHubProtocol) setDebugLogger(dbg StructuredLogger) {
m.dbg = log.WithPrefix(dbg, "ts", log.DefaultTimestampUTC, "protocol", "MSGP")
}
// UnmarshalArgument unmarshals raw bytes to a destination value. dst is the pointer to the destination value.
func (m *messagePackHubProtocol) UnmarshalArgument(src interface{}, dst interface{}) error {
rawSrc, ok := src.(msgpack.RawMessage)
if !ok {
return fmt.Errorf("invalid source %#v for UnmarshalArgument", src)
}
buf := bytes.NewBuffer(rawSrc)
decoder := msgpack.GetDecoder()
defer msgpack.PutDecoder(decoder)
decoder.Reset(buf)
// Default map decoding expects all maps to have string keys
decoder.SetMapDecoder(func(decoder *msgpack.Decoder) (interface{}, error) {
return decoder.DecodeUntypedMap()
})
// Ensure uppercase/lowercase mapping for struct member names
decoder.SetCustomStructTag("json")
if err := decoder.Decode(dst); err != nil {
return err
}
return nil
}