/
baidu_rpc_codec.go
184 lines (153 loc) · 4.12 KB
/
baidu_rpc_codec.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2018-present, NebulaChat Studio (https://nebula.chat).
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Benqi (wubenqi@gmail.com)
package brpc
import (
"github.com/nebula-chat/chatengine/pkg/net2"
"io"
"encoding/binary"
"net"
"bytes"
"github.com/golang/glog"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/nebula-chat/chatengine/pkg/util"
)
func init() {
net2.RegisterProtocol("brpc", &BaiduRpcProtocol{})
}
// PARSE_ERROR_TOO_BIG_DATA
var brpcMagicNumber = []byte{'P', 'R', 'B', 'C'}
type BaiduRpcProtocol struct {
}
func (m *BaiduRpcProtocol) NewCodec(rw io.ReadWriter) (cc net2.Codec, err error) {
codec := &BaiduRpcCodec{rw.(*net.TCPConn)}
return codec, nil
}
func NewBaiduRpcProtocol() net2.Protocol {
return new(BaiduRpcProtocol)
}
////////////////////////////////////////////////////////////////////////////////
type BaiduRpcMessage struct {
Meta *RpcMeta
Payload []byte
Attachment []byte
}
func (m *BaiduRpcMessage) encode() [] byte {
x := util.NewBufferOutput(512)
var (
payloadSize = len(m.Payload)
metaSize int
attachmentSize = len(m.Attachment)
)
if attachmentSize > 0 {
m.Meta.AttachmentSize = new(int32)
*m.Meta.AttachmentSize = int32(attachmentSize)
}
if m.Meta != nil {
metaSize = proto.Size(m.Meta)
}
x.Bytes(brpcMagicNumber)
x.Int32(int32(payloadSize + attachmentSize + metaSize))
x.Int32(int32(metaSize))
x.Bytes(m.Payload)
if attachmentSize > 0 {
x.Bytes(m.Attachment)
}
mData, _ := proto.Marshal(m.Meta)
x.Bytes(mData)
return x.Buf()
}
type BaiduRpcCodec struct {
*net.TCPConn
}
func (c *BaiduRpcCodec) Send(msg interface{}) error {
var (
err error
message *BaiduRpcMessage
)
// glog.Info("send: ", msg)
switch msg.(type) {
case *BaiduRpcMessage:
message, _ = msg.(*BaiduRpcMessage)
b := message.encode()
_, err = c.TCPConn.Write(b)
default:
err = fmt.Errorf("invalid zproto message: %v", msg)
}
return err
}
func (c *BaiduRpcCodec) Receive() (interface{}, error) {
var (
headBuf = make([]byte, 12)
bodySize uint32
metaSize uint32
)
_, err := io.ReadFull(c.TCPConn, headBuf)
if err != nil {
glog.Error(err)
return nil, err
}
if !bytes.Equal(headBuf[:4], brpcMagicNumber) {
err = fmt.Errorf("invalid magic number: %s", string(headBuf[:4]))
glog.Error(err)
return nil, err
}
bodySize = binary.LittleEndian.Uint32(headBuf[4:8])
metaSize = binary.LittleEndian.Uint32(headBuf[8:])
if bodySize > 64*1024*1024 {
// We need this log to report the body_size to give users some clues
// which is not printed in InputMessenger.
err = fmt.Errorf("body_size=%d, from %s is too large", bodySize, c.TCPConn.RemoteAddr())
return nil, err
}
if metaSize > bodySize {
err = fmt.Errorf("meta_size=%d is bigger than body_size=%d", metaSize, bodySize)
return nil, err
}
bodySize -= metaSize
bodyData := make([]byte, bodySize)
_, err = io.ReadFull(c.TCPConn, bodyData)
if err != nil {
glog.Error(err)
return nil, err
}
metaData := make([]byte, metaSize)
_, err = io.ReadFull(c.TCPConn, metaData)
if err != nil {
glog.Error(err)
return nil, err
}
meta := new(RpcMeta)
err = proto.Unmarshal(metaData, meta)
if err != nil {
glog.Error(err)
return nil, err
}
attachedSize := uint32(meta.GetAttachmentSize())
if attachedSize > bodySize {
err = fmt.Errorf("attached_size=%d is bigger than body_size=%d", attachedSize, bodySize)
return nil, err
}
message := &BaiduRpcMessage{
Meta: meta,
Payload: bodyData[:bodySize-attachedSize],
Attachment: bodyData[bodySize-attachedSize:],
}
return message, nil
}
func (c *BaiduRpcCodec) Close() error {
return c.TCPConn.Close()
}