A simple and fast .NET networking library compatible with .NET Standard 2, .NET 5, 6 and 7.
LiteServer.OnError
paramter from LiteConnection
to TUser
.IPAddress.Any
in LiteServerOptions
. (#54) (PR #55)LitePacketMode
, LitePacketStream
and ILitePacketStream
interface and all its dependencies
This removal affects the way the user handles packets on both LiteClient
and LiteServerUser
classes.
Previously, we had:public class User : LiteServerUser
{
public override Task HandleMessageAsync(ILitePacketStream packet)
{
}
}
This code should be replaced with:
public class User : LiteServerUser
{
public override Task HandleMessageAsync(byte[] packetBuffer)
{
// Use your own way to read the incoming packet buffer.
}
}
LitePacket
mechanism.
The LitePacketStream
will be kept to provide an alternative to the BinaryReader/Writer
implementation. Note that the LitePacketStream
uses a BinaryReader
and BinaryWriter
internally.virtual ILitePacketStream CreatePacket(byte[] buffer)
from LitePacketProcessor
and its abstraction.
No need for this method since we removed the ILitePacketStream
interface. We will pass directly the raw packet buffer to the HandleMessageAsync
method.Send(byte[])
and Send(Stream)
methods to LiteClient
and LiteServerUser
.
Send(byte[])
sends a raw packet to the remote end point.Send(Stream)
method sends the buffer contained in the given stream to the remote end point.By providing these two options, the user is free to choose which technique to use to send its packet. It can use the native .NET BinaryWriter
class or any other way to build its packet buffer.
using var stream = new MemoryStream();
using var packet = new BinaryWriter(stream);
packet.Write(42); // int32
packet.Write("Hello World!"); // string
byte[] buffer = packet.GetBuffer();
_user.Send(buffer); // byte[] overload
// or
_user.Send(packet); // Stream overload
AppendHeader(byte[])
method.
LikePacketProcessor
, it would add the length of the packet content as packet header. Stored as an int
https://github.com/Eastrall/LiteNetwork/blob/main/src/LiteNetwork/Protocol/LitePacketProcessor.cs#L12
ILiteConnection
abstraction.LiteConnection
with all basic information:
public Guid Id { get; }
public Socket? Socket { get: }
public abstract Task HandleMessageAsync(byte[] packetBuffer)
public virtual void Send(byte[] packetBuffer)
public virtual void Send(Stream packetStream)
ILiteServer
abstraction to be more straightforward and simple.ILiteServer
.ILiteServer
:
IEnumerable<TUser> ConnectedUsers { get; }
TUser? GetUser(Guid userId)
bool TryGetUser(Guid userId, out TUser? user)
void Start()
void Stop()
Task StopAsync(CancellationToken cancellationToken)
void DisconnectUser(Guid userId)
void SendTo(TUser user, ILitePacketStream packet)
void SendTo(IEnumerable<TUser> users, ILitePacketStream packet)
void SendToAll(ILitePacketStream packet)
Start()
, Stop()
and StopAsync(CancellationToken cancellationToken) methods from
LiteServer`.LiteServer
constructor public LiteServer(LiteServerOptions options)
ILiteBuilder AddLiteServer<TLiteServerUser>
LiteServerContext
mechanism to allow interaction with the server through the the LiteServerUser
class.IEnumerable<LiteConnection> Users { get; }
to ILiteServer
abstraction.void DisconnectUser(Guid userId);
to ILiteServer
abstraction.void DisconnectUser(LiteConnection connection);
to ILiteServer
abstraction. void SendTo(LiteConnection connection, byte[] packet);
to ILiteServer
abstraction.void SendTo(IEnumerable<LiteConnection> connections, byte[] packet);
to ILiteServer
abstraction.void SendToAll(byte[] packet);
to ILiteServer
abstraction.IEnumerable<TUser> ConnectedUsers { get; }
to IEnumerable<LiteConnection> Users { get; }
void SendTo(TUser user, byte[] packet)
to void SendTo(LiteConnection connection, byte[] packet)
void SendTo(IEnumerable<TUser> users, byte[] packet)
to void SendTo(IEnumerable<LiteConnection> connections, byte[] packet)
SendTo(TUser user, ILitePacketStream packet)
with void SendTo(TUser user, byte[] packet)
void SendTo(IEnumerable<TUser> users, ILitePacketStream packet)
with SendTo(IEnumerable<TUser> users, byte[] packet)
void SendToAll(ILitePacketStream packet)
with void SendToAll(byte[] packet)
ILiteBuilder AddLiteServer<TLiteServer, TLiteServerUser>
by ILiteBuilder AddLiteServer<TLiteServer>
Pass directly a LiteServer implementation to the builder.ILiteBuilder AddLiteServer<TLiteServer, TLiteServerImplementation, TLiteServerUser>
by ILiteBuilder AddLiteServer<TLiteServer, TLiteServerImplementation>
If the user wants to add an extra interface to its LiteServer he can also register the interface and the implementation together in the service provider.Id
, Socket
properties, HandleMessageAsync
, Send(byte[])
and Send(Stream)
as they were moved to LiteConnection
base class.ILiteClient
abstraction. Keeping it simple and extendable.Id
, Socket
properties, HandleMessageAsync
, Send(byte[])
and Send(Stream)
as they were moved to LiteConnection
base class.ILiteBuilder AddLiteClient()
method./// <inheritdoc />
documentation comments.Microsoft.Extensions.DependencyInjection
minimum version to 3.1.20Microsoft.Extensions.Hosting.Abstractions
minimum version to 3.1.20Microsoft.Extensions.Logging.Abstractions
minimum version to 3.1.20LiteServerContext
mechanism to allow interaction with the server through the the LiteServerUser
class.IEnumerable<LiteConnection> Users { get; }
to ILiteServer
abstraction.void DisconnectUser(Guid userId);
to ILiteServer
abstraction.void DisconnectUser(LiteConnection connection);
to ILiteServer
abstraction. void SendTo(LiteConnection connection, byte[] packet);
to ILiteServer
abstraction.void SendTo(IEnumerable<LiteConnection> connections, byte[] packet);
to ILiteServer
abstraction.void SendToAll(byte[] packet);
to ILiteServer
abstraction.IEnumerable<TUser> ConnectedUsers { get; }
to IEnumerable<LiteConnection> Users { get; }
void SendTo(TUser user, byte[] packet)
to void SendTo(LiteConnection connection, byte[] packet)
void SendTo(IEnumerable<TUser> users, byte[] packet)
to void SendTo(IEnumerable<LiteConnection> connections, byte[] packet)