Netsphere 0.18.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Netsphere --version 0.18.4                
NuGet\Install-Package Netsphere -Version 0.18.4                
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="Netsphere" Version="0.18.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Netsphere --version 0.18.4                
#r "nuget: Netsphere, 0.18.4"                
#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 Netsphere as a Cake Addin
#addin nuget:?package=Netsphere&version=0.18.4

// Install Netsphere as a Cake Tool
#tool nuget:?package=Netsphere&version=0.18.4                

Netsphere is a network library for C#

Nuget Build and Test

  • Netsphere is a transport protocol based on UDP.

  • Very versatile and easy to use.

  • Covers a wide range of network needs.

  • Full serialization features integrated with Tinyhand.

Table of Contents

Requirements

Visual Studio 2022 or later for Source Generator V2.

C# 13 or later for generated codes.

.NET 9 or later target framework.

Quick start

Install Netsphere using Package Manager Console.

Install-Package Netsphere

This is a small example code to use Netsphere.

First, define an interface shared between the client and server.

[NetServiceInterface] // Annotate NetServiceInterface attribute.
public interface ITestService : INetService // An interface for NetService must inherit from INetService.
{
    NetTask<string?> DoubleString(string input); // Declare the service method.
    // Ensure that both arguments and return values are serializable by Tinyhand serializer, and the return type must be NetTask or NetTask<T> or Task or Task<TResult>.
}

On the client side:

Create an instance, connect to the server, obtain the service interface, and call the function.

var unit = new NetControl.Builder().Build(); // Create a NetControl unit that implements communication functionality.
await unit.Run(new NetOptions(), true); // Execute the created unit with default options.

var netControl = unit.Context.ServiceProvider.GetRequiredService<NetControl>(); // Get a NetControl instance.
using (var connection = await netControl.NetTerminal.UnsafeConnect(new(IPAddress.Loopback, 1981)))
{// Connect to the server's address (loopback address).
    // All communication in Netsphere is encrypted, and connecting by specifying only the address is not recommended due to the risk of man-in-the-middle attacks.
    if (connection is null)
    {
        await Console.Out.WriteLineAsync("No connection");
    }
    else
    {
        var service = connection.GetService<ITestService>(); // Retrieve an instance of the target service.
        var input = "Nupo";
        var output = await service.DoubleString(input); // Arguments are sent to the server through the Tinyhand serializer, processed, and the results are received.
        await Console.Out.WriteLineAsync($"{input} -> {output}");
    }
}

await unit.Terminate(); // Perform the termination process for the unit.

On the server side:

Define a class that implements the interface and annotate it with NetServiceObject attribute.

[NetServiceObject] // Annotate NetServiceObject attribute.
internal class TestServiceImpl : ITestService
{
    async NetTask<string?> ITestService.DoubleString(string input)
        => input + input; // Simply repeat a string twice and return it.
}

Create a builder to instantiate, register Options and Services. From the builder, you create a Unit and execute it.

var builder = new NetControl.Builder() // Create a NetControl builder.
    .SetupOptions<NetOptions>((context, options) =>
    {// Modify NetOptions.
        options.NodeName = "Test server";
        options.Port = 1999; // Specify the port number.
        options.EnableEssential = true; // Required when using functions such as UnsafeGetNetNode() or Ping.
        options.EnableServer = true;
    })
    .Configure(context =>
    {
        context.Services.AddTransient<TestServiceAgent>(); // Register the service implementation. If a default constructor is available, an instance will be automatically created.
    })
    .ConfigureNetsphere(context =>
    {// Register the services provided by the server.
        context.AddNetService<ITestService, TestServiceAgent>();
        context.AddNetService<ITestService2, TestServiceAgent>();
    });

var unit = builder.Build(); // Create a unit that provides network functionality.
var options = unit.Context.ServiceProvider.GetRequiredService<NetOptions>();
await Console.Out.WriteLineAsync(options.ToString()); // Display the NetOptions.

// It is possible to unregister services, but frequent changes are not recommended (as the service table will be rebuilt). If frequent changes are necessary, consider using NetFilter or modifying the processing in the implementation class.
var netTerminal = unit.Context.ServiceProvider.GetRequiredService<NetTerminal>();
netTerminal.Services.Unregister<ITestService2>();

await unit.Run(options, true); // Execute the created unit with the specified options.
await Console.Out.WriteLineAsync("Server: Ctrl+C to exit");
await ThreadCore.Root.Delay(100_000); // Wait until the server shuts down.
await unit.Terminate(); // Perform the termination process for the unit.

Instance management

  • Service agent: An instance is created for each connection by the DI container.
  • Service filter: Generated for each agent type (not for each instance).

Adding NetService

NetService is the core functionality of Netsphere and is designed to be as easy to use as possible. However, due to its nature, several steps are required when using it:

  1. Define the interface shared between the client and server.

    [NetServiceInterface]
    public interface ITestService : INetService
    {
        Task<string?> DoubleString(string input);
    }
    
  2. Implement the NetService agent (implementation class) on the server side.

    [NetServiceObject]
    internal class TestServiceAgent : ITestService
    {
        async NetTask<string?> ITestService.DoubleString(string input)
            => input + input;
    }
    
  3. Add the network service to the server.

    .ConfigureNetsphere(context =>
    {
        context.AddNetService<ITestService, TestServiceAgent>();
    });
    

    It can also be dynamically added via NetTerminal.

    netTerminal.Services.Register<ITestService, TestServiceAgent>();
    
  4. Register the agent class in the DI container. If forgotten, it will be registered as Transient when the network service is registered.

    .Configure(context =>
    {
        context.Services.AddTransient<TestServiceAgent>();
    })
    
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Netsphere:

Package Downloads
Lp

Lp is an experimental program that create value.

Netsphere.Shared

Netsphere.Shared

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.18.5 29 2/21/2025
0.18.4 46 2/20/2025
0.18.3 54 2/20/2025
0.18.2 44 2/20/2025
0.18.1 56 2/18/2025
0.18.0 81 2/16/2025
0.17.0 72 1/27/2025
0.16.0 129 12/10/2024
0.15.0 170 11/13/2024
0.14.1 163 9/7/2024
0.14.0 141 8/30/2024
0.13.1 151 7/5/2024
0.13.0 175 6/19/2024
0.12.0 128 6/17/2024
0.11.0 131 6/6/2024
0.10.4 123 6/3/2024
0.10.3 131 5/8/2024
0.10.2 119 5/8/2024
0.10.1 137 4/11/2024
0.10.0 168 3/29/2024
0.9.0 526 11/15/2023
0.8.1 787 2/20/2023
0.8.0 872 11/13/2022
0.7.8 1,010 10/18/2022
0.7.7 1,020 10/17/2022
0.7.6 988 10/17/2022
0.7.5 990 10/15/2022
0.7.4 1,065 8/27/2022
0.7.3 1,012 8/21/2022
0.7.2 1,008 8/19/2022
0.7.1 1,090 8/19/2022
0.6.1 940 4/29/2022
0.6.0 872 4/29/2022
0.5.2 905 4/28/2022
0.5.1 915 3/3/2022
0.5.0 863 3/2/2022
0.4.0 871 1/14/2022
0.3.0 2,053 11/26/2021
0.1.0 2,484 8/19/2021
0.0.4 2,417 6/30/2021
0.0.3 2,440 6/29/2021
0.0.2 2,429 6/7/2021
0.0.1 2,381 6/7/2021