Pass the error to inject_listener_closed method (#1517)

* Pass the error to inject_listener_closed method

If there is an error when the listener closes, found in the
`NetworkEvent::ListenerClosed` `reason` field, we would like to pass it
on to the `inject_listener_closed()` method so that implementors of this
method have access to it.

Add an error parameter to `inject_listener_closed`.  Convert the
`reason` field from a `Result` to an `Option` and if there is an error
pass `Some(error)` at the method call site.

* Pass 'reason' as a Result

* Finish change

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
Tobin Harding 2020-04-01 22:49:42 +10:00 committed by GitHub
parent 3f12a5ceaa
commit b1059cd801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View File

@ -271,8 +271,8 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
return None return None
} }
Some(match field.ident { Some(match field.ident {
Some(ref i) => quote!(self.#i.inject_listener_closed(id);), Some(ref i) => quote!(self.#i.inject_listener_closed(id, reason);),
None => quote!(self.#field_n.inject_listener_closed(id);) None => quote!(self.#field_n.inject_listener_closed(id, reason);)
}) })
}) })
}; };
@ -469,7 +469,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
#(#inject_listener_error_stmts);* #(#inject_listener_error_stmts);*
} }
fn inject_listener_closed(&mut self, id: #listener_id) { fn inject_listener_closed(&mut self, id: #listener_id, reason: Result<(), &std::io::Error>) {
#(#inject_listener_closed_stmts);* #(#inject_listener_closed_stmts);*
} }

View File

@ -146,7 +146,7 @@ pub trait NetworkBehaviour: Send + 'static {
} }
/// A listener closed. /// A listener closed.
fn inject_listener_closed(&mut self, _id: ListenerId) { fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), &std::io::Error>) {
} }
/// Polls for things that swarm should do. /// Polls for things that swarm should do.
@ -304,4 +304,3 @@ impl Default for DialPeerCondition {
DialPeerCondition::Disconnected DialPeerCondition::Disconnected
} }
} }

View File

@ -593,7 +593,10 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
for addr in addresses.iter() { for addr in addresses.iter() {
this.behaviour.inject_expired_listen_addr(addr); this.behaviour.inject_expired_listen_addr(addr);
} }
this.behaviour.inject_listener_closed(listener_id); this.behaviour.inject_listener_closed(listener_id, match &reason {
Ok(()) => Ok(()),
Err(err) => Err(err),
});
return Poll::Ready(SwarmEvent::ListenerClosed { return Poll::Ready(SwarmEvent::ListenerClosed {
addresses, addresses,
reason, reason,