RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3k stars 489 forks source link

Add Ability to Pause NetManager Event Polling #512

Closed Moe-Baker closed 1 year ago

Moe-Baker commented 1 year ago

My scenario is as follows:

  1. I'm using Unity.
  2. I'm invoking NetManager's PollEvents method from Unity's Update loop.
  3. I sometimes will receive packets that require some time to be processed, namely scene-loading packets.
  4. These scene-loading packets should pause all other packets from being polled until the scenes they are referencing are loaded.

I modified the NetManager.cs file to achieve this as such:

public bool Pause;

public void PollEvents()
{
    if (Pause) return;

    if (UnsyncedEvents)
        return;

    int eventsCount;
    lock (_netEventsQueue)
        eventsCount = _netEventsQueue.Count;

    for(int i = 0; i < eventsCount; i++)
    {
        NetEvent evt;
        lock (_netEventsQueue)
            evt = _netEventsQueue.Dequeue();
        ProcessEvent(evt);

        if (Pause) return;
    }
}

It works so far, but I'm wondering if this would break something else down the pipe (keep-alive packets, acks, ... etc). And if it does, would it be possible to add this feature?

Library version: [release version (0.9.5.2)]

Framework: [Unity]

OS: [Windows]

Moe-Baker commented 1 year ago

After some thinking, I decided that queuing up received messages and processing them on my terms would be safer than messing with the library internals.