Struct glommio::net::UdpSocket[][src]

pub struct UdpSocket { /* fields omitted */ }
Expand description

An Udp Socket.

Implementations

Creates a UDP socket bound to the specified address.

Binding with port number 0 will request an available port from the OS.

This sets the ReusePort option on the socket, so if the OS-provided load balancing is enough, it is possible to just bind to the same address from multiple executors.

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let listener = UdpSocket::bind("127.0.0.1:8000").unwrap();
    println!("Listening on {}", listener.local_addr().unwrap());
});

Connects this UDP socket to a remote address, allowing the send and recv methods to be used to send data and also applies filters to only receive data from the specified address.

If addr yields multiple addresses, connect will be attempted with each of the addresses until the underlying OS function returns no error. Note that usually, a successful connect call does not specify that there is a remote server listening on the port, rather, such an error would only be detected after the first send. If the OS returns an error for each of the specified addresses, the error returned from the last connection attempt (the last address) is returned.

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    receiver
        .connect(sender.local_addr().unwrap())
        .await
        .unwrap();
    sender
        .connect(receiver.local_addr().unwrap())
        .await
        .unwrap();
});

Sets the buffer size used on the receive path

gets the buffer size used

Sets the read timeout to the timeout specified.

If the value specified is None, then read calls will block indefinitely. An Err is returned if the zero Duration is passed to this method.

Examples

let s = UdpSocket::bind("127.0.0.1:10000").unwrap();
s.set_read_timeout(Some(Duration::from_secs(1))).unwrap();

Sets the write timeout to the timeout specified.

If the value specified is None, then write calls will block indefinitely. An Err is returned if the zero Duration is passed to this method.

let s = UdpSocket::bind("127.0.0.1:10000").unwrap();
s.set_write_timeout(Some(Duration::from_secs(1))).unwrap();

Returns the read timeout of this socket.

Returns the write timeout of this socket.

Receives single datagram on the socket from the remote address to which it is connected, without removing the message from input queue. On success, returns the number of bytes peeked.

The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

To use this function, connect must have been called

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    receiver
        .connect(sender.local_addr().unwrap())
        .await
        .unwrap();
    sender
        .send_to(&[1; 1], receiver.local_addr().unwrap())
        .await
        .unwrap();
    let mut buf = vec![0; 32];
    let sz = receiver.peek(&mut buf).await.unwrap();
    assert_eq!(sz, 1);
})

Receives a single datagram message on the socket, without removing it from the queue. On success, returns the number of bytes read and the origin.

The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

Returns the socket address of the remote peer this socket was connected to.

Returns the socket address of the local half of this UDP connection.

Receives a single datagram message on the socket from the remote address to which it is connected.

On success, returns the number of bytes read. The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

To use this function, connect must have been called

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    receiver
        .connect(sender.local_addr().unwrap())
        .await
        .unwrap();
    sender
        .send_to(&[1; 1], receiver.local_addr().unwrap())
        .await
        .unwrap();
    let mut buf = vec![0; 32];
    let sz = receiver.recv(&mut buf).await.unwrap();
    assert_eq!(sz, 1);
})

Receives a single datagram message on the socket. On success, returns the number of bytes read and the origin.

The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    sender
        .send_to(&[1; 1], receiver.local_addr().unwrap())
        .await
        .unwrap();
    let mut buf = vec![0; 32];
    let (sz, addr) = receiver.recv_from(&mut buf).await.unwrap();
    assert_eq!(sz, 1);
    assert_eq!(addr, sender.local_addr().unwrap());
})

Sends data on the socket to the given address. On success, returns the number of bytes written. Address type can be any implementor of ToSocketAddrs trait. See its documentation for concrete examples. It is possible for addr to yield multiple addresses, but send_to will only send data to the first address yielded by addr.

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    sender
        .send_to(&[1; 1], receiver.local_addr().unwrap())
        .await
        .unwrap();
    let mut buf = vec![0; 32];
    let (sz, addr) = receiver.recv_from(&mut buf).await.unwrap();
    assert_eq!(sz, 1);
    assert_eq!(addr, sender.local_addr().unwrap());
})

Sends data on the socket to the remote address to which it is connected.

UdpSocket::connect will connect this socket to a remote address. This method will fail if the socket is not connected.

Examples

use glommio::{net::UdpSocket, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
    let sender = UdpSocket::bind("127.0.0.1:0").unwrap();
    sender
        .connect(receiver.local_addr().unwrap())
        .await
        .unwrap();
    sender.send(&[1; 1]).await.unwrap();
})

[UdpSocket::connect]: UdpSocket::connect

Trait Implementations

Extracts the raw file descriptor. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Constructs a new instance of Self from the given raw file descriptor. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more