mirror of
https://github.com/zaphar/icmp-socket.git
synced 2025-07-22 19:30:05 -04:00
Better timeout management in the API
This commit is contained in:
parent
e331d94a26
commit
3a73734015
@ -74,11 +74,12 @@ pub fn main() {
|
||||
socket4
|
||||
.send_to(address.parse::<Ipv4Addr>().unwrap(), packet)
|
||||
.unwrap();
|
||||
socket4.set_timeout(Duration::from_secs(1)).unwrap();
|
||||
loop {
|
||||
let (resp, sock_addr) = match socket4.rcv_with_timeout(Duration::from_secs(1)) {
|
||||
let (resp, sock_addr) = match socket4.rcv_from() {
|
||||
Ok(tpl) => tpl,
|
||||
Err(e) => {
|
||||
//eprintln!("{:?}", e);
|
||||
eprintln!("{:?}", e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
@ -72,11 +72,12 @@ pub fn main() {
|
||||
socket6
|
||||
.send_to(address.parse::<Ipv6Addr>().unwrap(), packet)
|
||||
.unwrap();
|
||||
socket6.set_timeout(Duration::from_secs(1)).unwrap();
|
||||
loop {
|
||||
let (resp, sock_addr) = match socket6.rcv_with_timeout(Duration::from_secs(1)) {
|
||||
let (resp, sock_addr) = match socket6.rcv_from() {
|
||||
Ok(tpl) => tpl,
|
||||
Err(e) => {
|
||||
//eprintln!("{:?}", e);
|
||||
eprintln!("{:?}", e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
@ -30,6 +30,8 @@ pub trait IcmpSocket {
|
||||
type AddrType;
|
||||
type PacketType;
|
||||
|
||||
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()>;
|
||||
|
||||
fn set_max_hops(&mut self, hops: u32);
|
||||
|
||||
fn bind<A: Into<Self::AddrType>>(&mut self, addr: A) -> std::io::Result<()>;
|
||||
@ -37,11 +39,6 @@ pub trait IcmpSocket {
|
||||
fn send_to(&mut self, dest: Self::AddrType, packet: Self::PacketType) -> std::io::Result<()>;
|
||||
|
||||
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)>;
|
||||
|
||||
fn rcv_with_timeout(
|
||||
&mut self,
|
||||
timeout: Duration,
|
||||
) -> std::io::Result<(Self::PacketType, SockAddr)>;
|
||||
}
|
||||
|
||||
pub struct Opts {
|
||||
@ -98,13 +95,8 @@ impl IcmpSocket for IcmpSocket4 {
|
||||
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||
}
|
||||
|
||||
fn rcv_with_timeout(
|
||||
&mut self,
|
||||
timeout: Duration,
|
||||
) -> std::io::Result<(Self::PacketType, SockAddr)> {
|
||||
self.inner.set_read_timeout(Some(timeout))?;
|
||||
let (read_count, addr) = self.inner.recv_from(&mut self.buf)?;
|
||||
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> {
|
||||
self.inner.set_read_timeout(Some(timeout))
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,13 +164,8 @@ impl IcmpSocket for IcmpSocket6 {
|
||||
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||
}
|
||||
|
||||
fn rcv_with_timeout(
|
||||
&mut self,
|
||||
timeout: Duration,
|
||||
) -> std::io::Result<(Self::PacketType, SockAddr)> {
|
||||
self.inner.set_read_timeout(Some(timeout))?;
|
||||
let (read_count, addr) = self.inner.recv_from(&mut self.buf)?;
|
||||
Ok((self.buf[0..read_count].try_into()?, addr))
|
||||
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> {
|
||||
self.inner.set_read_timeout(Some(timeout))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user