-
Notifications
You must be signed in to change notification settings - Fork 3
/
codec_encoder.go
111 lines (92 loc) · 2.5 KB
/
codec_encoder.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
package codec
import (
"bytes"
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/framework/net/gap"
"git.golaxy.org/framework/utils/binaryutil"
"io"
)
// DefaultEncoder 默认消息包编码器
func DefaultEncoder() Encoder {
return MakeEncoder()
}
// MakeEncoder 创建消息包编码器
func MakeEncoder() Encoder {
return Encoder{}
}
// Encoder 消息包编码器
type Encoder struct {
buffer bytes.Buffer // buffer
}
// Read implements io.Reader
func (e *Encoder) Read(p []byte) (int, error) {
return e.buffer.Read(p)
}
// WriteTo implements io.WriterTo
func (e *Encoder) WriteTo(w io.Writer) (int64, error) {
if w == nil {
return 0, fmt.Errorf("gap: %w: w is nil", core.ErrArgs)
}
return e.buffer.WriteTo(w)
}
// Reset 重置缓存
func (e *Encoder) Reset() {
e.buffer.Reset()
}
// Encode 编码消息包,写入缓存
func (e *Encoder) Encode(src string, seq int64, msg gap.MsgReader) error {
return e.EncodeWriter(&e.buffer, src, seq, msg)
}
// EncodeWriter 编码消息包,写入指定writer
func (e Encoder) EncodeWriter(writer io.Writer, src string, seq int64, msg gap.MsgReader) error {
if writer == nil {
return fmt.Errorf("gap: %w: writer is nil", core.ErrArgs)
}
mpBuf, err := e.encode(src, seq, msg)
if err != nil {
return err
}
defer mpBuf.Release()
_, err = writer.Write(mpBuf.Data())
if err != nil {
return fmt.Errorf("gap: write msg-packet failed, %w", err)
}
return nil
}
// EncodeBuff 编码消息包,写入指定buffer
func (e Encoder) EncodeBuff(buff *bytes.Buffer, src string, seq int64, msg gap.MsgReader) error {
if buff == nil {
return fmt.Errorf("gap: %w: buff is nil", core.ErrArgs)
}
return e.EncodeWriter(buff, src, seq, msg)
}
// EncodeBytes 编码消息包,返回可回收bytes
func (e Encoder) EncodeBytes(src string, seq int64, msg gap.MsgReader) (binaryutil.RecycleBytes, error) {
return e.encode(src, seq, msg)
}
// encode 编码消息包
func (Encoder) encode(src string, seq int64, msg gap.MsgReader) (ret binaryutil.RecycleBytes, err error) {
if msg == nil {
return binaryutil.NilRecycleBytes, fmt.Errorf("gap: %w: msg is nil", core.ErrArgs)
}
mp := gap.MsgPacket{
Head: gap.MsgHead{
MsgId: msg.MsgId(),
Src: src,
Seq: seq,
},
Msg: msg,
}
mp.Head.Len = uint32(mp.Size())
mpBuf := binaryutil.MakeRecycleBytes(binaryutil.BytesPool.Get(int(mp.Head.Len)))
defer func() {
if err != nil {
mpBuf.Release()
}
}()
if _, err := mp.Read(mpBuf.Data()); err != nil {
return binaryutil.NilRecycleBytes, err
}
return mpBuf, nil
}