Exemplo n.º 1
0
 public ServerChannelHandler(IChannelManager channelManager, ChannelConfig channelConfig, IExecutorPool executorPool, KcpListener kcpListener)
 {
     _channelManager = channelManager;
     _channelConfig  = channelConfig;
     _executorPool   = executorPool;
     _kcpListener    = kcpListener;
 }
Exemplo n.º 2
0
 public ServerChannelHandler(IChannelManager channelManager, ChannelConfig channelConfig, IExecutorPool executorPool, KcpListener kcpListener, IScheduleThread scheduleThread)
 {
     _channelManager = channelManager;
     _channelConfig  = channelConfig;
     _executorPool   = executorPool;
     _kcpListener    = kcpListener;
     _scheduleThread = scheduleThread;
 }
Exemplo n.º 3
0
 public KcpOnUdp(string host, int port, int conv, KcpListener listener)
 {
     client        = new UdpClient(host, port);
     kcp           = new Kcp(conv, this);
     KcpListener   = listener;
     this.received = new LinkedList <ByteBuf>();
     this.sendList = new LinkedList <ByteBuf>();
 }
Exemplo n.º 4
0
 public void init(int workSize, KcpListener kcpListener, ChannelConfig channelConfig, params int[] ports)
 {
     _executorPool = new ExecutorPool();
     for (int i = 0; i < workSize; i++)
     {
         _executorPool.CreateMessageExecutor();
     }
     init(_executorPool, kcpListener, channelConfig, ports);
 }
Exemplo n.º 5
0
        public void init(IExecutorPool executorPool, KcpListener kcpListener, ChannelConfig channelConfig, params int[] ports)
        {
            if (channelConfig.UseConvChannel)
            {
                int convIndex = 0;
                if (channelConfig.Crc32Check)
                {
                    convIndex += Ukcp.HEADER_CRC;
                }
                if (channelConfig.FecDataShardCount != 0 && channelConfig.FecParityShardCount != 0)
                {
                    convIndex += Fec.fecHeaderSizePlus2;
                }
                _channelManager = new ServerConvChannelManager(convIndex);
            }
            else
            {
                _channelManager = new ServerEndPointChannelManager();
            }

            int cpuNum    = Environment.ProcessorCount;
            int bindTimes = cpuNum;

            _eventLoopGroup = new MultithreadEventLoopGroup(cpuNum);
            _scheduleThread = new HashedWheelScheduleThread();

            _bootstrap = new Bootstrap();
            //TODO epoll模型 服务器端怎么支持?得试试成功没有
            _bootstrap.Option(ChannelOption.SoReuseport, true);

            _bootstrap.Option(ChannelOption.SoReuseaddr, true);
            _bootstrap.Group(_eventLoopGroup);
            _bootstrap.ChannelFactory(() => new SocketDatagramChannel(AddressFamily.InterNetwork));
            _bootstrap.Handler(new ActionChannelInitializer <SocketDatagramChannel>(channel =>
            {
                var pipeline = channel.Pipeline;
                pipeline.AddLast(new ServerChannelHandler(_channelManager, channelConfig, executorPool, kcpListener, _scheduleThread));
            }));

            foreach (var port in ports)
            {
                //                for (int i = 0; i < bindTimes; i++) {
                var task = Task.Run(() => _bootstrap.BindAsync(port));
                task.Wait();
                var channel = task.Result;
                _localAddress.Add(channel);
//                }
            }

            //TODO 如何启动关闭进程的钩子??
        }
Exemplo n.º 6
0
        /**
         * Creates a new instance.
         *
         * @param output output for kcp
         */
        public Ukcp(KcpOutput output, KcpListener kcpListener, IMessageExecutor iMessageExecutor,
                    ReedSolomon reedSolomon, ChannelConfig channelConfig)
        {
            this._channelConfig    = channelConfig;
            this.kcp               = new Kcp(channelConfig.Conv, output);
            this.active            = true;
            this._kcpListener      = kcpListener;
            this._iMessageExecutor = iMessageExecutor;
            //默认2<<11   可以修改
            _writeQueue = new MpscArrayQueue <IByteBuffer>(2 << 10);
            _readQueue  = new MpscArrayQueue <IByteBuffer>(2 << 10);
            //recieveList = new SpscLinkedQueue<>();
            int headerSize = 0;

            if (channelConfig.KcpTag)
            {
                headerSize += KCP_TAG;
            }

            //init crc32
            if (channelConfig.Crc32Check)
            {
                var kcpOutput = kcp.Output;
                kcpOutput   = new Crc32OutPut(kcpOutput, headerSize);
                kcp.Output  = kcpOutput;
                headerSize += HEADER_CRC;
            }

            //init fec
            if (reedSolomon != null)
            {
                var kcpOutput = kcp.Output;
                _fecEncode  = new FecEncode(headerSize, reedSolomon, channelConfig.Mtu);
                _fecDecode  = new FecDecode(3 * reedSolomon.getTotalShardCount(), reedSolomon, channelConfig.Mtu);
                kcpOutput   = new FecOutPut(kcpOutput, _fecEncode);
                kcp.Output  = kcpOutput;
                headerSize += Fec.fecHeaderSizePlus2;
            }

            kcp.setReserved(headerSize);
            intKcpConfig(channelConfig);
        }
Exemplo n.º 7
0
        /**
         * 连接一个服务器
         */
        public Ukcp connect(EndPoint remoteAddress, ChannelConfig channelConfig, KcpListener kcpListener)
        {
            var channel = bindLocal();

            return(connect(channel, remoteAddress, channelConfig, kcpListener));
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
 public ConnectTask(Ukcp ukcp, KcpListener listener)
 {
     _ukcp     = ukcp;
     _listener = listener;
 }