-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode.go
57 lines (44 loc) · 1.09 KB
/
opcode.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
package goentangle
import (
"fmt"
)
// Opcode.
type Opcode uint8
// Opcodes.
const (
// Request opcode.
RequestOpcode Opcode = iota
// Notification opcode.
NotificationOpcode
// Response opcode.
ResponseOpcode
// Exception opcode.
ExceptionOpcode
// Notification acknowledgement opcode.
NotificationAcknowledgementOpcode
// Compressed message opcode.
CompressedMessageOpcode = 0x7f
)
// Opcode names.
var opcodeNames = map[Opcode]string{
RequestOpcode: "request",
NotificationOpcode: "notification",
ResponseOpcode: "response",
NotificationAcknowledgementOpcode: "notification acknowledgement",
ExceptionOpcode: "exception",
}
func (o Opcode) String() string {
if name, ok := opcodeNames[o]; ok {
return name
}
return fmt.Sprintf("<invalid: %d>", o)
}
// Test if an opcode is valid.
func (o Opcode) Valid() bool {
return o == CompressedMessageOpcode || o >= RequestOpcode && o <= NotificationAcknowledgementOpcode
}
// Parse an opcode.
func ParseOpcode(input interface{}) (o Opcode, ok bool) {
raw, err := DeserializeUint8(input)
return Opcode(raw), err == nil
}