using System.Reflection;
using Autofac;
using DotNetty.Codecs;
using DotNetty.Handlers.Logging;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
namespace HybirdFrameworkDriver.TcpServer
{
///
/// netty server
///
[Scope("InstancePerDependency")]
public class Server : IDisposable
{
private readonly ILog Log = LogManager.GetLogger(typeof(Server));
static MultithreadEventLoopGroup? bossGroup;
static MultithreadEventLoopGroup? workerGroup;
static ServerBootstrap? bootstrap;
private int _port = 9000;
public Server()
{
bossGroup = new MultithreadEventLoopGroup();
workerGroup = new MultithreadEventLoopGroup();
bootstrap = new ServerBootstrap();
bootstrap
.Group(bossGroup, workerGroup) // 设置主和工作线程组
.Channel() // 设置通道模式为TcpSocket
.Option(ChannelOption.SoKeepalive, true) //保持连接
.Handler(new LoggingHandler())
.ChildHandler(new ActionChannelInitializer(channel =>
{
var serverListenerHandler = new ServerListenerHandler();
IChannelPipeline pipeline = channel.Pipeline;
pipeline.AddLast(new LoggingHandler(""));
pipeline.AddLast(serverListenerHandler);
pipeline.AddLast(new IdleStateHandler(0, 0, 180)); //检测空闲连接
//业务handler ,这里是实际处理业务的Handler
ResolveEncode(pipeline);
ResolveDecode(pipeline);
ResolveHandler(pipeline);
}));
}
private void ResolveEncode(IChannelPipeline pipeline)
{
IEncoder resolve = AppInfo.Container.Resolve();
pipeline.AddLast((MessageToByteEncoder)resolve);
}
private void ResolveDecode(IChannelPipeline pipeline)
{
IDecoder resolve = AppInfo.Container.Resolve();
pipeline.AddLast((ByteToMessageDecoder)resolve);
}
private void ResolveHandler(IChannelPipeline pipeline)
{
IEnumerable handlers = AppInfo.Container.Resolve>();
IMsgHandler[] msgHandlers = handlers.ToArray();
Array.Sort(msgHandlers, (handler, msgHandler) =>
{
OrderAttribute? orderAttribute1 = handler.GetType().GetCustomAttribute();
OrderAttribute? orderAttribute2 = msgHandler.GetType().GetCustomAttribute();
int h1Order = orderAttribute1?.Order ?? 0;
int h2Order = orderAttribute2?.Order ?? 0;
return h1Order.CompareTo(h2Order);
});
foreach (var msgHandler in msgHandlers)
{
pipeline.AddLast((ChannelHandlerAdapter)msgHandler);
}
}
public void Start(int port)
{
_port = port;
Log.Info(" Start Listen");
Task? channel = bootstrap?.BindAsync(_port);
Log.Info($"netty success listen {_port}");
}
///
/// ??netty
///
public void Dispose()
{
Log.Info(this + " Dispose");
bossGroup?.ShutdownGracefullyAsync();
}
}
}