70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
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;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|