断线重连

master
smartwyy 6 months ago
parent 1e06e06259
commit 8eb803cbd4

@ -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<TH, TD, TE> : ChannelHandlerAdapter where TH
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientListenerHandler<TH, TD, TE>));
public TcpClient<TH, TD, TE> Client { get; set; }
public ClientListenerHandler(TcpClient<TH, TD, TE> client)
{
Client = client;
}
public override void ChannelRegistered(IChannelHandlerContext context)
{
base.ChannelRegistered(context);
@ -37,15 +41,13 @@ public class ClientListenerHandler<TH, TD, TE> : 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<TH, TD, TE> tcpClient = AppInfo.Container.Resolve<TcpClient<TH, TD, TE>>();
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)

@ -13,7 +13,9 @@ using log4net;
namespace HybirdFrameworkDriver.TcpClient;
public class TcpClient<TH, TD, TE> where TH : IChannelHandler where TD: ByteToMessageDecoder,new() where TE: ChannelHandlerAdapter, new()
public class TcpClient<TH, TD, TE> where TH : IChannelHandler
where TD : ByteToMessageDecoder, new()
where TE : ChannelHandlerAdapter, new()
{
private Bootstrap? _bootstrap;
@ -24,9 +26,10 @@ public class TcpClient<TH, TD, TE> where TH : IChannelHandler where TD: ByteToMe
public string Host { get; set; }
public int Port { get; set; }
private static readonly ILog Log = LogManager.GetLogger(typeof(TcpClient<TH, TD, TE>));
public void InitBootstrap(string host, int port)
public void InitBootstrap(string host, int port, Action? channelInactiveHandler = null)
{
Host = host;
Port = port;
@ -37,7 +40,7 @@ public class TcpClient<TH, TD, TE> where TH : IChannelHandler where TD: ByteToMe
.Option(ChannelOption.TcpNodelay, true)
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var clientListenerHandler = new ClientListenerHandler<TH, TD, TE>();
var clientListenerHandler = new ClientListenerHandler<TH, TD, TE>(this);
IChannelPipeline pipeline = channel.Pipeline;
// 监听器
@ -66,7 +69,6 @@ public class TcpClient<TH, TD, TE> where TH : IChannelHandler where TD: ByteToMe
private void ResolveHandler(IChannelPipeline pipeline)
{
List<Type> list = new List<Type>();
foreach (IComponentRegistration reg in AppInfo.Container.ComponentRegistry.Registrations)
@ -87,7 +89,7 @@ public class TcpClient<TH, TD, TE> 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) =>
@ -125,24 +127,24 @@ public class TcpClient<TH, TD, TE> where TH : IChannelHandler where TD: ByteToMe
public void Connect()
{
Connected = false;
int num = 1;
while (!Connected)
Log.Info($"begin to connect {Host}:{Port}");
while (Connected)
{
try
{
Task<IChannel> task = _bootstrap!.ConnectAsync(new IPEndPoint(IPAddress.Parse(Host), Port));
Channel = task.Result;
Connected = Channel.Open;
if (Connected)
}
catch (Exception e)
{
break;
Log.Info($"connect {Host}:{Port} {e}");
}
Thread.Sleep(5000);
num++;
Log.Info($"connect {Host}:{Port} {Connected}");
Thread.Sleep(1000);
}
}
}
Loading…
Cancel
Save