RiptideNetworking / Riptide

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

Running Server.Stop() does not result in Client getting expected Disconnect Reason #157

Closed apkatsikas closed 1 month ago

apkatsikas commented 1 month ago

Running Server.Stop() should result in Client using Disconnected EventHandler getting DisconnectedEventArgs Reason of DisconnectReason.ServerStopped - instead it returns 80/unknown.

It can be repro-ed with this snippet:

using Riptide;
using Riptide.Utils;

public static class DemoBug
{
    private static Server? Server;
    private static Client? Client;
    private static bool running = true;
    private static bool triedToConnect;
    private static uint tickCount;

    static void Main(string[] args)
    {
        RiptideLogger.Initialize(Console.WriteLine, true);
        Server = new Server();

        Server!.Start(7777, 4);

        Client = new Client();
        Client.Disconnected += (_, dea) =>
        {
            if (dea.Reason != DisconnectReason.Disconnected)
                Console.WriteLine($"Disconnected. Reason is {dea.Reason}");
        };

        while (running)
        {
            Server.Update();
            Client.Update();
            Thread.Sleep(10);

            if (!triedToConnect)
            {
                triedToConnect = true;
                Client.Connect("127.0.0.1:7777");
            }

            switch (tickCount)
            {
                case 250:
                    Server.Stop();
                    break;
                case 500:
                    running = false;
                    break;
                default:
                    break;
            }
            tickCount++;
        }
    }
}
apkatsikas commented 1 month ago

Opened a fix PR: https://github.com/RiptideNetworking/Riptide/pull/158