-
Notifications
You must be signed in to change notification settings - Fork 3
/
msgmac.go
128 lines (106 loc) · 2.54 KB
/
msgmac.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package gtp
import (
"git.golaxy.org/framework/util/binaryutil"
)
// MsgMAC32 包含MAC(32bit)消息
type MsgMAC32 struct {
Data []byte
MAC uint32
}
// Read implements io.Reader
func (m MsgMAC32) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteBytes(m.Data); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteUint32(m.MAC); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (m *MsgMAC32) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
var err error
m.Data, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.MAC, err = bs.ReadUint32()
if err != nil {
return bs.BytesRead(), err
}
return bs.BytesRead(), nil
}
// Size 大小
func (m MsgMAC32) Size() int {
return binaryutil.SizeofBytes(m.Data) + binaryutil.SizeofUint32()
}
// MsgMAC64 包含MAC(64bit)消息
type MsgMAC64 struct {
Data []byte
MAC uint64
}
// Read implements io.Reader
func (m MsgMAC64) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteBytes(m.Data); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteUint64(m.MAC); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (m *MsgMAC64) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
var err error
m.Data, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.MAC, err = bs.ReadUint64()
if err != nil {
return bs.BytesRead(), err
}
return bs.BytesRead(), nil
}
// Size 大小
func (m MsgMAC64) Size() int {
return binaryutil.SizeofBytes(m.Data) + binaryutil.SizeofUint64()
}
// MsgMAC 包含MAC消息
type MsgMAC struct {
Data []byte
MAC []byte
}
// Read implements io.Reader
func (m MsgMAC) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteBytes(m.Data); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.MAC); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (m *MsgMAC) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
var err error
m.Data, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.MAC, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
return bs.BytesRead(), nil
}
// Size 大小
func (m MsgMAC) Size() int {
return binaryutil.SizeofBytes(m.Data) + binaryutil.SizeofBytes(m.MAC)
}