NetworkListener 1.0.0-rc6

This is a prerelease version of NetworkListener.
dotnet add package NetworkListener --version 1.0.0-rc6
NuGet\Install-Package NetworkListener -Version 1.0.0-rc6
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="NetworkListener" Version="1.0.0-rc6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetworkListener --version 1.0.0-rc6
#r "nuget: NetworkListener, 1.0.0-rc6"
#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 NetworkListener as a Cake Addin
#addin nuget:?package=NetworkListener&version=1.0.0-rc6&prerelease

// Install NetworkListener as a Cake Tool
#tool nuget:?package=NetworkListener&version=1.0.0-rc6&prerelease

Network Listener

Multithreaded network client listener with injectable client processing and support for secured communications over SSL/TLS.

NetworkListener Class

The NetworkListener class is the main component of this library. It handles the initialization of the listener, client threads, processing of client data over the network, and logging using Microsoft.Extensions.Logging. Details of how client data is handled is in the injectable INetworkClientDataProcessor implemented object you provide.

Using The Listener

After building a new listener instance, see NetworkListenerBuilder section, call the Listen() method to start the listening for and processing client connections.

using var netListener = BuildNetworkListener();
netListener.Listen();

You call also pass in a standard System.Threading.CancellationToken to allow for cancelling the listener and all client processing.

var cts = new CancellationTokenSource();

//...

using var netListener = BuildNetworkListener();
netListener.Listen(cts.Token);

NetworkListener Events

The NetworkListener class has several events that can be utilized.

Event Name Description
ClientConnected Event that is triggered when a client connects
ClientDataReceived Event that is triggered when data is received from the client
ClientDisconnected Event that is triggered when a client disconnects
ClientError Event that is triggered on a client processing error
Started Event that is triggered when the listener starts
Stopped Event that is triggered when the listener stops

INetworkClientDataProcessor Interface

To use NetworkListener you must first inject an object that implements INetworkClientDataProcessor. This defines the standard interfaces that NetworkListener class will call to processes client data.

The INetworkClientDataProcessor interface and the order that NetworkListener uses it are below:

Interface Order Description
int MaxBufferSize { get; } Max buffer size for byte arrays and received data
void Initialize(EndPoint remoteEndPoint); 1 Initialize the network client data processor. This is called before the listener starts processing client data.
bool ReceiveBytes(in byte[] bytes, in int received, in int iteration); 2 ... Receive bytes from the client socket. Called until no more bytes are available to process.
object? GetData(); 3 Get data from all received bytes gathered during previous step. Compile all the bytes you received prior here.
void ProcessData(object? data); 4 Process the data received from the client.
bool SendBytes(out byte[] bytes, in int sent, in int iteration); 5, back to 2 Send bytes to the client socket. This is where you respond to the client if needed- send null or 0 length array to not send a response.

There are several abstract classes, which implment INetworkClientDataProcessor, that define basic ways of processing client data:

  • MessageNetworkClientDataProcessor - Simple message which may contain end of read flags/marker.
  • MllpNetworkClientDataProcessor - Minimal Lower Layer Protocol, which is a common method of network communication that wrap a messages; derrives from MessageNetworkClientDataProcessor

NetworkListenerBuilder Class

The library comes with a builder class that will assist you in building a NetworkListener instance called, NetworkListenerBuilder. Call the Create() method to get a new instance of the builder itself, see below:

// Create network listener builder; you can inject logging here in the Create() static method
var netListenerBuilder = NetworkListenerBuilder.Create()
    .UsingPort(9373)
    .UsingNcdpFactory(() =>
    {
        // Custom simple client data processor based on MessageNetworkClientDataProcessor
        return new SimpleMessageNetworkClientDataProcessor();
    });

// Build listener; default streaming TCP listener 
var netListener = netListenerBuilder.Build();

The port to listen on and a factory function to generate your network client data processor is required before Build() can be called. The factory function is called for every client that connects to provide the client thread with the appropriate client data processor instance to process the client's data.

SSL/TLS Support

To handle secured communications you must provide a host name and a certificate that contains the host name (other configurations like DNS, or host name with IP use, is beyond this document). You provide the NetworkListener object this information with the builder, see example below.

// Create network listener builder; you can inject logging here int he Create() static method
var netListenerBuilder = NetworkListenerBuilder.Create()
    .UsingPort(9373)
    .UsingNcdpFactory(() =>
    {
        // Custom simple client data processor based on MessageNetworkClientDataProcessor
        return new SimpleMessageNetworkClientDataProcessor();
    });

// Load cert and provide to builder; will use host system SSL/TLS versions and cyphers
var cert = LoadX509Certificate();
if (cert is not null)
{
    netListenerBuilder.WithCert(cert);
    netListenerBuilder.WithHostName("test.my-host.com");
}

// Build listener
var netListener = netListenerBuilder
    .WithSocketType(System.Net.Sockets.SocketType.Stream)
    .WithProtocol(System.Net.Sockets.ProtocolType.Tcp)
    .WithMaxClientConnections(1000)
    .Build();
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-rc6 75 5/23/2023
1.0.0-rc5 71 5/17/2023