-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg_auth.go
71 lines (60 loc) · 1.66 KB
/
msg_auth.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
package gtp
import (
"bytes"
"git.golaxy.org/framework/util/binaryutil"
"strings"
)
// MsgAuth 鉴权(注意:为了提高解码性能,减少内存碎片,解码string与bytes字段时均使用引用类型,引用字节池中的bytes,GC时会被归还字节池,不要直接持有此类型字段)
type MsgAuth struct {
UserId string // 用户Id
Token string // 令牌
Extensions []byte // 扩展内容
}
// Read implements io.Reader
func (m MsgAuth) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteString(m.UserId); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteString(m.Token); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.Extensions); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (m *MsgAuth) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
var err error
m.UserId, err = bs.ReadStringRef()
if err != nil {
return bs.BytesRead(), err
}
m.Token, err = bs.ReadStringRef()
if err != nil {
return bs.BytesRead(), err
}
m.Extensions, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
return bs.BytesRead(), nil
}
// Size 大小
func (m MsgAuth) Size() int {
return binaryutil.SizeofString(m.UserId) + binaryutil.SizeofString(m.Token) + binaryutil.SizeofBytes(m.Extensions)
}
// MsgId 消息Id
func (MsgAuth) MsgId() MsgId {
return MsgId_Auth
}
// Clone 克隆消息对象
func (m *MsgAuth) Clone() Msg {
return &MsgAuth{
UserId: strings.Clone(m.UserId),
Token: strings.Clone(m.Token),
Extensions: bytes.Clone(m.Extensions),
}
}