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

ReadOnlyMemory<byte> support vs. byte[] and async/Task support? #535

Closed Simonl9l closed 7 months ago

Simonl9l commented 7 months ago

Hi iI'm a newbie to using you library so please excuse my question if Im just not getting it - yet. This library look massively powerful! Thanks for maintaining it!

I'm trying to plug your library onto an existing solution where our data is available as a ReadOnlyMemory<byte>, and we'd like to, in the NetManager.SendToAll(), not have to use ReadOnlyMemory<byte>.ToArray() as that allocates a new Array.

Do you have any plans to support ReadOnlyMemory<byte> natively?

Additionally, do you have any plans to support Task and async semantics with CancelationTokens, understandably only supported by .Net Core and possibly Unity?

EventBasedNetListener _listener = new();
NetManager _client;
IPEndPoint _ipEndpoint;

_ipEndpoint = new IPEndPoint(ipAddress, port);
_client = new NetManager(_listener);
_client.Start();
_client.Connect(_ipEndpoint, "key");

// Hander that its called:
 public  Task<bool> SendEventAsync(ReadOnlyMemory<byte> packet, CancellationToken token = default)
    {
        try
        {
            _client.SendToAll(packet.ToArray(), DeliveryMethod.ReliableOrdered);
            return Task.FromResult(true);
        }
        catch (Exception e)
        {
            Log.Error(e, "{Message}", e.Message);
            return Task.FromResult(true);
        }
    }

Library version: Nuget: 1.1.0

Framework: Net 8.0

OS: All

RevenantX commented 7 months ago

@Simonl9l this library already async and has background thread for send/receive operations. This way it works faster (for most use cases) than async implementation. You can call Send anywhere without pauses.

RevenantX commented 7 months ago

@Simonl9l also there is NetPeer.Send with ReadOnlySpan support.

Simonl9l commented 7 months ago

@RevenantX thanks!

Right - searching for ReadOnlySpan brought up this https://github.com/RevenantX/LiteNetLib/issues/323.

So to use this we'd need to:

EventBasedNetListener _listener = new();
NetManager _client;
IPEndPoint _ipEndpoint;

_ipEndpoint = new IPEndPoint(ipAddress, port);
_client = new NetManager(_listener);
_client.Start();
_client.Connect(_ipEndpoint, "key");

// Hander that its called:
 public  Task<bool> SendEventAsync(ReadOnlyMemory<byte> packet, CancellationToken token = default)
    {
        try
        {
            foreach (var peer in  _client.ConnectedPeerList)
            {
                peer.Send(packet.Span, DeliveryMethod.ReliableOrdered);
            }

            return Task.FromResult(true);
        }
        catch (Exception e)
        {
            Log.Error(e, "{Message}", e.Message);
            return Task.FromResult(true);
        }
    }

will probably dive into the code some in regards to the async aspects of the background thread. Initial reaction, is how cancelation support is achieved if one needs to pull the plug on that background thread.

Thanks again!

RevenantX commented 7 months ago

@Simonl9l it's mainly developed for realtime multiplayer games. There is almost no cases to use "Cancelation tokens" or something like this. Just NetManager.Stop - that stops all internal threads, also there is Manual mode that disables thread at all and in this scenario you possibly will need your own threading code/async or if it's game server it can work with just 1 main thread.

RevenantX commented 7 months ago

Added SendToAll with ReadOnlySpan argument