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); _messageExecutor.execute(new ConnectTask(ukcp, kcpListener)); var scheduleTask = new ScheduleTask(_channelManager, ukcp, _scheduleThread); _scheduleThread.schedule(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval())); return(ukcp); }
public void execute() { try { long now = _ukcp.currentMs(); //判断连接是否关闭 if (_ukcp.TimeoutMillis != 0 && now - _ukcp.TimeoutMillis > _ukcp.LastRecieveTime) { _ukcp.internalClose(); } 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); _scheduleThread.schedule(this, TimeSpan.FromMilliseconds(timeLeft)); return; } //long start = System.currentTimeMillis(); long next = _ukcp.flush(now); //System.err.println(next); //System.out.println("耗时 "+(System.currentTimeMillis()-start)); _scheduleThread.schedule(this, TimeSpan.FromMilliseconds(next)); //检测写缓冲区 如果能写则触发写事件 if (!_ukcp.WriteQueue.IsEmpty && _ukcp.canSend(false) ) { _ukcp.notifyWriteEvent(); } } catch (Exception e) { Console.WriteLine(e); } }
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, _scheduleThread); _scheduleThread.schedule(scheduleTask, TimeSpan.FromMilliseconds(ukcp.getInterval())); }