LiteNetLib icon indicating copy to clipboard operation
LiteNetLib copied to clipboard

Poll Single Event

Open NamelessPerson opened this issue 1 year ago • 2 comments

Currently NetManager can only poll all pending events. However we have logic that may want us to pause processing events after a certain event has been received. It would be helpful to have a PollSingleEvent() or PollEvents(int count) code path.

NamelessPerson avatar Aug 24 '23 15:08 NamelessPerson

@NamelessPerson that events contains not only "receive" events but also disconnect/connect/errors and many others. It's better implement "pause" in application code by storing incoming packets in Queue, and then just process them when your game "unpaused"

RevenantX avatar Aug 25 '23 07:08 RevenantX

Yes we thought of that solution, and it is doable but isn't simple with our current architecture. We would need a pretty large refactor to store caching messages and this is complicated by the fact that our other networking backend (steam) is designed around messages NOT being cached.

For the time being I implemented it as such and it appears to be working just fine.

public int PollEvents(int count) {
    if (UnsyncedEvents || count <= 0)
        return 0;
    int eventsCount;
    lock (_netEventsQueue)
        eventsCount = _netEventsQueue.Count;
    eventsCount = Math.Min(count, eventsCount);
    for (int i = 0; i < eventsCount; i++) {
        NetEvent evt;
        lock (_netEventsQueue)
            evt = _netEventsQueue.Dequeue();
        ProcessEvent(evt);
    }
    return eventsCount;
}

NamelessPerson avatar Sep 06 '23 15:09 NamelessPerson