RiptideNetworking / Riptide

Lightweight C# networking solution for multiplayer games.
https://riptide.tomweiland.net
MIT License
1.06k stars 141 forks source link

Add bandwidth up/down per Connection measurements. #94

Closed megalithos closed 8 months ago

megalithos commented 1 year ago

Feature #77 implemented.

I added some fields & properties to the Connection class. And function BandwidthMeasurementTick which should be called periodically to measure bandwidth.

.csproj seems to be changed too, I was trying to debug and enabled debug in the visual studio properties

in Server class I added SetBandwidthMeasurementsActive & flag that must be set to true to measure bandwidth

also in Server's update method we call periodically BandwidthMeasurementTick of every connection.

In UdpPeer I added extra out parameter internal void Send(byte[] dataBuffer, int numBytes, IPEndPoint toEndPoint) ---> internal void Send(byte[] dataBuffer, int numBytes, IPEndPoint toEndPoint, out int sentBytes)

How to use this? Call SetBandwidthMeasurementsActive with parameter true and then you can access each connection's up/down bandwidth like

Connection connection = / /; // do something: connection.BandwidthOut; // do something: connection.BandwidthIn;

megalithos commented 1 year ago

My main concern with this implementation is this: Is it going to be too slow to iterate all connections on every Server.Update() call? Would there be a faster approach?

Surely this is slower when there are more players, maybe I am just thinking this too thoroughly :D

However right now you need to enable the bandwidth measurements with SetBandwidthMeasurementsActive call to use so for normal users it won't do anything different really.

megalithos commented 1 year ago

ahh I already came up with a better solution (little sleep deprived today lol)

basically: have the time check in the Server.Update() and if 1s elapsed then we iterate all clients once and reset timer so instead of iterating 50 times (depending on server's fixed timestep) we iterate all connections just once in 1 second.

Imo this would be very optimized then.

megalithos commented 1 year ago

Ok the implementation is good now

tom-weiland commented 8 months ago

Thanks for the effort you put into this! It did help me think through how to do this, but in the end I figured it would be better to implement it in such a way that it doesn't require any internal iteration over clients/connections, and to just leave that up to the developer (as many people will probably never even use these metrics).

Just for future reference though, please avoid committing unnecessary changes such as those to the sln and csproj files (and the new RiptidideNetworking - Shortcut.lnk file that was added), and format your commit messages to fit the commit message formatting guidelines.