first commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Netty.Common.Model;
|
||||
using JSMachine.WMS.Netty.Common.Util;
|
||||
using JSMachine.WMS.Netty.NettyAsClient.Command;
|
||||
using JSMachine.WMS.Netty.NettyAsClient.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;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyAsClient.Transport
|
||||
{
|
||||
public class NettyClientTransport
|
||||
{
|
||||
private IChannelHandlerContext ChannelHandlerContext;
|
||||
private IPInfo RemoteIpInfo;
|
||||
private TcpEngineInitParam _tcpEngineInitParam;
|
||||
|
||||
[ImportMany("NettyAsClient")]
|
||||
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 NettyClientTransport(IChannelHandlerContext channel, TcpEngineInitParam tcpEngineInitParam)
|
||||
{
|
||||
_tcpEngineInitParam= tcpEngineInitParam;
|
||||
|
||||
_Container.Value.ComposeParts(this);
|
||||
|
||||
ChannelHandlerContext = channel;
|
||||
|
||||
foreach (BaseCmdhandler cmd in AllCmds)
|
||||
{
|
||||
cmd.Channel = channel;
|
||||
}
|
||||
|
||||
RemoteIpInfo = channel.ToIPInfo(AddressOnwer.Remote);
|
||||
}
|
||||
|
||||
public Task ChannelReadHandle(IByteBuffer byteBuffer)
|
||||
{
|
||||
string msg = byteBuffer.ToString(Encoding.UTF8);
|
||||
|
||||
//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)
|
||||
{
|
||||
BaseCmd baseCmd;
|
||||
if (msg.Length < _tcpEngineInitParam.MinCodeLength)
|
||||
baseCmd = new BarCodeReadExceptionCmd() { Msg = msg };
|
||||
else
|
||||
{
|
||||
if (_tcpEngineInitParam.DeviceType != DeviceType.PenmoPrint)
|
||||
baseCmd = new BarCodeUploadCmd() { Msg = msg };
|
||||
else
|
||||
baseCmd = new PenmoReadCmd() { Msg = msg };
|
||||
//baseCmd = new BarCodeUploadCmd() { Msg = msg };
|
||||
}
|
||||
bool isMsgHasBeenHandled = await cmd.Handle(baseCmd);
|
||||
if (isMsgHasBeenHandled)
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
using DotNetty.Transport.Channels;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Netty.NettyAsClient.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)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!DicClientChannels.ContainsKey(ip))
|
||||
{
|
||||
LogHelper.Error($"{ip}连接失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
IChannelHandlerContext channel = DicClientChannels[ip];
|
||||
|
||||
await channel.Channel.WriteAndFlushAsync(msg);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error($"发送消息出错: {ex.Message} \n {ex.StackTrace}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定客户端发送byte数组
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <returns></returns>
|
||||
public async static Task<bool> SendMsgByte(string ip, byte[] msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!DicClientChannels.ContainsKey(ip))
|
||||
{
|
||||
LogHelper.Error($"{ip}连接失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
IChannelHandlerContext channel = DicClientChannels[ip];
|
||||
|
||||
await channel.Channel.WriteAndFlushAsync(msg);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error($"发送消息出错: {ex.Message} \n {ex.StackTrace}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送广播消息
|
||||
/// </summary>
|
||||
/// <param name="messagePackage"></param>
|
||||
public static void BroadcastMsg(string brodCastMsg)
|
||||
{
|
||||
try
|
||||
{
|
||||
DicClientChannels.Keys.ToList().ForEach(async key =>
|
||||
{
|
||||
await DicClientChannels[key].Channel.WriteAndFlushAsync(brodCastMsg);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error($"发送广播消息出错: {ex.Message} \n {ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user