You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.6 KiB

8 months ago
using System.Reflection;
using Autofac;
6 months ago
using Autofac.Core;
8 months ago
using DotNetty.Codecs;
8 months ago
using DotNetty.Handlers.Logging;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
8 months ago
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
8 months ago
using log4net;
7 months ago
namespace HybirdFrameworkDriver.TcpServer
8 months ago
{
/// <summary>
/// netty server
/// </summary>
6 months ago
public class TcpServer <TH, TD, TE> : IDisposable where TH : IChannelHandler where TD: ByteToMessageDecoder,new() where TE: ChannelHandlerAdapter, new()
8 months ago
{
6 months ago
private static readonly ILog Log = LogManager.GetLogger(typeof(TcpServer<TH, TD, TE>));
8 months ago
static MultithreadEventLoopGroup? bossGroup;
static MultithreadEventLoopGroup? workerGroup;
static ServerBootstrap? bootstrap;
private int _port = 9000;
6 months ago
public TcpServer()
8 months ago
{
bossGroup = new MultithreadEventLoopGroup();
workerGroup = new MultithreadEventLoopGroup();
bootstrap = new ServerBootstrap();
bootstrap
.Group(bossGroup, workerGroup) // 设置主和工作线程组
.Channel<TcpServerSocketChannel>() // 设置通道模式为TcpSocket
.Option(ChannelOption.SoKeepalive, true) //保持连接
.Handler(new LoggingHandler())
.ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
6 months ago
var serverListenerHandler = new ServerListenerHandler<TH, TD, TE>();
8 months ago
IChannelPipeline pipeline = channel.Pipeline;
pipeline.AddLast(new LoggingHandler(""));
pipeline.AddLast(serverListenerHandler);
8 months ago
pipeline.AddLast(new IdleStateHandler(0, 0, 180)); //检测空闲连接
//业务handler 这里是实际处理业务的Handler
8 months ago
ResolveEncode(pipeline);
ResolveDecode(pipeline);
ResolveHandler(pipeline);
8 months ago
}));
}
8 months ago
private void ResolveEncode(IChannelPipeline pipeline)
{
6 months ago
pipeline.AddLast(new TE());
8 months ago
}
private void ResolveDecode(IChannelPipeline pipeline)
{
6 months ago
pipeline.AddLast(new TD());
8 months ago
}
private void ResolveHandler(IChannelPipeline pipeline)
{
6 months ago
List<Type> list = new List<Type>();
foreach (IComponentRegistration reg in AppInfo.Container.ComponentRegistry.Registrations)
{
foreach (Service service in reg.Services)
{
if (service is TypedService ts)
{
if (MatchHandlers(ts))
{
list.Add(ts.ServiceType);
}
}
}
}
List<TH> handlers = new List<TH>();
foreach (var type in list)
{
object resolve = AppInfo.Container.Resolve(type);
handlers.Add((TH) resolve);
}
handlers.Sort((handler, msgHandler) =>
8 months ago
{
OrderAttribute? orderAttribute1 = handler.GetType().GetCustomAttribute<OrderAttribute>();
OrderAttribute? orderAttribute2 = msgHandler.GetType().GetCustomAttribute<OrderAttribute>();
int h1Order = orderAttribute1?.Order ?? 0;
int h2Order = orderAttribute2?.Order ?? 0;
return h1Order.CompareTo(h2Order);
});
6 months ago
foreach (var msgHandler in handlers)
8 months ago
{
6 months ago
pipeline.AddLast((IChannelHandler)msgHandler);
8 months ago
}
}
6 months ago
private bool MatchHandlers(TypedService ts)
{
Type[] interfaces = ts.ServiceType.GetInterfaces();
if (interfaces.Length > 0)
{
foreach (Type type in interfaces)
{
if (type == typeof(TH))
{
return true;
}
}
}
8 months ago
6 months ago
return false;
}
8 months ago
8 months ago
public void Start(int port)
8 months ago
{
_port = port;
Log.Info(" Start Listen");
Task<IChannel>? channel = bootstrap?.BindAsync(_port);
Log.Info($"netty success listen {_port}");
}
/// <summary>
/// ??netty
/// </summary>
public void Dispose()
{
Log.Info(this + " Dispose");
bossGroup?.ShutdownGracefullyAsync();
}
}
8 months ago
}