-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessaging.go
237 lines (202 loc) · 5.91 KB
/
messaging.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package nativemessaging
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io"
"io/ioutil"
)
var (
// ErrInvalidMessageSize unable to read message size
ErrInvalidMessageSize = errors.New("Invalid message size")
// ErrByteOrderNotSet byter order is set on the first read
ErrByteOrderNotSet = errors.New("Byte order not set")
messageSizeInBytes = binary.Size(uint32(0))
)
// Writer is an interface that wraps the Write method.
type Writer interface {
// Write writes bytes from the given io.Reader to the underlying io.Writer
// It returns the number of bytes from io.Reader and any error from the write operation.
Write(io.Reader) (int, error)
}
// Reader is an interface that wraps the Read method.
type Reader interface {
// Read reads bytes from an io.Reader.
// It returns the bytes read and an error from the read operation.
// Read may return io.EOF when the underlying stream reach the end or is closed
Read() ([]byte, error)
}
// JSONEncoder writes JSON values to an output stream.
type JSONEncoder interface {
Encode(interface{}) error
}
// JSONDecoder reads and decodes JSON values from an input stream.
type JSONDecoder interface {
Decode(interface{}) error
}
type writer struct {
w io.Writer
bo binary.ByteOrder
}
// NewWriter returns a new writer that writes to w
// The data is preceded with 32-bit data length in the specified byte order
func NewWriter(w io.Writer, bo binary.ByteOrder) Writer {
return &writer{
w: w,
bo: bo,
}
}
// NewNativeWriter returns a new writer that writes to the given io.Writer
// The data is preceded with 32-bit data length in native byte order
func NewNativeWriter(w io.Writer) Writer {
return &writer{
w: w,
bo: NativeEndian,
}
}
func (w *writer) Write(r io.Reader) (int, error) {
return Write(w.w, r, w.bo)
}
type reader struct {
r io.Reader
bo binary.ByteOrder
}
// NewReader returns a new reader that reads from the given io.Reader
// interpreting the first 4 bytes as 32-bit data length in the specified byte order
func NewReader(r io.Reader, bo binary.ByteOrder) Reader {
return &reader{
r: r,
bo: bo,
}
}
// NewNativeReader returns a new reader that reads from the given io.Reader
// interpreting the first 4 bytes as 32-bit data length in native byte order
func NewNativeReader(r io.Reader) Reader {
return &reader{
r: r,
bo: NativeEndian,
}
}
func (r *reader) Read() ([]byte, error) {
return Read(r.r, r.bo)
}
type jsonEncoder struct {
w io.Writer
bo binary.ByteOrder
}
// NewJSONEncoder returns a new jsonEncoder that write to the given io.Writer
// The data is preceded with 32-bit data length in the specified byte order
func NewJSONEncoder(w io.Writer, bo binary.ByteOrder) JSONEncoder {
return &jsonEncoder{
w: w,
bo: bo,
}
}
// NewNativeJSONEncoder returns a new jsonEncoder that writes to the given io.Writer
// The data is preceded with 32-bit data length in native byte order
func NewNativeJSONEncoder(w io.Writer) JSONEncoder {
return &jsonEncoder{
w: w,
bo: NativeEndian,
}
}
func (e *jsonEncoder) Encode(v interface{}) error {
_, err := Encode(e.w, v, e.bo)
return err
}
type jsonDecoder struct {
r io.Reader
bo binary.ByteOrder
}
// NewJSONDecoder returns a new jsonDecoder that reads from the given io.Reader
// interpreting the first 4 bytes as 32-bit data length in the specified byte order
func NewJSONDecoder(r io.Reader, bo binary.ByteOrder) JSONDecoder {
return &jsonDecoder{
r: r,
bo: bo,
}
}
// NewNativeJSONDecoder returns a new jsonDecoder that reads from the given io.Reader
// interpreting the first 4 bytes as 32-bit data length in native byte order
func NewNativeJSONDecoder(r io.Reader) JSONDecoder {
return &jsonDecoder{
r: r,
bo: NativeEndian,
}
}
func (d *jsonDecoder) Decode(v interface{}) error {
return Decode(d.r, v, d.bo)
}
type host struct {
r io.Reader
w io.Writer
bo binary.ByteOrder
}
func (h *host) Read() ([]byte, error) {
return Read(h.r, h.bo)
}
func (h *host) Write(message io.Reader) (int, error) {
return Write(h.w, message, h.bo)
}
func (h *host) Send(v interface{}) (int, error) {
return Encode(h.w, v, h.bo)
}
func (h *host) Receive(v interface{}) error {
return Decode(h.r, v, h.bo)
}
// Read reads a message from the given io.Reader interpreting the
// leading first 4 bytes as a 32-bit unsigned integer encoded in the specified byte order
func Read(r io.Reader, order binary.ByteOrder) ([]byte, error) {
b := make([]byte, messageSizeInBytes)
i, err := r.Read(b)
if err != nil {
return nil, err
}
if i == 0 {
return nil, ErrInvalidMessageSize
}
ln := order.Uint32(b)
if ln == 0 {
return nil, ErrInvalidMessageSize
}
m := make([]byte, ln)
_, err = r.Read(m)
if err != nil {
return nil, err
}
return m, nil
}
// Decode parses the incoming JSON-encoded data and stores the result in the value pointed to by v.
// The leading first 4 bytes of the data is interpreted as a 32-bit unsigned integer encoded in the specified byte order
func Decode(r io.Reader, v interface{}, order binary.ByteOrder) error {
b, err := Read(r, order)
if err != nil {
return err
}
err = json.Unmarshal(b, v)
if err != nil {
return errors.New(err.Error() + string(b))
}
return nil
}
// Write writes to io.Writer the data read from the given io.Reader
// The data is preceded with a 32-bit unsigned integer data length in the specified byte order
func Write(w io.Writer, message io.Reader, order binary.ByteOrder) (i int, err error) {
data, err := ioutil.ReadAll(message)
if err != nil {
return 0, err
}
header := make([]byte, messageSizeInBytes)
order.PutUint32(header, uint32(len(data)))
return w.Write(append(header, data...))
}
// Encode writes to io.Writer the json encoded data of the given value
// The data is preceded with a 32-bit unsigned integer data length in the specified byte order
func Encode(w io.Writer, v interface{}, order binary.ByteOrder) (int, error) {
b, err := json.Marshal(v)
if err != nil {
return 0, err
}
return Write(w, bytes.NewReader(b), order)
}