Skip to content

Commit

Permalink
feat: Client connections, closing: #39, #40
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bv committed Jun 17, 2023
2 parents 27a2139 + 7189861 commit 87ad1ef
Show file tree
Hide file tree
Showing 23 changed files with 1,343 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ target
Cargo.lock

*.old/
*.profraw

raknet-test/
*.profraw
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rakrs"
version = "0.3.0-rc.4"
version = "0.3.0-rc.5"
authors = ["Bavfalcon9 <olybear9@gmail.com>"]
edition = "2021"

Expand All @@ -15,8 +15,8 @@ async_tokio = [ "tokio" ]
[dependencies]
rand = "0.8.3"
binary_utils = { git = "https://github.com/NetrexMC/BinaryUtil", tag = "v0.2.2" }
tokio = { version = "1.15.0", features = ["full"], optional = true }
tokio = { version = "1.28.2", features = ["full"], optional = true }
byteorder = "1.4.3"
futures = "0.3.19"
futures-executor = "0.3.19"
async-std = { version = "1.10.0", optional = true, features = [ "unstable" ] }
async-std = { version = "1.12.0", optional = true, features = [ "unstable" ] }
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Examples

**async-std:**
- [client](/examples/async-std/client)
- [server](/examples/async-std/server)
11 changes: 11 additions & 0 deletions examples/async-std/client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "client"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { version = "1.12.0", features = [ "unstable", "attributes" ] }
binary_utils = { git = "https://github.com/NetrexMC/BinaryUtil", tag = "v0.2.2" }
rakrs = { path = "../../../", features = [ "debug", "debug_all", "async-std" ]}
14 changes: 14 additions & 0 deletions examples/async-std/client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use rakrs::client::{Client, DEFAULT_MTU};
use std::net::ToSocketAddrs;

#[async_std::main]
async fn main() {
let mut client = Client::new(10, DEFAULT_MTU);
let mut addr = "zeqa.net:19132".to_socket_addrs().unwrap();
client.connect(addr.next().unwrap()).await.unwrap();

loop {
let pk = client.recv().await.unwrap();
println!("Received packet: {:?}", pk);
}
}
2 changes: 2 additions & 0 deletions examples/async-std/server/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]
11 changes: 11 additions & 0 deletions examples/async-std/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "raknet-server-async-std"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { version = "1.12.0", features = [ "attributes" ] }
console-subscriber = "0.1.8"
rakrs = { path = "../../../", features = [ "debug", "debug_all", "mcpe" ] }
31 changes: 31 additions & 0 deletions examples/async-std/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use rakrs::connection::Connection;
use rakrs::mcpe::motd::Gamemode;
use rakrs::Listener;

#[async_std::main]
async fn main() {
// console_subscriber::init();
let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();
server.motd.name = "RakNet Rust!".to_string();
server.motd.gamemode = Gamemode::Survival;

server.start().await.unwrap();

loop {
let conn = server.accept().await;
async_std::task::spawn(handle(conn.unwrap()));
}
}

async fn handle(mut conn: Connection) {
loop {
// keeping the connection alive
if conn.is_closed() {
println!("Connection closed!");
break;
}
if let Ok(pk) = conn.recv().await {
println!("(RAKNET RECIEVE SIDE) Got a connection packet {:?} ", pk);
}
}
}
2 changes: 2 additions & 0 deletions examples/tokio/server/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]
12 changes: 12 additions & 0 deletions examples/tokio/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "raknet-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { version = "1.12.0", features = [ "attributes" ] }
console-subscriber = "0.1.8"
rakrs = { path = "../", features = [ "debug", "debug_all", "mcpe", "async_tokio" ], default-features = false }
tokio = "1.23.0"
69 changes: 69 additions & 0 deletions examples/tokio/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use rakrs::Listener;
use rakrs::Motd;
use rakrs::connection::Connection;
use rakrs::mcpe;
use rakrs::mcpe::motd::Gamemode;
use rakrs::server::event::ServerEvent;
use rakrs::server::event::ServerEventResponse;


#[tokio::main]
async fn main() {
console_subscriber::init();
let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();
// let inner = server.recv_evnt.clone();
server.motd.name = "RakNet Rust!".to_string();
server.motd.gamemode = Gamemode::Survival;

server.start().await.unwrap();

loop {
// let mut recvr = inner.lock().await;
// tokio::select! {
// ev = recvr.recv() => {
// match ev {
// Some((event, shoot)) => {
// match event {
// ServerEvent::RefreshMotdRequest(_addr, mut motd) => {
// // force update the motd!!!
// motd.name = "You are a loser!!!!!!!!!".to_string();
// motd.player_max = 14_903;
// motd.player_count = 0;
// shoot.send(ServerEventResponse::RefreshMotd(motd)).unwrap();
// },
// _ => {
// println!("Got a response!");
// // YOU NEED TO ALWAYS REPLY TO EVENTS
// shoot.send(ServerEventResponse::Acknowledged);
// }
// }
// },
// None => {
// println!("Error!");
// break;
// }
// }
// },
// connection = server.accept() => {
// tokio::task::spawn(handle(connection.unwrap()));
// }

let conn = server.accept().await;
async_std::task::spawn(handle(conn.unwrap()));
// }
}
}

async fn handle(mut conn: Connection) {
loop {
// keeping the connection alive
if conn.is_closed() {
println!("Connection closed!");
break;
}
if let Ok(pk) = conn.recv().await {
println!("(RAKNET RECIEVE SIDE) Got a connection packet {:?} ", pk);
}
// conn.tick().await;
}
}
Loading

0 comments on commit 87ad1ef

Please sign in to comment.