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: PacketTypePacket type that carried the message.
message_id: MessageIdMessage identifier scoped to this engine.
bytes: [u8; 256]Fixed storage containing complete message bytes.
len: usizeNumber of valid message bytes in bytes.
Implementations§
Source§impl MessageEvent
impl MessageEvent
Sourcepub const fn as_bytes(&self) -> &[u8] ⓘ
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
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
impl Clone for MessageEvent
Source§fn clone(&self) -> MessageEvent
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for MessageEvent
Source§impl Debug for MessageEvent
impl Debug for MessageEvent
impl Eq for MessageEvent
Source§impl PartialEq for MessageEvent
impl PartialEq for MessageEvent
Source§fn eq(&self, other: &MessageEvent) -> bool
fn eq(&self, other: &MessageEvent) -> bool
Tests for
self and other values to be equal, and is used by ==.impl StructuralPartialEq for MessageEvent
Auto Trait Implementations§
impl Freeze for MessageEvent
impl RefUnwindSafe for MessageEvent
impl Send for MessageEvent
impl Sync for MessageEvent
impl Unpin for MessageEvent
impl UnsafeUnpin for MessageEvent
impl UnwindSafe for MessageEvent
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