first commit

This commit is contained in:
2026-09-02 16:31:50 +08:00
commit a461727193
911 changed files with 692450 additions and 0 deletions
@@ -0,0 +1,69 @@
using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using JSMachine.WMS.Netty.Common.Util;
using JSMachine.WMS.Netty.NettyAsServer.Command;
using JSMachine.WMS.Netty.NettyAsServer.CommandHandler;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Netty.NettyAsServer.Transport
{
public class NettyServerTransport
{
public IChannelHandlerContext ChannelHandlerContext;
public IPInfo RemoteIpInfo;
[ImportMany("NettyAsServer")]
private List<BaseCmdhandler> AllCmds { get; set; }
private Lazy<CompositionContainer> _Container = new(() =>
{
AggregateCatalog catalog = new();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
return new CompositionContainer(catalog);
});
public NettyServerTransport(IChannelHandlerContext channel)
{
_Container.Value.ComposeParts(this);
ChannelHandlerContext = channel;
foreach (BaseCmdhandler cmd in AllCmds)
{
cmd.Channel = channel;
}
RemoteIpInfo = channel.ToIPInfo(AddressOnwer.Remote);
if (TransportManager.DicClientChannels.ContainsKey(RemoteIpInfo.IP))
TransportManager.DicClientChannels.TryRemove(RemoteIpInfo.IP,out IChannelHandlerContext channelHandlerContext);
TransportManager.DicClientChannels.TryAdd(RemoteIpInfo.IP, channel);
}
public Task ChannelReadHandle(IByteBuffer byteBuffer)
{
int length = byteBuffer.ReadableBytes;
byte[] array = new byte[length];
byteBuffer.GetBytes(byteBuffer.ReaderIndex, array);
string msg = Encoding.UTF8.GetString(array);
return Task.Run(async () =>
{
foreach (BaseCmdhandler cmd in AllCmds)
{
bool isMsgHasBeenHandled = await cmd.Handle(new BarCodeUploadCmd { Msg = msg });
if (isMsgHasBeenHandled)
break;
}
});
}
}
}
@@ -0,0 +1,132 @@
using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using JSMachine.WMS.Common.Cache;
using JSMachine.WMS.Common.Cache.Model;
using JSMachine.WMS.Infrastructure.Helper;
using JSMachine.WMS.Netty.NettyAsServer.Command;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace JSMachine.WMS.Netty.NettyAsServer.Transport
{
public static class TransportManager
{
/// <summary>
/// 已连接的客户端
/// key-ip 一个客户端仅允许一个连接
/// </summary>
public static readonly ConcurrentDictionary<string, IChannelHandlerContext> DicClientChannels = new();
/// <summary>
/// 移除有问题通道
/// </summary>
/// <param name="channel"></param>
public static void RemoveInActiveChannel(IChannelHandlerContext channel)
{
try
{
DicClientChannels
.Where(p => p.Value == channel)
.Select(p => p.Key)
.ToList()
?.ForEach(p => DicClientChannels.Remove(p, out IChannelHandlerContext channelHandlerContext));
}
catch (Exception ex)
{
LogHelper.Error($"移除有问题通道出错: {ex.Message} \n {ex.StackTrace}");
}
}
/// <summary>
/// 指定客户端发送消息
/// </summary>
/// <param name="ip"></param>
/// <param name="msg"></param>
/// <returns></returns>
public async static Task<bool> SendMsg(string ip, string msg)
{
var result = false;
try
{
if (!DicClientChannels.ContainsKey(ip))
return result;
IChannelHandlerContext channel = DicClientChannels[ip];
await channel.Channel.WriteAndFlushAsync(msg);
result = true;
}
catch (Exception ex)
{
LogHelper.Error($"指定客户端发送消息出错: {ex.Message} \n {ex.StackTrace}");
}
return result;
}
/// <summary>
/// 发送广播消息
/// </summary>
/// <param name="messagePackage"></param>
public static void BroadcastMsg(IByteBuffer messagePackage)
{
try
{
DicClientChannels.Keys.ToList().ForEach(async key =>
{
await DicClientChannels[key].Channel.WriteAndFlushAsync(messagePackage);
});
}
catch (Exception ex)
{
LogHelper.Error($"发送广播消息出错: {ex.Message} \n {ex.StackTrace}");
}
}
/// <summary>
/// 发送扫码命令并获取返回结果
/// </summary>
/// <param name="ip">具体扫码器的IP</param>
/// <returns></returns>
public async static Task<BarCodeInfo> SendReadBarCodeCmd(string ip)
{
var barCodeInfo = new BarCodeInfo();
try
{
string msg = new BarCodeReadCmd().Msg;
bool bRet = await SendMsg(ip, msg);
if (!bRet)
{
LogHelper.Error($"向 {ip} 发送条码扫描命令失败");
return null;
}
DateTime sendTime = DateTime.Now;
int count = 0;
while (true)
{
count++;
Thread.Sleep(100);
barCodeInfo = GlobalMemoryCache.GetCach<string, BarCodeInfo>(ip);
if (barCodeInfo == null || barCodeInfo.AddTime < sendTime)
{
if (count < 20)
continue;
else
return null;
}
}
}
catch (Exception ex)
{
LogHelper.Error($"向 {ip} 发送条码扫描命令返回结果失败{ex.Message} \n {ex.StackTrace}");
}
return barCodeInfo;
}
}
}