pub struct UdpClient { /* private fields */ }Expand description
Connected UDP client adapter.
Implementations§
Source§impl UdpClient
impl UdpClient
Sourcepub async fn bind<A, R>(local: A, remote: R) -> Result<Self>where
A: ToSocketAddrs,
R: ToSocketAddrs,
pub async fn bind<A, R>(local: A, remote: R) -> Result<Self>where
A: ToSocketAddrs,
R: ToSocketAddrs,
Binds a local UDP socket and connects it to remote.
Sourcepub async fn bind_with_config<A, R>(
local: A,
remote: R,
config: EngineConfig,
) -> Result<Self>where
A: ToSocketAddrs,
R: ToSocketAddrs,
pub async fn bind_with_config<A, R>(
local: A,
remote: R,
config: EngineConfig,
) -> Result<Self>where
A: ToSocketAddrs,
R: ToSocketAddrs,
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}Sourcepub fn from_socket(socket: UdpSocket, config: EngineConfig) -> Self
pub fn from_socket(socket: UdpSocket, config: EngineConfig) -> Self
Creates a client from an already connected UDP socket.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
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}Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
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}Sourcepub fn peer_state(&self) -> PeerState
pub fn peer_state(&self) -> PeerState
Returns the endpoint peer state.
Sourcepub fn socket_mut(&mut self) -> &mut UdpSocket
pub fn socket_mut(&mut self) -> &mut UdpSocket
Returns a mutable reference to the UDP socket.
Sourcepub fn into_socket(self) -> UdpSocket
pub fn into_socket(self) -> UdpSocket
Consumes the adapter and returns the UDP socket.
Sourcepub fn disconnect(&mut self)
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}Sourcepub fn send(&mut self, message: &[u8]) -> Result<bool>
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}Sourcepub fn receive_available(&mut self) -> Result<usize>
pub fn receive_available(&mut self) -> Result<usize>
Receives currently available UDP datagrams and feeds them into MSRT.
Sourcepub async fn poll(&mut self) -> Result<UdpClientEvent>
pub async fn poll(&mut self) -> Result<UdpClientEvent>
Polls one adapter event and sends pending UDP datagrams.
Sourcepub async fn tick(&mut self) -> Result<UdpClientEvent>
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§
Auto Trait Implementations§
impl !Freeze for UdpClient
impl RefUnwindSafe for UdpClient
impl Send for UdpClient
impl Sync for UdpClient
impl Unpin for UdpClient
impl UnsafeUnpin for UdpClient
impl UnwindSafe for UdpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more