first commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.Command
|
||||
{
|
||||
public class BaseCmd : IBaseCmd
|
||||
{
|
||||
public virtual CommandType ComType { get; set; }
|
||||
}
|
||||
|
||||
public enum CommandType
|
||||
{
|
||||
/// <summary>
|
||||
/// 心跳包
|
||||
/// </summary>
|
||||
g_eKeepAlive = 1,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.Command
|
||||
{
|
||||
/// <summary>
|
||||
/// 心跳
|
||||
/// </summary>
|
||||
public class HeartBeartCmd : BaseCmd
|
||||
{
|
||||
public override CommandType ComType { get; set; } = CommandType.g_eKeepAlive;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.Command
|
||||
{
|
||||
public interface IBaseCmd
|
||||
{
|
||||
CommandType ComType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Netty.Common.Util;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.CommandHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 处理基类
|
||||
/// </summary>
|
||||
[InheritedExport("NettyUdp")]
|
||||
public abstract class BaseCmdhandler
|
||||
{
|
||||
public IChannelHandlerContext Channel { get; set; }
|
||||
public IPInfo RemoteIPInfo => Channel.ToIPInfo(AddressOnwer.Remote);
|
||||
public abstract Task<bool> Handle(string msg);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using JSMachine.WMS.Netty.NettyUdp.Command;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.CommandHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 命令处理器
|
||||
/// </summary>
|
||||
public class ShipDynamicsMsgHandler : BaseCmdhandler
|
||||
{
|
||||
public async override Task<bool> Handle(string msg)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using DotNetty.Transport.Channels;
|
||||
using DotNetty.Transport.Channels.Sockets;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Netty.NettyUdp.Transport;
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp
|
||||
{
|
||||
public class EchoClientChannelHandler : SimpleChannelInboundHandler<DatagramPacket>
|
||||
{
|
||||
public static NettyUdpTransport NettyTransport;
|
||||
|
||||
public override void ChannelActive(IChannelHandlerContext context)
|
||||
{
|
||||
NettyTransport = new NettyUdpTransport(context);
|
||||
base.ChannelActive(context);
|
||||
}
|
||||
|
||||
public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
|
||||
|
||||
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
|
||||
{
|
||||
LogHelper.Error($"JSMachine.WMS.Netty.NettyUdp.EchoClientChannelHandler.ChannelReadComplete 中发生异常,异常信息 {exception.Message}\n{exception.StackTrace}");
|
||||
|
||||
context.CloseAsync();
|
||||
}
|
||||
|
||||
public override void HandlerAdded(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerAdded(context);
|
||||
}
|
||||
|
||||
public override void HandlerRemoved(IChannelHandlerContext context)
|
||||
{
|
||||
Console.WriteLine($"服务端{context}下线.");
|
||||
base.HandlerRemoved(context);
|
||||
}
|
||||
|
||||
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket msg)
|
||||
{
|
||||
string msgStr = msg.Content.ToString(Encoding.UTF8);
|
||||
NettyTransport.ChannelReadHandle(msgStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Netty.Common.Util;
|
||||
using JSMachine.WMS.Netty.NettyUdp.Command;
|
||||
using JSMachine.WMS.Netty.NettyUdp.CommandHandler;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.ComponentModel.Composition.Hosting;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp.Transport
|
||||
{
|
||||
public class NettyUdpTransport
|
||||
{
|
||||
public IChannelHandlerContext ChannelHandlerContext;
|
||||
public IPInfo RemoteIpInfo;
|
||||
|
||||
[ImportMany("NettyUdp")]
|
||||
private List<BaseCmdhandler> AllCmds { get; set; }
|
||||
|
||||
private Lazy<CompositionContainer> _Container = new Lazy<CompositionContainer>(() =>
|
||||
{
|
||||
AggregateCatalog catalog = new();
|
||||
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
||||
|
||||
return new CompositionContainer(catalog);
|
||||
});
|
||||
|
||||
public NettyUdpTransport(IChannelHandlerContext channel)
|
||||
{
|
||||
_Container.Value.ComposeParts(this);
|
||||
|
||||
ChannelHandlerContext = channel;
|
||||
|
||||
foreach (BaseCmdhandler cmd in AllCmds)
|
||||
{
|
||||
cmd.Channel = channel;
|
||||
}
|
||||
|
||||
RemoteIpInfo = channel.ToIPInfo(AddressOnwer.Remote);
|
||||
}
|
||||
|
||||
public Task ChannelReadHandle(string msg)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
foreach (BaseCmdhandler cmd in AllCmds)
|
||||
{
|
||||
bool isMsgHasBeenHandled = await cmd.Handle(msg);
|
||||
if (isMsgHasBeenHandled)
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<bool> WriteMsg(BaseCmd msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ChannelHandlerContext.Channel.Active)
|
||||
{
|
||||
await ChannelHandlerContext.Channel.WriteAndFlushAsync(msg);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error($"向TCP通道写入数据失败,错误信息 {ex.Message}");
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using DotNetty.Transport.Bootstrapping;
|
||||
using DotNetty.Transport.Channels;
|
||||
using DotNetty.Transport.Channels.Sockets;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using System;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyUdp
|
||||
{
|
||||
public class UdpEngine
|
||||
{
|
||||
public async void InitEngine()
|
||||
{
|
||||
try
|
||||
{
|
||||
MultithreadEventLoopGroup group = new();
|
||||
Bootstrap bootstrap = new();
|
||||
bootstrap
|
||||
.Group(group)
|
||||
.Channel<SocketDatagramChannel>() //UDP连接方式
|
||||
.Option(ChannelOption.SoBroadcast, true) //广播形式获取数据
|
||||
.Option(ChannelOption.SoReuseaddr, true) //可以复用端口号
|
||||
.Handler(new EchoClientChannelHandler());
|
||||
await bootstrap.BindAsync(503); //绑定本地端口号开始监听
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error($"UDP连接失败,错误信息 {ex.Message}\n{ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user