Skip to main content

MessageEvent

Struct MessageEvent 

Source
pub struct MessageEvent {
    pub packet_type: PacketType,
    pub message_id: MessageId,
    pub bytes: [u8; 256],
    pub len: usize,
}
Expand description

A complete message delivered by the engine.

Fields§

§packet_type: PacketType

Packet type that carried the message.

§message_id: MessageId

Message identifier scoped to this engine.

§bytes: [u8; 256]

Fixed storage containing complete message bytes.

§len: usize

Number of valid message bytes in bytes.

Implementations§

Source§

impl MessageEvent

Source

pub const fn as_bytes(&self) -> &[u8]

Returns the valid message bytes.

Examples found in repository?
examples/server.rs (line 25)
14async fn main() -> msrt_udp::Result<()> {
15    let bind = env::args()
16        .nth(1)
17        .unwrap_or_else(|| DEFAULT_BIND.to_string());
18    let mut server = UdpServer::<16>::bind_with_config(&bind, demo_config()).await?;
19    println!("server listening on {}", server.local_addr()?);
20
21    let mut next_idle_sweep = Instant::now() + Duration::from_secs(1);
22
23    loop {
24        match server.tick().await? {
25            UdpServerEvent::Message { peer, message } if message.as_bytes() != [0] => {
26                let text = String::from_utf8_lossy(message.as_bytes());
27                println!("{peer}: {text}");
28                let reply = format!("echo from server: {text}");
29                let _ = server.send_to(peer, reply.as_bytes())?;
30            }
31            UdpServerEvent::Message { .. } | UdpServerEvent::Idle => {}
32            UdpServerEvent::SendFailed { peer, failed } => {
33                println!("{peer}: send failed: {failed:?}; disconnecting peer");
34                server.disconnect(peer);
35            }
36        }
37
38        if next_idle_sweep <= Instant::now() {
39            let disconnected = server.disconnect_idle(IDLE_TIMEOUT.as_millis() as u64);
40            if disconnected > 0 {
41                println!("disconnected {disconnected} idle peer(s)");
42            }
43            next_idle_sweep = Instant::now() + Duration::from_secs(1);
44        }
45
46        sleep(LOOP_SLEEP).await;
47    }
48}
More examples
Hide additional examples
examples/frontend.rs (line 42)
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 Clone for MessageEvent

Source§

fn clone(&self) -> MessageEvent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for MessageEvent

Source§

impl Debug for MessageEvent

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for MessageEvent

Source§

impl PartialEq for MessageEvent

Source§

fn eq(&self, other: &MessageEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for MessageEvent

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.