forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_stream.go
231 lines (197 loc) · 4.51 KB
/
mock_stream.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
package mocknet
import (
"bytes"
"errors"
"io"
"net"
"time"
inet "github.com/libp2p/go-libp2p-net"
protocol "github.com/libp2p/go-libp2p-protocol"
)
// stream implements inet.Stream
type stream struct {
write *io.PipeWriter
read *io.PipeReader
conn *conn
toDeliver chan *transportObject
reset chan struct{}
close chan struct{}
closed chan struct{}
state error
protocol protocol.ID
}
var ErrReset error = errors.New("stream reset")
var ErrClosed error = errors.New("stream closed")
type transportObject struct {
msg []byte
arrivalTime time.Time
}
func NewStream(w *io.PipeWriter, r *io.PipeReader) *stream {
s := &stream{
read: r,
write: w,
reset: make(chan struct{}, 1),
close: make(chan struct{}, 1),
closed: make(chan struct{}),
toDeliver: make(chan *transportObject),
}
go s.transport()
return s
}
// How to handle errors with writes?
func (s *stream) Write(p []byte) (n int, err error) {
l := s.conn.link
delay := l.GetLatency() + l.RateLimit(len(p))
t := time.Now().Add(delay)
select {
case <-s.closed: // bail out if we're closing.
return 0, s.state
case s.toDeliver <- &transportObject{msg: p, arrivalTime: t}:
}
return len(p), nil
}
func (s *stream) Protocol() protocol.ID {
return s.protocol
}
func (s *stream) SetProtocol(proto protocol.ID) {
s.protocol = proto
}
func (s *stream) Close() error {
select {
case s.close <- struct{}{}:
default:
}
<-s.closed
if s.state != ErrClosed {
return s.state
}
return nil
}
func (s *stream) Reset() error {
// Cancel any pending writes.
s.write.Close()
select {
case s.reset <- struct{}{}:
default:
}
<-s.closed
if s.state != ErrReset {
return s.state
}
return nil
}
func (s *stream) teardown() {
s.write.Close()
// at this point, no streams are writing.
s.conn.removeStream(s)
// Mark as closed.
close(s.closed)
s.conn.net.notifyAll(func(n inet.Notifiee) {
n.ClosedStream(s.conn.net, s)
})
}
func (s *stream) Conn() inet.Conn {
return s.conn
}
func (s *stream) SetDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) SetReadDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) SetWriteDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (s *stream) Read(b []byte) (int, error) {
return s.read.Read(b)
}
// transport will grab message arrival times, wait until that time, and
// then write the message out when it is scheduled to arrive
func (s *stream) transport() {
defer s.teardown()
bufsize := 256
buf := new(bytes.Buffer)
timer := time.NewTimer(0)
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
// cleanup
defer timer.Stop()
// writeBuf writes the contents of buf through to the s.Writer.
// done only when arrival time makes sense.
drainBuf := func() {
if buf.Len() > 0 {
_, err := s.write.Write(buf.Bytes())
if err != nil {
return
}
buf.Reset()
}
}
// deliverOrWait is a helper func that processes
// an incoming packet. it waits until the arrival time,
// and then writes things out.
deliverOrWait := func(o *transportObject) {
buffered := len(o.msg) + buf.Len()
// Yes, we can end up extending a timer multiple times if we
// keep on making small writes but that shouldn't be too much of an
// issue. Fixing that would be painful.
if !timer.Stop() {
// FIXME: So, we *shouldn't* need to do this but we hang
// here if we don't... Go bug?
select {
case <-timer.C:
default:
}
}
delay := o.arrivalTime.Sub(time.Now())
if delay >= 0 {
timer.Reset(delay)
} else {
timer.Reset(0)
}
if buffered >= bufsize {
select {
case <-timer.C:
case <-s.reset:
s.reset <- struct{}{}
return
}
drainBuf()
// write this message.
_, err := s.write.Write(o.msg)
if err != nil {
log.Error("mock_stream", err)
}
} else {
buf.Write(o.msg)
}
}
for {
// Reset takes precedent.
select {
case <-s.reset:
s.state = ErrReset
s.read.CloseWithError(ErrReset)
return
default:
}
select {
case <-s.reset:
s.state = ErrReset
s.read.CloseWithError(ErrReset)
return
case <-s.close:
s.state = ErrClosed
drainBuf()
return
case o := <-s.toDeliver:
deliverOrWait(o)
case <-timer.C: // ok, due to write it out.
drainBuf()
}
}
}