From 6f0ab1b44e94adf8cc1d02c7c5c94f19647a4725 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Thu, 11 Apr 2024 19:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edriver=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkCore/Autofac/AppInfo.cs | 8 ++ .../Autofac/Attribute/OrderAttribute.cs | 11 +++ .../Autofac/AutofacModuleRegister.cs | 74 ++++++++++++------ .../Debug/net6.0/ref/HybirdFrameworkCore.dll | Bin 6656 -> 0 bytes .../net6.0/refint/HybirdFrameworkCore.dll | Bin 6656 -> 0 bytes .../ChargerServer}/ChannelUtils.cs | 2 +- .../ChargerServer/IDecoder.cs | 5 ++ .../ChargerServer/IEncoder.cs | 6 ++ .../ChargerServer/IMsgHandler.cs | 7 ++ .../ChargerServer}/IoSession.cs | 3 +- .../ChargerServer}/Server.cs | 64 ++++++++++----- .../ChargerServer}/ServerListenerHandler.cs | 3 +- .../ChargerServer}/SessionMgr.cs | 6 +- .../HybirdFrameworkDriver.csproj | 25 ++++++ .../{Netty => Charger/Codec}/Decoder.cs | 7 +- .../{Netty => Charger/Codec}/Encoder.cs | 5 +- .../Charger/Handler/Cmd106Handler.cs | 15 ++++ .../Charger/Handler/IoSessionHandler.cs | 14 ++++ HybirdFrameworkServices/Charger/Msg/Cmd106.cs | 9 +++ .../HybirdFrameworkServices.csproj | 1 + HybirdFrameworkServices/Netty/MsgHandler.cs | 18 ----- HybirdFrameworkServices/System/AppInfo.cs | 8 -- .../System/SysBatteryReplaceLogServices.cs | 5 +- .../System/SysDataSourceServices.cs | 4 +- WebStarter/Program.cs | 15 ++-- WebStarter/WebStarter.csproj | 1 + WinFormStarter/Form2.cs | 10 ++- WinFormStarter/Program.cs | 5 +- WinFormStarter/WinFormStarter.csproj | 1 + hybirdMyFrame.sln | 9 +++ 30 files changed, 247 insertions(+), 94 deletions(-) create mode 100644 HybirdFrameworkCore/Autofac/AppInfo.cs create mode 100644 HybirdFrameworkCore/Autofac/Attribute/OrderAttribute.cs delete mode 100644 HybirdFrameworkCore/obj/Debug/net6.0/ref/HybirdFrameworkCore.dll delete mode 100644 HybirdFrameworkCore/obj/Debug/net6.0/refint/HybirdFrameworkCore.dll rename {HybirdFrameworkServices/Netty => HybirdFrameworkDriver/ChargerServer}/ChannelUtils.cs (94%) create mode 100644 HybirdFrameworkDriver/ChargerServer/IDecoder.cs create mode 100644 HybirdFrameworkDriver/ChargerServer/IEncoder.cs create mode 100644 HybirdFrameworkDriver/ChargerServer/IMsgHandler.cs rename {HybirdFrameworkServices/Netty => HybirdFrameworkDriver/ChargerServer}/IoSession.cs (93%) rename {HybirdFrameworkServices/Netty => HybirdFrameworkDriver/ChargerServer}/Server.cs (52%) rename {HybirdFrameworkServices/Netty => HybirdFrameworkDriver/ChargerServer}/ServerListenerHandler.cs (97%) rename {HybirdFrameworkServices/Netty => HybirdFrameworkDriver/ChargerServer}/SessionMgr.cs (81%) create mode 100644 HybirdFrameworkDriver/HybirdFrameworkDriver.csproj rename HybirdFrameworkServices/{Netty => Charger/Codec}/Decoder.cs (58%) rename HybirdFrameworkServices/{Netty => Charger/Codec}/Encoder.cs (67%) create mode 100644 HybirdFrameworkServices/Charger/Handler/Cmd106Handler.cs create mode 100644 HybirdFrameworkServices/Charger/Handler/IoSessionHandler.cs create mode 100644 HybirdFrameworkServices/Charger/Msg/Cmd106.cs delete mode 100644 HybirdFrameworkServices/Netty/MsgHandler.cs delete mode 100644 HybirdFrameworkServices/System/AppInfo.cs diff --git a/HybirdFrameworkCore/Autofac/AppInfo.cs b/HybirdFrameworkCore/Autofac/AppInfo.cs new file mode 100644 index 0000000..328b09c --- /dev/null +++ b/HybirdFrameworkCore/Autofac/AppInfo.cs @@ -0,0 +1,8 @@ +using Autofac; + +namespace HybirdFrameworkCore.Autofac; + +public class AppInfo +{ + public static ILifetimeScope Container { get; set; } +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Autofac/Attribute/OrderAttribute.cs b/HybirdFrameworkCore/Autofac/Attribute/OrderAttribute.cs new file mode 100644 index 0000000..c21b862 --- /dev/null +++ b/HybirdFrameworkCore/Autofac/Attribute/OrderAttribute.cs @@ -0,0 +1,11 @@ +namespace HybirdFrameworkCore.Autofac.Attribute; + +public class OrderAttribute : System.Attribute +{ + public readonly int Order; + + public OrderAttribute(int order) + { + this.Order = order; + } +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs index 7f1d6bf..1fd984d 100644 --- a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs +++ b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs @@ -17,6 +17,7 @@ namespace HybirdFrameworkCore.Autofac #region 带有接口层的服务注入 var repositoryDllFile = Path.Combine(basePath, "HybirdFrameworkRepository.dll"); + var driverDllFile = Path.Combine(basePath, "HybirdFrameworkDriver.dll"); var servicesDllFile = Path.Combine(basePath, "HybirdFrameworkServices.dll"); if (!(File.Exists(servicesDllFile) && File.Exists(repositoryDllFile))) @@ -25,32 +26,21 @@ namespace HybirdFrameworkCore.Autofac throw new Exception(msg); } - // AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。 - //var cacheType = new List(); - //if (AppSettingsConstVars.RedisConfigEnabled) - //{ - // builder.RegisterType(); - // cacheType.Add(typeof(RedisCacheAop)); - //} - //else - //{ - // builder.RegisterType(); - // cacheType.Add(typeof(MemoryCacheAop)); - //} - - // 获取 Service.dll 程序集服务,并注册 - SplitInject(Assembly.LoadFrom(servicesDllFile), builder); - + // 获取 driver.dll 程序集服务,并注册 + SplitInject(Assembly.LoadFrom(driverDllFile), builder); // 获取 Repository.dll 程序集服务,并注册 SplitInject(Assembly.LoadFrom(repositoryDllFile), builder); + // 获取 Service.dll 程序集服务,并注册 + SplitInject(Assembly.LoadFrom(servicesDllFile), builder); + #endregion } private void SplitInject(Assembly assemblysServices, ContainerBuilder builder) { - List InstancePerLifetimeScopeList = new List(); + List instancePerLifetimeScopeList = new List(); List instancePerDependencyList = new List(); List defaultList = new List(); foreach (var type in assemblysServices.ExportedTypes) @@ -60,21 +50,20 @@ namespace HybirdFrameworkCore.Autofac { if (ScopeConst.InstancePerLifetimeScope == scope.Scope) { - InstancePerLifetimeScopeList.Add(type); + Log.Debug($"register InstancePerLifetimeScope {type}"); + instancePerLifetimeScopeList.Add(type); } else if (ScopeConst.InstancePerDependency == scope.Scope) { + Log.Debug($"register InstancePerDependency {type}"); instancePerDependencyList.Add(type); } else { + Log.Debug($"register SingleInstance {type}"); defaultList.Add(type); } } - else - { - defaultList.Add(type); - } } if (defaultList.Count > 0) @@ -85,13 +74,46 @@ namespace HybirdFrameworkCore.Autofac if (instancePerDependencyList.Count > 0) { - builder.RegisterTypes(instancePerDependencyList.ToArray()).InstancePerDependency() - .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); //支持属性注入依赖重复 + foreach (Type type in instancePerDependencyList) + { + if (type.IsGenericType) + { + builder.RegisterGeneric(type).InstancePerDependency() + .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); + } + else + { + Type? iInterface = type.GetInterface("HybirdFrameworkDriver.ChargerServer.IMsgHandler"); + if (iInterface != null) + { + builder.RegisterType(type).As(iInterface).InstancePerDependency() + .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); + } + + iInterface = type.GetInterface("HybirdFrameworkDriver.ChargerServer.IDecoder"); + if (iInterface != null) + { + builder.RegisterType(type).As(iInterface).InstancePerDependency() + .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); + } + + iInterface = type.GetInterface("HybirdFrameworkDriver.ChargerServer.IEncoder"); + if (iInterface != null) + { + builder.RegisterType(type).As(iInterface).InstancePerDependency() + .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); + } + + + builder.RegisterType(type).InstancePerDependency() + .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); + } + } } - if (InstancePerLifetimeScopeList.Count > 0) + if (instancePerLifetimeScopeList.Count > 0) { - builder.RegisterTypes(InstancePerLifetimeScopeList.ToArray()).InstancePerMatchingLifetimeScope() + builder.RegisterTypes(instancePerLifetimeScopeList.ToArray()).InstancePerMatchingLifetimeScope() .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); //支持属性注入依赖重复 } } diff --git a/HybirdFrameworkCore/obj/Debug/net6.0/ref/HybirdFrameworkCore.dll b/HybirdFrameworkCore/obj/Debug/net6.0/ref/HybirdFrameworkCore.dll deleted file mode 100644 index c3c46292d78d7bb5ba78a1d2187b989219258820..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6656 zcmeHLPi!1#6@Rlc-fT9BYo~5W{{*(R8_~2~IZawfN$A>MJ6Yn`o9x<2i}dwNfge6{11nk_!S9At52m0n`KH0Ea3f{N9^c z?`~{L;DAb%&z|4BfAhZg-n{wdn?3&g%cK*Lf#c>)qPKA7krDi0XhGfoi68aTn_I6v z^p-Y#?V;k5<)`YNTlLIZs%$!r8>C7qU-_keoqglwrQhq4oM=E?w!UtgCm85Xh6=fFm4ub{4%}^j2N93USw1L@0=r+MHttQ z5zR8OK-A(w+&WrB+adc2>>j*T_d$GSJ#?{3fIr%0pRTP~74Q;T3IuX##|=tGhG;w; zsB;0hQ4RtEsYx8?u8~6YY0vk{kVy^;Ah-rYIJ!m#9@3t&T^O=EYT@8{cj4$78O+g=wSf7T!Li8t`uQOc;Jj<#2Z1K~r;qo;+ zSD>q)d5s)CGMCHZ{44OUAh_Xi+byv%29_}G)NUfEh~C%s)4gH*_kiX3c^PcKO8C45 z$0Im6M_&GjcIsiiaZ1K+=y7^e@KNp0`d#!OqKi{YBn|Bku{eD@)`HD@8siTHe=fLJ z!ibaS(Ar3huCW zLuZcj9iRdtItbbz4eIm8Q~D(7sIG>3eNB4`x@{3R5evC3^a8GX&N!pzpxY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9Nk(&+9@TAC=SQJ*Yo*kEF+ec0dPFO&;I zcn#9UWqzL*V>`xd+g&~0umh`ZD+spcoF(O1SUcHl8vE0KXVxcsKp+N^Yw@{f$nOZ< zl4xJgqWhW7Ftu^C_y3{hEb5fC?c;c6sD{^xr<6RutiAaNd~BFzqsKdv|KAGmw}bo~ z)auPax{uJ{8{}-q-;Spjh(`E1kH(080MtBof@gt~bP`k!-#RnkbHJ(aS2udw_{%@S zy;XaE6`r4s{P3#5Ye@IVg3;jBBoBTpd{1IiL=G)tN6%vf`*Wfqv?ioJ^d<$cbHRn< zb>kwOYKSR-l?8cq6Hg0bGm5;!zXMd_8X(^xTsr}OHLA-TO=M((Q{w(8HDPJNa|K>G z3f8Q`-ou`iuk*w*FMiq?WFhmhALe-KJ@MYIzfmlX+Ik-I@aMr}C+~gOtB||I_)y`;v!W7cn(Z%b+V8zHhg8Uq=j}BNP5c^7x?-Nj!JL_@7qbpBHhE-2eap diff --git a/HybirdFrameworkCore/obj/Debug/net6.0/refint/HybirdFrameworkCore.dll b/HybirdFrameworkCore/obj/Debug/net6.0/refint/HybirdFrameworkCore.dll deleted file mode 100644 index c3c46292d78d7bb5ba78a1d2187b989219258820..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6656 zcmeHLPi!1#6@Rlc-fT9BYo~5W{{*(R8_~2~IZawfN$A>MJ6Yn`o9x<2i}dwNfge6{11nk_!S9At52m0n`KH0Ea3f{N9^c z?`~{L;DAb%&z|4BfAhZg-n{wdn?3&g%cK*Lf#c>)qPKA7krDi0XhGfoi68aTn_I6v z^p-Y#?V;k5<)`YNTlLIZs%$!r8>C7qU-_keoqglwrQhq4oM=E?w!UtgCm85Xh6=fFm4ub{4%}^j2N93USw1L@0=r+MHttQ z5zR8OK-A(w+&WrB+adc2>>j*T_d$GSJ#?{3fIr%0pRTP~74Q;T3IuX##|=tGhG;w; zsB;0hQ4RtEsYx8?u8~6YY0vk{kVy^;Ah-rYIJ!m#9@3t&T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z~5$25EhPpYbgXU?N^IdHx9L|MzMI<6mB zWxp+op2>9kp(rSC#VRX5YI*qQw)Cmp?Oau#j$38V_1(n)*HzRQ8m{SEKKWtPK6OQh z4E$i;^dM|Mli?HK(-iu(5ST&3r`ggMa4m5!@%q7OXCM3A-G_<#v6boH-5;k| z3?~-voJ(kUG4c2~?}>`Bdrx_0ea3Yr&y`i3JD|AaxvRbgyDhR_AJ%ABdS172 zc+^~nSDsFf!a@Jg`a+Vm{JL#6dDTtu6)9Nk(&+9@TAC=SQJ*Yo*kEF+ec0dPFO&;I zcn#9UWqzL*V>`xd+g&~0umh`ZD+spcoF(O1SUcHl8vE0KXVxcsKp+N^Yw@{f$nOZ< zl4xJgqWhW7Ftu^C_y3{hEb5fC?c;c6sD{^xr<6RutiAaNd~BFzqsKdv|KAGmw}bo~ z)auPax{uJ{8{}-q-;Spjh(`E1kH(080MtBof@gt~bP`k!-#RnkbHJ(aS2udw_{%@S zy;XaE6`r4s{P3#5Ye@IVg3;jBBoBTpd{1IiL=G)tN6%vf`*Wfqv?ioJ^d<$cbHRn< zb>kwOYKSR-l?8cq6Hg0bGm5;!zXMd_8X(^xTsr}OHLA-TO=M((Q{w(8HDPJNa|K>G z3f8Q`-ou`iuk*w*FMiq?WFhmhALe-KJ@MYIzfmlX+Ik-I@aMr}C+~gOtB||I_)y`;v!W7cn(Z%b+V8zHhg8Uq=j}BNP5c^7x?-Nj!JL_@7qbpBHhE-2eap diff --git a/HybirdFrameworkServices/Netty/ChannelUtils.cs b/HybirdFrameworkDriver/ChargerServer/ChannelUtils.cs similarity index 94% rename from HybirdFrameworkServices/Netty/ChannelUtils.cs rename to HybirdFrameworkDriver/ChargerServer/ChannelUtils.cs index 4f2ba4d..65f44ef 100644 --- a/HybirdFrameworkServices/Netty/ChannelUtils.cs +++ b/HybirdFrameworkDriver/ChargerServer/ChannelUtils.cs @@ -1,7 +1,7 @@ using DotNetty.Common.Utilities; using DotNetty.Transport.Channels; -namespace HybirdFrameworkServices.Netty; +namespace HybirdFrameworkDriver.ChargerServer; public static class ChannelUtils { diff --git a/HybirdFrameworkDriver/ChargerServer/IDecoder.cs b/HybirdFrameworkDriver/ChargerServer/IDecoder.cs new file mode 100644 index 0000000..71b7946 --- /dev/null +++ b/HybirdFrameworkDriver/ChargerServer/IDecoder.cs @@ -0,0 +1,5 @@ +namespace HybirdFrameworkDriver.ChargerServer; + +public interface IDecoder +{ +} \ No newline at end of file diff --git a/HybirdFrameworkDriver/ChargerServer/IEncoder.cs b/HybirdFrameworkDriver/ChargerServer/IEncoder.cs new file mode 100644 index 0000000..e0264ce --- /dev/null +++ b/HybirdFrameworkDriver/ChargerServer/IEncoder.cs @@ -0,0 +1,6 @@ +namespace HybirdFrameworkDriver.ChargerServer; + +public interface IEncoder +{ + +} \ No newline at end of file diff --git a/HybirdFrameworkDriver/ChargerServer/IMsgHandler.cs b/HybirdFrameworkDriver/ChargerServer/IMsgHandler.cs new file mode 100644 index 0000000..a69c1f4 --- /dev/null +++ b/HybirdFrameworkDriver/ChargerServer/IMsgHandler.cs @@ -0,0 +1,7 @@ +using DotNetty.Transport.Channels; + +namespace HybirdFrameworkDriver.ChargerServer; + +public interface IMsgHandler +{ +} \ No newline at end of file diff --git a/HybirdFrameworkServices/Netty/IoSession.cs b/HybirdFrameworkDriver/ChargerServer/IoSession.cs similarity index 93% rename from HybirdFrameworkServices/Netty/IoSession.cs rename to HybirdFrameworkDriver/ChargerServer/IoSession.cs index 2041bcb..99d92d1 100644 --- a/HybirdFrameworkServices/Netty/IoSession.cs +++ b/HybirdFrameworkDriver/ChargerServer/IoSession.cs @@ -2,7 +2,8 @@ using DotNetty.Transport.Channels; using log4net; -namespace HybirdFrameworkServices.Netty; +namespace HybirdFrameworkDriver.ChargerServer; + public class IoSession { diff --git a/HybirdFrameworkServices/Netty/Server.cs b/HybirdFrameworkDriver/ChargerServer/Server.cs similarity index 52% rename from HybirdFrameworkServices/Netty/Server.cs rename to HybirdFrameworkDriver/ChargerServer/Server.cs index 0091a26..d9b7000 100644 --- a/HybirdFrameworkServices/Netty/Server.cs +++ b/HybirdFrameworkDriver/ChargerServer/Server.cs @@ -1,18 +1,21 @@ -using DotNetty.Buffers; +using System.Reflection; +using Autofac; +using DotNetty.Codecs; using DotNetty.Handlers.Logging; 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 HybirdFrameworkServices.Netty +namespace HybirdFrameworkDriver.ChargerServer { /// /// netty server /// - [Scope("SingleInstance")] + [Scope("InstancePerDependency")] public class Server : IDisposable { private readonly ILog Log = LogManager.GetLogger(typeof(Server)); @@ -23,11 +26,8 @@ namespace HybirdFrameworkServices.Netty private int _port = 9000; - public void connect(int port) + public Server() { - _port = port; - IByteBuffer delimiter = Unpooled.CopiedBuffer(new byte[] { 0xAA, 0xF5 }); - bossGroup = new MultithreadEventLoopGroup(); workerGroup = new MultithreadEventLoopGroup(); bootstrap = new ServerBootstrap(); @@ -42,21 +42,48 @@ namespace HybirdFrameworkServices.Netty IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new LoggingHandler("")); pipeline.AddLast(serverListenerHandler); - //pipeline.AddLast(new FixedLengthFrameDecoder(12)); //定长数据,12byte - pipeline.AddLast(new IdleStateHandler(0, 0, 180));//检测空闲连接 - //pipeline.AddLast(msgHandler); - //业务handler ,这里是实际处理业务的Handler + pipeline.AddLast(new IdleStateHandler(0, 0, 180)); //检测空闲连接 + //业务handler ,这里是实际处理业务的Handler - pipeline.AddLast(new Decoder()); - pipeline.AddLast(new Encoder()); - pipeline.AddLast(new MsgHandler()); - + ResolveEncode(pipeline); + ResolveDecode(pipeline); + ResolveHandler(pipeline); })); - Begin(_port); } + private void ResolveEncode(IChannelPipeline pipeline) + { + IEncoder resolve = AppInfo.Container.Resolve(); + pipeline.AddLast((MessageToByteEncoder)resolve); + } + + private void ResolveDecode(IChannelPipeline pipeline) + { + IDecoder resolve = AppInfo.Container.Resolve(); + pipeline.AddLast((ByteToMessageDecoder)resolve); + } + + private void ResolveHandler(IChannelPipeline pipeline) + { + IEnumerable handlers = AppInfo.Container.Resolve>(); + IMsgHandler[] msgHandlers = handlers.ToArray(); + Array.Sort(msgHandlers, (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 msgHandlers) + { + pipeline.AddLast((ChannelHandlerAdapter)msgHandler); + } + } + + - public void Begin(int port) + public void Start(int port) { _port = port; Log.Info(" Start Listen"); @@ -74,5 +101,4 @@ namespace HybirdFrameworkServices.Netty bossGroup?.ShutdownGracefullyAsync(); } } -} - +} \ No newline at end of file diff --git a/HybirdFrameworkServices/Netty/ServerListenerHandler.cs b/HybirdFrameworkDriver/ChargerServer/ServerListenerHandler.cs similarity index 97% rename from HybirdFrameworkServices/Netty/ServerListenerHandler.cs rename to HybirdFrameworkDriver/ChargerServer/ServerListenerHandler.cs index 2349a93..c54e1fa 100644 --- a/HybirdFrameworkServices/Netty/ServerListenerHandler.cs +++ b/HybirdFrameworkDriver/ChargerServer/ServerListenerHandler.cs @@ -2,7 +2,8 @@ using HybirdFrameworkCore.Autofac.Attribute; using log4net; -namespace HybirdFrameworkServices.Netty +namespace HybirdFrameworkDriver.ChargerServer + { [Scope("InstancePerDependency")] public class ServerListenerHandler : ChannelHandlerAdapter diff --git a/HybirdFrameworkServices/Netty/SessionMgr.cs b/HybirdFrameworkDriver/ChargerServer/SessionMgr.cs similarity index 81% rename from HybirdFrameworkServices/Netty/SessionMgr.cs rename to HybirdFrameworkDriver/ChargerServer/SessionMgr.cs index f41c705..b515174 100644 --- a/HybirdFrameworkServices/Netty/SessionMgr.cs +++ b/HybirdFrameworkDriver/ChargerServer/SessionMgr.cs @@ -3,7 +3,7 @@ using DotNetty.Buffers; using DotNetty.Transport.Channels; using log4net; -namespace HybirdFrameworkServices.Netty; +namespace HybirdFrameworkDriver.ChargerServer; public class SessionMgr { @@ -15,7 +15,9 @@ public class SessionMgr public static void RegisterSession(string key, IoSession ioSession) { ioSession.Key = key; - Dictionary.GetOrAdd(key, ioSession); + IoSession? session; + Dictionary.Remove(ioSession.Key, out session); + Dictionary.AddOrUpdate(key, ioSession, (k, oldSession) => ioSession); } public static void UnregisterSession(IChannel channel) diff --git a/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj new file mode 100644 index 0000000..5a7cb30 --- /dev/null +++ b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + + + + diff --git a/HybirdFrameworkServices/Netty/Decoder.cs b/HybirdFrameworkServices/Charger/Codec/Decoder.cs similarity index 58% rename from HybirdFrameworkServices/Netty/Decoder.cs rename to HybirdFrameworkServices/Charger/Codec/Decoder.cs index a55f626..84c619c 100644 --- a/HybirdFrameworkServices/Netty/Decoder.cs +++ b/HybirdFrameworkServices/Charger/Codec/Decoder.cs @@ -2,14 +2,15 @@ using DotNetty.Codecs; using DotNetty.Transport.Channels; using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.ChargerServer; -namespace HybirdFrameworkServices.Netty; +namespace HybirdFrameworkServices.Charger.Codec; [Scope("InstancePerDependency")] -public class Decoder : ByteToMessageDecoder +public class Decoder : ByteToMessageDecoder, IDecoder { protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List output) { - throw new NotImplementedException(); + output.Add(new IoSession(context.Channel)); } } \ No newline at end of file diff --git a/HybirdFrameworkServices/Netty/Encoder.cs b/HybirdFrameworkServices/Charger/Codec/Encoder.cs similarity index 67% rename from HybirdFrameworkServices/Netty/Encoder.cs rename to HybirdFrameworkServices/Charger/Codec/Encoder.cs index 369b1fa..cae0493 100644 --- a/HybirdFrameworkServices/Netty/Encoder.cs +++ b/HybirdFrameworkServices/Charger/Codec/Encoder.cs @@ -2,11 +2,12 @@ using DotNetty.Codecs; using DotNetty.Transport.Channels; using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.ChargerServer; -namespace HybirdFrameworkServices.Netty; +namespace HybirdFrameworkServices.Charger.Codec; [Scope("InstancePerDependency")] -public class Encoder : MessageToByteEncoder +public class Encoder : MessageToByteEncoder, IEncoder { protected override void Encode(IChannelHandlerContext context, byte[] message, IByteBuffer output) { diff --git a/HybirdFrameworkServices/Charger/Handler/Cmd106Handler.cs b/HybirdFrameworkServices/Charger/Handler/Cmd106Handler.cs new file mode 100644 index 0000000..f9bfb47 --- /dev/null +++ b/HybirdFrameworkServices/Charger/Handler/Cmd106Handler.cs @@ -0,0 +1,15 @@ +using DotNetty.Transport.Channels; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.ChargerServer; +using HybirdFrameworkServices.Charger.Msg; + +namespace HybirdFrameworkServices.Charger.Handler; + +[Order(9)] +[Scope("InstancePerDependency")] +public class Cmd106Handler : SimpleChannelInboundHandler, IMsgHandler +{ + protected override void ChannelRead0(IChannelHandlerContext ctx, Cmd106 msg) + { + } +} \ No newline at end of file diff --git a/HybirdFrameworkServices/Charger/Handler/IoSessionHandler.cs b/HybirdFrameworkServices/Charger/Handler/IoSessionHandler.cs new file mode 100644 index 0000000..e1881c2 --- /dev/null +++ b/HybirdFrameworkServices/Charger/Handler/IoSessionHandler.cs @@ -0,0 +1,14 @@ +using DotNetty.Transport.Channels; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.ChargerServer; + +namespace HybirdFrameworkServices.Charger.Handler; + +[Order(8)] +[Scope("InstancePerDependency")] +public class IoSessionHandler : SimpleChannelInboundHandler, IMsgHandler +{ + protected override void ChannelRead0(IChannelHandlerContext ctx, IoSession msg) + { + } +} \ No newline at end of file diff --git a/HybirdFrameworkServices/Charger/Msg/Cmd106.cs b/HybirdFrameworkServices/Charger/Msg/Cmd106.cs new file mode 100644 index 0000000..c0f3ecd --- /dev/null +++ b/HybirdFrameworkServices/Charger/Msg/Cmd106.cs @@ -0,0 +1,9 @@ +using HybirdFrameworkCore.Autofac.Attribute; + +namespace HybirdFrameworkServices.Charger.Msg; + +[Scope("InstancePerDependency")] +public class Cmd106 +{ + +} \ No newline at end of file diff --git a/HybirdFrameworkServices/HybirdFrameworkServices.csproj b/HybirdFrameworkServices/HybirdFrameworkServices.csproj index 5379441..b6d54f4 100644 --- a/HybirdFrameworkServices/HybirdFrameworkServices.csproj +++ b/HybirdFrameworkServices/HybirdFrameworkServices.csproj @@ -19,6 +19,7 @@ + diff --git a/HybirdFrameworkServices/Netty/MsgHandler.cs b/HybirdFrameworkServices/Netty/MsgHandler.cs deleted file mode 100644 index 899f646..0000000 --- a/HybirdFrameworkServices/Netty/MsgHandler.cs +++ /dev/null @@ -1,18 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Channels; -using HybirdFrameworkCore.Autofac.Attribute; -using log4net; - -namespace HybirdFrameworkServices.Netty; - -[Scope("InstancePerDependency")] -public class MsgHandler : ChannelHandlerAdapter -{ - private readonly ILog Log = LogManager.GetLogger(typeof(MsgHandler)); - - public override void ChannelRead(IChannelHandlerContext ctx, object msg) - { - Log.Info($"{this.GetHashCode()} read from {ctx.Channel.RemoteAddress}"); - ctx.WriteAndFlushAsync(Unpooled.WrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7 })); - } -} \ No newline at end of file diff --git a/HybirdFrameworkServices/System/AppInfo.cs b/HybirdFrameworkServices/System/AppInfo.cs deleted file mode 100644 index 9e0ce5d..0000000 --- a/HybirdFrameworkServices/System/AppInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -using Autofac; - -namespace HybirdFrameworkServices.System; - -public class AppInfo -{ - public static IContainer Container { get; set; } -} \ No newline at end of file diff --git a/HybirdFrameworkServices/System/SysBatteryReplaceLogServices.cs b/HybirdFrameworkServices/System/SysBatteryReplaceLogServices.cs index 72f2a6b..8fc54a1 100644 --- a/HybirdFrameworkServices/System/SysBatteryReplaceLogServices.cs +++ b/HybirdFrameworkServices/System/SysBatteryReplaceLogServices.cs @@ -1,9 +1,12 @@ -using HybirdFrameworkEntity.DbModel; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkEntity.DbModel; using HybirdFrameworkRepository.System; using HybirdFrameworkRepository.UnitOfWork; + namespace HybirdFrameworkServices.System { + [Scope("SingleInstance")] public class SysBatteryReplaceLogServices : BaseServices { SysBatteryReplaceLogRepository _dal; diff --git a/HybirdFrameworkServices/System/SysDataSourceServices.cs b/HybirdFrameworkServices/System/SysDataSourceServices.cs index 4cc2146..bebbf7f 100644 --- a/HybirdFrameworkServices/System/SysDataSourceServices.cs +++ b/HybirdFrameworkServices/System/SysDataSourceServices.cs @@ -1,9 +1,11 @@ -using HybirdFrameworkEntity.DbModel; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkEntity.DbModel; using HybirdFrameworkRepository.System; using HybirdFrameworkRepository.UnitOfWork; namespace HybirdFrameworkServices.System { + [Scope("SingleInstance")] public class SysDataSourceServices : BaseServices { private readonly SysDataSourceRepository _dal; diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index df3c2dc..6299c62 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -1,8 +1,8 @@ -using System.Text.Json; using Autofac; using Autofac.Extensions.DependencyInjection; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Configuration; +using HybirdFrameworkDriver.ChargerServer; using SqlSugar; using SqlSugar.IOC; @@ -13,7 +13,6 @@ builder.Host.ConfigureContainer(cb => { cb.Register(c => { - var db = new SqlSugarClient(new ConnectionConfig { ConfigId = AppSettingsConstVars.ConfigId, @@ -24,13 +23,11 @@ builder.Host.ConfigureContainer(cb => }); return db; }).As().InstancePerLifetimeScope(); - + cb.RegisterModule(new AutofacModuleRegister()); }); -// Add services to the container. - builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -39,6 +36,7 @@ builder.Logging.AddLog4Net("log4net.config"); var app = builder.Build(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -50,4 +48,9 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +AppInfo.Container = app.Services.GetAutofacRoot(); + +Server server = AppInfo.Container.Resolve(); +server.Start(9000); + +app.Run(); \ No newline at end of file diff --git a/WebStarter/WebStarter.csproj b/WebStarter/WebStarter.csproj index 08c4b6a..fff4ed2 100644 --- a/WebStarter/WebStarter.csproj +++ b/WebStarter/WebStarter.csproj @@ -15,6 +15,7 @@ + diff --git a/WinFormStarter/Form2.cs b/WinFormStarter/Form2.cs index ef85d94..d37f9be 100644 --- a/WinFormStarter/Form2.cs +++ b/WinFormStarter/Form2.cs @@ -1,9 +1,15 @@ -namespace WinFormStarter; +using HybirdFrameworkDriver.ChargerServer; + +namespace WinFormStarter; public partial class Form2 : Form { - public Form2() + + private Server _server; + public Form2(Server server) { + _server = server; InitializeComponent(); + _server.Start(9000); } } \ No newline at end of file diff --git a/WinFormStarter/Program.cs b/WinFormStarter/Program.cs index 0a68914..4851cc3 100644 --- a/WinFormStarter/Program.cs +++ b/WinFormStarter/Program.cs @@ -1,7 +1,6 @@ using Autofac; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Configuration; -using HybirdFrameworkServices.System; using log4net.Config; using SqlSugar; using SqlSugar.IOC; @@ -50,8 +49,8 @@ static class Program builder.RegisterModule(new AutofacModuleRegister()); // 构建容器 Container = builder.Build(); - AppInfo.Container = Container; - Application.Run(Container.ResolveNamed
("Form2")); + AppInfo.Container = Container.BeginLifetimeScope("root"); + Application.Run(AppInfo.Container.ResolveNamed("Form2")); } private static void Application_ThreadException(object sender, ThreadExceptionEventArgs ex) diff --git a/WinFormStarter/WinFormStarter.csproj b/WinFormStarter/WinFormStarter.csproj index 880e306..1cfba00 100644 --- a/WinFormStarter/WinFormStarter.csproj +++ b/WinFormStarter/WinFormStarter.csproj @@ -16,6 +16,7 @@ + diff --git a/hybirdMyFrame.sln b/hybirdMyFrame.sln index 0b62e60..a047ffb 100644 --- a/hybirdMyFrame.sln +++ b/hybirdMyFrame.sln @@ -27,6 +27,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormStarter", "WinFormSt EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HybirdFrameworkEntity", "HybirdFrameworkEntity\HybirdFrameworkEntity.csproj", "{C2380814-15D4-491D-ADF2-ADC68617C3FA}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HybirdFramework.Driver", "HybirdFramework.Driver", "{EF6B2DEC-ADAA-4A6D-AE93-0F98A555B265}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HybirdFrameworkDriver", "HybirdFrameworkDriver\HybirdFrameworkDriver.csproj", "{6ACFA707-E72E-4BA1-8262-9F2E5B758D46}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,6 +61,10 @@ Global {C2380814-15D4-491D-ADF2-ADC68617C3FA}.Debug|Any CPU.Build.0 = Debug|Any CPU {C2380814-15D4-491D-ADF2-ADC68617C3FA}.Release|Any CPU.ActiveCfg = Release|Any CPU {C2380814-15D4-491D-ADF2-ADC68617C3FA}.Release|Any CPU.Build.0 = Release|Any CPU + {6ACFA707-E72E-4BA1-8262-9F2E5B758D46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6ACFA707-E72E-4BA1-8262-9F2E5B758D46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6ACFA707-E72E-4BA1-8262-9F2E5B758D46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6ACFA707-E72E-4BA1-8262-9F2E5B758D46}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -68,6 +76,7 @@ Global {A6C2AA7F-B2A2-4AE0-AE84-49BE36B990EC} = {5F67ED42-6437-4FC6-86D2-0495DE990BC4} {A6757DAD-EF5A-41FD-9323-F3FCF05ED777} = {A0BA21DB-6630-41AB-A0FD-594DBB197E0E} {C2380814-15D4-491D-ADF2-ADC68617C3FA} = {6203689E-8261-4814-BFC2-013188AED6A1} + {6ACFA707-E72E-4BA1-8262-9F2E5B758D46} = {EF6B2DEC-ADAA-4A6D-AE93-0F98A555B265} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E10BF3D-9914-44B1-A6AA-FCF013C3F155}