Added simple rate limiting of packets

This commit is contained in:
MysterD 2021-07-30 19:33:09 -07:00
parent c038de1c97
commit 90361b05a8
2 changed files with 57 additions and 2 deletions

33
developer/proto-8.sh Normal file
View File

@ -0,0 +1,33 @@
#!/bin/bash
set -e
if [ $# -eq 0 ]; then
make BETTERCAMERA=1 NODRAWINGDISTANCE=1 DEBUG=1 IMMEDIATELOAD=1 DEVELOPMENT=1 STRICT=1 -j
else
make BETTERCAMERA=1 NODRAWINGDISTANCE=1 DEBUG=1 IMMEDIATELOAD=1 DEVELOPMENT=1 -j
fi
# find file
FILE=./build/us_pc/sm64.us.f3dex2e.exe
if [ ! -f "$FILE" ]; then
FILE=./build/us_pc/sm64.us.f3dex2e
fi
$FILE --server 27015 --configfile sm64config_p1.txt &
sleep 4
$FILE --client 127.0.0.1 27015 --configfile sm64config_p2.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p3.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p4.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p5.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p6.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p7.txt &
sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_p8.txt &
#sleep 2
#winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 27015 --configfile sm64config_p1.txt' -ex 'quit'
#winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --client 127.0.0.1 27015 --configfile sm64config_p4.txt' -ex 'quit'

View File

@ -25,6 +25,7 @@ struct NetworkSystem* gNetworkSystem = &gNetworkSystemSocket;
#endif
#define LOADING_LEVEL_THRESHOLD 10
#define MAX_PACKETS_PER_SECOND_PER_PLAYER ((u16)70)
u16 networkLoadingLevel = 0;
bool gNetworkAreaLoaded = false;
@ -169,9 +170,30 @@ void network_send_to(u8 localIndex, struct Packet* p) {
assert(p->dataLength < PACKET_LENGTH);
// rate limit packets
bool tooManyPackets = false;
int maxPacketsPerSecond = (gNetworkType == NT_SERVER) ? (MAX_PACKETS_PER_SECOND_PER_PLAYER * (u16)network_player_connected_count()) : MAX_PACKETS_PER_SECOND_PER_PLAYER;
static int sPacketsPerSecond[MAX_PLAYERS] = { 0 };
static clock_t sPacketsPerSecondClock[MAX_PLAYERS] = { 0 };
clock_t currentClock = clock();
if ((currentClock - sPacketsPerSecondClock[localIndex]) > CLOCKS_PER_SEC) {
if (sPacketsPerSecond[localIndex] > maxPacketsPerSecond) {
LOG_ERROR("Too many packets sent to localIndex %d! Attempted %d. Connected count %d.", localIndex, sPacketsPerSecond[localIndex], network_player_connected_count());
}
sPacketsPerSecondClock[localIndex] = currentClock;
sPacketsPerSecond[localIndex] = 1;
} else {
sPacketsPerSecond[localIndex]++;
if (sPacketsPerSecond[localIndex] > maxPacketsPerSecond) {
tooManyPackets = true;
}
}
// send
if (!tooManyPackets) {
int rc = gNetworkSystem->send(localIndex, p->buffer, p->cursor + sizeof(u32));
if (rc == SOCKET_ERROR) { LOG_ERROR("send error %d", rc); return; }
}
p->sent = true;
gNetworkPlayers[localIndex].lastSent = clock();