LiteNetwork 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package LiteNetwork --version 1.0.3                
NuGet\Install-Package LiteNetwork -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LiteNetwork" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteNetwork --version 1.0.3                
#r "nuget: LiteNetwork, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LiteNetwork as a Cake Addin
#addin nuget:?package=LiteNetwork&version=1.0.3

// Install LiteNetwork as a Cake Tool
#tool nuget:?package=LiteNetwork&version=1.0.3                

LiteNetwork

.NET Codacy Badge NuGet

LiteNetwork is a simple and fast networking library built with C# and compatible with .NET Standard 2. Its main goal is to simply the creation of basic socket servers over the TCP/IP protocol. It has been initialy developed for game development networking, but can also be used for other purposes.

How to install

LiteNetwork is shiped as a single package, you can install it through the Visual Studio project package manager or using the following command in the Package Manager Console:

$> Install-Package LiteNetwork

Or you can use the dotnet command:

$> dotnet add package LiteNetwork

Getting started

Create a server

There is two ways of building a TCP server with LiteNetwork:

  • The instance way, by creating a LiteServer instance and then run it manually
  • The service way
    • In fact, LiteNetwork provides an extension to the ServiceCollection object, and can be integrated in a .NET Generic Host (used by ASP.NET Core, MAUI).
Common code

First of all, you will need to create the user class that will represent a connected user on your server. Simple create a new class that implements the LiteServerUser class.

using LiteNetwork.Server;

public class ClientUser : LiteServerUser
{
}

Within this class, you will be able to handle this client's incoming message sent by a client program thanks to the HandleMessageAsync() method. You can also be notified when the client connects to the server or disconnects.

using LiteNetwork.Protocol.Abstractions;
using LiteNetwork.Server;

public class ClientUser : LiteServerUser
{
    public override Task HandleMessageAsync(ILitePacketStream incomingPacketStream)
    {
        // Handle incoming messages thanks to the ILitePacketStream interface.
    }

    protected override void OnConnected()
    {
        // When the client connects.
    }

    protected override void OnDisconnected()
    {
        // When the client disconnects.
    }
}

Once the server user is ready, you can create the server itself that will handle this ClientUser type of users. Create another new class, and implement the LiteServer<T> class where T is the previously created ClientUser.

public class MyTcpServer : LiteServer<ClientUser>
{
    public MyTcpServer(LiteServerOptions options)
        : base(options)
    {
    }
}

The server has some hooks that allows you to control its life time, such as:

Method Description
OnBeforeStart() Called before the server starts.
OnAfterStart() Called after the server starts.
OnBeforeStop() Called before the server stops.
OnAfterStop() Called after the server stops.
OnError(ILiteConnection, Exception) Called when there is an unhandled error witht the given ILiteConnection.
Create the server via instance

Now that the server and user classes are built, you can now instanciate your server and call the Start() method to start the server.

// Using minimal API
using LiteNetwork.Server;
using System;

// Create the server configuration, to listen on "127.0.0.1" and port "4444"
var configuration = new LiteServerOptions()
{
    Host = "127.0.0.1",
    Port = 4444
};

// Create the server instance by givin the server options and start it.
using var server = new MyTcpServer(configuration);
server.Start();

// Just for the example, otherwise the console will just shutdown.
// Do not use in production environment.
Console.ReadKey(); 
Create the server via service

For this example, you will need to install the Microsoft.Extensions.Hosting package from nuget in order to build a .NET Generic Host.

// Using minimal API
using Microsoft.Extensions.Hosting;
using System;

var host = new HostBuilder()
    .UseConsoleLifetime()
    .Build();

return host.RunAsync();

Then, once your host is setup and running, you can configure the LiteServer service using the ConfigureLiteNetwork() method located in the LiteNetwork.Hosting namespace:

// Using minimal API
using LiteNetwork.Hosting;
using LiteNetwork.Server.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;

var host = new HostBuilder()
    // Configures the LiteNetwork context.
    .ConfigureLiteNetwork((context, builder) =>
    {
        // Adds a LiteServer instance based on the MyTcpServer and ClientUser.
        builder.AddLiteServer<MyTcpServer, ClientUser>(options =>
        {
            // This configures the server's LiteServerOptions instance.
            options.Host = "127.0.0.1";
            options.Port = 4444;
        });
    })
    .UseConsoleLifetime()
    .Build();

return host.RunAsync();

Your server is now listening on "127.0.0.1" and port "4444". Also, since you are using a .NET generic host, it also provides dependency injection into the server and client classes. Hence, you can inject services, configuration (IOptions<T> if configured, etc..).

Note: You can also add as many servers you want into a single .NET generic host by calling the builder.AddLiteServer<>() method with different parameters.

Create a client

Protocol

TBA.

Thanks

I would like to thank everyone that contributed to this library directly by fixing bugs or add new features, but also the people with who I had the chance to discuss about networking problematics which helped me to improve this library.

Credits

Package Icon : from Icons8

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on LiteNetwork:

Package Downloads
LiteNetwork.Extensions

Super simple packet handling support for LiteNetwork.

HeavyNetwork

Package Description

LiteNetwork.Extensions.Processing

Super simple packet handling support for LiteNetwork.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.4.0 579 1/21/2024
2.3.0 1,535 7/30/2023
2.2.0 1,056 12/28/2022
2.1.0 992 11/12/2022
2.0.0 1,051 5/27/2022
2.0.0-rc.6 213 5/14/2022
2.0.0-rc.5 131 4/24/2022
2.0.0-rc.4 129 4/23/2022
2.0.0-rc.3 118 4/23/2022
2.0.0-rc.2 131 4/23/2022
2.0.0-rc.1 126 4/23/2022
1.0.4 1,052 1/23/2022
1.0.3 1,010 1/22/2022
1.0.2 852 12/22/2021
1.0.1 807 12/22/2021
1.0.0 909 11/8/2021