BindlLocal
parent
add66ce981
commit
065c76af4e
|
|
@ -32,7 +32,7 @@ namespace dotNetty_kcp
|
||||||
private IScheduleThread _scheduleThread;
|
private IScheduleThread _scheduleThread;
|
||||||
|
|
||||||
|
|
||||||
private static IChannel bindLocal(Bootstrap bootstrap,EndPoint localAddress = null)
|
private static IChannel bindLocal(Bootstrap bootstrap, EndPoint localAddress = null)
|
||||||
{
|
{
|
||||||
if (localAddress == null)
|
if (localAddress == null)
|
||||||
{
|
{
|
||||||
|
|
@ -44,22 +44,36 @@ namespace dotNetty_kcp
|
||||||
|
|
||||||
public System.Threading.Tasks.Task<IChannel> BindLocal()
|
public System.Threading.Tasks.Task<IChannel> BindLocal()
|
||||||
{
|
{
|
||||||
var localAddress = new IPEndPoint(IPAddress.Any, 0);
|
//var localAddress = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
var localAddress = new IPEndPoint(GetLocalIPAddress(), 0);
|
||||||
return bootstrap.BindAsync(localAddress);
|
return bootstrap.BindAsync(localAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(ChannelConfig channelConfig,ExecutorPool executorPool,IEventLoopGroup eventLoopGroup)
|
public static string GetLocalIPAddress()
|
||||||
{
|
{
|
||||||
if(channelConfig.UseConvChannel)
|
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
foreach (var ip in host.AddressList)
|
||||||
|
{
|
||||||
|
if (ip.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
return ip.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Exception("No network adapters with an IPv4 address in the system!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(ChannelConfig channelConfig, ExecutorPool executorPool, IEventLoopGroup eventLoopGroup)
|
||||||
|
{
|
||||||
|
if (channelConfig.UseConvChannel)
|
||||||
{
|
{
|
||||||
var convIndex = 0;
|
var convIndex = 0;
|
||||||
if(channelConfig.Crc32Check)
|
if (channelConfig.Crc32Check)
|
||||||
{
|
{
|
||||||
convIndex+=Ukcp.HEADER_CRC;
|
convIndex += Ukcp.HEADER_CRC;
|
||||||
}
|
}
|
||||||
if(channelConfig.FecDataShardCount!=0&&channelConfig.FecParityShardCount!=0)
|
if (channelConfig.FecDataShardCount != 0 && channelConfig.FecParityShardCount != 0)
|
||||||
{
|
{
|
||||||
convIndex+= Fec.fecHeaderSizePlus2;
|
convIndex += Fec.fecHeaderSizePlus2;
|
||||||
}
|
}
|
||||||
_channelManager = new ClientConvChannelManager(convIndex);
|
_channelManager = new ClientConvChannelManager(convIndex);
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +95,7 @@ namespace dotNetty_kcp
|
||||||
bootstrap.Handler(new ActionChannelInitializer<SocketDatagramChannel>(channel =>
|
bootstrap.Handler(new ActionChannelInitializer<SocketDatagramChannel>(channel =>
|
||||||
{
|
{
|
||||||
var pipeline = channel.Pipeline;
|
var pipeline = channel.Pipeline;
|
||||||
pipeline.AddLast(new ClientChannelHandler(_channelManager,channelConfig));
|
pipeline.AddLast(new ClientChannelHandler(_channelManager, channelConfig));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,7 +105,7 @@ namespace dotNetty_kcp
|
||||||
{
|
{
|
||||||
var executorPool = new ExecutorPool();
|
var executorPool = new ExecutorPool();
|
||||||
executorPool.CreateMessageExecutor();
|
executorPool.CreateMessageExecutor();
|
||||||
init(channelConfig,executorPool,new MultithreadEventLoopGroup());
|
init(channelConfig, executorPool, new MultithreadEventLoopGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -101,12 +115,13 @@ namespace dotNetty_kcp
|
||||||
* 在4G切换为wifi等场景使用
|
* 在4G切换为wifi等场景使用
|
||||||
* @param ukcp
|
* @param ukcp
|
||||||
*/
|
*/
|
||||||
public void reconnect(Ukcp ukcp){
|
public void reconnect(Ukcp ukcp)
|
||||||
|
{
|
||||||
if (!(_channelManager is ServerConvChannelManager))
|
if (!(_channelManager is ServerConvChannelManager))
|
||||||
{
|
{
|
||||||
throw new Exception("reconnect can only be used in convChannel");
|
throw new Exception("reconnect can only be used in convChannel");
|
||||||
}
|
}
|
||||||
ukcp.IMessageExecutor.execute(new ReconnectTask(ukcp,bootstrap));
|
ukcp.IMessageExecutor.execute(new ReconnectTask(ukcp, bootstrap));
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ReconnectTask : ITask
|
private class ReconnectTask : ITask
|
||||||
|
|
@ -129,7 +144,7 @@ namespace dotNetty_kcp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Ukcp connect(IChannel localChannel,EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
|
public Ukcp connect(IChannel localChannel, EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
|
||||||
{
|
{
|
||||||
|
|
||||||
KcpOutput kcpOutput = new KcpOutPutImp();
|
KcpOutput kcpOutput = new KcpOutPutImp();
|
||||||
|
|
@ -146,22 +161,22 @@ namespace dotNetty_kcp
|
||||||
var user = new User(localChannel, remoteAddress, localChannel.LocalAddress);
|
var user = new User(localChannel, remoteAddress, localChannel.LocalAddress);
|
||||||
ukcp.user(user);
|
ukcp.user(user);
|
||||||
|
|
||||||
_channelManager.New(localChannel.LocalAddress, ukcp,null);
|
_channelManager.New(localChannel.LocalAddress, ukcp, null);
|
||||||
|
|
||||||
_messageExecutor.execute(new ConnectTask(ukcp, kcpListener));
|
_messageExecutor.execute(new ConnectTask(ukcp, kcpListener));
|
||||||
|
|
||||||
var scheduleTask = new ScheduleTask( _channelManager, ukcp,_scheduleThread);
|
var scheduleTask = new ScheduleTask(_channelManager, ukcp, _scheduleThread);
|
||||||
|
|
||||||
_scheduleThread.schedule(scheduleTask,TimeSpan.FromMilliseconds(ukcp.getInterval()));
|
_scheduleThread.schedule(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval()));
|
||||||
return ukcp;
|
return ukcp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接一个服务器
|
* 连接一个服务器
|
||||||
*/
|
*/
|
||||||
public Ukcp connect(EndPoint localAddress,EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
|
public Ukcp connect(EndPoint localAddress, EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
|
||||||
{
|
{
|
||||||
var channel = bindLocal(bootstrap,localAddress);
|
var channel = bindLocal(bootstrap, localAddress);
|
||||||
return connect(channel, remoteAddress, channelConfig, kcpListener);
|
return connect(channel, remoteAddress, channelConfig, kcpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,7 +197,7 @@ namespace dotNetty_kcp
|
||||||
ukcp.close();
|
ukcp.close();
|
||||||
}
|
}
|
||||||
_executorPool?.stop(false);
|
_executorPool?.stop(false);
|
||||||
if (_eventLoopGroup != null&&!_eventLoopGroup.IsShuttingDown)
|
if (_eventLoopGroup != null && !_eventLoopGroup.IsShuttingDown)
|
||||||
{
|
{
|
||||||
_eventLoopGroup?.ShutdownGracefullyAsync().Wait();
|
_eventLoopGroup?.ShutdownGracefullyAsync().Wait();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue