diff --git a/ConsoleStarter/ConsoleStarter.csproj b/ConsoleStarter/ConsoleStarter.csproj
index f422989..8ac72a1 100644
--- a/ConsoleStarter/ConsoleStarter.csproj
+++ b/ConsoleStarter/ConsoleStarter.csproj
@@ -15,6 +15,8 @@
+
+
diff --git a/ConsoleStarter/ExportDb.cs b/ConsoleStarter/ExportDb.cs
new file mode 100644
index 0000000..f2524ed
--- /dev/null
+++ b/ConsoleStarter/ExportDb.cs
@@ -0,0 +1,62 @@
+using log4net;
+using SqlSugar;
+
+namespace ConsoleStarter;
+
+public class ExportDb
+{
+ private static readonly ILog Log = LogManager.GetLogger(typeof(ExportDb));
+
+ private SqlSugarClient Db = new(new ConnectionConfig
+ {
+ ConnectionString =
+ "server=106.12.36.89;Database=chassis_track_swap0;Uid=remote_user;Pwd=Rszn123;Charset=utf8;",
+ DbType = DbType.MySql,
+ IsAutoCloseConnection = true,
+ InitKeyType = InitKeyType.Attribute
+ });
+
+ private static readonly string[] UsedTable = new[]
+ {
+ "t_bs_charging_bin_info",
+ "t_bs_cloud_charge_model_recv_record",
+ "t_bs_cloud_elec_price_recv_record",
+ "t_bs_eqm_fault_base_info",
+ "t_bs_net_cloud_param_info",
+ "t_bs_net_eqm_param_info",
+ "t_bs_station_config_info",
+ "t_bs_station_elec_price_info",
+ "t_bs_station_info",
+ "t_cb_amt_order_info",
+ "t_cb_station_order_batt_log_info",
+ "t_cb_station_order_sended_log",
+ "t_cb_station_order_state_log",
+ "t_fl_repaired_info",
+ "t_fl_un_repair_info",
+ "t_rm_charger_record_report",
+ "t_ss_authority_to_role",
+ "t_ss_button_info",
+ "t_ss_menu_info",
+ "t_ss_role_info",
+ "t_ss_user_info",
+ "t_ss_user_to_role"
+ };
+
+ public void Export()
+ {
+ List tableInfoList = Db.DbMaintenance.GetTableInfoList(false);
+ foreach (DbTableInfo tableInfo in tableInfoList)
+ {
+ if (UsedTable.Contains(tableInfo.Name))
+ {
+ Log.Info($"{tableInfo.Name}:{tableInfo.Description}");
+
+ List columnInfos = Db.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name, false);
+ foreach (DbColumnInfo columnInfo in columnInfos)
+ {
+ Log.Info($" {columnInfo.DbColumnName}:{columnInfo.ColumnDescription}");
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/ConsoleStarter/Program.cs b/ConsoleStarter/Program.cs
index 0b92eeb..689e389 100644
--- a/ConsoleStarter/Program.cs
+++ b/ConsoleStarter/Program.cs
@@ -1,49 +1,23 @@
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
-using System.IO.Pipes;
-using HslCommunication.Core;
+using ConsoleStarter;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.ModbusTcpMaster;
+using log4net.Config;
internal class Program
{
public static void Main(string[] args)
{
- ModbusTcpMaster master = new ModbusTcpMaster()
- {
- Ip = "192.168.1.5",
- //默认DataFormat.ABCD
- //DataFormat = DataFormat.ABCD,
- ReadAction = ReadFunc
- };
-
- bool connected = master.Connect();
- Debug.Assert(connected, "连接modbus server 失败");
-
- #region 测试写
- WaterCoolData coolData = new WaterCoolData();
-
- //写入ushort
- coolData.ushortType.Value = 18;
- bool writeResult4 = master.WriteValue(coolData.ushortType);
- Debug.Assert(writeResult4, "写入失败");
- //写入byte 0-8 byte[1]
- coolData.Status.Value = 12;
- bool writeResult1 = master.WriteValue(coolData.Status);
- Debug.Assert(writeResult1, "写入失败");
- //写入bit
- coolData.DraughtFan1.Value = true;
- bool writeResult3 = master.WriteValue(coolData.DraughtFan1);
- Debug.Assert(writeResult3, "写入失败");
+ XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.xml"));
+ ExportDb exportDb = new ExportDb();
+ exportDb.Export();
+ }
- coolData.Temperature1.Value = 12.7f;
- bool writeResult5 = master.WriteValue(coolData.Temperature1);
- Debug.Assert(writeResult5, "写入失败");
+ #region test
- #endregion 测试写
- }
private static WaterCoolData coolData = new WaterCoolData();
private static void ReadFunc(ModbusTcpMaster master)
{
@@ -156,6 +130,8 @@ internal class Program
Console.WriteLine(BitUtls.BytesToHexStr(bytes));
}
+
+ #endregion
}
public struct EncodeData
diff --git a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
index 52e89c1..60a7306 100644
--- a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
+++ b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
@@ -8,7 +8,7 @@ namespace HybirdFrameworkCore.Autofac
{
public class AutofacModuleRegister : Module
{
- private readonly ILog Log = LogManager.GetLogger(typeof(AutofacModuleRegister));
+ private static readonly ILog Log = LogManager.GetLogger(typeof(AutofacModuleRegister));
protected override void Load(ContainerBuilder builder)
{
@@ -68,7 +68,7 @@ namespace HybirdFrameworkCore.Autofac
if (defaultList.Count > 0)
{
- builder.RegisterTypes(defaultList.ToArray()).SingleInstance()
+ builder.RegisterTypes(defaultList.ToArray()).AsSelf().SingleInstance()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); //支持属性注入依赖重复
}
@@ -83,39 +83,27 @@ namespace HybirdFrameworkCore.Autofac
}
else
{
- Type? iInterface = type.GetInterface("HybirdFrameworkDriver.TcpServer.IMsgHandler");
- if (iInterface != null)
- {
- builder.RegisterType(type).As(iInterface).InstancePerDependency()
+ builder.RegisterType(type).InstancePerDependency()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
}
+ }
+ }
- iInterface = type.GetInterface("HybirdFrameworkDriver.TcpServer.IDecoder");
- if (iInterface != null)
+ if (instancePerLifetimeScopeList.Count > 0)
{
- builder.RegisterType(type).As(iInterface).InstancePerDependency()
- .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
+ builder.RegisterTypes(instancePerLifetimeScopeList.ToArray()).InstancePerMatchingLifetimeScope()
+ .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); //支持属性注入依赖重复
+ }
}
- iInterface = type.GetInterface("HybirdFrameworkDriver.TcpServer.IEncoder");
+ private void RegisterToInterface(String interfaceName, Type type, ContainerBuilder builder)
+ {
+ Type? iInterface = type.GetInterface(interfaceName);
if (iInterface != null)
{
builder.RegisterType(type).As(iInterface).InstancePerDependency()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
}
-
-
- builder.RegisterType(type).InstancePerDependency()
- .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
- }
- }
- }
-
- if (instancePerLifetimeScopeList.Count > 0)
- {
- builder.RegisterTypes(instancePerLifetimeScopeList.ToArray()).InstancePerMatchingLifetimeScope()
- .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); //支持属性注入依赖重复
- }
}
}
}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/Common/IToBytes.cs b/HybirdFrameworkDriver/Common/IToBytes.cs
new file mode 100644
index 0000000..8d13ce3
--- /dev/null
+++ b/HybirdFrameworkDriver/Common/IToBytes.cs
@@ -0,0 +1,6 @@
+namespace HybirdFrameworkDriver.Common;
+
+public interface IToBytes
+{
+ byte[] ToBytes();
+}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
index ef60ba5..cacd12e 100644
--- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
+++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
@@ -1,6 +1,4 @@
-using System;
-using System.Collections;
-using HslCommunication;
+using HslCommunication;
using HslCommunication.Core;
using HslCommunication.ModBus;
using HybirdFrameworkCore.Utils;
@@ -12,8 +10,8 @@ public class ModbusTcpMaster
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusTcpMaster));
- // public string Ip { get; set; } = "127.0.0.1";
- public string Ip { get; set; } = "192.168.1.5";
+ public string Ip { get; set; } = "127.0.0.1";
+
public int Port { get; set; } = 502;
public static DataFormat DataFormat { get; set; } = DataFormat.ABCD;
@@ -21,6 +19,8 @@ public class ModbusTcpMaster
public int Duration { get; set; } = 1000;
public bool Connected { get; set; } = false;
+ public string connectId { get; set; }
+
public delegate void MyReadAction(ModbusTcpMaster str);
public MyReadAction? ReadAction { get; set; }
@@ -29,7 +29,6 @@ public class ModbusTcpMaster
private ModbusTcpNet ModbusTcpNet;
-
ILog GetLog()
{
return Log;
@@ -46,6 +45,7 @@ public class ModbusTcpMaster
ModbusTcpNet = new ModbusTcpNet(Ip, Port);
ModbusTcpNet.DataFormat = DataFormat;
OperateResult result = ModbusTcpNet.ConnectServer();
+ connectId = ModbusTcpNet.ConnectionId;
if (result.IsSuccess)
{
Connected = true;
@@ -185,15 +185,16 @@ public class ModbusTcpMaster
else
preWriteCont[i - 1] = setValue[i];
}
+
break;
case DataFormat.BADC:
Array.Copy(setValue, preWriteCont, setValue.Length);
break;
}
+
operateResult = ModbusTcpNet.Write("x=16;" + (registerNo), preWriteCont);
result = operateResult.IsSuccess;
}
-
}
else if (setValue.Length == length * 2)
{
@@ -209,21 +210,26 @@ public class ModbusTcpMaster
else
preWriteCont[i - 1] = setValue[i];
}
+
break;
case DataFormat.BADC:
//Array.Copy(setValue, preWriteCont, setValue.Length);
break;
}
+
operateResult = ModbusTcpNet.Write("x=16;" + (registerNo), preWriteCont);
result = operateResult.IsSuccess;
}
+
break;
case ModbusDataType.Bit:
result = WriteRegisterOneBit(registerNo, start, Convert.ToBoolean(value));
break;
}
+
return result;
}
+
///
/// 写寄存器中的一个bit
///
@@ -253,6 +259,7 @@ public class ModbusTcpMaster
mask = (ushort)(~(1 << location));
registerValue1 &= mask;
}
+
OperateResult writeResult = ModbusTcpNet.Write("x=6;" + addr, registerValue1);
if (writeResult.IsSuccess)
{
@@ -260,7 +267,7 @@ public class ModbusTcpMaster
}
}
}
+
return result;
}
-
}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/ChannelUtils.cs b/HybirdFrameworkDriver/Session/ChannelUtils.cs
similarity index 94%
rename from HybirdFrameworkDriver/TcpServer/ChannelUtils.cs
rename to HybirdFrameworkDriver/Session/ChannelUtils.cs
index 0bdc145..d228136 100644
--- a/HybirdFrameworkDriver/TcpServer/ChannelUtils.cs
+++ b/HybirdFrameworkDriver/Session/ChannelUtils.cs
@@ -1,7 +1,7 @@
using DotNetty.Common.Utilities;
using DotNetty.Transport.Channels;
-namespace HybirdFrameworkDriver.TcpServer;
+namespace HybirdFrameworkDriver.Session;
public static class ChannelUtils
{
diff --git a/HybirdFrameworkDriver/TcpServer/IoSession.cs b/HybirdFrameworkDriver/Session/IoSession.cs
similarity index 61%
rename from HybirdFrameworkDriver/TcpServer/IoSession.cs
rename to HybirdFrameworkDriver/Session/IoSession.cs
index ef2a2d8..2cd6edb 100644
--- a/HybirdFrameworkDriver/TcpServer/IoSession.cs
+++ b/HybirdFrameworkDriver/Session/IoSession.cs
@@ -1,25 +1,30 @@
-using DotNetty.Buffers;
+using System.Collections.Concurrent;
+using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using log4net;
-namespace HybirdFrameworkDriver.TcpServer;
-
+namespace HybirdFrameworkDriver.Session;
public class IoSession
{
- private readonly ILog Log = LogManager.GetLogger(typeof(IoSession));
+ private static readonly ILog Log = LogManager.GetLogger(typeof(IoSession));
- private IChannel Channel { get; }
+ public IChannel Channel { get; }
private String IpAddr { get; }
public String Key { get; set; }
+
private bool Reconnected { get; set; }
+ //业务数据
+ public ConcurrentDictionary BusinessMap { get; } = new ConcurrentDictionary();
+
public IoSession(IChannel channel)
{
this.Channel = channel;
this.IpAddr = ChannelUtils.GetIp(channel);
}
+
public void Send(IByteBuffer buffer)
{
Channel.WriteAndFlushAsync(buffer);
diff --git a/HybirdFrameworkDriver/Session/ModbusSession.cs b/HybirdFrameworkDriver/Session/ModbusSession.cs
new file mode 100644
index 0000000..3c39c70
--- /dev/null
+++ b/HybirdFrameworkDriver/Session/ModbusSession.cs
@@ -0,0 +1,37 @@
+using System.Collections.Concurrent;
+using DotNetty.Buffers;
+using DotNetty.Transport.Channels;
+using HybirdFrameworkDriver.ModbusTcpMaster;
+using log4net;
+
+namespace HybirdFrameworkDriver.Session;
+
+
+public class ModbusSession
+{
+ private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusSession));
+
+ public ModbusTcpMaster.ModbusTcpMaster ModbusTcpMaster;
+ private String IpAddr { get; }
+ public String Key { get; set; }
+
+ public ConcurrentDictionary BusinessMap { get;set; }
+ public ModbusSession(ModbusTcpMaster.ModbusTcpMaster modbusTcpMaster)
+ {
+ this.ModbusTcpMaster = modbusTcpMaster;
+ this.IpAddr = modbusTcpMaster.Ip;
+ this.Key = modbusTcpMaster.connectId;
+ }
+
+
+ public bool Write(ModbusProperty property)
+ {
+ return ModbusTcpMaster.WriteValue(property);
+ }
+ public byte[]? Read(int registerNo, int length)
+ {
+ return ModbusTcpMaster.BatchRead(registerNo,length);
+ }
+
+
+}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/Session/SessionMgr.cs b/HybirdFrameworkDriver/Session/SessionMgr.cs
new file mode 100644
index 0000000..b256b32
--- /dev/null
+++ b/HybirdFrameworkDriver/Session/SessionMgr.cs
@@ -0,0 +1,154 @@
+using System.Collections.Concurrent;
+using DotNetty.Buffers;
+using DotNetty.Transport.Channels;
+using log4net;
+
+namespace HybirdFrameworkDriver.Session;
+
+public class SessionMgr
+{
+ private static readonly ILog Log = LogManager.GetLogger(typeof(SessionMgr));
+
+ private static readonly ConcurrentDictionary Dictionary =
+ new ConcurrentDictionary();
+
+ private static readonly ConcurrentDictionary ModbusDictionary =
+ new ConcurrentDictionary();
+
+ public ConcurrentDictionary? BusinessMap { get; set; }
+
+ public static IoSession? GetSession(string key)
+ {
+ if (Dictionary.ContainsKey(key))
+ {
+ IoSession? value;
+ Dictionary.TryGetValue(key, out value);
+
+ return value;
+ }
+
+ return null;
+ }
+
+ public static List GetSessionList()
+ {
+ return Dictionary.Values.ToList();
+ }
+
+ public static ModbusSession GetModbusSession(string key)
+ {
+ if (ModbusDictionary.ContainsKey(key))
+ {
+ ModbusSession value;
+ ModbusDictionary.TryGetValue(key, out value);
+
+ return value;
+ }
+
+ return null;
+ }
+
+ public static void RegisterSession(IChannel channel, IoSession ioSession)
+ {
+ var oldKey = ioSession.Key;
+ if (oldKey != null)
+ {
+ IoSession? session;
+ Dictionary.Remove(oldKey, out session);
+ }
+
+ ChannelUtils.AddChannelSession(channel, ioSession);
+ ioSession.Key = channel.Id.ToString();
+ Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession);
+ }
+
+
+ public static void RegisterModbusSession(string key, ModbusSession ioSession)
+ {
+ var oldKey = ioSession.Key;
+ if (oldKey != null)
+ {
+ ModbusSession? session;
+ ModbusDictionary.Remove(oldKey, out session);
+ }
+
+ ioSession.Key = key;
+ ModbusDictionary.AddOrUpdate(key, ioSession, (k, oldSession) => ioSession);
+ }
+
+ public static void UnregisterSession(IChannel channel)
+ {
+ IoSession session = ChannelUtils.GetSessionBy(channel);
+ IoSession? outSession;
+ Dictionary.Remove(session.Key, out outSession);
+ session.Close();
+ }
+
+
+ public static void Broadcast(IByteBuffer buffer, ConcurrentDictionary dictionary)
+ {
+ foreach (IoSession session in dictionary.Values)
+ {
+ session.Send(buffer);
+ }
+ }
+
+ public static Object GetAttr(IoSession session, String key)
+ {
+ Object? obj;
+ session.BusinessMap.TryGetValue(key, out obj);
+ return obj;
+ }
+
+ public static void SetAttr(IoSession session, String key, Object obj)
+ {
+ session.BusinessMap.TryAdd(key, obj);
+ }
+
+ public static Object GetAttrByKey(String? key, String mapKey)
+ {
+ if (!string.IsNullOrEmpty(key))
+ {
+ Dictionary.TryGetValue(key, out IoSession? session);
+
+ if (session != null)
+ {
+ session.BusinessMap.TryGetValue(mapKey, out Object? obj);
+
+ if (obj != null)
+ {
+ return (int)obj;
+ }
+ }
+ }
+
+ return 0;
+ }
+
+ public static void SetAttrModbus(ModbusSession session, String key, Object obj)
+ {
+ if (session.BusinessMap == null)
+ {
+ session.BusinessMap = new ConcurrentDictionary();
+ }
+
+ session.BusinessMap.TryAdd(key, obj);
+ }
+
+ public static Object GetAttrModbus(String key, String mapKey)
+ {
+ ModbusDictionary.TryGetValue(key, out ModbusSession? session);
+
+ if (session != null)
+ {
+ session.BusinessMap.TryGetValue(mapKey, out Object? obj);
+
+ if (obj != null)
+ {
+ return (int)obj;
+ }
+ }
+
+ return 0;
+ }
+}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs
new file mode 100644
index 0000000..7a934aa
--- /dev/null
+++ b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs
@@ -0,0 +1,73 @@
+using System.Net;
+using Autofac;
+using DotNetty.Codecs;
+using DotNetty.Handlers.Timeout;
+using DotNetty.Transport.Channels;
+using HybirdFrameworkCore.Autofac;
+using HybirdFrameworkDriver.Session;
+using log4net;
+
+namespace HybirdFrameworkDriver.TcpClient;
+
+public class ClientListenerHandler : ChannelHandlerAdapter where TH : IChannelHandler
+ where TD : ByteToMessageDecoder, new()
+ where TE : ChannelHandlerAdapter, new()
+{
+ private static readonly ILog Log = LogManager.GetLogger(typeof(ClientListenerHandler | ));
+
+ public override void ChannelRegistered(IChannelHandlerContext context)
+ {
+ base.ChannelRegistered(context);
+ Log.Info("register " + context.Channel);
+ }
+
+ public override void ChannelUnregistered(IChannelHandlerContext context)
+ {
+ base.ChannelUnregistered(context);
+ Log.Info("unregister " + context.Channel);
+ }
+
+ public override void ChannelActive(IChannelHandlerContext context)
+ {
+ base.ChannelActive(context);
+ SessionMgr.RegisterSession(context.Channel, new IoSession(context.Channel));
+ Log.Info("active " + context.Channel);
+ }
+
+ public override void ChannelInactive(IChannelHandlerContext context)
+ {
+ base.ChannelInactive(context);
+ var ioSession = SessionMgr.GetSession(context.Channel.Id.ToString());
+ SessionMgr.UnregisterSession(context.Channel);
+ Log.Info("inactive " + context.Channel);
+ //处理重连
+
+ TcpClient | tcpClient = AppInfo.Container.Resolve>();
+ IPEndPoint channelRemoteAddress = (IPEndPoint)ioSession.Channel.RemoteAddress;
+ tcpClient.InitBootstrap(channelRemoteAddress.Address.ToString(), channelRemoteAddress.Port);
+ tcpClient.Connect();
+ }
+
+ public override void UserEventTriggered(IChannelHandlerContext context, object evt)
+ {
+ if (evt is IdleStateEvent)
+ {
+ if (context.Channel.Open)
+ {
+ context.Channel.CloseAsync();
+ context.Channel.CloseCompletion.Wait();
+ Log.Info($"channel {context.Channel.Id} timeout close");
+ }
+ }
+
+ base.UserEventTriggered(context, evt);
+ Log.Info("UserEventTriggered " + context.Channel);
+ }
+
+ public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
+ {
+ base.ExceptionCaught(context, exception);
+ context.Channel.CloseAsync();
+ Log.Info("exception " + context.Channel);
+ }
+}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpClient/TcpClient.cs b/HybirdFrameworkDriver/TcpClient/TcpClient.cs
new file mode 100644
index 0000000..08820da
--- /dev/null
+++ b/HybirdFrameworkDriver/TcpClient/TcpClient.cs
@@ -0,0 +1,148 @@
+using System.Net;
+using System.Reflection;
+using Autofac;
+using Autofac.Core;
+using DotNetty.Codecs;
+using DotNetty.Handlers.Timeout;
+using DotNetty.Transport.Bootstrapping;
+using DotNetty.Transport.Channels;
+using DotNetty.Transport.Channels.Sockets;
+using HybirdFrameworkCore.Autofac;
+using HybirdFrameworkCore.Autofac.Attribute;
+using log4net;
+
+namespace HybirdFrameworkDriver.TcpClient;
+
+public class TcpClient where TH : IChannelHandler where TD: ByteToMessageDecoder,new() where TE: ChannelHandlerAdapter, new()
+{
+ private Bootstrap? _bootstrap;
+
+ public IChannel Channel { get; set; }
+
+ public bool Connected { get; set; } = false;
+
+ public string Host { get; set; }
+ public int Port { get; set; }
+
+ private static readonly ILog Log = LogManager.GetLogger(typeof(TcpClient | ));
+
+ public void InitBootstrap(string host, int port)
+ {
+ Host = host;
+ Port = port;
+ _bootstrap = new Bootstrap();
+ _bootstrap
+ .Group(new MultithreadEventLoopGroup())
+ .Channel()
+ .Option(ChannelOption.TcpNodelay, true)
+ .Handler(new ActionChannelInitializer(channel =>
+ {
+ var clientListenerHandler = new ClientListenerHandler();
+
+ IChannelPipeline pipeline = channel.Pipeline;
+ // 监听器
+ pipeline.AddLast(clientListenerHandler);
+ pipeline.AddLast("idleStateHandler", new IdleStateHandler(30, 0, 0)); // 触发读取超时
+
+
+ // 可以添加编解码器等
+ ResolveDecode(pipeline);
+ ResolveEncode(pipeline);
+
+ ResolveHandler(pipeline);
+ }));
+ }
+
+
+ private void ResolveEncode(IChannelPipeline pipeline)
+ {
+ pipeline.AddLast(new TE());
+ }
+
+ private void ResolveDecode(IChannelPipeline pipeline)
+ {
+ pipeline.AddLast(new TD());
+ }
+
+ private void ResolveHandler(IChannelPipeline pipeline)
+ {
+
+ List list = new List();
+
+ 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 handlers = new List | ();
+ foreach (var type in list)
+ {
+ object resolve = AppInfo.Container.Resolve(type);
+ handlers.Add((TH) resolve);
+ }
+
+ handlers.Sort((handler, msgHandler) =>
+ {
+ OrderAttribute? orderAttribute1 = handler.GetType().GetCustomAttribute();
+ OrderAttribute? orderAttribute2 = msgHandler.GetType().GetCustomAttribute();
+ int h1Order = orderAttribute1?.Order ?? 0;
+ int h2Order = orderAttribute2?.Order ?? 0;
+ return h1Order.CompareTo(h2Order);
+ });
+ foreach (var msgHandler in handlers)
+ {
+ pipeline.AddLast((IChannelHandler)msgHandler);
+ }
+ }
+
+ 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;
+ }
+ }
+ }
+
+ return false;
+ }
+
+
+ public void Connect()
+ {
+ Connected = false;
+ int num = 1;
+ while (!Connected)
+ {
+ Task task = _bootstrap!.ConnectAsync(new IPEndPoint(IPAddress.Parse(Host), Port));
+
+ Channel = task.Result;
+ Connected = Channel.Open;
+
+ if (Connected)
+ {
+ break;
+ }
+
+ Thread.Sleep(5000);
+ num++;
+ }
+
+ }
+
+
+}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/IDecoder.cs b/HybirdFrameworkDriver/TcpServer/IDecoder.cs
deleted file mode 100644
index 65c6d68..0000000
--- a/HybirdFrameworkDriver/TcpServer/IDecoder.cs
+++ /dev/null
@@ -1,5 +0,0 @@
-namespace HybirdFrameworkDriver.TcpServer;
-
-public interface IDecoder
-{
-}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/IEncoder.cs b/HybirdFrameworkDriver/TcpServer/IEncoder.cs
deleted file mode 100644
index 9cbfde5..0000000
--- a/HybirdFrameworkDriver/TcpServer/IEncoder.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace HybirdFrameworkDriver.TcpServer;
-
-public interface IEncoder
-{
-
-}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/IMsgHandler.cs b/HybirdFrameworkDriver/TcpServer/IMsgHandler.cs
deleted file mode 100644
index ef24d8b..0000000
--- a/HybirdFrameworkDriver/TcpServer/IMsgHandler.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-using DotNetty.Transport.Channels;
-
-namespace HybirdFrameworkDriver.TcpServer;
-
-public interface IMsgHandler
-{
-}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs b/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs
index 82d6f06..4fb6914 100644
--- a/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs
+++ b/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs
@@ -1,14 +1,18 @@
-using DotNetty.Transport.Channels;
+using DotNetty.Codecs;
+using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Autofac.Attribute;
+using HybirdFrameworkDriver.Session;
using log4net;
namespace HybirdFrameworkDriver.TcpServer
{
[Scope("InstancePerDependency")]
- public class ServerListenerHandler : ChannelHandlerAdapter
+ public class ServerListenerHandler : ChannelHandlerAdapter where TH : IChannelHandler
+ where TD : ByteToMessageDecoder, new()
+ where TE : ChannelHandlerAdapter, new()
{
- private readonly ILog Log = LogManager.GetLogger(typeof(ServerListenerHandler));
+ private static readonly ILog Log = LogManager.GetLogger(typeof(ServerListenerHandler | ));
public override void ChannelRegistered(IChannelHandlerContext context)
{
diff --git a/HybirdFrameworkDriver/TcpServer/SessionMgr.cs b/HybirdFrameworkDriver/TcpServer/SessionMgr.cs
deleted file mode 100644
index 6bf8b0e..0000000
--- a/HybirdFrameworkDriver/TcpServer/SessionMgr.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-using System.Collections.Concurrent;
-using DotNetty.Buffers;
-using DotNetty.Transport.Channels;
-using log4net;
-
-namespace HybirdFrameworkDriver.TcpServer;
-
-public class SessionMgr
-{
- private static readonly ILog Log = LogManager.GetLogger(typeof(SessionMgr));
-
- private static readonly ConcurrentDictionary Dictionary =
- new ConcurrentDictionary();
-
- public static void RegisterSession(IChannel channel, IoSession ioSession)
- {
- ioSession.Key = channel.Id.ToString();
- IoSession? session;
- Dictionary.Remove(ioSession.Key, out session);
- ChannelUtils.AddChannelSession(channel, ioSession);
- Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession);
- }
-
- public static void UnregisterSession(IChannel channel)
- {
- IoSession session = ChannelUtils.GetSessionBy(channel);
- IoSession? outSession;
- Dictionary.Remove(session.Key, out outSession);
- session.Close();
- }
-
- public static void Broadcast(IByteBuffer buffer)
- {
- foreach (IoSession session in Dictionary.Values)
- {
- session.Send(buffer);
- }
- }
-}
\ No newline at end of file
diff --git a/HybirdFrameworkDriver/TcpServer/Server.cs b/HybirdFrameworkDriver/TcpServer/TcpServer.cs
similarity index 62%
rename from HybirdFrameworkDriver/TcpServer/Server.cs
rename to HybirdFrameworkDriver/TcpServer/TcpServer.cs
index 35a83b2..9e83895 100644
--- a/HybirdFrameworkDriver/TcpServer/Server.cs
+++ b/HybirdFrameworkDriver/TcpServer/TcpServer.cs
@@ -1,5 +1,6 @@
using System.Reflection;
using Autofac;
+using Autofac.Core;
using DotNetty.Codecs;
using DotNetty.Handlers.Logging;
using DotNetty.Handlers.Timeout;
@@ -15,10 +16,9 @@ namespace HybirdFrameworkDriver.TcpServer
///
/// netty server
///
- [Scope("InstancePerDependency")]
- public class Server : IDisposable
+ public class TcpServer : IDisposable where TH : IChannelHandler where TD: ByteToMessageDecoder,new() where TE: ChannelHandlerAdapter, new()
{
- private readonly ILog Log = LogManager.GetLogger(typeof(Server));
+ private static readonly ILog Log = LogManager.GetLogger(typeof(TcpServer | ));
static MultithreadEventLoopGroup? bossGroup;
static MultithreadEventLoopGroup? workerGroup;
@@ -26,7 +26,7 @@ namespace HybirdFrameworkDriver.TcpServer
private int _port = 9000;
- public Server()
+ public TcpServer()
{
bossGroup = new MultithreadEventLoopGroup();
workerGroup = new MultithreadEventLoopGroup();
@@ -38,7 +38,7 @@ namespace HybirdFrameworkDriver.TcpServer
.Handler(new LoggingHandler())
.ChildHandler(new ActionChannelInitializer(channel =>
{
- var serverListenerHandler = new ServerListenerHandler();
+ var serverListenerHandler = new ServerListenerHandler();
IChannelPipeline pipeline = channel.Pipeline;
pipeline.AddLast(new LoggingHandler(""));
pipeline.AddLast(serverListenerHandler);
@@ -53,21 +53,40 @@ namespace HybirdFrameworkDriver.TcpServer
private void ResolveEncode(IChannelPipeline pipeline)
{
- IEncoder resolve = AppInfo.Container.Resolve();
- pipeline.AddLast((MessageToByteEncoder)resolve);
+ pipeline.AddLast(new TE());
}
private void ResolveDecode(IChannelPipeline pipeline)
{
- IDecoder resolve = AppInfo.Container.Resolve();
- pipeline.AddLast((ByteToMessageDecoder)resolve);
+ pipeline.AddLast(new TD());
}
private void ResolveHandler(IChannelPipeline pipeline)
{
- IEnumerable handlers = AppInfo.Container.Resolve>();
- IMsgHandler[] msgHandlers = handlers.ToArray();
- Array.Sort(msgHandlers, (handler, msgHandler) =>
+ List list = new List();
+
+ 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 handlers = new List | ();
+ foreach (var type in list)
+ {
+ object resolve = AppInfo.Container.Resolve(type);
+ handlers.Add((TH) resolve);
+ }
+
+ handlers.Sort((handler, msgHandler) =>
{
OrderAttribute? orderAttribute1 = handler.GetType().GetCustomAttribute();
OrderAttribute? orderAttribute2 = msgHandler.GetType().GetCustomAttribute();
@@ -75,13 +94,28 @@ namespace HybirdFrameworkDriver.TcpServer
int h2Order = orderAttribute2?.Order ?? 0;
return h1Order.CompareTo(h2Order);
});
- foreach (var msgHandler in msgHandlers)
+ foreach (var msgHandler in handlers)
{
- pipeline.AddLast((ChannelHandlerAdapter)msgHandler);
+ pipeline.AddLast((IChannelHandler)msgHandler);
}
}
+
+ 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;
+ }
+ }
+ }
-
+ return false;
+ }
public void Start(int port)
{
diff --git a/HybirdFrameworkRepository/bin/Debug/net6.0/HybirdFrameworkRepository.deps.json b/HybirdFrameworkRepository/bin/Debug/net6.0/HybirdFrameworkRepository.deps.json
index e543d05..3377398 100644
--- a/HybirdFrameworkRepository/bin/Debug/net6.0/HybirdFrameworkRepository.deps.json
+++ b/HybirdFrameworkRepository/bin/Debug/net6.0/HybirdFrameworkRepository.deps.json
@@ -208,6 +208,14 @@
}
}
},
+ "Microsoft.Extensions.Logging.Abstractions/6.0.0": {
+ "runtime": {
+ "lib/net6.0/Microsoft.Extensions.Logging.Abstractions.dll": {
+ "assemblyVersion": "6.0.0.0",
+ "fileVersion": "6.0.21.52210"
+ }
+ }
+ },
"Microsoft.Extensions.Primitives/7.0.0": {
"dependencies": {
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
@@ -347,6 +355,17 @@
}
}
},
+ "Pipelines.Sockets.Unofficial/2.2.8": {
+ "dependencies": {
+ "System.IO.Pipelines": "5.0.1"
+ },
+ "runtime": {
+ "lib/net5.0/Pipelines.Sockets.Unofficial.dll": {
+ "assemblyVersion": "1.0.0.0",
+ "fileVersion": "2.2.8.1080"
+ }
+ }
+ },
"SQLitePCLRaw.bundle_e_sqlite3/2.1.4": {
"dependencies": {
"SQLitePCLRaw.lib.e_sqlite3": "2.1.4",
@@ -551,6 +570,18 @@
}
}
},
+ "StackExchange.Redis/2.7.33": {
+ "dependencies": {
+ "Microsoft.Extensions.Logging.Abstractions": "6.0.0",
+ "Pipelines.Sockets.Unofficial": "2.2.8"
+ },
+ "runtime": {
+ "lib/net6.0/StackExchange.Redis.dll": {
+ "assemblyVersion": "2.0.0.0",
+ "fileVersion": "2.7.33.41805"
+ }
+ }
+ },
"System.Collections/4.3.0": {
"dependencies": {
"Microsoft.NETCore.Platforms": "5.0.0",
@@ -703,6 +734,14 @@
"System.Threading.Tasks": "4.3.0"
}
},
+ "System.IO.Pipelines/5.0.1": {
+ "runtime": {
+ "lib/netcoreapp3.0/System.IO.Pipelines.dll": {
+ "assemblyVersion": "5.0.0.1",
+ "fileVersion": "5.0.120.57516"
+ }
+ }
+ },
"System.Memory/4.5.3": {},
"System.Reflection/4.3.0": {
"dependencies": {
@@ -895,6 +934,7 @@
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0",
"Microsoft.Extensions.Configuration.Json": "7.0.0",
"Newtonsoft.Json": "13.0.3",
+ "StackExchange.Redis": "2.7.33",
"log4net": "2.0.15"
},
"runtime": {
@@ -1022,6 +1062,13 @@
"path": "microsoft.extensions.filesystemglobbing/7.0.0",
"hashPath": "microsoft.extensions.filesystemglobbing.7.0.0.nupkg.sha512"
},
+ "Microsoft.Extensions.Logging.Abstractions/6.0.0": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-/HggWBbTwy8TgebGSX5DBZ24ndhzi93sHUBDvP1IxbZD7FDokYzdAr6+vbWGjw2XAfR2EJ1sfKUotpjHnFWPxA==",
+ "path": "microsoft.extensions.logging.abstractions/6.0.0",
+ "hashPath": "microsoft.extensions.logging.abstractions.6.0.0.nupkg.sha512"
+ },
"Microsoft.Extensions.Primitives/7.0.0": {
"type": "package",
"serviceable": true,
@@ -1127,6 +1174,13 @@
"path": "oracle.manageddataaccess.core/3.21.100",
"hashPath": "oracle.manageddataaccess.core.3.21.100.nupkg.sha512"
},
+ "Pipelines.Sockets.Unofficial/2.2.8": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-zG2FApP5zxSx6OcdJQLbZDk2AVlN2BNQD6MorwIfV6gVj0RRxWPEp2LXAxqDGZqeNV1Zp0BNPcNaey/GXmTdvQ==",
+ "path": "pipelines.sockets.unofficial/2.2.8",
+ "hashPath": "pipelines.sockets.unofficial.2.2.8.nupkg.sha512"
+ },
"SQLitePCLRaw.bundle_e_sqlite3/2.1.4": {
"type": "package",
"serviceable": true,
@@ -1183,6 +1237,13 @@
"path": "sqlsugarcore.kdbndp/7.4.0",
"hashPath": "sqlsugarcore.kdbndp.7.4.0.nupkg.sha512"
},
+ "StackExchange.Redis/2.7.33": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-2kCX5fvhEE824a4Ab5Imyi8DRuGuTxyklXV01kegkRpsWJcPmO6+GAQ+HegKxvXAxlXZ8yaRspvWJ8t3mMClfQ==",
+ "path": "stackexchange.redis/2.7.33",
+ "hashPath": "stackexchange.redis.2.7.33.nupkg.sha512"
+ },
"System.Collections/4.3.0": {
"type": "package",
"serviceable": true,
@@ -1260,6 +1321,13 @@
"path": "system.io/4.3.0",
"hashPath": "system.io.4.3.0.nupkg.sha512"
},
+ "System.IO.Pipelines/5.0.1": {
+ "type": "package",
+ "serviceable": true,
+ "sha512": "sha512-qEePWsaq9LoEEIqhbGe6D5J8c9IqQOUuTzzV6wn1POlfdLkJliZY3OlB0j0f17uMWlqZYjH7txj+2YbyrIA8Yg==",
+ "path": "system.io.pipelines/5.0.1",
+ "hashPath": "system.io.pipelines.5.0.1.nupkg.sha512"
+ },
"System.Memory/4.5.3": {
"type": "package",
"serviceable": true,
diff --git a/HybirdFrameworkServices/Charger/Client/ChargerClient.cs b/HybirdFrameworkServices/Charger/Client/ChargerClient.cs
new file mode 100644
index 0000000..0de0f3d
--- /dev/null
+++ b/HybirdFrameworkServices/Charger/Client/ChargerClient.cs
@@ -0,0 +1,29 @@
+using HybirdFrameworkCore.Autofac.Attribute;
+using HybirdFrameworkDriver.Session;
+using HybirdFrameworkDriver.TcpClient;
+using HybirdFrameworkServices.Charger.Codec;
+using HybirdFrameworkServices.Charger.Handler;
+
+namespace HybirdFrameworkServices.Charger.Client;
+
+///
+/// 示例程序
+///
+[Scope("InstancePerDependency")]
+public class ChargerClient : TcpClient
+{
+ public void SessionAttr(int sn, int fEqmTypeNo, string eqmCode, string destAddr)
+ {
+ IoSession? ioSession = SessionMgr.GetSession(this.Channel.Id.ToString());
+ if (ioSession == null)
+ {
+ ioSession = new IoSession(this.Channel);
+ SessionMgr.RegisterSession(this.Channel, ioSession);
+ }
+
+ SessionMgr.SetAttr(ioSession, "charger_sn", sn);
+ SessionMgr.SetAttr(ioSession, "eqm_type_no", fEqmTypeNo);
+ SessionMgr.SetAttr(ioSession, "eqm_code", eqmCode);
+ SessionMgr.SetAttr(ioSession, "dest_addr", destAddr);
+ }
+}
\ No newline at end of file
diff --git a/HybirdFrameworkServices/Charger/Client/ChargerServer.cs b/HybirdFrameworkServices/Charger/Client/ChargerServer.cs
new file mode 100644
index 0000000..66bf615
--- /dev/null
+++ b/HybirdFrameworkServices/Charger/Client/ChargerServer.cs
@@ -0,0 +1,14 @@
+using HybirdFrameworkCore.Autofac.Attribute;
+using HybirdFrameworkDriver.TcpServer;
+using HybirdFrameworkServices.Charger.Codec;
+using HybirdFrameworkServices.Charger.Handler;
+
+namespace HybirdFrameworkServices.Charger.Client;
+
+///
+/// 示例程序
+///
+[Scope("InstancePerDependency")]
+public class ChargerServer : TcpServer
+{
+}
\ No newline at end of file
diff --git a/HybirdFrameworkServices/Charger/Client/ClientMgr.cs b/HybirdFrameworkServices/Charger/Client/ClientMgr.cs
new file mode 100644
index 0000000..dcd493f
--- /dev/null
+++ b/HybirdFrameworkServices/Charger/Client/ClientMgr.cs
@@ -0,0 +1,29 @@
+using System.Text;
+using Autofac;
+using HybirdFrameworkCore.Autofac;
+using HybirdFrameworkCore.Autofac.Attribute;
+using HybirdFrameworkServices.Charger.Msg.Host.Req;
+
+namespace HybirdFrameworkServices.Charger.Client;
+
+///
+/// 示例程序
+///
+[Scope("SingleInstance")]
+public class ClientMgr
+{
+ public void InitClient()
+ {
+ Auth auth = new Auth(1, Encoding.UTF8.GetBytes("ddddddddd"), 1);
+ auth.DestAddr = new byte[] { 0x01, 0x02, 0x03 };
+
+ ChargerClient chargerClient2 = AppInfo.Container.Resolve();
+ chargerClient2.InitBootstrap("127.0.0.1", 9998);
+ chargerClient2.Connect();
+ chargerClient2.SessionAttr(12, 12, "12", "2");
+ chargerClient2.Channel.WriteAndFlushAsync(auth);
+
+ ChargerServer chargerServer = AppInfo.Container.Resolve();
+ chargerServer.Start(9000);
+ }
+}
\ No newline at end of file
diff --git a/HybirdFrameworkServices/Charger/Codec/Decoder.cs b/HybirdFrameworkServices/Charger/Codec/Decoder.cs
index ee1e1ea..bb230cc 100644
--- a/HybirdFrameworkServices/Charger/Codec/Decoder.cs
+++ b/HybirdFrameworkServices/Charger/Codec/Decoder.cs
@@ -1,16 +1,14 @@
using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
-using HybirdFrameworkCore.Autofac.Attribute;
-using HybirdFrameworkDriver.TcpServer;
+using HybirdFrameworkServices.Charger.Msg.Charger.Req;
namespace HybirdFrameworkServices.Charger.Codec;
-[Scope("InstancePerDependency")]
-public class Decoder : ByteToMessageDecoder, IDecoder
+public class Decoder : ByteToMessageDecoder
{
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List | | | | | | | |