Add UdpSocket::is_open, similar to TcpSocket::is_open in function.

Fixes #31.
This commit is contained in:
whitequark 2017-07-30 01:17:48 +00:00
parent be0854127a
commit ae903e8841
4 changed files with 16 additions and 7 deletions

View File

@ -208,7 +208,8 @@ It responds to:
* TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`),
which will be ignored.
The buffers are only 64 bytes long, for convenience of testing resource exhaustion conditions.
Except for the socket on port 6971. the buffers are only 64 bytes long, for convenience
of testing resource exhaustion conditions.
### examples/client.rs

View File

@ -63,7 +63,7 @@ fn main() {
// udp:6969: respond "yo dawg"
{
let socket: &mut UdpSocket = sockets.get_mut(udp_handle).as_socket();
if !socket.endpoint().is_specified() {
if !socket.is_open() {
socket.bind(6969).unwrap()
}

View File

@ -348,8 +348,9 @@ impl<'a> TcpSocket<'a> {
/// Start listening on the given endpoint.
///
/// This function returns an error if the socket was open; see [is_open](#method.is_open).
/// It also returns an error if the specified port is zero.
/// This function returns `Err(Error::Illegal)` if the socket was already open
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
/// if the port in the given endpoint is zero.
pub fn listen<T>(&mut self, local_endpoint: T) -> Result<()>
where T: Into<IpEndpoint> {
let local_endpoint = local_endpoint.into();

View File

@ -101,18 +101,25 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
/// Bind the socket to the given endpoint.
///
/// Returns `Err(Error::Illegal)` if the socket is already bound,
/// and `Err(Error::Unaddressable)` if the port is unspecified.
/// This function returns `Err(Error::Illegal)` if the socket was open
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
/// if the port in the given endpoint is zero.
pub fn bind<T: Into<IpEndpoint>>(&mut self, endpoint: T) -> Result<()> {
let endpoint = endpoint.into();
if endpoint.port == 0 { return Err(Error::Unaddressable) }
if self.endpoint.port != 0 { return Err(Error::Illegal) }
if self.is_open() { return Err(Error::Illegal) }
self.endpoint = endpoint;
Ok(())
}
/// Check whether the socket is open.
#[inline]
pub fn is_open(&self) -> bool {
self.endpoint.port != 0
}
/// Check whether the transmit buffer is full.
#[inline]
pub fn can_send(&self) -> bool {