ProAndPro.Util.NetworkHelper 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ProAndPro.Util.NetworkHelper --version 1.0.0
                    
NuGet\Install-Package ProAndPro.Util.NetworkHelper -Version 1.0.0
                    
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="ProAndPro.Util.NetworkHelper" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ProAndPro.Util.NetworkHelper" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ProAndPro.Util.NetworkHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ProAndPro.Util.NetworkHelper --version 1.0.0
                    
#r "nuget: ProAndPro.Util.NetworkHelper, 1.0.0"
                    
#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.
#:package ProAndPro.Util.NetworkHelper@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ProAndPro.Util.NetworkHelper&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ProAndPro.Util.NetworkHelper&version=1.0.0
                    
Install as a Cake Tool

Network helper

Introduction

NetworkHelper is a library created to simplify a connection based on network stream in a thread safe context.

The library will allow you to create a client (to connect to a server) or a server (to connect to a client). In the next section we show the preliminary operations that can be used in both cases.

Preliminary operations

  1. We need to define a class that manage the information exchanged between client-server. Only for exemple we try implement this class:
public  class Information{ 
  
  public string message {get; set;}
  public string details {get; set;} 

  public Information(string message, string details){
    this.message = message;
    this.details = details;
  }
}
  1. Now we need to define how to format the message which will be used for information exchange between client-server. To do this we need to implement IParser<T> interface. This Interface contain two methods used to serialize and deserialize information. Only for exemple we try to build the class :
 public class Parser : IParser<Information>{

    public  Information Deserializes(string objectTODeserialize){

        // here define how to deserialize the object
    }

    public string Serialize(Information objectTOSerialize){
           // here define how to serialize the object
    }
  1. (Optional): if we want to use a custom logger ( in order to take track server/client activities) is necessary create instance of a class that implement ILogger interface (only for this exemple call this class MyPersonalLog).

Client implementation

Now we can define how to correctly start a client listener.

  1. Instantiate the following classes:

    Information.

    Parser.

    MyPersonalLog (Optional)

  2. Now we are ready to Instantiate the class ConcreteClientListener<Information>. There is two way to use this class: one using your personal logger (in the example above the logger is called: MyPersonalLog) two using default logger and passing only the directory path and the file name (.txt).

// with personal logger:

ConcreteClientListener<Information> clientListener = ConcreteClientListener<Information>(serverIp, serverPort, parser,logger,maxReconnectionAttempt,deleyReconnectionAttempt)

//with default logger:

ConcreteClientListener<Information> clientListener = ConcreteClientListener<Information>(serverIp, serverPort, parser,maxReconnectionAttempt,deleyReconnectionAttempt, completeDirectoryPath, filename)
  1. Now we can sign in some events to get information during communication.
 clientListener.errorEvent += errorManagment;
 clientListener.sendDataEvent += sentOKManagment;
 clientListener.recivedDataEvent += recivedManagment;
 clientListener.noServerFoundEvent += noServerConnectedManagment;

 // methods 
  public static void errorManagment(Object sender, CommunicationErrorEventArgs e){
      // a method that manage error during clientListener usage    
  }

  public static void sentOKManagment(Object sender, CommunicationDataEventArgs<string> e){
    // a method that comunicate the message parsed that client sent to server   
  }

  public static void recivedManagment(Object sender, CommunicationDataEventArgs<Information> e){
   // a method that manage received data during clientListener usage  
  } 

  public void noServerConnectedManagment(Object sender, EventArgs e){
    // a method to notify that client can't connect/reconnect to server
  }

  1. Last stap need to implement to IObserver<bool> interface (in class that contain the instance of ConcreteClientListener) to observe the connection status with the server.

  2. Now we can start client.

clientListener.Start();
  1. To send message you can use the method clientListener.SendMessageToClient(info) where info is an instance of Information class.

  2. To stop client you can use the method 'clientListener.Stop()`.

Server implementation

In this section we can define how to correctly start a server listner, do first preliminary operations (read the top of document).

  1. Instantiate the following classes:

    Information.

    Parser.

    MyPersonalLog (Optional)

  2. Now we are ready to Instantiate the class ConcreteServerListener<Information>. There is two way to use this class: one using your personal logger (in the example above the logger is called: MyPersonalLog) two using default logger and passing only the directory path and the file name (.txt).

// with personal logger:

ConcreteServerListener<Information> servertListener = ConcreteServerListener<Information>(ClientMultiplicity.MULTIPLE_CLIENT, serverIp, serverPort, parser, logger)

//with default logger:

ConcreteServerListener<Information> servertListener = ConcreteServerListener<Information>(client allowed, serverIp, serverPort, parser, completeDirectoryPath, filename)

//NOTE client allowed can be
 1. ClientMultiplicity.MULTIPLE_CLIENT //--> to accept infinite client.
 2. ClientMultiplicity.SINGLE_CLIENT  //--> to accept only one client 

  1. Now we can sign in some events to get information during communication.
 servertListener.errorEvent += errorManagment;
 servertListener.sendDataEvent += sentOKManagment;
 servertListener.recivedDataEvent += recivedManagment;
 servertListener.noClientFoundEvent += noClientConnectedManagment;

 // methods 
  public static void errorManagment(Object sender, CommunicationErrorEventArgs e){
      // a method that manage error during servertListener usage    
  }

  public static void sentOKManagment(Object sender, CommunicationDataEventArgs<string> e){
    // a method that comunicate the message parsed that server sent to server   
  }

  public static void recivedManagment(Object sender, CommunicationDataEventArgs<Information> e){
   // a method that manage received data during servertListener usage  
  } 

  public static void noClientConnectedManagment(Object sender, EventArgs e){
    // a method that notify that server correctly sent message but there is no client connected
  }
 
  1. Last stap need to implement to IObserver<String> interface (in class that contain the instance of ConcreteServerListener) to observe the number of client connected.

  2. Now we can start server.

serverListener.Start();

  1. To send message you can use the method serverListener.SendMessageToClient(info) where info is an instance of Information class.

  2. To stop server you can use the method 'serverListener.Stop()`.

Product Compatible and additional computed target framework versions.
.NET Framework net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7

    • No dependencies.

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.1 407 12/2/2022
1.0.0 392 11/11/2022

Firs implementation of library to quick and easy connect to server/client listener used in togheter project (Pro&Pro)