public Ukcp connect(IChannel localChannel, EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener) { KcpOutput kcpOutput = new KcpOutPutImp(); ReedSolomon reedSolomon = null; if (channelConfig.FecDataShardCount != 0 && channelConfig.FecParityShardCount != 0) { reedSolomon = ReedSolomon.create(channelConfig.FecDataShardCount, channelConfig.FecParityShardCount); } var _messageExecutor = _executorPool.GetAutoMessageExecutor(); var ukcp = new Ukcp(kcpOutput, kcpListener, _messageExecutor, reedSolomon, channelConfig); var user = new User(localChannel, remoteAddress, localChannel.LocalAddress); ukcp.user(user); _channelManager.New(localChannel.LocalAddress, ukcp, null); var scheduleTask = new ScheduleTask(_channelManager, ukcp); KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval())); return(ukcp); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var msg = (DatagramPacket)message; var channel = context.Channel; var ukcp = _channelManager.get(msg); var content = msg.Content; User user; if (ukcp != null) { user = ukcp.user(); //每次收到消息重绑定地址 user.RemoteAddress = msg.Sender; ukcp.read(content); return; } //如果是新连接第一个包的sn必须为0 var sn = getSn(content, _channelConfig); if (sn != 0) { msg.Release(); return; } var messageExecutor = _executorPool.GetAutoMessageExecutor(); KcpOutput kcpOutput = new KcpOutPutImp(); ReedSolomon reedSolomon = null; if (_channelConfig.FecDataShardCount != 0 && _channelConfig.FecParityShardCount != 0) { reedSolomon = ReedSolomon.create(_channelConfig.FecDataShardCount, _channelConfig.FecParityShardCount); } ukcp = new Ukcp(kcpOutput, _kcpListener, messageExecutor, reedSolomon, _channelConfig); user = new User(channel, msg.Sender, msg.Recipient); ukcp.user(user); _channelManager.New(msg.Sender, ukcp, msg); messageExecutor.execute(new ConnectTask(ukcp, _kcpListener)); ukcp.read(content); var scheduleTask = new ScheduleTask(_channelManager, ukcp); KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval())); }
public override void ChannelRead(IChannelHandlerContext context, object message) { long last_ts = DateTime.Now.Ticks; var msg = (DatagramPacket)message; var channel = context.Channel; var ukcp = _channelManager.get(msg); var content = msg.Content; User user; if (ukcp != null) { user = ukcp.user(); //每次收到消息重绑定地址 user.RemoteAddress = msg.Sender; ukcp.read(content); //Console.WriteLine(string.Format("(LAGS1){0}", (DateTime.Now.Ticks - last_ts)/10000.0)); return; } var messageExecutor = _executorPool.GetAutoMessageExecutor(); KcpOutput kcpOutput = new KcpOutPutImp(); ReedSolomon reedSolomon = null; if (_channelConfig.FecDataShardCount != 0 && _channelConfig.FecParityShardCount != 0) { reedSolomon = ReedSolomon.create(_channelConfig.FecDataShardCount, _channelConfig.FecParityShardCount); } ukcp = new Ukcp(kcpOutput, _kcpListener, messageExecutor, reedSolomon, _channelConfig); user = new User(channel, msg.Sender, msg.Recipient); ukcp.user(user); _channelManager.New(msg.Sender, ukcp, msg); ukcp.connect(); ukcp.read(content); var scheduleTask = new ScheduleTask(_channelManager, ukcp); //Console.WriteLine(ukcp.getInterval()); KcpUntils.scheduleHashedWheel(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval())); Console.WriteLine(string.Format("(LAGS2){0}", (DateTime.Now.Ticks - last_ts) / 10000.0)); }
public override void execute() { try { long now = _ukcp.currentMs(); //判断连接是否关闭 if (_ukcp.ChannelConfig.TimeoutMillis != 0 && now - _ukcp.ChannelConfig.TimeoutMillis > _ukcp.LastRecieveTime) { _ukcp.close(); } if (!_ukcp.isActive()) { var user = _ukcp.user(); //抛回网络线程处理连接删除 user.Channel.EventLoop.Execute(() => { _channelManager.del(_ukcp); }); _ukcp.release(); return; } long timeLeft = _ukcp.getTsUpdate() - now; //判断执行时间是否到了 if (timeLeft > 0) { //System.err.println(timeLeft); KcpUntils.scheduleHashedWheel(this, TimeSpan.FromMilliseconds(timeLeft)); return; } //long start = System.currentTimeMillis(); long next = _ukcp.flush(now); //System.err.println(next); //System.out.println("耗时 "+(System.currentTimeMillis()-start)); KcpUntils.scheduleHashedWheel(this, TimeSpan.FromMilliseconds(next)); //检测写缓冲区 如果能写则触发写事件 if (!_ukcp.WriteQueue.IsEmpty && _ukcp.canSend(false) ) { _ukcp.notifyWriteEvent(); } } catch (Exception e) { Console.WriteLine(e); } }