ZayniFramework.Middle.TelnetService 2.2.3

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

// Install ZayniFramework.Middle.TelnetService as a Cake Tool
#tool nuget:?package=ZayniFramework.Middle.TelnetService&version=2.2.3                

Middle.TelnetService example.

The Middle.TelnetService and Common module provide the API of ICommand, ConsoleCommand and RemoteCommand for your application. You can use it to build a telnet service to receive the remote telnet command from any telent client very easily!

Add namespace using.

using ZayniFramework.Common;
using ZayniFramework.Middle.TelnetService;

Write your ConsoleCommand class and extends the ConsoleCommand base class.

public sealed class YourConsoleCommand : ConsoleCommand
{
    /// <summary>Execute the command.
    /// </summary>
    public override void ExecuteCommand() 
    {
        // Implement your code here...
        Result.Success = true;
    } 
}

Write your telnet RemoteCommand class and extends the RemoteCommand base class.

public class YourRemoteCommand : RemoteCommand
{
    /// <summary>Execute the command.
    /// </summary>
    public override void ExecuteCommand()
    {
        // Implement your code here...
        Result.Success = r.Success;
    }
}

Then, you have to register your ICommand object to the CommandContainer. Like this,

private static CommandContainer RegisterCommands()
{
    var commandContainer = new CommandContainer();
    commandContainer.RegisterCommand<YourConsoleCommand>( "the command name 1", commandText => 0 == string.Compare( commandText, "some command line", true ) );
    commandContainer.RegisterCommand<YourRemoteCommand>( "the command name 12", commandText => 0 == string.Compare( commandText, "some command line", true ) );
    commandContainer.RegisterCommand<ClearConsoleCommand>( "cls", commandText => 0 == string.Compare( commandText, "cls", true ) );
    commandContainer.RegisterUnknowCommand<UnknowRemoteCommand>();
    return commandContainer;
}

Start to process to receive the console command in your application. And create a TelnetServer, then start the telnet service.

private static void ProcessConsoleCommand( CommandContainer container )
{
    while ( true )
    {
        string commandText = null;

        try
        {
            commandText = Console.ReadLine();

            if ( commandText.IsNullOrEmpty() )
            {
                continue;
            }

            var command = container.Get<ConsoleCommand>( commandText );

            if ( command.IsNull() )
            {
                continue;
            }

            command.Command = commandText;
            command.Execute();    
        }
        catch ( Exception ex )
        {
            Logger.WriteExceptionLog( nameof ( Program ), ex, $"Process console command occur exception. Console command: {commandText}. {Environment.NewLine}{ex.ToString()}" );
            continue;
        }
    }
}

private static void StartTelnetService( CommandContainer commandContainer ) 
{
    if ( !ConfigManagement.AppSettings.ContainsKey( "TelnetServerPort" ) )
    {
        return;
    }

    var strPort = ConfigManagement.AppSettings[ "TelnetServerPort" ];

    if ( strPort.IsNullOrEmpty() || !int.TryParse( strPort, out int port ) || port <= 0 )
    {
        return;
    }

    var password      = ConfigManagement.AppSettings.ContainsKey( "TelnetServerPassword" ) ? ConfigManagement.AppSettings[ "TelnetServerPassword" ] : null;
    var telnetService = new TelnetServer( port, password );
    telnetService.StartService( ( reader, writer, client ) => ProcessTelnetCommand( reader, writer, client, commandContainer ) );
    Console.WriteLine( $"Start telnet server success. Listen tcp port on {strPort}." );
}

private static void ProcessTelnetCommand( StreamReader reader, StreamWriter writer, TcpClient tcpClient, CommandContainer container ) 
{
    if ( reader.IsNull() || writer.IsNull() || !tcpClient.Connected )
    {
        return;
    }

    if ( !reader.BaseStream.CanRead || !writer.BaseStream.CanWrite )
    {
        return;
    }

    while ( true ) 
    {
        try
        {
            if ( !reader.BaseStream.CanRead || !writer.BaseStream.CanWrite )
            {
                break;
            }

            var commandText = reader.ReadLine();
            var command     = container.Get<RemoteCommand>( commandText );

            if ( command.IsNull() )
            {
                continue;
            }

            command.Command      = commandText;
            command.StreamReader = reader;
            command.StreamWriter = writer;
            command.TelnetClient = tcpClient;    
            command.Execute();
        }
        catch ( Exception ex )
        {
            Logger.WriteExceptionLog( nameof ( Program ), ex, $"Process telnet command occur exception. {Environment.NewLine}{ex.ToString()}" );
            continue;
        }
    }
}

Finally, you can start the console command and telnet remote command service to receive the commands. Like this.

var commandContainer = RegisterCommands();
StartTelnetService( commandContainer );
ProcessConsoleCommand( commandContainer );

More detail and sample. You can go to the Test/ServiceHostTest/ServiceHost.Client.App directory.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
8.0.142 173 3/19/2024
8.0.141 176 1/4/2024
8.0.140 128 12/17/2023
6.0.138 123 3/19/2024
6.0.137 186 9/20/2023
6.0.136 158 9/14/2023
6.0.135 155 9/10/2023
6.0.134 177 7/12/2023
6.0.133 168 7/10/2023
6.0.132 172 7/7/2023
6.0.131 176 6/19/2023
6.0.130 176 6/19/2023
6.0.129 10,433 7/2/2022
6.0.128 552 1/16/2022
6.0.127 510 1/15/2022
6.0.126 482 1/15/2022
6.0.125 341 1/8/2022
6.0.125-hotfix1 217 1/8/2022
6.0.124 340 1/7/2022
6.0.123 441 11/14/2021
5.0.129 584 7/2/2022
5.0.128 540 1/16/2022
5.0.125 342 1/8/2022
5.0.124 331 1/7/2022
5.0.123 471 11/14/2021
5.0.122 522 10/11/2021
5.0.121 556 5/1/2021
3.1.137 149 9/20/2023
3.1.136 150 9/14/2023
3.1.135 143 9/10/2023
3.1.134 183 7/12/2023
3.1.133 171 7/11/2023
3.1.132 170 7/8/2023
3.1.131 185 6/19/2023
3.1.130 320 2/1/2023
3.1.129 528 7/2/2022
3.1.128 502 1/16/2022
3.1.125 307 1/8/2022
3.1.124 290 1/7/2022
3.1.123 459 11/14/2021
3.1.122 509 10/11/2021
3.1.121 514 5/1/2021
2.31.120 438 3/14/2021
2.30.115 3,303 3/6/2021
2.30.114 417 3/6/2021
2.20.101 1,115 3/1/2021
2.19.3 505 2/11/2021
2.19.2 10,082 2/6/2021
2.19.1 661 1/6/2021
2.19.0 633 1/1/2021
2.18.3 573 12/27/2020
2.18.2 1,339 8/29/2020
2.18.1 538 8/26/2020
2.18.0 4,811 8/20/2020
2.17.135 566 8/19/2020
2.17.134 597 7/28/2020
2.17.133 636 7/27/2020
2.17.132 631 7/18/2020
2.17.131 830 7/11/2020
2.16.130 766 6/13/2020
2.15.128 654 6/3/2020
2.15.127 634 5/31/2020
2.15.126 1,467 4/30/2020
2.14.122 637 4/13/2020
2.13.100 672 3/12/2020
2.12.51 702 2/18/2020
2.11.50 629 2/10/2020
2.10.44 684 1/30/2020
2.9.43 857 1/11/2020
2.8.42 672 1/10/2020
2.8.41 689 1/5/2020
2.7.40 749 1/2/2020
2.7.39 669 1/2/2020
2.7.38 741 1/1/2020
2.6.37 696 12/23/2019
2.6.35 873 12/4/2019
2.6.1 659 12/2/2019
2.6.0 635 11/28/2019
2.5.2 835 11/26/2019
2.5.1 754 11/12/2019
2.5.0 615 11/9/2019
2.4.3 806 10/16/2019
2.4.2 688 10/16/2019
2.4.1 807 9/20/2019
2.3.113 449 3/6/2021
2.3.112 461 3/6/2021
2.3.28 661 9/19/2019
2.3.27 852 8/30/2019
2.3.26 804 8/20/2019
2.3.25 679 8/12/2019
2.3.22 693 7/31/2019
2.3.21 862 7/20/2019
2.3.20 777 6/22/2019
2.3.19 672 6/14/2019
2.3.18 718 6/13/2019
2.3.17 699 6/13/2019
2.3.15 700 6/8/2019
2.3.14 675 6/8/2019
2.3.13 720 5/30/2019
2.3.12 622 5/24/2019
2.3.11 626 5/24/2019
2.3.10 627 5/21/2019
2.3.9 628 5/9/2019
2.3.8 624 5/8/2019
2.3.7 676 4/30/2019
2.3.6 741 4/23/2019
2.3.5 657 4/19/2019
2.3.4 623 4/18/2019
2.3.3 652 4/17/2019
2.3.2 649 4/6/2019
2.3.1 756 12/15/2018
2.3.0 712 12/7/2018
2.2.6 753 11/25/2018
2.2.5 757 11/23/2018
2.2.3 778 11/20/2018
2.2.2 779 11/19/2018
2.2.1 786 11/17/2018
1.1.0 838 11/15/2018
1.0.1 748 11/6/2018
1.0.0 800 11/4/2018