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
|
package core
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"time"
)
type Wrapper interface {
Wrap() (PayloadType, []any)
}
type PayloadType uint8
const (
PayloadUnknown PayloadType = 0x00
PayloadSuccess = 0x01
PayloadError = 0x02
PayloadHandshake = 0x03
PayloadAuth = 0x04
PayloadMessage = 0x05
PayloadTest = 0xFF
)
type ErrorType uint8
const (
ErrorUnknown ErrorType = 0x00
ErrorAuthFailed = 0x01
)
type Frame struct {
Length uint16
Type PayloadType
Payload []any
}
type Binary struct {
Length uint16
Payload []byte
}
func Encode(w Wrapper) ([]byte, error) {
payloadType, payload := w.Wrap()
buf := bytes.NewBuffer(make([]byte, 3))
for _, p := range payload {
switch v := p.(type) {
case string:
strBytes := []byte(v)
err := binary.Write(buf, binary.BigEndian, uint16(len(strBytes)))
if err != nil {
return []byte{}, fmt.Errorf("cannot encode string length: %v", err)
}
_, err = buf.Write(strBytes)
if err != nil {
return []byte{}, fmt.Errorf("cannot encode string: %v", err)
}
case []byte:
err := binary.Write(buf, binary.BigEndian, uint16(len(v)))
if err != nil {
return []byte{}, fmt.Errorf("cannot encode byte slice length: %v", err)
}
_, err = buf.Write(v)
if err != nil {
return []byte{}, fmt.Errorf("cannot encode byte slice: %v", err)
}
case time.Time:
err := binary.Write(buf, binary.BigEndian, uint64(v.Unix()))
if err != nil {
return []byte{}, fmt.Errorf("cannot encode time: %v", err)
}
default:
err := binary.Write(buf, binary.BigEndian, v)
if err != nil {
return []byte{}, fmt.Errorf("cannot encode: %v", err)
}
}
}
frame := buf.Bytes()
frame[0] = uint8(payloadType)
binary.BigEndian.PutUint16(frame[1:], uint16(len(frame)-3))
return frame, nil
}
func Decode(buf io.Reader) (any, error) {
var pTypeByte uint8
err := binary.Read(buf, binary.BigEndian, &pTypeByte)
if err != nil {
return nil, fmt.Errorf("cannot read payload type: %v", err)
}
pType := PayloadType(pTypeByte)
var pLen uint16
err = binary.Read(buf, binary.BigEndian, &pLen)
if err != nil {
return nil, fmt.Errorf("cannot read payload length: %v", err)
}
switch pType {
case PayloadSuccess:
return Success{}, nil
case PayloadError:
return DecodeError(buf)
case PayloadHandshake:
return DecodeHandshake(buf)
case PayloadAuth:
return DecodeAuth(buf)
case PayloadMessage:
return DecodeMessage(buf)
case PayloadTest:
return DecodeTest(buf)
default:
return nil, fmt.Errorf("invalid payload type: %v", pType)
}
return pType, nil
}
func decodeNumeric(buf io.Reader, ptr any) error {
err := binary.Read(buf, binary.BigEndian, ptr)
if err != nil {
return fmt.Errorf("cannot decode: %v", err)
}
return nil
}
func decodeTime(buf io.Reader, ptr *time.Time) error {
var timeBuf uint64
err := binary.Read(buf, binary.BigEndian, &timeBuf)
if err != nil {
return fmt.Errorf("cannot decode time: %v", err)
}
*ptr = time.Unix(int64(timeBuf), 0)
return nil
}
func decodeBin(buf io.Reader, ptr *[]byte) error {
var payloadLen uint16
err := binary.Read(buf, binary.BigEndian, &payloadLen)
if err != nil {
return fmt.Errorf("cannot decode payload length: %v", err)
}
payload := make([]byte, payloadLen)
_, err = buf.Read(payload)
if err != nil {
return fmt.Errorf("cannot decode payload: %v", err)
}
*ptr = payload
return nil
}
func decodeString(buf io.Reader, ptr *string) error {
var strBytes []byte
err := decodeBin(buf, &strBytes)
if err != nil {
return err
}
*ptr = string(strBytes)
return nil
}
|