Repository navigation
Expand file tree
/
Copy pathframed.go
More file actions
177 lines (145 loc) · 4.25 KB
/
Copy pathframed.go
File metadata and controls
177 lines (145 loc) · 4.25 KB
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
/*
Package framed provides an implementations of io.Writer and io.Reader that write
and read whole frames only.
Frames are length-prefixed. The first two bytes are an unsigned 16 bit int
stored in little-endian byte order indicating the length of the content. The
remaining bytes are the actual content of the frame.
The use of a uint16 means that the maximum possible frame size (MaxFrameSize)
is 65535.
*/
package framed
import (
"encoding/binary"
"fmt"
"io"
"sync"
)
const (
// FrameHeaderBits is the size of the frame header in bits
FrameHeaderBits = 16
// FrameHeaderLength is the size of the frame header in bytes
FrameHeaderLength = FrameHeaderBits / 8
// MaxFrameLength is the maximum possible size of a frame (not including the
// length prefix)
MaxFrameLength = 1<<FrameHeaderBits - 1
tooLongError = "Attempted to write frame of length %d which is longer than maximum allowed length of %d"
)
var endianness = binary.LittleEndian
/*
A Reader enhances an io.ReadCloser to read data in contiguous frames. It
implements the io.Reader interface, but unlike typical io.Readers it only
returns whole frames.
A Reader also supports the ability to read frames using dynamically allocated
buffers via the ReadFrame method.
*/
type Reader struct {
Stream io.Reader // the raw underlying connection
mutex sync.Mutex
}
/*
A Writer enhances an io.WriteCLoser to write data in contiguous frames. It
implements the io.Writer interface, but unlike typical io.Writers, it includes
information that allows a corresponding Reader to read whole frames without them
being fragmented.
A Writer also supports a method that writes multiple buffers to the underlying
stream as a single frame.
*/
type Writer struct {
Stream io.Writer // the raw underlying connection
mutex sync.Mutex
}
func NewReader(r io.Reader) *Reader {
return &Reader{Stream: r}
}
func NewWriter(w io.Writer) *Writer {
return &Writer{Stream: w}
}
/*
Read implements the function from io.Reader. Unlike io.Reader.Read,
frame.Read only returns full frames of data (assuming that the data was written
by a framed.Writer).
*/
func (framed *Reader) Read(buffer []byte) (n int, err error) {
framed.mutex.Lock()
defer framed.mutex.Unlock()
var nb uint16
err = binary.Read(framed.Stream, endianness, &nb)
if err != nil {
return
}
n = int(nb)
bufferSize := len(buffer)
if n > bufferSize {
return 0, fmt.Errorf("Buffer of size %d is too small to hold frame of size %d", bufferSize, n)
}
// Read into buffer
n, err = io.ReadFull(framed.Stream, buffer[:n])
return
}
// ReadFrame reads the next frame, using a new buffer sized to hold the frame.
func (framed *Reader) ReadFrame() (frame []byte, err error) {
framed.mutex.Lock()
defer framed.mutex.Unlock()
var nb uint16
err = binary.Read(framed.Stream, endianness, &nb)
if err != nil {
return
}
l := int(nb)
frame = make([]byte, l)
// Read into buffer
_, err = io.ReadFull(framed.Stream, frame)
return
}
/*
Write implements the Write method from io.Writer. It prepends a frame length
header that allows the framed.Reader on the other end to read the whole frame.
*/
func (framed *Writer) Write(frame []byte) (n int, err error) {
framed.mutex.Lock()
defer framed.mutex.Unlock()
n = len(frame)
if n > MaxFrameLength {
return 0, fmt.Errorf(tooLongError, n, MaxFrameLength)
}
// Write the length header
if err = binary.Write(framed.Stream, endianness, uint16(n)); err != nil {
return
}
// Write the data
var written int
if written, err = framed.Stream.Write(frame); err != nil {
return
}
if written != n {
err = fmt.Errorf("%d bytes written, expected to write %d", written, n)
}
return
}
func (framed *Writer) WritePieces(pieces ...[]byte) (n int, err error) {
framed.mutex.Lock()
defer framed.mutex.Unlock()
for _, piece := range pieces {
n = n + len(piece)
}
if n > MaxFrameLength {
return 0, fmt.Errorf(tooLongError, n, MaxFrameLength)
}
// Write the length header
if err = binary.Write(framed.Stream, endianness, uint16(n)); err != nil {
return
}
// Write the data
var written int
for _, piece := range pieces {
var nw int
if nw, err = framed.Stream.Write(piece); err != nil {
return
}
written = written + nw
}
if written != n {
err = fmt.Errorf("%d bytes written, expected to write %d", written, n)
}
return
}