LiteNetLib
LiteNetLib copied to clipboard
Poll Single Event
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 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"
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;
}