Skip to content

Commit

Permalink
Merge pull request meqif#15 from vinipsmaker/ignore-udp-errors2
Browse files Browse the repository at this point in the history
ignore expected errors on Windows
  • Loading branch information
Peter Jankuliak committed Jan 14, 2016
2 parents 91f8603 + e51a9b4 commit 99ba1a3
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/socket.rs
Expand Up @@ -534,6 +534,44 @@ impl UtpSocket {
}
}

#[cfg(windows)]
fn ignore_udp_error(e: &Error) -> bool {
// On Windows, the recv_from operation on the UDP socket may return the
// following errors, which are expected and should be ignored:
//
// - 10054 (WSAECONNRESET): Windows can send this error if a previous
// send operation resulted in an ICMP Port Unreachable. And if it's a
// loopback interface, it can know whether there is already another
// end to communicate.
// - 10040 (WSAEMSGSIZE): This error was randomly appearing in a test
// that I conducted. Not really sure why it's happening. The frequency
// decreased when I increased the receive buffer size, but it was not
// important to get a network up and running.
//
// Without these changes, it was impossible to get a relatively large
// network running without issues. By large I mean a test that might be
// too bursting for a single machine to run.
//
// More references:
//
// - http://stackoverflow.com/questions/30749423/is-winsock-error-10054-wsaeconnreset-normal-with-udp-to-from-localhost#comment49588739_30749423
// - https://github.com/maidsafe/crust/pull/454
const WSAECONNRESET: i32 = 10054;
const WSAEMSGSIZE: i32 = 10040;
match e.raw_os_error() {
Some(e) => match e {
WSAECONNRESET | WSAEMSGSIZE => true,
_ => false,
},
None => false,
}
}

#[cfg(not(windows))]
fn ignore_udp_error(_: &Error) -> bool {
false
}

fn recv(&mut self, buf: &mut [u8], use_user_timeout: bool) -> Result<(usize, SocketAddr)> {
let mut b = [0; BUF_SIZE + HEADER_SIZE];
let now = SteadyTime::now();
Expand Down Expand Up @@ -609,6 +647,7 @@ impl UtpSocket {
self.retries += 1;
}
}
Err(ref e) if Self::ignore_udp_error(e) => (),
Err(e) => return Err(e),
};

Expand Down

0 comments on commit 99ba1a3

Please sign in to comment.