/
msg.go
63 lines (54 loc) · 1.25 KB
/
msg.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
package msg
import (
"reflect"
)
// The Message is defined as interface{},
// which is the base class for all messages.
type Message interface{}
// Proxy describes a proxy message for a client
// to connect to the server.
type Proxy struct {
ID string
Token string
Version string
Hostname string
Os string
Arch string
}
// Error describes an error message that is primarily
// used by the server to return to the client.
type Error struct {
Message string
}
// Version contains the server release information back
// to the client when the client tries to establish a
// proxy with the server.
type Version struct {
Version string
}
// Worker indicates that the server initiates a
// reverse connection to the client.
type Worker struct {
ID string
}
// Get the type of an interface.
func typeof(v interface{}) reflect.Type {
return reflect.TypeOf(v).Elem()
}
var (
// Records the byte corresponding to the message type.
msgTypes = make(map[reflect.Type]byte)
// Registers all message types.
typeMsgs = []reflect.Type{
typeof((*Proxy)(nil)),
typeof((*Error)(nil)),
typeof((*Version)(nil)),
typeof((*Worker)(nil)),
}
)
func init() {
// Associative message type with byte.
for i, t := range typeMsgs {
msgTypes[t] = byte(i)
}
}