Struct transit::udp::Transit [] [src]

pub struct Transit {
    // some fields omitted
}

Methods

impl Transit

Sends and receives types over UDP, removing any knowledge of buffers and dealing with the std library.

This use the bincode crate to serialize objects. Does not currently support securely sending packets over the network or ensuring that only packets of the correct type are serialized.

Examples

use std::io;
use transit::udp::*;

let mut transit = Transit::new("localhost:65000").unwrap();
let mut transit2 = Transit::new("localhost:65001").unwrap();
let test = String::from("hello, rust");

let res = transit.send_to(&test, "localhost:65001");
assert!(res.is_ok());
let res: Result<(String, _), TransitError> = transit2.recv_from();
assert!(res.is_ok());
let (data, _addr) = res.unwrap();
assert_eq!(data, "hello, rust");

fn new<A>(addr: A) -> Result<Transit, TransitError> where A: ToSocketAddrs

fn recv_from<T>(&mut self) -> Result<(T, SocketAddr), TransitError> where T: Deserialize

On success, this function returns the type deserialized using the Deserialize trait implementation. It is not defined what happens when Transit trys to deserialize a different type into another currently.

fn send_to<T, A>(&mut self, pkt: &T, addr: A) -> Result<(), TransitError> where T: Serialize, A: ToSocketAddrs

Transforms the packet into a byte array and sends it to the associated address.

fn local_addr(&self) -> Result<SocketAddr, TransitError>