Skip to content

Instantly share code, notes, and snippets.

@RJ
Last active September 11, 2023 16:27
Show Gist options
  • Save RJ/2cadf8cabde953517d737d8918753eef to your computer and use it in GitHub Desktop.
Save RJ/2cadf8cabde953517d737d8918753eef to your computer and use it in GitHub Desktop.
Testing multiple renet clients with varying simulated latency levels

Testing with varying levels of lag per Renet client

I want to run multiple clients locally, each with different amounts of lag.

Achieved by specifying the client's local port number when binding socket, and setting up a pf rule based on local port. (all connecting to renet's default port of 5000)

This is for Mac OS, but something similar would be possible on linux.

#!/bin/bash -ex
#
SERVER_IP="10.0.0.99"
SERVER_PORT="5000"

sudo pfctl -E
# remove rules
pfctl -a ts -F all
# clear up configured pipes
dnctl -q flush
# make pipes for various levels of lag
# two pipes per lag category, for in and out rules.

# 2
dnctl pipe 2 config bw 100Mbit/s delay 10
dnctl pipe 3 config bw 100Mbit/s delay 10
# 5
dnctl pipe 5 config bw 100Mbit/s delay 25
dnctl pipe 6 config bw 100Mbit/s delay 25
# 10
dnctl pipe 10 config bw 100Mbit/s delay 50 plr 0.0005
dnctl pipe 11 config bw 100Mbit/s delay 50 plr 0.0005
# 15
dnctl pipe 15 config bw 100Mbit/s delay 75 plr 0.001
dnctl pipe 16 config bw 100Mbit/s delay 75 plr 0.001
# 20
dnctl pipe 20 config bw 100Mbit/s delay 100 plr 0.005
dnctl pipe 21 config bw 100Mbit/s delay 100 plr 0.005


# add 60,000 to the pipe to get the local port number for the rule

pfctl -f -  << EOF
dummynet out proto udp from any port 60002 to   $SERVER_IP port $SERVER_PORT pipe 2
dummynet in  proto udp from $SERVER_IP port $SERVER_PORT to any port 60002 pipe 3

dummynet out proto udp from any port 60005 to   $SERVER_IP port $SERVER_PORT pipe 5
dummynet in  proto udp from $SERVER_IP port $SERVER_PORT to any port 60005 pipe 6

dummynet out proto udp from any port 60010 to   $SERVER_IP port $SERVER_PORT pipe 10
dummynet in  proto udp from $SERVER_IP port $SERVER_PORT to any port 60010 pipe 11

dummynet out proto udp from any port 60015 to   $SERVER_IP port $SERVER_PORT pipe 15
dummynet in  proto udp from $SERVER_IP port $SERVER_PORT to any port 60015 pipe 16

dummynet out proto udp from any port 60020 to   $SERVER_IP port $SERVER_PORT pipe 20
dummynet in  proto udp from $SERVER_IP port $SERVER_PORT to any port 60020 pipe 21
EOF

Usage

Run game client with --lagpipe 100 to use the 100ms delay pipe.

let local_port = match args.lagpipe {
    0 => 0,
    20 | 50 | 100 | 150 | 200 => 60000 + (args.lagpipe as u16 / 10),
    _ => panic!("Invalid lag pipe"),
};
let socket = UdpSocket::bind(("127.0.0.1", local_port)).unwrap();
// etc..;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment