-
Notifications
You must be signed in to change notification settings - Fork 5
/
raw-stream-rwc.go
153 lines (132 loc) · 3.05 KB
/
raw-stream-rwc.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
package srpc
import (
"context"
"io"
"sync"
"github.com/aperturerobotics/util/broadcast"
)
// RawStreamRwc implements io.ReadWriteCloser with a raw stream.
type RawStreamRwc struct {
// writer is used to write messages to the remote.
writer Writer
// mtx guards below fields
mtx sync.Mutex
// bcast is broadcasted when data is added to readQueue
bcast broadcast.Broadcast
// readQueue holds incoming data to read
readQueue [][]byte
// closed indicates whether the stream is closed
closed bool
// closeErr stores the error, if any, when closing the stream
closeErr error
}
func NewRawStreamRwc(ctx context.Context, ctorFn RawStreamCtor) (*RawStreamRwc, error) {
rwc := &RawStreamRwc{}
var err error
rwc.writer, err = ctorFn(ctx, rwc.handlePacketData, rwc.handleClose)
if err != nil {
return nil, err
}
return rwc, nil
}
// handlePacketData implements PacketDataHandler.
func (r *RawStreamRwc) handlePacketData(pkt []byte) error {
r.mtx.Lock()
defer r.mtx.Unlock()
if r.closed {
return io.ErrClosedPipe
}
r.readQueue = append(r.readQueue, pkt)
r.bcast.Broadcast()
return nil
}
// handleClose handles the stream closing with an optional error.
func (r *RawStreamRwc) handleClose(closeErr error) {
r.mtx.Lock()
defer r.mtx.Unlock()
if r.closed {
return
}
r.closed = true
r.closeErr = closeErr
r.bcast.Broadcast()
}
// Read reads data from the stream to p.
// Implements io.Reader.
func (r *RawStreamRwc) Read(p []byte) (n int, err error) {
readBuf := p
for len(readBuf) != 0 && err == nil {
// if the buffer has data, read from it.
var rn int
var read []byte
r.mtx.Lock()
if len(r.readQueue) != 0 {
nrq := r.readQueue[0]
rn = len(readBuf)
if nrLen := len(nrq); nrLen < rn {
rn = nrLen
}
read = nrq[:rn]
nrq = nrq[rn:]
if len(nrq) == 0 {
r.readQueue[0] = nil
r.readQueue = r.readQueue[1:]
} else {
r.readQueue[0] = nrq
}
}
closed, closedErr := r.closed, r.closeErr
var wait <-chan struct{}
if rn == 0 && !closed {
wait = r.bcast.GetWaitCh()
}
r.mtx.Unlock()
// if we read data, copy it to the output buf
if rn != 0 {
// advance readBuf by rn
copy(readBuf, read)
n += rn
readBuf = readBuf[rn:]
continue
}
// if we read data to p already, return now.
if n != 0 {
break
}
// if closed or error, return.
if closed {
if closedErr != nil {
return n, closedErr
}
return n, io.EOF
}
// wait for data or closed
<-wait
}
return n, err
}
// Write writes data to the stream.
func (r *RawStreamRwc) Write(p []byte) (int, error) {
return r.writer.Write(p)
}
// WritePacket writes a packet to the remote.
func (r *RawStreamRwc) WritePacket(p *Packet) error {
return r.writer.WritePacket(p)
}
// Close closes the stream.
func (r *RawStreamRwc) Close() error {
r.mtx.Lock()
defer r.mtx.Unlock()
if r.closed {
return r.closeErr
}
r.closed = true
r.closeErr = r.writer.Close()
r.bcast.Broadcast()
return r.closeErr
}
// _ is a type assertion
var (
_ io.ReadWriteCloser = ((*RawStreamRwc)(nil))
_ Writer = ((*RawStreamRwc)(nil))
)