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