#transport #networking #gamedev

renet2_steam

steam transport for the renet2 crate

1 unstable release

new 0.1.0 Dec 23, 2024

#1828 in Game dev


Used in bevy_renet2

MIT/Apache

145KB
2.5K SLoC

Renet Steam

Latest version Documentation MIT Apache

Transport layer for the renet2 crate using the steamworks-rs.

Usage

This crate adds SteamServerTransport and SteamClientTransport to replace the default transport layer that comes with renet:

Server

// Setup steam client
let (steam_client, single) = Client::init_app(480).unwrap();
steam_client.networking_utils().init_relay_network_access();

// Create renet server
let connection_config = ConnectionConfig::default();
let mut server: RenetServer = RenetServer::new(connection_config);

// Create steam transport
let access_permission = AccessPermission::Public;
let steam_transport_config = SteamServerConfig {
    max_clients: 10,
    access_permission,
};
let mut steam_transport = SteamServerTransport::new(&steam_client, steam_transport_config).unwrap();

// Your gameplay loop
loop {
    let delta_time = Duration::from_millis(16);

    single.run_callbacks(); // Update steam callbacks
   
    server.update(delta_time);
    steam_transport.update(&mut server);

    // Handle connect/disconnect events
    while let Some(event) = server.get_event() {
        match event {
            ServerEvent::ClientConnected { client_id } => {
                println!("Client {} connected.", client_id)
            }
            ServerEvent::ClientDisconnected { client_id, reason } => {
                println!("Client {} disconnected: {}", client_id, reason);
            }
        }
    }

    // Code for sending/receiving messages can go here
    // Check the examples/demos 

    steam_transport.send_packets(&mut server);
    thread::sleep(delta_time);
}

Client

// Setup steam client
let (steam_client, single) = Client::init_app(480).unwrap();
steam_client.networking_utils().init_relay_network_access();

// Create renet client
let connection_config = ConnectionConfig::default();
let mut client = RenetClient::new(connection_config);

// Create steam transport
let server_steam_id = SteamId::from_raw(0); // Here goes the steam id of the host
let mut steam_transport = SteamClientTransport::new(&steam_client, &server_steam_id).unwrap();

// Your gameplay loop
loop {
    let delta_time = Duration::from_millis(16);

    single.run_callbacks(); // Update steam callbacks
    client.update(delta_time);
    steam_transport.update(&mut client);

    // Code for sending/receiving messages can go here
    // Check the examples/demos 

    steam_transport.send_packets(&mut client).unwrap();
    thread::sleep(delta_time);
}

Example

You can try the steam echo example with (steam needs to be running in the background):

  • server: cargo run --example echo server
  • client: cargo run --example echo client [HOST_STEAM_ID]

The HOST_STEAM_ID is printed in the console when the server starts.

You can also run the echo example using steam lobbies. Only steam users connected to the lobby will be able to connect to the server. In the echo example, they will first try connect to the steam lobby and later to the renet server:

  • server: cargo run --example echo server lobby
  • client: cargo run --example echo client [HOST_STEAM_ID] [LOBBY_ID]

The LOBBY_ID is printed in the console when the server starts.

Bevy

If you are using bevy, you can use the bevy_renet2 crate with the steam feature. That crate has SteamServerPlugin and SteamClientPlugin, with a similar setup.

You can check the Bevy Demo for how to use the default and steam transport switching between them using feature flags.

Dependencies

~18MB
~176K SLoC