feat: add fiels
This commit is contained in:
92
udp_laminar/simple_udp.rs
Normal file
92
udp_laminar/simple_udp.rs
Normal file
@@ -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::<DataType>(pkt.payload()).unwrap()]
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
enum DataType {
|
||||||
|
Coords {
|
||||||
|
longitude: f32,
|
||||||
|
latitude: f32,
|
||||||
|
altitude: f32,
|
||||||
|
},
|
||||||
|
Text {
|
||||||
|
string: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
76
udp_laminar/udp.rs
Normal file
76
udp_laminar/udp.rs
Normal file
@@ -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() {}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "udp_laminar_client"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
|
||||||
edition = "2018"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
laminar = "0.3"
|
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user