Closed Simonl9l closed 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.
@Simonl9l also there is NetPeer.Send with ReadOnlySpan support.
@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!
@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.
Added SendToAll with ReadOnlySpan
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 theNetManager.SendToAll()
, not have to useReadOnlyMemory<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
andasync
semantics withCancelationTokens
, understandably only supported by .Net Core and possibly Unity?Library version: Nuget: 1.1.0
Framework: Net 8.0
OS: All