Usage

If you're looking to easily send and receive Udp Messages then this crate is perfect for you. It gives you the ability to define your own Net Messages by simply creating a struct and implementing a trait.

Important to note:

If you have any suggestions for this crate, let me know! If something is not clear or confusing, let me know!

Example

```rust use udpnetmsg::{NetMessenger, Datagram}; use std::io::Cursor; use byteorder::{BigEndian,ReadBytesExt, WriteBytesExt}; use udpnetmsg::utilities::{ReadString, WriteString};

[derive(Debug)]

struct UpdatePos { pub id: u32, pub x: f32, pub y: f32, pub z: f32, pub s: String }

impl Datagram for UpdatePos { fn frombuffer(mut buffer: Cursor>)->Box { let id = buffer.readu32::().unwrap(); let x = buffer.readf32::().unwrap(); let y = buffer.readf32::().unwrap(); let z = buffer.readf32::().unwrap(); let s = buffer.readstring().unwrap(); return Box::new(UpdatePos{id,x,y,z,s}) }

fn to_buffer(&self)->Vec<u8> {
    let mut wtr: Vec<u8> = Vec::new();
    wtr.write_u32::<BigEndian>(self.id).unwrap();
    wtr.write_f32::<BigEndian>(self.x).unwrap();
    wtr.write_f32::<BigEndian>(self.y).unwrap();
    wtr.write_f32::<BigEndian>(self.z).unwrap();
    wtr.write_string( self.ip.clone()).unwrap();
    return wtr
}

fn header()->u32 {return 834227670}

} fn main() {

let source_ip = String::from("0.0.0.0:12000");
let dest_ip = String::from("127.0.0.1:12000");
let recv_buffer_size_bytes = 100;
let mut net_msg = NetMessenger::new(
    source_ip,
    dest_ip,
    recv_buffer_size_bytes);

//register the struct so it knows how to read datagram!
net_msg.register(UpdatePos::header(), UpdatePos::from_buffer);

match net_msg.send(Box::from(UpdatePos{id: 16, x: 5.0f32, y:5.0f32, z:5.0f32, s: String::from("Hello How are you?")}), true) {
    Ok(_) => println!("datagram sent!"),
    Err(e) => println!("datagram failed to send because: {}", e)
}

//msg.recv(...) returns Some(datagram) or None
match net_msg.recv(false, true) {
    //Some(Msg: Box<Datagram>)
    Some(msg) => {

        //now downcast to particular struct here
        if let Some(t) = msg.downcast_ref::<UpdatePos>() {
            println!("Received: [id {} at ({},{},{})] with String: {}", t.id, t.x, t.y, t.z, t.s);}
        //else if let Some(t) = msg.downcast_ref::<Another msg type>() {}
    }
    None => {println!("no Datagram received!")}
}

} ```

To do