From 8eb803cbd482a21d168f5cd529e6ee443cc38d20 Mon Sep 17 00:00:00 2001
From: smartwyy <645583145@qq.com>
Date: Wed, 15 May 2024 20:53:32 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=AD=E7=BA=BF=E9=87=8D=E8=BF=9E?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TcpClient/ClientListenerHandler.cs | 20 ++++----
HybirdFrameworkDriver/TcpClient/TcpClient.cs | 50 ++++++++++---------
2 files changed, 37 insertions(+), 33 deletions(-)
diff --git a/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs
index 7a934aa..0388c05 100644
--- a/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs
+++ b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs
@@ -1,9 +1,6 @@
-using System.Net;
-using Autofac;
using DotNetty.Codecs;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Channels;
-using HybirdFrameworkCore.Autofac;
using HybirdFrameworkDriver.Session;
using log4net;
@@ -15,6 +12,13 @@ public class ClientListenerHandler
: ChannelHandlerAdapter where TH
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientListenerHandler | ));
+ public TcpClient | Client { get; set; }
+
+ public ClientListenerHandler(TcpClient | client)
+ {
+ Client = client;
+ }
+
public override void ChannelRegistered(IChannelHandlerContext context)
{
base.ChannelRegistered(context);
@@ -37,15 +41,13 @@ public class ClientListenerHandler | : ChannelHandlerAdapter where TH
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();
+ context.Channel.CloseAsync().Wait();
+ context.Channel.CloseCompletion.Wait();
+
+ new Thread(new ThreadStart(Client.Connect)).Start();
}
public override void UserEventTriggered(IChannelHandlerContext context, object evt)
diff --git a/HybirdFrameworkDriver/TcpClient/TcpClient.cs b/HybirdFrameworkDriver/TcpClient/TcpClient.cs
index 08820da..69affb5 100644
--- a/HybirdFrameworkDriver/TcpClient/TcpClient.cs
+++ b/HybirdFrameworkDriver/TcpClient/TcpClient.cs
@@ -13,20 +13,23 @@ using log4net;
namespace HybirdFrameworkDriver.TcpClient;
-public class TcpClient where TH : IChannelHandler where TD: ByteToMessageDecoder,new() where TE: ChannelHandlerAdapter, new()
+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)
+ private static readonly ILog Log = LogManager.GetLogger(typeof(TcpClient | ));
+
+ public void InitBootstrap(string host, int port, Action? channelInactiveHandler = null)
{
Host = host;
Port = port;
@@ -37,7 +40,7 @@ public class TcpClient | where TH : IChannelHandler where TD: ByteToMe
.Option(ChannelOption.TcpNodelay, true)
.Handler(new ActionChannelInitializer(channel =>
{
- var clientListenerHandler = new ClientListenerHandler();
+ var clientListenerHandler = new ClientListenerHandler | (this);
IChannelPipeline pipeline = channel.Pipeline;
// 监听器
@@ -66,9 +69,8 @@ public class TcpClient | where TH : IChannelHandler where TD: ByteToMe
private void ResolveHandler(IChannelPipeline pipeline)
{
-
List list = new List();
-
+
foreach (IComponentRegistration reg in AppInfo.Container.ComponentRegistry.Registrations)
{
foreach (Service service in reg.Services)
@@ -87,9 +89,9 @@ public class TcpClient where TH : IChannelHandler where TD: ByteToMe
foreach (var type in list)
{
object resolve = AppInfo.Container.Resolve(type);
- handlers.Add((TH) resolve);
+ handlers.Add((TH)resolve);
}
-
+
handlers.Sort((handler, msgHandler) =>
{
OrderAttribute? orderAttribute1 = handler.GetType().GetCustomAttribute();
@@ -125,24 +127,24 @@ public class TcpClient where TH : IChannelHandler where TD: ByteToMe
public void Connect()
{
Connected = false;
- int num = 1;
- while (!Connected)
- {
- Task task = _bootstrap!.ConnectAsync(new IPEndPoint(IPAddress.Parse(Host), Port));
+ Log.Info($"begin to connect {Host}:{Port}");
- Channel = task.Result;
- Connected = Channel.Open;
-
- if (Connected)
+ while (Connected)
+ {
+ try
+ {
+ Task task = _bootstrap!.ConnectAsync(new IPEndPoint(IPAddress.Parse(Host), Port));
+ Channel = task.Result;
+ Connected = Channel.Open;
+ }
+ catch (Exception e)
{
- break;
+ Log.Info($"connect {Host}:{Port} {e}");
}
- Thread.Sleep(5000);
- num++;
+ Log.Info($"connect {Host}:{Port} {Connected}");
+ Thread.Sleep(1000);
}
-
+
}
-
-
}
\ No newline at end of file
| | | | |