All Projects → Eastrall → LiteNetwork

Eastrall / LiteNetwork

Licence: MIT license
A simple and fast .NET networking library compatible with .NET Standard 2, .NET 5, 6 and 7.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to LiteNetwork

ddos
Simple dos attack utility
Stars: ✭ 36 (-45.45%)
Mutual labels:  socket, tcp, packet
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+3474.24%)
Mutual labels:  socket, tcp
Zserver4d
ZServer4D 是一套从商业项目剥离而出的云服务器中间件,可以承载百万级的分布式负载服务,并且支持IoT及内网穿透
Stars: ✭ 199 (+201.52%)
Mutual labels:  socket, tcp
Pypacker
📦 The fastest and simplest packet manipulation lib for Python
Stars: ✭ 216 (+227.27%)
Mutual labels:  socket, tcp
Kalm.js
The socket manager
Stars: ✭ 155 (+134.85%)
Mutual labels:  socket, tcp
Iot Dc3
IOT DC3 is an open source, distributed Internet of Things (IOT) platform based on Spring Cloud. It is used for rapid development of IOT projects and management of IOT devices. It is a set of solutions for IOT system.
Stars: ✭ 195 (+195.45%)
Mutual labels:  socket, tcp
Tcpprobe
Modern TCP tool and service for network performance observability.
Stars: ✭ 207 (+213.64%)
Mutual labels:  socket, tcp
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (+100%)
Mutual labels:  socket, tcp
Ssokit Qmake
A Simple & Strong Tool for TCP&UDP Debug
Stars: ✭ 231 (+250%)
Mutual labels:  socket, tcp
Unitysocketprotobuf3demo
主要实现了用Unity对接了Leaf服务器。其次带了些小工具。
Stars: ✭ 244 (+269.7%)
Mutual labels:  socket, tcp
ytmous
Anonymous Youtube Proxy
Stars: ✭ 60 (-9.09%)
Mutual labels:  easy-to-use, lite
GenericProtocol
⚡️ A fast TCP event based buffered server/client protocol for transferring data over the (inter)net in .NET 🌐
Stars: ✭ 38 (-42.42%)
Mutual labels:  socket, tcp
Nettychat
基于Netty+TCP+Protobuf实现的Android IM库,包含Protobuf序列化、TCP拆包与粘包、长连接握手认证、心跳机制、断线重连机制、消息重发机制、读写超时机制、离线消息、线程池等功能。
Stars: ✭ 1,979 (+2898.48%)
Mutual labels:  socket, tcp
gnb udp over tcp
gnb_udp_over_tcp 是一个为GNB开发的通过tcp链路中转UDP分组转发的服务
Stars: ✭ 32 (-51.52%)
Mutual labels:  socket, tcp
Ether.network
https://github.com/Eastrall/Sylver
Stars: ✭ 147 (+122.73%)
Mutual labels:  socket, easy-to-use
Ohsce
PHP HI-REL SOCKET TCP/UDP/ICMP/Serial .高可靠性PHP通信&控制框架SOCKET-TCP/UDP/ICMP/硬件Serial-RS232/RS422/RS485 AND MORE!
Stars: ✭ 206 (+212.12%)
Mutual labels:  socket, tcp
go-eventserver
A socket server which reads events from an event source and forwards them to the user clients when appropriate
Stars: ✭ 18 (-72.73%)
Mutual labels:  socket, tcp
Async Sockets Cpp
Simple thread-based asynchronous TCP & UDP Socket classes in C++.
Stars: ✭ 127 (+92.42%)
Mutual labels:  socket, tcp
Socketpushclient
最近项目中要求做推送,基于内网的。由于工期不是很紧,需求不是很严格,所以放弃了使用三方的推送框架,基于Socket自己写了消息推送功能(服务端+android端)。服务器端使用java,客户端使用Android。本人是做Android开发的,所以demo重点介绍客户端的一些代码,包括Socket的TCP长连接及发消息,Service如何不被轻易杀死,通过aidl实现界面实时与Service的通信,可以在界面上控制Socket的连接与断开以及发消息,并采用了Parcelable对象实现aidl传参
Stars: ✭ 129 (+95.45%)
Mutual labels:  socket, tcp
Simplenetwork
simple TCP server / client C++ linux socket
Stars: ✭ 225 (+240.91%)
Mutual labels:  socket, tcp

LiteNetwork

Build NuGet Nuget Downloads

LiteNetwork is a simple and fast networking library built with C# and compatible with .NET Standard 2, .NET 5 and .NET 6. Its main goal is to simply the creation of basic socket servers over the TCP/IP protocol.

Initially, LiteNetwork 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 TcpUser : LiteServerUser
{
    public override Task HandleMessageAsync(byte[] packetBuffer)
    {
        // Handle incoming messages using a BinaryReader or any other solution for reading a byte[].
    }

    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 TcpUser type of users. Create another new class, and implement the LiteServer<T> class where T is the previously created TcpUser.

public class MyTcpServer : LiteServer<TcpUser>
{
    public MyTcpServer(LiteServerOptions options, IServiceProvider serviceProvider = null)
        : base(options, serviceProvider)
    {
    }
}

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 top-level statement
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 top-level statement
using Microsoft.Extensions.Hosting;
using System;

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

await 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 top-level statement
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 for the MyTcpServer class.
        builder.AddLiteServer<MyTcpServer>(options =>
        {
            // This configures the server's LiteServerOptions instance.
            options.Host = "127.0.0.1";
            options.Port = 4444;
        });
    })
    .UseConsoleLifetime()
    .Build();

await 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

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

  • The instance way: by creating a LiteClient instance and then connect to the remote server 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 ned to create a new class that inherit from the LiteClient class.

using LiteNetwork.Client;

public class MyTcpClient : LiteClient
{
    public EchoClient(LiteClientOptions options, IServiceProvider serviceProvider = null) 
        : base(options, serviceProvider)
    {
    }
}

Just like a LiteNetwork server, the client has some hooks that allows you to control the client lifetime, such as:

Method Description
HandleMessageAsync() Called when the client receives a message from the server.
OnConnected() Called when the client is connected to the remote server.
OnDisconnected() Called when the client is disconnected from the remote server.
OnError(Exception) Called when there is an unhandled error within the client process.

Create the client via instance

Using the previously created client, you can now create a new instance of the MyTcpClient class, set the correct options to connect to the remote server and then, call the ConnectAsync() method.

// Using top-level statement
using LiteNetwork.Client;
using System;

var options = new LiteClientOptions()
{
    Host = "127.0.0.1",
    Port = 4444
};
var client = new CustomClient(options);
Console.WriteLine("Press any key to connect to server.");
Console.ReadKey();

await client.ConnectAsync();

// Do something while client is connected.

Create the client 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 top-level statement
using LiteNetwork.Client.Hosting;
using LiteNetwork.Hosting;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureLiteNetwork((context, builder) =>
    {
        builder.AddLiteClient<MyTcpClient>(options =>
        {
            options.Host = "127.0.0.1";
            options.Port = 4444;
        });
    })
    .UseConsoleLifetime()
    .Build();

// At this point, the client will connect automatically once the host starts running.
await host.RunAsync(); 

Once your program starts, your MyTcpClient will try to connect to the remote server ("127.0.0.1" and port 4444). Also, since you are using the .NET generic host, it also provides the dependency injection mechanism into the client. Hence, you can inject services, configuration (IOptions<T> if configured), logger, etc...

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

Protocol

Packet Processor

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].