Skip to content

Commit 71c2976

Browse files
author
0x_antoni
committed
fix wrong udpsocket read write migration
1 parent e9d81bb commit 71c2976

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

crates/network/src/service.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ use serde::{Deserialize, Serialize};
4444
use crate::{
4545
discovery::Discovery,
4646
handshake::BYPASS_CRYPTOGRAPHY,
47-
iolib::{IoContext, IoHandler, IoService, StreamToken, TimerToken},
47+
iolib::{
48+
IoContext, IoHandler, IoService, MapNonBlock, StreamToken, TimerToken,
49+
},
4850
ip_utils::{map_external_address, select_public_address},
4951
node_database::NodeDatabase,
5052
node_table::*,
@@ -1389,12 +1391,13 @@ impl NetworkServiceInner {
13891391
}
13901392

13911393
let mut buf = [0u8; MAX_DATAGRAM_SIZE];
1392-
match udp_socket.recv_from(&mut buf) {
1393-
Ok((len, address)) => self
1394+
match udp_socket.recv_from(&mut buf).map_non_block() {
1395+
Ok(Some((len, address))) => self
13941396
.on_udp_packet(&buf[0..len], address)
13951397
.unwrap_or_else(|e| {
13961398
debug!("Error processing UDP packet: {:?}", e);
13971399
}),
1400+
Ok(_) => {}
13981401
Err(e) => {
13991402
debug!("Error reading UDP socket: {:?}", e);
14001403
}
@@ -1420,11 +1423,18 @@ impl NetworkServiceInner {
14201423
let udp_socket = self.udp_socket.lock();
14211424
let mut udp_channel = self.udp_channel.write();
14221425
while let Some(data) = udp_channel.dequeue_send() {
1423-
match udp_socket.send_to(&data.payload, data.address) {
1424-
Ok(size) if size == data.payload.len() => {}
1425-
Ok(_) => {
1426+
match udp_socket
1427+
.send_to(&data.payload, data.address)
1428+
.map_non_block()
1429+
{
1430+
Ok(Some(size)) if size == data.payload.len() => {}
1431+
Ok(Some(_)) => {
14261432
warn!("UDP sent incomplete datagram");
14271433
}
1434+
Ok(None) => {
1435+
udp_channel.requeue_send(data);
1436+
return;
1437+
}
14281438
Err(e) => {
14291439
debug!(
14301440
"UDP send error: {:?}, address: {:?}",

crates/util/io/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use crate::service_mio::{
3232
};
3333

3434
use mio::{Registry, Token};
35-
use mio_util::NotifyError;
35+
pub use mio_util::{would_block, MapNonBlock, NotifyError};
3636
use std::{cell::Cell, env, error, fmt, io};
3737

3838
thread_local! {

crates/util/io/src/mio_util/io.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Re-export the io::Result / Error types for convenience
2+
pub use std::io::{Error, ErrorKind, Read, Result, Write};
3+
4+
// TODO: Delete this
5+
/// A helper trait to provide the map_non_block function on Results.
6+
pub trait MapNonBlock<T> {
7+
/// Maps a `Result<T>` to a `Result<Option<T>>` by converting
8+
/// operation-would-block errors into `Ok(None)`.
9+
fn map_non_block(self) -> Result<Option<T>>;
10+
}
11+
12+
impl<T> MapNonBlock<T> for Result<T> {
13+
fn map_non_block(self) -> Result<Option<T>> {
14+
match self {
15+
Ok(value) => Ok(Some(value)),
16+
Err(err) => {
17+
if let ErrorKind::WouldBlock = err.kind() {
18+
Ok(None)
19+
} else {
20+
Err(err)
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
/// Returns a std `WouldBlock` error without allocating
28+
pub fn would_block() -> Error { ErrorKind::WouldBlock.into() }

crates/util/io/src/mio_util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![allow(unused)]
22
mod event_loop;
33
mod handler;
4+
mod io;
45
mod notify_error;
56

67
pub use event_loop::{EventLoop, EventLoopBuilder, Sender};
78
pub use handler::Handler;
9+
pub use io::{would_block, MapNonBlock};
810
pub use notify_error::NotifyError;

0 commit comments

Comments
 (0)