Skip to main content

UdpClient

Struct UdpClient 

Source
pub struct UdpClient { /* private fields */ }
Expand description

Connected UDP client adapter.

Implementations§

Source§

impl UdpClient

Source

pub async fn bind<A, R>(local: A, remote: R) -> Result<Self>

Binds a local UDP socket and connects it to remote.

Source

pub async fn bind_with_config<A, R>( local: A, remote: R, config: EngineConfig, ) -> Result<Self>

Binds a local UDP socket, connects it to remote, and uses config.

Examples found in repository?
examples/frontend.rs (line 20)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}
Source

pub fn from_socket(socket: UdpSocket, config: EngineConfig) -> Self

Creates a client from an already connected UDP socket.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the local socket address.

Examples found in repository?
examples/frontend.rs (line 23)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}
Source

pub fn peer_addr(&self) -> Result<SocketAddr>

Returns the remote socket address.

Examples found in repository?
examples/frontend.rs (line 24)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}
Source

pub fn peer_state(&self) -> PeerState

Returns the endpoint peer state.

Source

pub const fn socket(&self) -> &UdpSocket

Returns a shared reference to the UDP socket.

Source

pub fn socket_mut(&mut self) -> &mut UdpSocket

Returns a mutable reference to the UDP socket.

Source

pub fn into_socket(self) -> UdpSocket

Consumes the adapter and returns the UDP socket.

Source

pub fn connect(&mut self) -> Result<()>

Starts a fresh client session.

Examples found in repository?
examples/frontend.rs (line 72)
71fn reconnect(client: &mut UdpClient) -> msrt_udp::Result<()> {
72    client.connect()?;
73    println!("session started");
74    Ok(())
75}
Source

pub fn disconnect(&mut self)

Drops the active client session.

Examples found in repository?
examples/frontend.rs (line 48)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}
Source

pub fn send(&mut self, message: &[u8]) -> Result<bool>

Queues an application message.

Examples found in repository?
examples/frontend.rs (line 34)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}
Source

pub fn receive_available(&mut self) -> Result<usize>

Receives currently available UDP datagrams and feeds them into MSRT.

Source

pub async fn poll(&mut self) -> Result<UdpClientEvent>

Polls one adapter event and sends pending UDP datagrams.

Source

pub async fn tick(&mut self) -> Result<UdpClientEvent>

Runs receive_available followed by poll.

Examples found in repository?
examples/frontend.rs (line 41)
15async fn main() -> msrt_udp::Result<()> {
16    let server = env::args()
17        .nth(1)
18        .unwrap_or_else(|| DEFAULT_SERVER.to_string());
19
20    let mut client = UdpClient::bind_with_config("127.0.0.1:0", &server, demo_config()).await?;
21    println!(
22        "frontend local={} remote={}",
23        client.local_addr()?,
24        client.peer_addr()?
25    );
26
27    reconnect(&mut client)?;
28    let mut sequence = 0_u64;
29    let mut next_send = Instant::now();
30
31    loop {
32        if next_send <= Instant::now() {
33            let payload = format!("hello udp {sequence}");
34            if client.send(payload.as_bytes())? {
35                println!("queued: {payload}");
36                sequence = sequence.wrapping_add(1);
37            }
38            next_send = Instant::now() + SEND_INTERVAL;
39        }
40
41        match client.tick().await? {
42            UdpClientEvent::Message(message) if message.as_bytes() != [0] => {
43                println!("message: {}", String::from_utf8_lossy(message.as_bytes()));
44            }
45            UdpClientEvent::Message(_) | UdpClientEvent::Idle => {}
46            UdpClientEvent::SendFailed(failed) => {
47                println!("send failed: {failed:?}; reconnecting");
48                client.disconnect();
49                reconnect(&mut client)?;
50                next_send = Instant::now();
51            }
52            UdpClientEvent::TransportUnavailable { kind } => {
53                println!("transport unavailable: {kind:?}; reconnecting");
54                client.disconnect();
55                reconnect(&mut client)?;
56                next_send = Instant::now() + reconnect_delay(kind);
57            }
58        }
59
60        sleep(LOOP_SLEEP).await;
61    }
62}

Trait Implementations§

Source§

impl Debug for UdpClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.