This repository has been archived by the owner on Apr 18, 2018. It is now read-only.
/
types.go
109 lines (92 loc) · 1.73 KB
/
types.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
package bercon
import (
"io"
"net"
"sync"
"time"
)
//Config contains all data required by BE Connections
type Config struct {
Addr *net.UDPAddr
Password string
KeepAliveTimer int
KeepAliveTolerance int64
}
//BeCfg is the Interface providing Configs for the Client
type BeCfg interface {
GetConfig() Config
}
//GetConfig returns BeConfig
func (bec Config) GetConfig() Config {
return bec
}
type transmission struct {
packet []byte
command []byte
sequence byte
response []byte
timestamp time.Time
writeCloser io.WriteCloser
}
//Client is the the Object Handling the Connection
type Client struct {
//Config
addr *net.UDPAddr
password string
keepAliveTimer int
keepAliveTolerance int64
reconnectTimeout int
init bool
con *net.UDPConn
readBuffer []byte
cmdChan chan transmission
looping bool
sequence struct {
sync.RWMutex
s byte
}
cmdMap map[byte]transmission
cmdLock sync.RWMutex
keepAliveCount int64
pingbackCount int64
chatWriter struct {
sync.Mutex
io.Writer
}
eventWriter struct {
sync.Mutex
io.Writer
}
}
var packetType = struct {
Login byte
Command byte
MultiCommand byte
ServerMessage byte
}{
Login: 0x00,
Command: 0x01,
MultiCommand: 0x00,
ServerMessage: 0x02,
}
var packetResponse = struct {
LoginOk byte
LoginFail byte
MultiCommand byte
}{
LoginOk: 0x01,
LoginFail: 0x00,
MultiCommand: 0x00,
}
func responseType(data []byte) (byte, error) {
if len(data) < 8 {
return 0, ErrInvalidSize
}
return data[7], nil
}
func getSequence(data []byte) (byte, error) {
if len(data) < 9 {
return 0, ErrInvalidSizeNoSequence
}
return data[8], nil
}