Replies: 2 comments 1 reply
-
further more, I wanted to understand why the rx is always dropped before the message could be sent from the tx, with the below code snipped: let (mut tx, rx) = tokio::sync::oneshot::channel::<Vec<Ipv4Addr>>();
let mut rx = rx.fuse();
let r = Arc::new(socket);
let s = r.clone();
let xid = msg.xid();
tokio::spawn(async move {
let mut buf = vec![0u8; 576];
let get_response = async move {
loop {
let (n_read, _) = r
.recv_from(&mut buf)
.await
.expect("failed to receive DHCP offer");
// fucking deep if-else hell
if let Ok(reply) = dhcproto::v4::Message::from_bytes(&buf[..n_read]) {
if let Some(op) = reply.opts().get(dhcproto::v4::OptionCode::MessageType) {
match op {
dhcproto::v4::DhcpOption::MessageType(msg_type) => {
if msg_type == &dhcproto::v4::MessageType::Offer {
if reply.xid() == xid {
if let Some(op) = reply
.opts()
.get(dhcproto::v4::OptionCode::DomainNameServer)
{
match op {
dhcproto::v4::DhcpOption::DomainNameServer(dns) => {
debug!("got NS servers {:?} from DHCP", dns);
return dns.clone();
}
_ => yield_now().await,
}
}
}
yield_now().await
}
}
_ => yield_now().await,
}
}
}
}
};
tokio::select! {
_ = tx.closed() => {debug!("rx closed, quiting")},
value = get_response => tx.send(value).expect("must send")
}
});
s.send_to(&msg.to_vec().expect("must encode"), "255.255.255.255:67")
.await?;
debug!("waiting for DHCP reply");
let mut interval = interval(Duration::from_millis(100));
let result = tokio::select! {
result = &mut rx => result.map_err(|x| io::Error::new(io::ErrorKind::Other, "channel error")),
// _ = interval.tick() => { debug!("Another 100ms"); return Err(io::Error::new(io::ErrorKind::Other, "DHCP timeout")) },
_ = tokio::time::sleep(Duration::from_secs(10)) => {
debug!("DHCP timeout after 10 secs");
return Err(io::Error::new(io::ErrorKind::Other, "dhcp timeout"));
}
};
debug!("rx dropped here");
result the the |
Beta Was this translation helpful? Give feedback.
1 reply
-
I'm super late, but the reason you need a |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
https://docs.rs/tokio/latest/tokio/sync/oneshot/index.html
could you elaborate why?
Beta Was this translation helpful? Give feedback.
All reactions