Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server_client example #5

Merged
merged 1 commit into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "laminar"
version = "0.0.0"
version = "0.1.0"
authors = [
"Lucio Franco <luciofranco14@gmail.com>",
"Fletcher Haynes <fletcher@subnetzero.io>",
"Timon <timonp40@gmail.com>"
"Lucio Franco <luciofranco14@gmail.com>",
"Fletcher Haynes <fletcher@capitalprawn.com>",
"TimonPost <timonpost@hotmail.nl>",
]
description = "A simple semi-reliable UDP/TCP protocol for multiplayer games"
keywords = ["gamedev", "networking", "udp", "amethyst"]
Expand Down
99 changes: 99 additions & 0 deletions examples/server_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Note that the terms "client" and "server" here are purely what we logically associate with them.
//! Technically, they both work the same.
//! Note that in practice you don't want to implement a chat client using UDP.

extern crate laminar;

use std::io::stdin;

use laminar::{
error::Result,
NetworkConfig,
Packet,
UdpSocket,
};

const SERVER: &str = "localhost:12351";

fn server() -> Result<()> {
let mut socket = UdpSocket::bind(SERVER, NetworkConfig::default())?;

println!("Listening for connections to {}", SERVER);

loop {
match socket.recv()? {
Some(packet) => {
let msg = packet.payload();

if msg == b"Bye!" {
break;
}

let msg = String::from_utf8_lossy(msg);
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
let ip = packet.addr().ip();

println!("Received {:?} from {:?}", msg, ip);

socket.send(Packet::new(packet.addr(), "Copy that!".as_bytes().to_vec()))?;
}
None => {}
}
}

Ok(())
}

fn client() -> Result<()> {
let mut socket = UdpSocket::bind("localhost:12352", NetworkConfig::default())?;

let server = SERVER.parse()?;

println!("Type a message and press Enter to send. Send `Bye!` to quit.");

let stdin = stdin();
let mut s_buffer = String::new();

loop {
s_buffer.clear();
stdin.read_line(&mut s_buffer)?;
let line = s_buffer.replace(|x| x == '\n' || x == '\r', "");

socket.send(Packet::new(server, line.clone().into_bytes()))?;

if line == "Bye!" {
break;
}

let back = socket.recv()?;
torkleyy marked this conversation as resolved.
Show resolved Hide resolved

match back {
Some(packet) => {
if packet.addr() == server {
println!("Server sent: {}", String::from_utf8_lossy(packet.payload()));
} else {
println!("Unknown sender.");
}
}
None => println!("Silence.."),
}
}

Ok(())
}

fn main() -> Result<()> {
let stdin = stdin();

println!("Please type in `server` or `client`.");

let mut s = String::new();
stdin.read_line(&mut s)?;

if s.starts_with("s") {
println!("Starting server..");
server()
torkleyy marked this conversation as resolved.
Show resolved Hide resolved
} else {
println!("Starting client..");
client()
torkleyy marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ extern crate log;
#[macro_use]
extern crate failure_derive;

pub mod events;
pub mod error;
pub mod net;
pub mod packet;
pub mod error;
pub mod events;

/// This functions checks how many times a number fits into another number and will round up.
///
Expand Down Expand Up @@ -51,3 +51,5 @@ fn total_fragments_needed(payload_length: u16, fragment_size: u16) -> u16
((payload_length / fragment_size) + remainder)
}

pub use net::{NetworkConfig, UdpSocket};
pub use packet::Packet;