ProAndPro.Util.NetworkHelper
1.0.0
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
<PackageReference Include="ProAndPro.Util.NetworkHelper" Version="1.0.0" />
<PackageVersion Include="ProAndPro.Util.NetworkHelper" Version="1.0.0" />
<PackageReference Include="ProAndPro.Util.NetworkHelper" />
paket add ProAndPro.Util.NetworkHelper --version 1.0.0
#r "nuget: ProAndPro.Util.NetworkHelper, 1.0.0"
#:package ProAndPro.Util.NetworkHelper@1.0.0
#addin nuget:?package=ProAndPro.Util.NetworkHelper&version=1.0.0
#tool nuget:?package=ProAndPro.Util.NetworkHelper&version=1.0.0
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
- 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;
}
}
- 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
}
- (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.
Instantiate the following classes:
Information.
Parser.
MyPersonalLog (Optional)
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)
- 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
}
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.Now we can start client.
clientListener.Start();
To send message you can use the method
clientListener.SendMessageToClient(info)
whereinfo
is an instance of Information class.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).
Instantiate the following classes:
Information.
Parser.
MyPersonalLog (Optional)
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
- 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
}
Last stap need to implement to
IObserver<String>
interface (in class that contain the instance of ConcreteServerListener) to observe the number of client connected.Now we can start server.
serverListener.Start();
To send message you can use the method
serverListener.SendMessageToClient(info)
where info is an instance of Information class.To stop server you can use the method 'serverListener.Stop()`.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.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.
Firs implementation of library to quick and easy connect to server/client listener used in togheter project (Pro&Pro)