MidLevel / Ruffles

Lightweight and fully managed reliable UDP library.
MIT License
212 stars 28 forks source link

ReliableChannel stops receiving after 65535 packets #34

Open neto-kokku opened 2 years ago

neto-kokku commented 2 years ago

https://github.com/MidLevel/Ruffles/blob/378751e90c57e8359e2c032b37998950ecd69cbd/Ruffles/Channeling/Channels/ReliableChannel.cs#L60

This line here is missing the cast to ushort, causing the ReliableChannel to stop accepting packets past sequence 65535.

TwoTenPvP commented 2 years ago

Good catch, thanks!

The design is meant to roll over. I will work on fixing this.

collectVood commented 1 year ago

Hey, has this been resolved yet or is there a work-around for it?

The Ruffles project looks awesome! :)

neto-kokku commented 1 year ago

Just change the line to: else if (sequence == (ushort)(_incomingLowestAckedSequence + 1))

collectVood commented 1 year ago

Ohhhh... I wasn't even aware that this could grow out of the ushort size like this. The more you know! Thanks a lot <3 😄

neto-kokku commented 1 year ago

C# doesn't have short/ushort literals, so the 1 in the expression is an int. Adding an ushort to an int returns an int, and comparing an ushort to an int implicitly casts the ushort to int. So when _incomingLowestAckedSequence is 65535 (the largest value an ushort can hold) the addition results in 65536 instead of wrapping around back to 0.

This was a really annoying bug do fix since it manifested as the game simply breaking down after playing for over 20 minutes or so.

collectVood commented 1 year ago

This does make a lot of sense, you're right.

Glad you found it and shared it here to save everyone some time!

neto-kokku commented 1 year ago

Here's a example where you can see this in action: https://dotnetfiddle.net/dKKfaf