Skip to content

Commit

Permalink
merge(v3): 0.1.0 merged.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bv committed Aug 21, 2023
2 parents 25f8eeb + af5dfba commit 0f4014b
Show file tree
Hide file tree
Showing 74 changed files with 6,090 additions and 3,021 deletions.
Binary file added .github/resources/img/Connection Handshake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions .github/resources/mddoc/Raknet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## RakNet

RakNet doesn't have a lot of resources on it's internal functionality, and to better understand it I've written these docs.

RakNet is composed and derived of multiple parts, so for simplicity I've narrowed it down into the following parts:

- [Protocol](./protocol/README.md)

- [Connection Handshake](./protocol/handshake.md)

- [Datagrams](./protocol/datagrams)

- [Packet Reliability](./reliability.md)

- [Congestion Control](./congestion.md)

- [Plugins]()

- [Server]()

- [Client]()

It's important to note that this documentation does conceptualize and take a different approach from the original RakNet implementation, however it does not deviate from the behavior of RakNet; rather providing a basis on how I believe it should be implemented.
1 change: 1 addition & 0 deletions .github/resources/mddoc/protocol/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# RakNet Protocol
9 changes: 9 additions & 0 deletions .github/resources/mddoc/reliability.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Reliability

Reliability within RakNet specifies when a packet should arrive and how it should be treated. The term "Reliability" within RakNet generally refers to the following possible states of a packet:

- **Unreliable**. The packet sent is not reliable and not important. Later packets will make up for it's loss. This is the same as a regular UDP packet with the added benefit that duplicates are discarded.

- **UnreliableSequenced**. This packet is not necessarily important, if it arrives later than another packet, you should discard it. Otherwise you can treat it normally. *This reliability is generally not used*

- **Reliable**. The packet should be acknow
22 changes: 0 additions & 22 deletions .github/workflows/devskim-analysis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.DS_Store
target

# Ignore lock files
Cargo.lock

*.old/
*.profraw
11 changes: 0 additions & 11 deletions .vscode/settings.json

This file was deleted.

18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
[package]
name = "rakrs"
version = "0.3.0-rc.2"
name = "rak-rs"
version = "0.1.0"
authors = ["Bavfalcon9 <olybear9@gmail.com>"]
edition = "2021"

[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "./resources/header.html"]

[features]
default = [ "async_tokio" ]
default = [ "async_std" ]
# default = ["async_tokio" ]
mcpe = []
debug = []
debug_all = []
async_std = [ "async-std" ]
async_tokio = [ "tokio" ]

[dependencies]
rand = "0.8.3"
binary_utils = { git = "https://github.com/NetrexMC/BinaryUtil", tag = "v0.2.2" }
netrex_events = { git = "https://github.com/NetrexMC/Events", branch = "master" }
tokio = { version = "1.15.0", features = ["full"], optional = true }
binary-util = "0.3.4"
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 }
async-std = { version = "1.12.0", optional = true, features = [ "unstable" ] }
352 changes: 178 additions & 174 deletions LICENSE.md

Large diffs are not rendered by default.

101 changes: 99 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,100 @@
# RakNet
# rak-rs

A fully functional RakNet implementation in rust, asynchronously driven.
A fully functional RakNet implementation in pure rust, asynchronously driven.

## Getting Started

RakNet (rak-rs) is available on [crates.io](), to use it, add the following to your `Cargo.toml`:

```toml
[dependencies]
rak-rs = "0.1.0"
```

## Features

This RakNet implementation comes with 3 primary features, `async_std`, `async_tokio` and `mcpe`. However, by default, only `async_std` is enabled, and `mcpe` requires you to modify your `Cargo.toml`.

If you wish to use these features, add them to your `Cargo.toml` as seen below:

```toml
[dependencies]
rak-rs = { version = "0.1.0", default-features = false, features = [ "async_tokio", "mcpe" ] }
```



rak-rs also provides the following modules:

- [`rak_rs::client`](https://docs.rs/rak-rs/latest/rak-rs/client) - A client implementation of RakNet, allowing you to connect to a RakNet server.
- [`rak_rs::connection`](https://docs.rs/rak-rs/latest/rak-rs/client) - A bare-bones implementation of a Raknet peer, this is mainly used for types.
- [`rak_rs::error`](https://docs.rs/rak-rs/latest/rak-rs/error) - A module with errors that both the Client and Server can respond with.
- [`rak_rs::protocol`](https://docs.rs/rak-rs/latest/rak-rs/protocol) - A lower level implementation of RakNet, responsible for encoding and decoding packets.
- [`rak_rs::server`](https://docs.rs/rak-rs/latest/rak-rs/server) - The base server implementation of RakNet.
- [`rak_rs::util`](https://docs.rs/rak-rs/latest/rak-rs/utils) - General utilities used within `rak-rs`.

# Client

The `client` module provides a way for you to interact with RakNet servers with code.

**Example:**

```rust
use rak_rs::client::{Client, DEFAULT_MTU};
use std::net::ToSocketAddrs;

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

client.connect(addr.next().unwrap()).await.unwrap();

// receive packets
loop {
let packet = client.recv().await.unwrap();

println!("Received a packet! {:?}", packet);

client.send_ord(vec![254, 0, 1, 1], Some(1));
}
}

```

# Server

A RakNet server implementation in pure rust.

**Example:**

```rust
use rakrs::connection::Connection;
use rakrs::Listener;
use rakrs::

#[async_std::main]
async fn main() {
let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();
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!("Got a connection packet {:?} ", pk);
}
}
}
```
3 changes: 3 additions & 0 deletions coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os
os.system("CARGO_INCREMENTAL=0 RUSTFLAGS='-Cinstrument-coverage' LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw' cargo test")
os.system("gcov . --binary-path ./target/debug/deps -s . -t html --branch --ignore-not-existing --ignore ../* --ignore /* --ignore xtask/* --ignore */src/tests/* -o coverage/html")
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-util = "0.3.0"
rak-rs = { path = "../../../", features = [ "debug", "debug_all", "async-std" ]}
19 changes: 19 additions & 0 deletions examples/async-std/client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rak_rs::client::{Client, DEFAULT_MTU};
use std::{net::ToSocketAddrs, vec};

#[async_std::main]
async fn main() {
let mut client = Client::new(10, DEFAULT_MTU);
let mut addr = "zeqa.net:19132".to_socket_addrs().unwrap();
if let Err(_) = client.connect(addr.next().unwrap()).await {
// here you could attempt to retry, but in this case, we'll just exit
println!("Failed to connect to server!");
return;
}

loop {
let pk = client.recv().await.unwrap();
println!("Received packet: {:?}", pk);
client.send_ord(&vec![ 254, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 ], 1).await.unwrap();
}
}
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"
rak-rs = { path = "../../../", features = [ "debug", "debug_all", "mcpe" ] }
33 changes: 33 additions & 0 deletions examples/async-std/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use rak_rs::connection::Connection;
use rak_rs::mcpe::motd::Gamemode;
use rak_rs::Listener;

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

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().await {
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"
rak-rs = { path = "../../../", features = [ "debug", "debug_all", "mcpe", "async_tokio" ], default-features = false }
tokio = "1.23.0"
36 changes: 36 additions & 0 deletions examples/tokio/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use rak_rs::Listener;
use rak_rs::Motd;
use rak_rs::connection::Connection;
use rak_rs::mcpe;
use rak_rs::mcpe::motd::Gamemode;
use rak_rs::server::event::ServerEvent;
use rak_rs::server::event::ServerEventResponse;


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

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

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

async fn handle(mut conn: Connection) {
loop {
// keeping the connection alive
if conn.is_closed().await {
println!("Connection closed!");
break;
}
if let Ok(pk) = conn.recv().await {
println!("(RAKNET RECIEVE SIDE) Got a connection packet {:?} ", pk);
}
}
}
20 changes: 20 additions & 0 deletions resources/header.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

.warning-2 {
background: rgba(255,240,76,0.34) !important;
padding: 0.75em;
border-left: 2px solid #fce811;
}

.warning-2 code {
background: rgba(211,201,88,0.64) !important;
}

.notice-2 {
background: rgba(76, 240, 255, 0.629) !important;
padding: 0.75em;
border-left: 2px solid #4c96ff;
}

.notice-2 code {
background: rgba(88, 211, 255, 0.64) !important;
}
1 change: 1 addition & 0 deletions resources/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="stylesheet" href="/resources/header.css">
Loading

0 comments on commit 0f4014b

Please sign in to comment.