Quellcode durchsuchen

问题修改,版本更新

Shun vor 2 Jahren
Ursprung
Commit
c4dbbdd376

+ 2 - 2
src/YSAI.DAQ/YSAI.Core/YSAI.Core.csproj

@@ -5,7 +5,7 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
     <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-    <Version>1.0.0.60</Version>
+    <Version>1.0.0.61</Version>
     <Authors>Shun</Authors>
     <Company>YSAI</Company>
     <Product>SCADA</Product>
@@ -17,7 +17,7 @@
 		<PackageReference Include="Microsoft.ClearScript" Version="7.4.4" />
 		<PackageReference Include="System.IO.Ports" Version="7.0.0" />
 		<PackageReference Include="YSAI.Log" Version="1.0.0.9" />
-		<PackageReference Include="YSAI.Unility" Version="1.0.0.19" />
+		<PackageReference Include="YSAI.Unility" Version="1.0.0.20" />
 	</ItemGroup>
 
 </Project>

+ 15 - 22
src/YSAI.DAQ/YSAI.Netty/client/NettyClientOperate.cs

@@ -3,19 +3,13 @@ using DotNetty.Handlers.Tls;
 using DotNetty.Transport.Bootstrapping;
 using DotNetty.Transport.Channels;
 using DotNetty.Transport.Channels.Sockets;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
 using System.Collections.Concurrent;
 using System.Net;
 using System.Net.Security;
 using System.Security.Cryptography.X509Certificates;
-using System.Text;
-using System.Threading.Channels;
 using YSAI.Core.data;
 using YSAI.Core.@interface;
-using YSAI.Log;
 using YSAI.Unility;
-using static YSAI.Netty.client.NettyClientOperate.SecureChatClientHandler;
 
 namespace YSAI.Netty.client
 {
@@ -205,6 +199,21 @@ namespace YSAI.Netty.client
                 Off();
             }
         }
+        /// <summary>
+        /// 信息
+        /// </summary>
+        public class Info
+        {
+            /// <summary>
+            /// 通道处理程序上下文
+            /// </summary>
+            public IChannelHandlerContext Context { get; set; }
+            /// <summary>
+            /// 信息
+            /// </summary>
+            public string Msg { get; set; }
+        }
+
 
         /// <summary>
         /// 消息接收处理
@@ -223,22 +232,6 @@ namespace YSAI.Netty.client
             {
                 this.MessageEvent = MessageEvent;
             }
-
-            /// <summary>
-            /// 信息
-            /// </summary>
-            public class Info
-            {
-                /// <summary>
-                /// 通道处理程序上下文
-                /// </summary>
-                public IChannelHandlerContext Context { get; set; }
-                /// <summary>
-                /// 信息
-                /// </summary>
-                public string Msg { get; set; }
-            }
-
             /// <summary>
             /// 通道消息
             /// </summary>

+ 130 - 27
src/YSAI.DAQ/YSAI.Netty/service/NettyServiceOperate.cs

@@ -8,6 +8,7 @@ using DotNetty.Transport.Channels.Sockets;
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Diagnostics.Metrics;
 using System.Linq;
 using System.Net;
 using System.Net.NetworkInformation;
@@ -89,7 +90,6 @@ namespace YSAI.Netty.service
         /// 通道
         /// </summary>
         private IChannel Channel;
-
         /// <summary>
         /// 构造函数
         /// </summary>
@@ -98,6 +98,25 @@ namespace YSAI.Netty.service
             this.basics = basics;
         }
 
+        /// <summary>
+        /// 内部消息接收
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        protected void OnMe(object? sender, EventResult e)
+        {
+            if (!e.State)
+            {
+                //出现异常
+                OnEventHandler(this, new EventResult(false, $"服务端异常:{e.Message}"));
+                Off();
+            }
+            else
+            {
+                OnEventHandler(this, new EventResult(true, e.Message));
+            }
+        }
+
         /// <summary>
         /// 处理程序
         /// </summary>
@@ -106,12 +125,11 @@ namespace YSAI.Netty.service
             /// <summary>
             /// 消息事件
             /// </summary>
-            Action<object?, EventResult> MessageEvent;
+            private Action<object?, EventResult> MessageEvent;
             /// <summary>
             /// 组
             /// </summary>
-            static volatile IChannelGroup group;
-
+            private static volatile IChannelGroup ChannelGroup;
             /// <summary>
             /// 构造函数
             /// </summary>
@@ -120,45 +138,73 @@ namespace YSAI.Netty.service
             {
                 this.MessageEvent = MessageEvent;
             }
+            /// <summary>
+            /// 通道激活
+            /// </summary>
+            /// <param name="contex"></param>
             public override void ChannelActive(IChannelHandlerContext contex)
             {
-                IChannelGroup g = group;
+                IChannelGroup g = ChannelGroup;
                 if (g == null)
                 {
                     lock (this)
                     {
-                        if (group == null)
+                        if (ChannelGroup == null)
                         {
-                            g = group = new DefaultChannelGroup(contex.Executor);
+                            g = ChannelGroup = new DefaultChannelGroup(contex.Executor);
+                            MessageEvent?.Invoke(this, new EventResult(true, $"{contex.Channel.RemoteAddress} 激活了通道"));
                         }
                     }
                 }
                 g.Add(contex.Channel);
             }
-
-            class EveryOneBut : IChannelMatcher
+            public override Task DisconnectAsync(IChannelHandlerContext context)
             {
-                readonly IChannelId id;
-
-                public EveryOneBut(IChannelId id)
-                {
-                    this.id = id;
-                }
-
-                public bool Matches(IChannel channel) => channel.Id != this.id;
+                MessageEvent?.Invoke(this, new EventResult(true, $"{context.Channel.RemoteAddress} 释放并断开连接"));
+                return base.DisconnectAsync(context);
+            }
+            /// <summary>
+            /// 通道关闭
+            /// </summary>
+            /// <returns></returns>
+            public override Task CloseAsync(IChannelHandlerContext context)
+            {
+                MessageEvent?.Invoke(this, new EventResult(true, $"{context.Channel.RemoteAddress} 关闭连接"));
+                return base.CloseAsync(context);
+            }
+            /// <summary>
+            /// 通道连接
+            /// </summary>
+            /// <returns></returns>
+            public override Task ConnectAsync(IChannelHandlerContext context, EndPoint remoteAddress, EndPoint localAddress)
+            {
+                MessageEvent?.Invoke(this, new EventResult(true, $"{remoteAddress} 与 {localAddress} 建立连接 "));
+                return base.ConnectAsync(context, remoteAddress, localAddress);
             }
 
+            /// <summary>
+            /// 群发送至连上服务器的所有客户端,过滤掉发送端 
+            /// </summary>
+            //class EveryOneBut : IChannelMatcher
+            //{
+            //    readonly IChannelId id;
+            //    public EveryOneBut(IChannelId id)
+            //    {
+            //        this.id = id;
+            //    }
+            //    public bool Matches(IChannel channel) => channel.Id != this.id;
+            //}
+
             protected override void ChannelRead0(IChannelHandlerContext contex, string msg)
             {
-                //数据转发
-                string broadcast = string.Format("{0}\n", msg);
-                string response = string.Format("{0}\n", msg);
-                group.WriteAndFlushAsync(broadcast, new EveryOneBut(contex.Channel.Id));
-                contex.WriteAndFlushAsync(response);
-                if (string.Equals("bye", msg, StringComparison.OrdinalIgnoreCase))
-                {
-                    contex.CloseAsync();
-                }
+                //群发
+                ChannelGroup.WriteAndFlushAsync(string.Format("{0}\n", msg));
+
+                //群发过滤发送端
+                //group.WriteAndFlushAsync(string.Format("{0}\n", msg),new EveryOneBut(contex.Channel.Id));
+
+                //给发送端回显数据
+                //contex.WriteAndFlushAsync(string.Format("{0}\n", msg));
             }
 
             public override void ChannelReadComplete(IChannelHandlerContext ctx) => ctx.Flush();
@@ -176,6 +222,30 @@ namespace YSAI.Netty.service
                 //通道关闭
                 contex.CloseAsync();
             }
+            /// <summary>
+            /// 数据发送
+            /// </summary>
+            /// <param name="Data"></param>
+            public static OperateResult Send(string Data)
+            {
+                try
+                {
+                    if (ChannelGroup != null && ChannelGroup.Count > 0)
+                    {
+                        //群发
+                        ChannelGroup.WriteAndFlushAsync(string.Format("{0}\n", Data)).Wait();
+                        return new OperateResult(true, string.Empty, 1);
+                    }
+                    else
+                    {
+                        return new OperateResult(false, "当前没有客户端连接", 1);
+                    }
+                }
+                catch (Exception ex)
+                {
+                    return new OperateResult(false, ex.Message, 1);
+                }
+            }
 
             public override bool IsSharable => true;
         }
@@ -226,7 +296,7 @@ namespace YSAI.Netty.service
                 var STRING_ENCODER = new StringEncoder();
                 var STRING_DECODER = new StringDecoder();
                 //实例化处理程序
-                var SERVER_HANDLER = new SecureChatServerHandler(OnEventHandler);
+                var SERVER_HANDLER = new SecureChatServerHandler(OnMe);
 
                 //走TCP通道
                 Communication
@@ -299,5 +369,38 @@ namespace YSAI.Netty.service
         {
             return Task.Run(() => Off());
         }
+
+        /// <summary>
+        /// 群发数据
+        /// </summary>
+        /// <param name="Data">数据</param>
+        /// <returns></returns>
+        public OperateResult Send(string Data)
+        {
+            Depart("Send");
+            try
+            {
+                if (Channel == null)
+                {
+                    return Break("Send", false, "未连接");
+                }
+                //等待发送完成
+               return SecureChatServerHandler.Send(Data);
+            }
+            catch (Exception ex)
+            {
+                return Break("Send", false, ex.Message, Exception: ex);
+            }
+        }
+
+        /// <summary>
+        /// 群发数据
+        /// </summary>
+        /// <param name="Data">数据</param>
+        /// <returns></returns>
+        public Task<OperateResult> SendAsync(string Data)
+        {
+            return Task.Run(()=> Send(Data));
+        }
     }
 }

+ 22 - 10
src/YSAI.DAQ/YSAI.Test.All/Program.cs

@@ -21,18 +21,18 @@ NettyClientOperate operate1 = NettyClientOperate.Instance(new NettyClientData.Ba
     SN = "1"
 });
 
-NettyClientOperate operate2 = NettyClientOperate.Instance(new NettyClientData.Basics
-{
-    Host = "127.0.0.1",
-    Port = 8007,
-    SN = "2"
-});
+//NettyClientOperate operate2 = NettyClientOperate.Instance(new NettyClientData.Basics
+//{
+//    Host = "127.0.0.1",
+//    Port = 8007,
+//    SN = "2"
+//});
 
 //打开
 OperateResult result = operate1.On();
 LogHelper.Info(result.ToJson().JsonFormatting());
-result = operate2.On();
-LogHelper.Info(result.ToJson().JsonFormatting());
+//result = operate2.On();
+//LogHelper.Info(result.ToJson().JsonFormatting());
 
 //消费
 operate1.OnEvent += delegate (object? sender, EventResult e)
@@ -44,8 +44,20 @@ LogHelper.Info(result.ToJson().JsonFormatting());
 
 while (true)
 {
-    //生产
-    operate2.Produce("测试", new Random().NextDouble().ToString());
+    for (int i = 0; i < 2; i++)
+    {
+        if (i == 0)
+        {
+            //生产
+            operate1.Produce("测试", new Random().NextDouble().ToString());
+        }
+        else
+        {
+            //生产
+            nettyServiceOperate.Send("测试");
+        }
+    }
+   
 }
 
 

+ 12 - 5
src/YSAI.DAQ/YSAI.Unility/ExtensionTool.cs

@@ -1357,14 +1357,21 @@ namespace YSAI.Unility
         /// <returns></returns>
         public static bool IsJson(this string json)
         {
-            JToken jToken = JToken.Parse(json);
-            if (jToken.Type != JTokenType.Object)
+            try
             {
-                return false;
+                JToken jToken = JToken.Parse(json);
+                if (jToken.Type != JTokenType.Object)
+                {
+                    return false;
+                }
+                else
+                {
+                    return true;
+                }
             }
-            else
+            catch
             {
-                return true;
+                return false;
             }
         }
 

+ 1 - 1
src/YSAI.DAQ/YSAI.Unility/YSAI.Unility.csproj

@@ -5,7 +5,7 @@
 		<ImplicitUsings>enable</ImplicitUsings>
 		<Nullable>enable</Nullable>
 		<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-		<Version>1.0.0.19</Version>
+		<Version>1.0.0.20</Version>
 		<Authors>Shun</Authors>
 		<Company>YSAI</Company>
 		<Product>SCADA</Product>