DotNetty.Wraper 0.7.2

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

// Install DotNetty.Wraper as a Cake Tool
#tool nuget:?package=DotNetty.Wraper&version=0.7.2                

DotNetty.Wraper

Description

将微软的Azure/DotNetty 代码重新整理,支持netstandard2.0;net472;net45,可以直接在各个 版本中使用。 并将多个类库合并成到一个项目中,加了些封装类,开箱即用。

封装类的示例:

Client

using DotNetty.Wraper;
using Examples.Common;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace TcpSocket.Client
{
    class Program
    {
        static async Task Main(string[] args)
        {
            ILogger _logger = new CNative.Logging.ConfigLogger();// LoggingHelper.Logger;
            X509Certificate2 cert = null;
            if (ClientSettings.IsSsl)
            {
                cert = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
            }

            var theClientBuild = SocketBuilderFactory.GetTcpSocketClientBuilder(new IPEndPoint(Examples.Common.ClientSettings.Host, Examples.Common.ClientSettings.Port));
          
            theClientBuild
                //.AddLast("framing-enc", new DotNetty.Codecs.LengthFieldPrepender(2))
                //.AddLast("framing-dec", new DotNetty.Codecs.LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2))
  
                .OnChannelRegistered((client, channel) =>
                {
                    _logger.LogDebug("OnChannelRegistered: " + channel.Id);
                })
                .OnChannelRegistered((client, channel) =>
                {
                    _logger.LogDebug("OnChannelRegistered: " + channel.Id);
                })
                .OnClientStarted(client =>
                {
                    _logger.LogInformation($"客户端启动");
                    client.Send("Hello world" + DateTime.Now);
                })
                .OnClientClose(client =>
                {
                    _logger.LogInformation($"客户端关闭");
                })
                .OnException(ex =>
                {
                    _logger.LogError($"异常:{ex.Message}");
                })
                .OnRecieve((client, bytes) =>
                {
                    _logger.LogInformation($"客户端:收到数据:{Encoding.UTF8.GetString(bytes)}");
                    client.Send("Hello world " + DateTime.Now);
                })
                .OnSend((client, bytes) =>
                {
                    _logger.LogInformation($"客户端:发送数据:{Encoding.UTF8.GetString(bytes)}");
                });

            var theClient = await theClientBuild .BuildAsync(null, (pipeline) =>
            {
                pipeline.AddLast("framing-enc", new DotNetty.Codecs.LengthFieldPrepender(2));
                pipeline.AddLast("framing-dec", new DotNetty.Codecs.LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
            }, ClientSettings.UseLibuv, cert);
                //.BuildAsync((tcpCleint) => { return new EchoClientHandler(tcpCleint as IChannelEvent); });

            //while (true)
            //{
            //    await theClient.Send(Guid.NewGuid().ToString());

            //    await Task.Delay(1000);
            //}
            Console.ReadLine();

            theClient?.Close();
        }
    }
}

Server


using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

using DotNetty.Wraper;
using Examples.Common;
using Microsoft.Extensions.Logging;

namespace TcpSocket.Server
{
    class Program
    {
        static async Task Main(string[] args)
        {
            ILogger _logger = new CNative.Logging.ConfigLogger();// LoggingHelper.Logger;

            X509Certificate2 tlsCertificate = null;
            if (ServerSettings.IsSsl)
            {
                tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
            }

            var theServerBuild = SocketBuilderFactory.GetTcpSocketServerBuilder(Examples.Common.ServerSettings.Port);
            theServerBuild
             //.AddLast(new DotNetty.Handlers.Logging.LoggingHandler("SRV-CONN"))
             //.AddLast("framing-enc", new DotNetty.Codecs.LengthFieldPrepender(2))
             //.AddLast("framing-dec", new DotNetty.Codecs.LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2))
             .OnConnectionClose((server, connection) =>
             {
                 _logger.LogInformation($"连接关闭,连接名[{connection.ConnectionName}],当前连接数:{server.GetConnectionCount()}");
             })
             .OnException(ex =>
             {
                 _logger.LogError($"服务端异常:{ex.Message}");
             })
             .OnChannelRegistered((server, channel) =>
             {
                 _logger.LogDebug("ChannelUnregistered: " + channel.Id);
             })
             .OnChannelRegistered((server, channel) =>
             {
                 _logger.LogDebug("ChannelRegistered: " + channel.Id);
             })
             .OnNewConnection((server, connection) =>
             {
                 connection.ConnectionName = $"名字{connection.ConnectionId}";
                 _logger.LogInformation($"新的连接[{connection.ClientAddress.Address.MapToIPv4().ToString()}]:{connection.ConnectionName},当前连接数:{server.GetConnectionCount()}");
             })
             .OnRecieve((server, connection, bytes) =>
             {
                 _logger.LogInformation($"服务端:数据{Encoding.UTF8.GetString(bytes)}");
                 connection.Send(bytes);
             })
             .OnSend((server, connection, bytes) =>
             {
                 _logger.LogInformation($"向连接名[{connection.ConnectionName}]发送数据:{Encoding.UTF8.GetString(bytes)}");
             })
             .OnServerStarted(server =>
             {
                 _logger.LogInformation($"服务启动");
             });
            var theServer = await theServerBuild.BuildAsync(null, (pipeline) =>
            {
                pipeline.AddLast(new DotNetty.Handlers.Logging.LoggingHandler("SRV-CONN"));
                pipeline.AddLast("framing-enc", new DotNetty.Codecs.LengthFieldPrepender(2));
                pipeline.AddLast("framing-dec", new DotNetty.Codecs.LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
            }, ServerSettings.UseLibuv, tlsCertificate, 100, 1);
            //.BuildAsync((tcpServer) => { return new EchoServerHandler(tcpServer as IChannelEvent, ServerSettings.UseLibuv, 100, 1); });

            Console.ReadLine();

            theServer?.Close();
        }
    }
}

DotNetty Project

Join the chat at https://gitter.im/Azure/DotNetty Available on NuGet https://www.nuget.org/packages?q=DotNetty.Wraper AppVeyor

DotNetty.Wraper is a port of Netty, asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.

Use

  • Official releases are on NuGet.
  • Nightly builds are available on MyGet.

Contribute

We gladly accept community contributions.

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 net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  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
0.7.31 952 5/19/2022
0.7.3 875 5/15/2022
0.7.2 853 5/15/2022