RevenantX / LiteNetLib

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

Handle packet with Async method. #553

Closed nvh2001 closed 3 months ago

nvh2001 commented 3 months ago

I want all clients to not have to wait for others and a client just handle 1 package at the same time.

private async void Handle(NetPeer client, NetPacketReader reader){
    lock (client) //handle one packet in same time, wait to complete then handle next packet
    {
         //handle packet in here
    }
}

Am i wrong with this code?

RevenantX commented 3 months ago

@nvh2001 you don't need if you put packet into thread/async method after OnReceive.

nvh2001 commented 3 months ago

@nvh2001 you don't need if you put packet into thread/async method after OnReceive. Do I need to lock client to ensure that a client can only process one of its packet at a time?

RevenantX commented 3 months ago

@nvh2001 you don't need if you put packet into thread/async method after OnReceive. Do I need to lock client to ensure that a client can only process one of its packet at a time?

No

alec1o commented 3 months ago

In other words, each data you receive is a different and distinct object, if you receive 100 packets simultaneously the 100 packets will be a different instance and will contain different contents so there is no need to block the data.

Only the internal buffer blocks the data, because the internal buffer is just an instance that receives the data and then moves the content to a new instance (you receive this new instance as a parameter).

So what you were thinking is that you have access to the internal buffer and that's not what happens.

What happens is that you have access to a copy of the internal buffer (that's why the size of the paramter buffer only allocates the size of the content) the internal buffer allocates more space to be able to receive dynamic amounts of data.

Tell me if you have any questions! @vanhaodev @nvh2001