Skip to content

Commit

Permalink
Correct minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
umgefahren committed Nov 20, 2023
1 parent 60b4ccc commit 71487fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
36 changes: 16 additions & 20 deletions protocols/autonatv2/src/client/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ where
&mut self,
_cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, <Handler as ConnectionHandler>::FromBehaviour>> {
if let Some(event) = self.pending_events.pop_front() {
return Poll::Ready(event);
let pending_event = self.poll_pending_events();
if pending_event.is_ready() {
return pending_event;
}
self.pending_req_for_peer.retain(|_, reqs| !reqs.is_empty());
for (peer, dial_requests) in &mut self.pending_req_for_peer {
Expand All @@ -210,8 +211,8 @@ where
self.pending_requests.push_front(dial_request);
} else {
let peer = self.known_servers.choose(&mut self.rng).unwrap();

self.submit_req_for_peer(*peer, dial_request);
return self.poll_pending_events();
}
}
Poll::Pending
Expand Down Expand Up @@ -253,28 +254,23 @@ where
.unwrap_or_default()
{
if let Some(new_peer) = self.known_servers.choose(&mut self.rng) {
if let Some(conn_id) = self.peers_to_handlers.get(new_peer) {
self.pending_events.push_back(ToSwarm::NotifyHandler {
peer_id: *new_peer,
handler: NotifyHandler::One(*conn_id),
event: Either::Left(RequestFromBehaviour::PerformRequest(dial_request)),
})
} else {
self.pending_events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(*new_peer)
.condition(PeerCondition::DisconnectedAndNotDialing)
.build(),
});
self.pending_req_for_peer
.entry(*new_peer)
.or_default()
.push_back(dial_request);
}
self.submit_req_for_peer(*new_peer, dial_request);
} else {
self.pending_requests.push_front(dial_request);
}
}
}

fn poll_pending_events(
&mut self,
) -> Poll<
ToSwarm<<Self as NetworkBehaviour>::ToSwarm, <Handler as ConnectionHandler>::FromBehaviour>,
> {
if let Some(event) = self.pending_events.pop_front() {
return Poll::Ready(event);
}
Poll::Pending
}
}

fn addr_is_local(addr: &Multiaddr) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions protocols/autonatv2/src/client/handler/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) enum Error {
#[error("server chose not to dial any provided address")]
ServerChoseNotToDialAnyAddress,
#[error("server ran into an internal error")]
InternalServerError,
InternalServer,
#[error("server did not respond correctly to dial request")]
InvalidResponse,
#[error("server was unable to connect to address: {addr:?}")]
Expand Down Expand Up @@ -253,7 +253,7 @@ fn test_end_from_dial_response(
match (resp.status, resp.dial_status) {
(ResponseStatus::E_REQUEST_REJECTED, _) => Err(Error::ServerRejectedDialRequest),
(ResponseStatus::E_DIAL_REFUSED, _) => Err(Error::ServerChoseNotToDialAnyAddress),
(ResponseStatus::E_INTERNAL_ERROR, _) => Err(Error::InternalServerError),
(ResponseStatus::E_INTERNAL_ERROR, _) => Err(Error::InternalServer),
(ResponseStatus::OK, DialStatus::UNUSED) => Err(Error::InvalidResponse),
(ResponseStatus::OK, DialStatus::E_DIAL_ERROR) => {
Err(Error::UnableToConnectOnSelectedAddress {
Expand Down

0 comments on commit 71487fc

Please sign in to comment.