-
Notifications
You must be signed in to change notification settings - Fork 83
/
frame.go
214 lines (176 loc) · 5.96 KB
/
frame.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
205
206
207
208
209
210
211
212
213
214
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"encoding/json"
"fmt"
"io"
"math"
"github.com/uber/tchannel-go/typed"
)
const (
// MaxFrameSize is the total maximum size for a frame
MaxFrameSize = math.MaxUint16
// FrameHeaderSize is the size of the header element for a frame
FrameHeaderSize = 16
// MaxFramePayloadSize is the maximum size of the payload for a single frame
MaxFramePayloadSize = MaxFrameSize - FrameHeaderSize
)
// FrameHeader is the header for a frame, containing the MessageType and size
type FrameHeader struct {
// The size of the frame including the header
size uint16
// The type of message represented by the frame
messageType messageType
// Left empty
reserved1 byte
// The id of the message represented by the frame
ID uint32
// Left empty
reserved [8]byte
}
// SetPayloadSize sets the size of the frame payload
func (fh *FrameHeader) SetPayloadSize(size uint16) {
fh.size = size + FrameHeaderSize
}
// PayloadSize returns the size of the frame payload
func (fh FrameHeader) PayloadSize() uint16 {
return fh.size - FrameHeaderSize
}
// FrameSize returns the total size of the frame
func (fh FrameHeader) FrameSize() uint16 {
return fh.size
}
// MessageType returns the type of the message
func (fh FrameHeader) MessageType() byte {
return byte(fh.messageType)
}
func (fh FrameHeader) String() string { return fmt.Sprintf("%v[%d]", fh.messageType, fh.ID) }
// MarshalJSON returns a `{"id":NNN, "msgType":MMM, "size":SSS}` representation
func (fh FrameHeader) MarshalJSON() ([]byte, error) {
s := struct {
ID uint32 `json:"id"`
MsgType messageType `json:"msgType"`
Size uint16 `json:"size"`
}{fh.ID, fh.messageType, fh.size}
return json.Marshal(s)
}
func (fh *FrameHeader) read(r *typed.ReadBuffer) error {
fh.size = r.ReadUint16()
fh.messageType = messageType(r.ReadSingleByte())
fh.reserved1 = r.ReadSingleByte()
fh.ID = r.ReadUint32()
r.ReadBytes(len(fh.reserved))
return r.Err()
}
func (fh *FrameHeader) write(w *typed.WriteBuffer) error {
w.WriteUint16(fh.size)
w.WriteSingleByte(byte(fh.messageType))
w.WriteSingleByte(fh.reserved1)
w.WriteUint32(fh.ID)
w.WriteBytes(fh.reserved[:])
return w.Err()
}
// A Frame is a header and payload
type Frame struct {
buffer []byte // full buffer, including payload and header
headerBuffer []byte // slice referencing just the header
// The header for the frame
Header FrameHeader
// The payload for the frame
Payload []byte
}
// NewFrame allocates a new frame with the given payload capacity
func NewFrame(payloadCapacity int) *Frame {
f := &Frame{}
f.buffer = make([]byte, payloadCapacity+FrameHeaderSize)
f.Payload = f.buffer[FrameHeaderSize:]
f.headerBuffer = f.buffer[:FrameHeaderSize]
return f
}
// ReadBody takes in a previously read frame header, and only reads in the body
// based on the size specified in the header. This allows callers to defer
// the frame allocation till the body needs to be read.
func (f *Frame) ReadBody(header []byte, r io.Reader) error {
// Copy the header into the underlying buffer so we have an assembled frame
// that can be directly forwarded.
copy(f.buffer, header)
// Parse the header into our typed struct.
if err := f.Header.read(typed.NewReadBuffer(header)); err != nil {
return err
}
switch payloadSize := f.Header.PayloadSize(); {
case payloadSize > MaxFramePayloadSize:
return fmt.Errorf("invalid frame size %v", f.Header.size)
case payloadSize > 0:
_, err := io.ReadFull(r, f.SizedPayload())
return err
default:
// No payload to read
return nil
}
}
// ReadIn reads the frame from the given io.Reader.
// Deprecated: Only maintained for backwards compatibility. Callers should
// use ReadBody instead.
func (f *Frame) ReadIn(r io.Reader) error {
header := make([]byte, FrameHeaderSize)
if _, err := io.ReadFull(r, header); err != nil {
return err
}
return f.ReadBody(header, r)
}
// WriteOut writes the frame to the given io.Writer
func (f *Frame) WriteOut(w io.Writer) error {
var wbuf typed.WriteBuffer
wbuf.Wrap(f.headerBuffer)
if err := f.Header.write(&wbuf); err != nil {
return err
}
fullFrame := f.buffer[:f.Header.FrameSize()]
if _, err := w.Write(fullFrame); err != nil {
return err
}
return nil
}
// SizedPayload returns the slice of the payload actually used, as defined by the header
func (f *Frame) SizedPayload() []byte {
return f.Payload[:f.Header.PayloadSize()]
}
// messageType returns the message type.
func (f *Frame) messageType() messageType {
return f.Header.messageType
}
func (f *Frame) write(msg message) error {
var wbuf typed.WriteBuffer
wbuf.Wrap(f.Payload[:])
if err := msg.write(&wbuf); err != nil {
return err
}
f.Header.ID = msg.ID()
f.Header.reserved1 = 0
f.Header.messageType = msg.messageType()
f.Header.SetPayloadSize(uint16(wbuf.BytesWritten()))
return nil
}
func (f *Frame) read(msg message) error {
var rbuf typed.ReadBuffer
rbuf.Wrap(f.SizedPayload())
return msg.read(&rbuf)
}