-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.go
204 lines (170 loc) · 4.49 KB
/
wire.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protorpc
import (
"fmt"
"hash/crc32"
"io"
wire "github.com/chai2010/protorpc/wire.pb"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
)
func writeRequest(w io.Writer, id uint64, method string, request proto.Message) error {
// marshal request
pbRequest := []byte{}
if request != nil {
var err error
pbRequest, err = proto.Marshal(request)
if err != nil {
return err
}
}
// compress serialized proto data
compressedPbRequest := snappy.Encode(nil, pbRequest)
// generate header
header := &wire.RequestHeader{
Id: id,
Method: method,
RawRequestLen: uint32(len(pbRequest)),
SnappyCompressedRequestLen: uint32(len(compressedPbRequest)),
Checksum: crc32.ChecksumIEEE(compressedPbRequest),
}
// check header size
pbHeader, err := proto.Marshal(header)
if err != err {
return err
}
if len(pbHeader) > int(wire.Const_MAX_REQUEST_HEADER_LEN) {
return fmt.Errorf("protorpc.writeRequest: header larger than max_header_len: %d.", len(pbHeader))
}
// send header (more)
if err := sendFrame(w, pbHeader); err != nil {
return err
}
// send body (end)
if err := sendFrame(w, compressedPbRequest); err != nil {
return err
}
return nil
}
func readRequestHeader(r io.Reader, header *wire.RequestHeader) (err error) {
// recv header (more)
pbHeader, err := recvFrame(r)
if err != nil {
return err
}
// Marshal Header
err = proto.Unmarshal(pbHeader, header)
if err != nil {
return err
}
return nil
}
func readRequestBody(r io.Reader, header *wire.RequestHeader, request proto.Message) error {
// recv body (end)
compressedPbRequest, err := recvFrame(r)
if err != nil {
return err
}
// checksum
if crc32.ChecksumIEEE(compressedPbRequest) != header.Checksum {
return fmt.Errorf("protorpc.readRequestBody: unexpected checksum.")
}
// decode the compressed data
pbRequest, err := snappy.Decode(nil, compressedPbRequest)
if err != nil {
return err
}
// check wire header: rawMsgLen
if uint32(len(pbRequest)) != header.RawRequestLen {
return fmt.Errorf("protorpc.readRequestBody: Unexcpeted header.RawRequestLen.")
}
// Unmarshal to proto message
if request != nil {
err = proto.Unmarshal(pbRequest, request)
if err != nil {
return err
}
}
return nil
}
func writeResponse(w io.Writer, id uint64, serr string, response proto.Message) (err error) {
// clean response if error
if serr != "" {
response = nil
}
// marshal response
pbResponse := []byte{}
if response != nil {
pbResponse, err = proto.Marshal(response)
if err != nil {
return err
}
}
// compress serialized proto data
compressedPbResponse := snappy.Encode(nil, pbResponse)
// generate header
header := &wire.ResponseHeader{
Id: id,
Error: serr,
RawResponseLen: uint32(len(pbResponse)),
SnappyCompressedResponseLen: uint32(len(compressedPbResponse)),
Checksum: crc32.ChecksumIEEE(compressedPbResponse),
}
// check header size
pbHeader, err := proto.Marshal(header)
if err != err {
return
}
// send header (more)
if err = sendFrame(w, pbHeader); err != nil {
return
}
// send body (end)
if err = sendFrame(w, compressedPbResponse); err != nil {
return
}
return nil
}
func readResponseHeader(r io.Reader, header *wire.ResponseHeader) error {
// recv header (more)
pbHeader, err := recvFrame(r)
if err != nil {
return err
}
// Marshal Header
err = proto.Unmarshal(pbHeader, header)
if err != nil {
return err
}
return nil
}
func readResponseBody(r io.Reader, header *wire.ResponseHeader, response proto.Message) error {
// recv body (end)
compressedPbResponse, err := recvFrame(r)
if err != nil {
return err
}
// checksum
if crc32.ChecksumIEEE(compressedPbResponse) != header.Checksum {
return fmt.Errorf("protorpc.readResponseBody: unexpected checksum.")
}
// decode the compressed data
pbResponse, err := snappy.Decode(nil, compressedPbResponse)
if err != nil {
return err
}
// check wire header: rawMsgLen
if uint32(len(pbResponse)) != header.RawResponseLen {
return fmt.Errorf("protorpc.readResponseBody: Unexcpeted header.RawResponseLen.")
}
// Unmarshal to proto message
if response != nil {
err = proto.Unmarshal(pbResponse, response)
if err != nil {
return err
}
}
return nil
}