/
params.go
105 lines (92 loc) · 2.2 KB
/
params.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
package protocol
import (
"bytes"
"encoding/binary"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
)
// 终端参数
type Param struct {
id uint32
serialized []byte
}
// 参数ID
func (param *Param) ID() uint32 {
return param.id
}
// 设为Byte
func (param *Param) SetByte(id uint32, b byte) *Param {
param.id = id
param.serialized = []byte{b}
return param
}
// 设为Bytes
func (param *Param) SetBytes(id uint32, b []byte) *Param {
param.id = id
buffer := make([]byte, len(b))
copy(buffer, b)
param.serialized = buffer
return param
}
// 设为Uint16
func (param *Param) SetUint16(id uint32, n uint16) *Param {
param.id = id
var buffer [2]byte
binary.BigEndian.PutUint16(buffer[:], n)
param.serialized = buffer[:]
return param
}
// 设为Uint32
func (param *Param) SetUint32(id uint32, n uint32) *Param {
param.id = id
var buffer [4]byte
binary.BigEndian.PutUint32(buffer[:], n)
param.serialized = buffer[:]
return param
}
// 设为字符串
func (param *Param) SetString(id uint32, s string) *Param {
if len(s) == 0 {
return param.SetBytes(id, nil)
}
data, _ := ioutil.ReadAll(transform.NewReader(
bytes.NewReader([]byte(s)), simplifiedchinese.GB18030.NewEncoder()))
return param.SetBytes(id, data)
}
// 读取Byte
func (param *Param) GetByte() (byte, error) {
if len(param.serialized) < 1 {
return 0, ErrInvalidBody
}
return param.serialized[0], nil
}
// 读取Bytes
func (param *Param) GetBytes() ([]byte, error) {
data := make([]byte, len(param.serialized))
copy(data, param.serialized)
return data, nil
}
// 读取Uint16
func (param *Param) GetUint16() (uint16, error) {
if len(param.serialized) < 2 {
return 0, ErrInvalidBody
}
return binary.BigEndian.Uint16(param.serialized[:2]), nil
}
// 读取Uint32
func (param *Param) GetUint32() (uint32, error) {
if len(param.serialized) < 4 {
return 0, ErrInvalidBody
}
return binary.BigEndian.Uint32(param.serialized[:4]), nil
}
// 读取字符串
func (param *Param) GetString() (string, error) {
data, err := ioutil.ReadAll(transform.NewReader(
bytes.NewReader(param.serialized), simplifiedchinese.GB18030.NewDecoder()))
if err != nil {
return "", err
}
return string(data), nil
}