File tree 1 file changed +28
-14
lines changed
1 file changed +28
-14
lines changed Original file line number Diff line number Diff line change @@ -83,17 +83,21 @@ type impl struct {
83
83
}
84
84
85
85
type streamMessageSender struct {
86
- to peer.ID
87
- stream network.Stream
88
- connected bool
89
- bsnet * impl
90
- opts * MessageSenderOpts
86
+ to peer.ID
87
+ stream network.Stream
88
+ bsnet * impl
89
+ opts * MessageSenderOpts
90
+ }
91
+
92
+ type HasContext interface {
93
+ Context () context.Context
91
94
}
92
95
93
96
// Open a stream to the remote peer
94
97
func (s * streamMessageSender ) Connect (ctx context.Context ) (network.Stream , error ) {
95
- if s .connected {
96
- return s .stream , nil
98
+ stream := s .stream
99
+ if stream != nil {
100
+ return stream , nil
97
101
}
98
102
99
103
tctx , cancel := context .WithTimeout (ctx , s .opts .SendTimeout )
@@ -109,28 +113,38 @@ func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, erro
109
113
}
110
114
111
115
s .stream = stream
112
- s .connected = true
113
- return s .stream , nil
116
+ return stream , nil
114
117
}
115
118
116
119
// Reset the stream
117
120
func (s * streamMessageSender ) Reset () error {
118
- if s .stream != nil {
119
- err := s .stream .Reset ()
120
- s .connected = false
121
+ stream := s .stream
122
+ if stream != nil {
123
+ err := stream .Reset ()
124
+ s .stream = nil
121
125
return err
122
126
}
123
127
return nil
124
128
}
125
129
126
130
// Close the stream
127
131
func (s * streamMessageSender ) Close () error {
128
- return s .stream .Close ()
132
+ stream := s .stream
133
+ if stream != nil {
134
+ err := stream .Close ()
135
+ s .stream = nil
136
+ return err
137
+ }
138
+ return nil
129
139
}
130
140
131
141
// Indicates whether the peer supports HAVE / DONT_HAVE messages
132
142
func (s * streamMessageSender ) SupportsHave () bool {
133
- return s .bsnet .SupportsHave (s .stream .Protocol ())
143
+ stream := s .stream
144
+ if stream == nil {
145
+ return false
146
+ }
147
+ return s .bsnet .SupportsHave (stream .Protocol ())
134
148
}
135
149
136
150
// Send a message to the peer, attempting multiple times
You can’t perform that action at this time.
0 commit comments