1
- package rpc
2
-
3
-
4
- const (
5
- DISCONNECT uint8 = iota
6
- CONNECT
7
- CONNECT_ACK
8
- HEARTBEAT
9
- REQUEST
10
- REQUEST_END
11
- RESPONSE
12
- RESPONSE_END
13
- STREAM
14
- STREAM_END
15
- PUBLISH
16
- PUBLISH_END
17
- PUBLISH_ACK
18
- SUBSCRIBE
19
- SUBSCRIBE_ACK
20
- UNSUBSCRIBE
21
- )
22
-
23
- const (
24
- BINARY uint8 = iota
25
- JSON
26
- XML
27
- YAML
28
- CSV
29
- MSGPACK
30
- PROTOBUF
31
- )
32
-
33
-
34
- type Pack struct {
35
- Type uint8
36
- Encoding uint8
37
- Id uint16
38
- Length uint16
39
- }
1
+ package rpc
2
+
3
+ import (
4
+ "encoding/binary"
5
+ "encoding/xml"
6
+ "errors"
7
+ "github.com/gocarina/gocsv"
8
+ "github.com/goccy/go-json"
9
+ "github.com/shamaton/msgpack/v2"
10
+ "gopkg.in/yaml.v3"
11
+ )
12
+
13
+ var ErrEncoding = errors .New ("编码不支持" )
14
+ var ErrNotEnough = errors .New ("长度不足" )
15
+
16
+ type Encoder func (any ) ([]byte , error )
17
+ type Decoder func ([]byte , any ) error
18
+
19
+ const MAGIC = "rgc"
20
+
21
+ const (
22
+ DISCONNECT uint8 = iota
23
+ CONNECT
24
+ CONNECT_ACK
25
+ HEARTBEAT
26
+ REQUEST
27
+ REQUEST_END
28
+ RESPONSE
29
+ RESPONSE_END
30
+ STREAM
31
+ STREAM_END
32
+ PUBLISH
33
+ PUBLISH_END
34
+ PUBLISH_ACK
35
+ SUBSCRIBE
36
+ SUBSCRIBE_ACK
37
+ UNSUBSCRIBE
38
+ )
39
+
40
+ const (
41
+ BINARY uint8 = iota
42
+ JSON
43
+ XML
44
+ YAML
45
+ CSV
46
+ MSGPACK
47
+ PROTOBUF
48
+ )
49
+
50
+ var encoders = map [uint8 ]Encoder {
51
+ JSON : json .Marshal ,
52
+ XML : xml .Marshal ,
53
+ YAML : yaml .Marshal ,
54
+ CSV : gocsv .MarshalBytes ,
55
+ MSGPACK : msgpack .Marshal ,
56
+ }
57
+
58
+ var decoders = map [uint8 ]Decoder {
59
+ JSON : json .Unmarshal ,
60
+ XML : xml .Unmarshal ,
61
+ YAML : yaml .Unmarshal ,
62
+ CSV : gocsv .UnmarshalBytes ,
63
+ MSGPACK : msgpack .Unmarshal ,
64
+ }
65
+
66
+ func RegisterEncoding (typ uint8 , encoder Encoder , decoder Decoder ) {
67
+ encoders [typ ] = encoder
68
+ decoders [typ ] = decoder
69
+ }
70
+
71
+ type Pack struct {
72
+ Type uint8
73
+ Encoding uint8
74
+ Id uint16
75
+ Length uint16
76
+ Data []byte
77
+ Content any
78
+ }
79
+
80
+ func (p * Pack ) Encode () (buf []byte , err error ) {
81
+ if p .Content != nil && p .Encoding > 0 {
82
+ if encoder , ok := encoders [p .Encoding ]; ok {
83
+ p .Data , err = encoder (p .Content )
84
+ } else {
85
+ err = ErrEncoding
86
+ }
87
+
88
+ if err != nil {
89
+ return
90
+ }
91
+ }
92
+
93
+ //构建包
94
+ p .Length = uint16 (len (p .Data ))
95
+ buf = make ([]byte , 8 + len (p .Data ))
96
+ copy (buf , MAGIC )
97
+ buf [3 ] = p .Type << 4 + p .Encoding & 0xF0
98
+ binary .BigEndian .PutUint16 (buf [4 :], p .Id )
99
+ binary .BigEndian .PutUint16 (buf [6 :], p .Length )
100
+ if p .Data != nil {
101
+ copy (buf [8 :], p .Data ) //内存复制了
102
+ }
103
+ return
104
+ }
105
+
106
+ func (p * Pack ) Decode (buf []byte ) (err error ) {
107
+ p .Id = binary .BigEndian .Uint16 (buf [4 :])
108
+ p .Length = binary .BigEndian .Uint16 (buf [6 :])
109
+ p .Type = buf [3 ] >> 4
110
+ p .Encoding = buf [3 ] & 0xF0
111
+
112
+ if p .Length > 0 {
113
+ if len (buf ) < 8 + int (p .Length ) {
114
+ return ErrNotEnough
115
+ }
116
+ p .Data = buf [8 : 8 + p .Length ]
117
+
118
+ if p .Encoding > 0 {
119
+ if decoder , ok := decoders [p .Encoding ]; ok {
120
+ err = decoder (p .Data , p .Content )
121
+ } else {
122
+ err = ErrEncoding
123
+ }
124
+ } else {
125
+ p .Content = p .Data
126
+ }
127
+ if err != nil {
128
+ return
129
+ }
130
+ }
131
+ return
132
+ }
0 commit comments