Skip to content
forked from lucaspoffo/renet

Server/Client network library for multiplayer games with authentication and connection management made with Rust

License

Notifications You must be signed in to change notification settings

OleStrohm/renet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0e2dcac · Aug 30, 2022
Jun 4, 2022
Aug 28, 2022
Aug 30, 2022
Aug 30, 2022
Aug 9, 2022
Aug 9, 2022
Aug 30, 2022
Aug 3, 2022
Jul 7, 2021
Aug 1, 2022
Aug 3, 2022
Jul 30, 2021
Jul 30, 2021
Jul 25, 2022
May 12, 2022
Apr 13, 2022

Repository files navigation

Renet

Latest version Documentation MIT Apache

Renet is a network library for Server/Client games written in rust. Built on top of UDP, it is focused on fast-paced games such as FPS, and competitive games that need authentication. Provides the following features:

  • Client/Server connection management
  • Authentication and encryption, checkout renetcode
  • Multiple types of channels:
    • Reliable Ordered: garantee ordering and delivery of all messages
    • Unreliable Unordered: messages that don't require any garantee of delivery or ordering
    • Block Reliable: for bigger messages, such as level initialization
  • Packet fragmention and reassembly

Sections:

Usage

Renet aims to have a simple API that is easy to integrate with any code base. Pool for new messages at the start of a frame with update, messages sent during a frame - or that need to be resent - are aggregated and sent together with sent_packets.

Server

let delta_time = Duration::from_millis(16);
let mut server = RenetServer::new(...);
let channel_id = 0;

// Your gameplay loop
loop {
    // Receive new messages and update clients
    server.update(delta_time)?;
    
    // Check for client connections/disconnections
    while let Some(event) = server.get_event() {
        match event {
            ServerEvent::ClientConnected(id, user_data) => {
                println!("Client {} connected", id);
            }
            ServerEvent::ClientDisconnected(id) => {
                println!("Client {} disconnected", id);
            }
        }
    }

    // Receive message from channel
    for client_id in server.clients_id().into_iter() {
        while let Some(message) = server.receive_message(client_id, channel_id) {
            // Handle received message
        }
    }
    
    // Send a text message for all clients
    server.broadcast_message(channel_id, "server message".as_bytes().to_vec());
    
    // Send message to only one client
    let client_id = ...;
    server.send_message(client_id, channel_id, "server message".as_bytes().to_vec());
 
    // Send packets to clients
    server.send_packets()?;
}

Client

let delta_time = Duration::from_millis(16);
let mut client = RenetClient::new(...);
let channel_id = 0;

// Your gameplay loop
loop {
    // Receive new messages and update client
    client.update(delta_time)?;
    
    if client.is_connected() {
        // Receive message from server
        while let Some(message) = client.receive_message(channel_id) {
            // Handle received message
        }
        
        // Send message
        client.send_message(channel_id, "client text".as_bytes().to_vec());
    }
 
    // Send packets to server
    client.send_packets()?;
}

Demos

You can checkout the echo example for a simple usage of the library. Or you can look into the two demos that have more complex uses of renet:

Bevy Demo
Simple bevy application to demonstrate how you could replicate entities and send reliable messages as commands from the server/client using renet:

Bevy.Demo.webm

Repository

Chat Demo
Simple chat application made with egui to demonstrate how you could handle errors, states transitions and client self hosting:

Chat.Demo.webm

Repository

Plugins

Checkout bevy_renet if you want to use renet as a plugin with the Bevy engine.

Visualizer

Checkout renet_visualizer for a egui plugin to plot metrics data from renet clients and servers:

renet_visualizer.mp4

About

Server/Client network library for multiplayer games with authentication and connection management made with Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%