Reputation: 2717
std::old_io::net::udp::UdpSocket has been replaced with std::net::UdpSocket, and fn set_timeout(&mut self, timeout_ms: Option<u64>)
has no equivalent.
Any ideas how to achieve that?
Upvotes: 2
Views: 1338
Reputation: 127771
There is no way to set timeout on any network operations in std::net
right now. There is an RFC on that, so for Rust 1.0 it will definitely be fixed. Stay tuned!
In the meantime you can use AsRawFd
implementation which each socket has and call low-level socket functions on the file descriptor manually. AsRawFd::as_raw_fd()
returns raw file descriptor which represents the underlying socket in the operating system. You can call various low-level socket functions on this file descriptor, like setsockopt
. These functions are available through libc
crate. You can find how it could be done in libstd
sources, for example, here. You would also probably want to use libc
from crates.io - the built-in one is behind a feature gate and hence not available in beta.
In order to set send/read timeout you would need to call setsockopt
with SO_SNDTIMEO
/SO_RCVTIMEO
option, providing a pointer to a timeval
instance. This is essentially how it could be done in C. I could expand on this later (in a few hours, probably) if you want.
Upvotes: 5