From d55b768421e3f262561907977255a0a5f1f4a7ba Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 30 Aug 2020 00:52:01 +0800 Subject: [PATCH] feat: add fiels --- udp_laminar/simple_udp.rs | 92 ++++++++++++++++++++++ udp_laminar/udp.rs | 76 ++++++++++++++++++ udp_laminar/udp_laminar_client/Cargo.toml | 11 --- udp_laminar/udp_laminar_client/src/main.rs | 3 - 4 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 udp_laminar/simple_udp.rs create mode 100644 udp_laminar/udp.rs delete mode 100644 udp_laminar/udp_laminar_client/Cargo.toml delete mode 100644 udp_laminar/udp_laminar_client/src/main.rs diff --git a/udp_laminar/simple_udp.rs b/udp_laminar/simple_udp.rs new file mode 100644 index 0000000..eccafc4 --- /dev/null +++ b/udp_laminar/simple_udp.rs @@ -0,0 +1,92 @@ +//! This module provides an simple client, server examples with communication over udp. +//! 1. setting up server to receive data. +//! 2. setting up client to send data. +//! 3. serialize data to send and deserialize when received. +use std::net::SocketAddr; +use std::time::Instant; + +use bincode::{deserialize, serialize}; +use serde_derive::{Deserialize, Serialize}; + +use laminar::{Packet, Socket, SocketEvent}; + +/// The socket address of where the server is located. +const SERVER_ADDR: &'static str = "127.0.0.1:12345"; +// The client address from where the data is sent. +const CLIENT_ADDR: &'static str = "127.0.0.1:12346"; + +fn client_address() -> SocketAddr { + CLIENT_ADDR.parse().unwrap() +} + +fn server_address() -> SocketAddr { + SERVER_ADDR.parse().unwrap() +} + +/// This will run an simple example with client and server communicating. +#[allow(unused_must_use)] +pub fn main() { + let mut server = Socket::bind(server_address()).unwrap(); + + /* setup our `Client` and send some test data. */ + let mut client = Socket::bind(client_address()).unwrap(); + + client.send(Packet::unreliable( + server_address(), + serialize(&DataType::Coords { + latitude: 10.55454, + longitude: 10.555, + altitude: 1.3, + }) + .unwrap(), + )); + + client.send(Packet::unreliable( + server_address(), + serialize(&DataType::Coords { + latitude: 3.344, + longitude: 5.4545, + altitude: 1.33, + }) + .unwrap(), + )); + + client.send(Packet::unreliable( + server_address(), + serialize(&DataType::Text { + string: String::from("Some information"), + }) + .unwrap(), + )); + + // Send the queued send operations + client.manual_poll(Instant::now()); + + // Check for any new packets + server.manual_poll(Instant::now()); + + // ==== results ==== + // Coords { longitude: 10.555, latitude: 10.55454, altitude: 1.3 } + // Coords { longitude: 5.4545, latitude: 3.344, altitude: 1.33 } + // Text { string: "Some information" } + while let Some(pkt) = server.recv() { + match pkt { + SocketEvent::Packet(pkt) => { + println!["{:?}", deserialize::(pkt.payload()).unwrap()] + } + _ => {} + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +enum DataType { + Coords { + longitude: f32, + latitude: f32, + altitude: f32, + }, + Text { + string: String, + }, +} diff --git a/udp_laminar/udp.rs b/udp_laminar/udp.rs new file mode 100644 index 0000000..8d59a9c --- /dev/null +++ b/udp_laminar/udp.rs @@ -0,0 +1,76 @@ +//! This module provides examples for the UDP api. +//! 1. sending data +//! 2. receiving data +//! 3. constructing the packet for sending. +use std::net::SocketAddr; + +use laminar::{Packet, Result, Socket, SocketEvent}; + +/// The socket address of where the server is located. +const SERVER_ADDR: &'static str = "127.0.0.1:12345"; +// The client address from where the data is sent. +const CLIENT_ADDR: &'static str = "127.0.0.1:12346"; + +fn client_address() -> SocketAddr { + CLIENT_ADDR.parse().unwrap() +} + +fn server_address() -> SocketAddr { + SERVER_ADDR.parse().unwrap() +} + +/// This is an example of how to send data to an specific address. +pub fn send_data() -> Result<()> { + // Setup a udp socket and bind it to the client address. + let mut socket = Socket::bind(client_address()).unwrap(); + + let packet = construct_packet(); + + // next send or packet to the endpoint we earlier putted into the packet. + socket.send(packet) +} + +/// This is an example of how to receive data over udp. +pub fn receive_data() { + // setup an udp socket and bind it to the client address. + let mut socket = Socket::bind(server_address()).unwrap(); + + // Next start receiving. + loop { + if let Some(result) = socket.recv() { + match result { + SocketEvent::Packet(packet) => { + let endpoint: SocketAddr = packet.addr(); + let received_data: &[u8] = packet.payload(); + + // you can here deserialize your bytes into the data you have passed it when sending. + + println!( + "Received packet from: {:?} with length {}", + endpoint, + received_data.len() + ); + } + _ => {} + } + break; + } + } +} + +/// This is an example of how to construct a packet. +pub fn construct_packet() -> Packet { + // this is the destination address of the packet. + let destination: SocketAddr = server_address(); + + // lets construct some payload (raw data) for or packet. + let raw_data = "example data".as_bytes(); + + // lets construct or packet by passing in the destination for this packet and the bytes needed to be send.. + let packet: Packet = Packet::reliable_unordered(destination, raw_data.to_owned()); + + packet +} + +// TODO: Use functions in example +fn main() {} diff --git a/udp_laminar/udp_laminar_client/Cargo.toml b/udp_laminar/udp_laminar_client/Cargo.toml deleted file mode 100644 index a044e6d..0000000 --- a/udp_laminar/udp_laminar_client/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "udp_laminar_client" -version = "0.1.0" -authors = ["Hatter Jiang "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -laminar = "0.3" - diff --git a/udp_laminar/udp_laminar_client/src/main.rs b/udp_laminar/udp_laminar_client/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/udp_laminar/udp_laminar_client/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -}