-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
232 lines (205 loc) · 5.16 KB
/
Copy pathprotocol.go
File metadata and controls
232 lines (205 loc) · 5.16 KB
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
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/vmihailenco/msgpack/v5"
)
// Ably protocol message action constants.
const (
ActionHeartbeat = 0
ActionAck = 1
ActionNack = 2
ActionConnect = 3
ActionConnected = 4
ActionDisconnect = 5
ActionDisconnected = 6
ActionClose = 7
ActionClosed = 8
ActionError = 9
ActionAttach = 10
ActionAttached = 11
ActionDetach = 12
ActionDetached = 13
ActionPresence = 14
ActionMessage = 15
ActionSync = 16
ActionAuth = 17
)
var actionNames = map[string]int{
"HEARTBEAT": ActionHeartbeat,
"ACK": ActionAck,
"NACK": ActionNack,
"CONNECT": ActionConnect,
"CONNECTED": ActionConnected,
"DISCONNECT": ActionDisconnect,
"DISCONNECTED": ActionDisconnected,
"CLOSE": ActionClose,
"CLOSED": ActionClosed,
"ERROR": ActionError,
"ATTACH": ActionAttach,
"ATTACHED": ActionAttached,
"DETACH": ActionDetach,
"DETACHED": ActionDetached,
"PRESENCE": ActionPresence,
"MESSAGE": ActionMessage,
"SYNC": ActionSync,
"AUTH": ActionAuth,
}
var actionNumbers = map[int]string{}
func init() {
for name, num := range actionNames {
actionNumbers[num] = name
}
}
// ActionFromString converts an action name (e.g. "ATTACH") or numeric string (e.g. "10") to an int.
// Returns -1 if the string is not recognized.
func ActionFromString(s string) int {
// Try as name first
if n, ok := actionNames[strings.ToUpper(s)]; ok {
return n
}
// Try as number
if n, err := strconv.Atoi(s); err == nil {
return n
}
return -1
}
// ActionName returns the name for an action number, or the number as a string.
func ActionName(action int) string {
if name, ok := actionNumbers[action]; ok {
return name
}
return strconv.Itoa(action)
}
// ProtocolMessage is a minimal representation of an Ably protocol message,
// containing only the fields needed for rule matching.
type ProtocolMessage struct {
Action int
Channel string
Error *ErrorInfo
}
// ErrorInfo is a minimal representation of an Ably error.
type ErrorInfo struct {
Code int `json:"code"`
StatusCode int `json:"statusCode"`
Message string `json:"message"`
}
// ParseProtocolMessage decodes a WebSocket frame into a ProtocolMessage.
// The format is taken from the connection's "format" query parameter:
// "msgpack" is decoded as msgpack, everything else (including "json" and an
// absent value) is decoded as JSON.
// Returns the parsed message. On failure, returns a message with Action=-1.
func ParseProtocolMessage(data []byte, format string) ProtocolMessage {
if format == "msgpack" {
return parseMsgpack(data)
}
return parseJSON(data)
}
func parseJSON(data []byte) ProtocolMessage {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return ProtocolMessage{Action: -1}
}
pm := ProtocolMessage{Action: -1}
if actionRaw, ok := raw["action"]; ok {
// Action can be int or string
var actionInt int
if err := json.Unmarshal(actionRaw, &actionInt); err == nil {
pm.Action = actionInt
} else {
var actionStr string
if err := json.Unmarshal(actionRaw, &actionStr); err == nil {
pm.Action = ActionFromString(actionStr)
}
}
}
if channelRaw, ok := raw["channel"]; ok {
json.Unmarshal(channelRaw, &pm.Channel)
}
if errorRaw, ok := raw["error"]; ok {
var ei ErrorInfo
if err := json.Unmarshal(errorRaw, &ei); err == nil {
pm.Error = &ei
}
}
return pm
}
func parseMsgpack(data []byte) ProtocolMessage {
// Ably msgpack can be either a map or an array.
// Try map first (the common wire format).
var rawMap map[string]interface{}
if err := msgpack.Unmarshal(data, &rawMap); err == nil {
return parseMsgpackMap(rawMap)
}
// Fall back to array format.
var rawArray []interface{}
if err := msgpack.Unmarshal(data, &rawArray); err == nil {
return parseMsgpackArray(rawArray)
}
return ProtocolMessage{Action: -1}
}
func parseMsgpackMap(m map[string]interface{}) ProtocolMessage {
pm := ProtocolMessage{Action: -1}
if action, ok := m["action"]; ok {
pm.Action = toInt(action)
}
if channel, ok := m["channel"]; ok {
if s, ok := channel.(string); ok {
pm.Channel = s
}
}
if errObj, ok := m["error"]; ok {
if errMap, ok := errObj.(map[string]interface{}); ok {
pm.Error = &ErrorInfo{
Code: toInt(errMap["code"]),
StatusCode: toInt(errMap["statusCode"]),
Message: fmt.Sprintf("%v", errMap["message"]),
}
}
}
return pm
}
func parseMsgpackArray(a []interface{}) ProtocolMessage {
pm := ProtocolMessage{Action: -1}
if len(a) > 0 {
pm.Action = toInt(a[0])
}
if len(a) > 1 {
if s, ok := a[1].(string); ok {
pm.Channel = s
}
}
return pm
}
func toInt(v interface{}) int {
switch n := v.(type) {
case int:
return n
case int8:
return int(n)
case int16:
return int(n)
case int32:
return int(n)
case int64:
return int(n)
case uint:
return int(n)
case uint8:
return int(n)
case uint16:
return int(n)
case uint32:
return int(n)
case uint64:
return int(n)
case float32:
return int(n)
case float64:
return int(n)
default:
return -1
}
}