-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
640 lines (588 loc) · 16.9 KB
/
message.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package sox
import (
"bufio"
"encoding/binary"
"errors"
"io"
"sync/atomic"
)
// MessageOptions represents message feature options
type MessageOptions struct {
// ReadByteOrder sets which byte order will be used when reading data
ReadByteOrder binary.ByteOrder
// WriteByteOrder sets which byte order will be used when writing data
WriteByteOrder binary.ByteOrder
// ReadProto sets which protocol type will be used when reading data
ReadProto UnderlyingProtocol
// WriteProto sets which protocol type will be used when writing data
WriteProto UnderlyingProtocol
// ReadLimit is the maximum message payload data size
// A ReadLimit of zero indicates that there is no limit
ReadLimit int
// Nonblock if the nonblock flag is true, Message will not block on I/O
Nonblock bool
}
var defaultMessageOptions = MessageOptions{
ReadByteOrder: binary.BigEndian,
WriteByteOrder: binary.BigEndian,
ReadProto: UnderlyingProtocolStream,
WriteProto: UnderlyingProtocolStream,
Nonblock: false,
}
// MessageOptionsTCPSocket sets feature options for TCP sockets
var MessageOptionsTCPSocket = func(options *MessageOptions) {
options.ReadByteOrder = binary.BigEndian
options.WriteByteOrder = binary.BigEndian
options.ReadProto = UnderlyingProtocolStream
options.WriteProto = UnderlyingProtocolStream
}
// MessageOptionsSCTPSocket sets feature options for SCTP sockets
var MessageOptionsSCTPSocket = func(options *MessageOptions) {
options.ReadByteOrder = binary.BigEndian
options.WriteByteOrder = binary.BigEndian
options.ReadProto = UnderlyingProtocolSeqPacket
options.WriteProto = UnderlyingProtocolSeqPacket
}
// MessageOptionsNetworkOrder sets byte order to big endian
var MessageOptionsNetworkOrder = func(options *MessageOptions) {
options.ReadByteOrder = binary.BigEndian
options.WriteByteOrder = binary.BigEndian
}
// MessageOptionsNonblock sets message nonblock
var MessageOptionsNonblock = func(options *MessageOptions) {
options.Nonblock = true
}
// NewMessageReader creates and returns a new io.Reader to read messages
func NewMessageReader(reader io.Reader, opts ...func(options *MessageOptions)) io.Reader {
return &messageReader{message: newMessage(reader, nil, opts...)}
}
// NewMessageWriter creates and returns a new io.Writer to write messages
func NewMessageWriter(writer io.Writer, opts ...func(options *MessageOptions)) io.Writer {
return &messageWriter{message: newMessage(nil, writer, opts...)}
}
// NewMessageReadWriter creates and returns a new io.ReadWriter to read and write messages
func NewMessageReadWriter(reader io.Reader, writer io.Writer, opts ...func(options *MessageOptions)) io.ReadWriter {
return &messageReadWriter{
messageReader: &messageReader{newMessage(reader, nil, opts...)},
messageWriter: &messageWriter{newMessage(nil, writer, opts...)},
}
}
// NewMessagePipe creates and returns a synchronous in-memory message pipe
func NewMessagePipe(opts ...func(options *MessageOptions)) (reader io.Reader, writer io.Writer) {
r, w := io.Pipe()
pipe := NewMessageReadWriter(r, w, opts...)
reader, writer = pipe, pipe
return
}
// UnderlyingProtocol represents transmission protocol features
type UnderlyingProtocol int
const (
// UnderlyingProtocolStream means the underlying protocol works like stream
UnderlyingProtocolStream UnderlyingProtocol = 1
// UnderlyingProtocolDgram means the underlying protocol works like datagram
UnderlyingProtocolDgram UnderlyingProtocol = 2
// UnderlyingProtocolSeqPacket means the underlying protocol works like sequenced packet
UnderlyingProtocolSeqPacket UnderlyingProtocol = 5
)
// PreserveBoundary returns true if the underlying protocol preserves message boundaries
func (t UnderlyingProtocol) PreserveBoundary() bool {
switch t {
case UnderlyingProtocolDgram, UnderlyingProtocolSeqPacket:
return true
default:
return false
}
}
//
// We defined an original message protocol format as follows:
//
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +--------------+-------------------------------+---------------+
// |Payload Length| Extended Payload Length | Ext. Length |
// | (8) | (16/56) | continued ... |
// | | (if payload length==254/255) | if len == 255 |
// +--------------+ - - - - - - - - - - - - - - - - - - - - - - - +
// | Extended payload length continued, if payload len == 255 |
// | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
// | Payload Data |
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
// : Payload Data continued ... :
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
// | Payload Data continued ... |
// +--------------------------------------------------------------+
//
// Payload length: 8 bits, 8+16 bits, or 8+56 bits
// The length of the "Payload data", in bytes: if 0-253, that is the
// payload length. If 254, the following 2 bytes interpreted as a
// 2-bytes unsigned integer are the payload length. If 255, the
// following 7 bytes interpreted as a 56-bits unsigned integer are
// the payload length. Multibyte length quantities are expressed in
// network byte order. oad Length to encode the length.
var (
// ErrMsgInvalidArguments will be returned when got invalid parameter
ErrMsgInvalidArguments = errors.New("message invalid argument")
// ErrMsgInvalidRead will be returned when read operation invalid
ErrMsgInvalidRead = errors.New("message invalid read result")
// ErrMsgInvalidWrite will be returned when write operation invalid
ErrMsgInvalidWrite = errors.New("message invalid write result")
// ErrMsgTooLong will be returned when try to read or write a message which is too long
ErrMsgTooLong = errors.New("message too long")
// ErrMsgClosed will be returned when try to read or write on a closed reader or writer
ErrMsgClosed = errors.New("message closed")
)
const (
messageHeaderLength = 1
messagePayloadMaxLength8Bits = 1<<8 - 3
messagePayloadMaxLength16Bits = 1<<16 - 1
messagePayloadMaxLength56Bits = 1<<56 - 1
messageStatusRead uint32 = 4
messageStatusWrite uint32 = 2
messageStatusClosed uint32 = 0x2000
)
type message struct {
rd io.Reader
rbo binary.ByteOrder
rpr UnderlyingProtocol
wr io.Writer
wbo binary.ByteOrder
wpr UnderlyingProtocol
status atomic.Uint32
header [8]byte
length int64
offset int64
count atomic.Int32
readLimit int64
nonblock bool
done bool
}
func (msg *message) close() error {
if msg.done {
return nil
}
for sw := NewParamSpinWait(); !sw.Closed(); {
status := msg.status.Load()
if (status & (messageStatusRead | messageStatusWrite)) == (messageStatusRead | messageStatusWrite) {
if msg.nonblock {
return ErrTemporarilyUnavailable
}
sw.Once()
continue
}
if msg.status.CompareAndSwap(status, status|messageStatusClosed) {
msg.done = true
return nil
}
sw.OnceWithLevel(SpinWaitLevelPreempting)
}
return nil
}
func (msg *message) setReadWriter(rw io.ReadWriter, order binary.ByteOrder, typ UnderlyingProtocol) {
msg.setReader(rw, order, typ)
msg.setWriter(rw, order, typ)
}
func (msg *message) setReader(r io.Reader, order binary.ByteOrder, typ UnderlyingProtocol) {
msg.rd = r
msg.rbo = order
msg.rpr = typ
}
func (msg *message) setWriter(w io.Writer, order binary.ByteOrder, typ UnderlyingProtocol) {
msg.wr = w
msg.wbo = order
msg.wpr = typ
}
func (msg *message) read(p []byte) (n int, err error) {
if msg.done {
return 0, io.EOF
}
if _, ok := msg.enterRead(); !ok {
return 0, ErrTemporarilyUnavailable
}
if msg.rpr.PreserveBoundary() {
return msg.readPacket(p)
}
return msg.readStream(p)
}
func (msg *message) readStream(p []byte) (n int, err error) {
defer func() {
if err != ErrTemporarilyUnavailable {
msg.exitRead()
}
}()
for rn := 0; msg.offset < messageHeaderLength; {
rn, err = msg.readOnce(msg.header[msg.offset:messageHeaderLength])
msg.offset += int64(rn)
if err != nil && err != io.EOF && (err != ErrTemporarilyUnavailable || msg.nonblock) {
return
}
if err == io.EOF {
if msg.offset < messageHeaderLength {
return 0, io.ErrUnexpectedEOF
}
break
}
}
exLengthBytes := int64(0)
if msg.offset >= messageHeaderLength {
if msg.readLimit > 0 && msg.length > msg.readLimit {
return 0, ErrMsgTooLong
}
if msg.header[0] == messagePayloadMaxLength8Bits+1 {
exLengthBytes = 2
} else if msg.header[0] == messagePayloadMaxLength8Bits+2 {
exLengthBytes = 7
}
}
if len(msg.header) < int(messageHeaderLength+exLengthBytes) {
return 0, io.ErrShortBuffer
}
if msg.offset < messageHeaderLength+exLengthBytes {
for rn := 0; msg.offset < messageHeaderLength+exLengthBytes; {
rn, err = msg.readOnce(msg.header[messageHeaderLength : messageHeaderLength+exLengthBytes])
msg.offset += int64(rn)
if err != nil && err != io.EOF && (err != ErrTemporarilyUnavailable || msg.nonblock) {
return
}
if err == io.EOF {
if msg.offset < messageHeaderLength+exLengthBytes {
return 0, io.ErrUnexpectedEOF
}
break
}
}
}
if msg.offset == messageHeaderLength+exLengthBytes {
if exLengthBytes == 2 {
msg.length = int64(msg.rbo.Uint16(msg.header[messageHeaderLength : messageHeaderLength+exLengthBytes]))
} else if exLengthBytes == 7 {
u64 := msg.rbo.Uint64(msg.header[:])
if msg.rbo == binary.LittleEndian {
msg.length = int64(u64 >> 8)
} else if msg.rbo == binary.BigEndian {
msg.length = int64(u64 & messagePayloadMaxLength56Bits)
}
} else {
msg.length = int64(msg.header[0])
}
}
if msg.readLimit > 0 && msg.length > msg.readLimit {
return 0, ErrMsgTooLong
}
// we assume that generally a 4K buffer p []byte will be given
if msg.length > int64(len(p)) {
if msg.length < (1 << 16) {
// TODO: acquire a 64k buffer from pool
} else if msg.length < (1 << 20) {
// TODO: acquire a 1m buffer from pool
} else if msg.length < (1 << 24) {
// TODO: acquire a 16m buffer from pool
} else {
// TODO: non-buffered work mode
}
return 0, io.ErrShortBuffer
}
for rn := 0; msg.offset < messageHeaderLength+exLengthBytes+msg.length; {
rn, err = msg.readOnce(p[msg.offset-messageHeaderLength-exLengthBytes : msg.length])
msg.offset += int64(rn)
n += rn
if err != nil && err != io.EOF && (err != ErrTemporarilyUnavailable || msg.nonblock) {
return
}
if err == io.EOF {
if msg.offset < messageHeaderLength+exLengthBytes+msg.length {
return n, io.ErrUnexpectedEOF
}
break
}
}
msg.count.Add(-1)
msg.reset()
return
}
func (msg *message) readPacket(p []byte) (n int, err error) {
defer msg.exitRead()
for {
n, err = msg.readOnce(p)
if err == ErrTemporarilyUnavailable {
if msg.nonblock {
return
}
continue
}
if err != nil && err != io.EOF {
return
}
if n > messagePayloadMaxLength56Bits {
return n, ErrMsgTooLong
} else if n == len(p) {
break
}
}
msg.count.Add(-1)
msg.reset()
return
}
func (msg *message) readOnce(p []byte) (n int, err error) {
if msg.rd == nil {
return 0, ErrMsgInvalidArguments
}
for {
n, err = msg.rd.Read(p)
if err != ErrTemporarilyUnavailable {
break
}
if msg.nonblock {
break
}
}
return
}
func (msg *message) enterRead() (oldStatus uint32, ok bool) {
if msg.wr == nil {
return 0, true
}
sw := SpinWait{}
for {
oldStatus = msg.status.Load()
if (oldStatus & ^messageStatusRead) == 0 {
if msg.status.CompareAndSwap(oldStatus, oldStatus|messageStatusRead) {
return oldStatus, true
}
sw.Once()
continue
} else if !msg.nonblock {
sw.Once()
continue
}
return oldStatus, false
}
}
func (msg *message) exitRead() (oldStatus uint32) {
if msg.wr == nil {
return 0
}
sw := SpinWait{}
for {
oldStatus = msg.status.Load()
if msg.status.CompareAndSwap(oldStatus, oldStatus&^messageStatusRead) {
break
}
sw.Once()
}
return
}
func (msg *message) write(p []byte) (n int, err error) {
if msg.done {
return 0, ErrMsgClosed
}
if _, ok := msg.enterWrite(); !ok {
return 0, ErrTemporarilyUnavailable
}
if msg.wpr.PreserveBoundary() {
return msg.writePacket(p)
}
return msg.writeStream(p)
}
func (msg *message) writeStream(p []byte) (n int, err error) {
defer func() {
if err != ErrTemporarilyUnavailable {
msg.exitWrite()
}
}()
if msg.length > messagePayloadMaxLength56Bits {
return 0, ErrMsgTooLong
}
if msg.offset == 0 {
msg.length = int64(len(p))
}
exLengthBytes := int64(0)
if msg.length <= messagePayloadMaxLength8Bits {
exLengthBytes = 0
} else if msg.length <= messagePayloadMaxLength16Bits {
exLengthBytes = 2
} else {
exLengthBytes = 7
}
if msg.offset == 0 {
if msg.length <= messagePayloadMaxLength8Bits {
msg.header[0] = byte(msg.length)
} else if msg.length <= messagePayloadMaxLength16Bits {
msg.header[0] = messagePayloadMaxLength8Bits + 1
msg.wbo.PutUint16(msg.header[messageHeaderLength:messageHeaderLength+exLengthBytes], uint16(msg.length))
} else {
if msg.wbo == binary.LittleEndian {
msg.wbo.PutUint64(msg.header[:], uint64(msg.length)<<8)
} else {
msg.wbo.PutUint64(msg.header[:], uint64(msg.length&messagePayloadMaxLength56Bits))
}
msg.header[0] = messagePayloadMaxLength8Bits + 2
}
}
for wn := 0; msg.offset < messageHeaderLength+exLengthBytes; {
wn, err = msg.writeOnce(msg.header[msg.offset : messageHeaderLength+exLengthBytes])
msg.offset += int64(wn)
if err != nil && (err != ErrTemporarilyUnavailable || msg.nonblock) {
return
}
}
if msg.length != msg.offset-messageHeaderLength-exLengthBytes+int64(len(p)) {
return 0, io.ErrShortWrite
}
for wn := 0; msg.offset < messageHeaderLength+exLengthBytes+msg.length; {
wn, err = msg.writeOnce(p[:msg.length-(msg.offset-messageHeaderLength-exLengthBytes)])
msg.offset += int64(wn)
n += wn
if err != nil && (err != ErrTemporarilyUnavailable || msg.nonblock) {
break
}
}
if msg.offset < messageHeaderLength+msg.length {
return n, io.ErrShortWrite
}
msg.count.Add(1)
msg.reset()
return
}
func (msg *message) writePacket(p []byte) (n int, err error) {
defer msg.exitWrite()
if len(p) > messagePayloadMaxLength56Bits {
return 0, bufio.ErrTooLong
}
for {
n, err = msg.writeOnce(p)
if err == ErrTemporarilyUnavailable {
if msg.nonblock {
return
}
continue
}
if err != nil {
return
}
if n < len(p) {
return n, io.ErrShortWrite
} else if n == len(p) {
break
}
}
msg.count.Add(1)
msg.reset()
return
}
func (msg *message) writeOnce(p []byte) (n int, err error) {
if msg.wr == nil {
return 0, ErrMsgInvalidArguments
}
for {
n, err = msg.wr.Write(p)
if err != ErrTemporarilyUnavailable {
break
}
if msg.nonblock {
break
}
}
return
}
func (msg *message) enterWrite() (oldStatus uint32, ok bool) {
if msg.rd == nil {
return 0, true
}
sw := SpinWait{}
for {
oldStatus = msg.status.Load()
if (oldStatus & ^messageStatusWrite) == 0 {
if msg.status.CompareAndSwap(oldStatus, oldStatus|messageStatusWrite) {
return oldStatus, true
}
sw.Once()
continue
} else if !msg.nonblock {
sw.Once()
continue
}
return oldStatus, false
}
}
func (msg *message) exitWrite() (oldStatus uint32) {
if msg.rd == nil {
return 0
}
sw := SpinWait{}
for {
oldStatus = msg.status.Load()
if msg.status.CompareAndSwap(oldStatus, oldStatus&^messageStatusWrite) {
break
}
sw.Once()
}
return
}
func (msg *message) readFrom(reader io.Reader) (n int64, err error) {
if msg.done {
return 0, ErrMsgClosed
}
if msg.wr == nil {
return 0, ErrMsgInvalidArguments
}
return io.Copy(msg.wr, reader)
}
func (msg *message) writeTo(writer io.Writer) (n int64, err error) {
if msg.done {
return 0, io.EOF
}
if msg.rd == nil {
return 0, ErrMsgInvalidArguments
}
return io.Copy(writer, msg.rd)
}
func (msg *message) reset() {
msg.offset = 0
}
func newMessage(reader io.Reader, writer io.Writer, opts ...func(options *MessageOptions)) *message {
opt := defaultMessageOptions
for _, fn := range opts {
fn(&opt)
}
m := &message{
status: atomic.Uint32{},
header: [8]byte{},
length: 0,
offset: 0,
count: atomic.Int32{},
readLimit: int64(opt.ReadLimit),
nonblock: opt.Nonblock,
done: false,
}
if reader != nil {
m.setReader(reader, opt.ReadByteOrder, opt.ReadProto)
}
if writer != nil {
m.setWriter(writer, opt.WriteByteOrder, opt.WriteProto)
}
return m
}
type messageReader struct {
*message
}
func (msg *messageReader) Read(b []byte) (n int, err error) {
return msg.read(b)
}
func (msg *messageReader) WriteTo(writer io.Writer) (n int64, err error) {
return msg.writeTo(writer)
}
type messageWriter struct {
*message
}
func (msg *messageWriter) Write(b []byte) (n int, err error) {
return msg.write(b)
}
func (msg *messageWriter) ReadFrom(reader io.Reader) (n int64, err error) {
return msg.readFrom(reader)
}
type messageReadWriter struct {
*messageReader
*messageWriter
}