|
|
@@ -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));
|
|
|
+ }
|
|
|
}
|
|
|
}
|