Example #1
0
        IKcpSession IKcpSessionBuilder.Build(uint conv, EndPoint remoteAddress, IKcpClosable closable)
        {
            if (this.isStop)
            {
                throw new Exception("The channel has stopped working");
            }

            if (this.isInvalid)
            {
                throw new Exception("Channel failure, call the StopAsync method to release the channel");
            }

            var localAddress = this.session.LocalAddress;
            var pipeline     = base.CreateFilterPipeline();

            return(new KcpSession(conv, remoteAddress, localAddress, options, session, this, pipeline, closable));
        }
Example #2
0
        public KcpSession(uint conv, EndPoint remoteAddress, EndPoint localAddress, KcpOptions options, IUdpSession udpSession, IEventSubscriber subscriber, FilterPipeline <IKcpSession> pipeline, IKcpClosable closable)
        {
            this.Id            = IdGeneratorHelper.GetNextId();
            this.Conv          = conv;
            this.LocalAddress  = localAddress;
            this.RemoteAddress = remoteAddress;

            this.Order      = options.Order;
            this.udpSession = udpSession;
            this.subscriber = subscriber;
            this.Pipeline   = pipeline;
            this.closable   = closable;

            var littleEndian = this.Order == BinaryOrder.LittleEndian;

            this.kcpKit = new KcpKit(conv, littleEndian, this.MemoryPool);
            this.kcpKit.SettingMtu(options.Mtu);
            this.kcpKit.SettingNoDelay(options.NoDelay);
            this.kcpKit.SettingWndSize(options.WndSize);
            this.kcpKit.SettingStreamMode(options.StreamMode);
            this.kcpKit.SettingReservedSize(options.ReservedSize);

            this.kcpKit.onRcv += this.OnKcpRcvEvent;
            this.kcpKit.onSnd += this.OnKcpSndEvent;

            this.kcpOperators = BinaryOrderOperatorsFactory.GetOperators(this.Order);
            this.rcvPool      = new WrappedMemoryPool(this.MemoryPool, MemoryFlag.Kcp);
            this.sndPool      = new WrappedMemoryPool(this.MemoryPool, MemoryFlag.Kcp);

            this.sndMemory       = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Kcp);
            this.readerUdpMemory = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Udp);
            this.writerUdpMemory = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Udp);

            this.readerFlushDelegate = (pos, endPos) => { };
            this.writerFlushDelegate = this.OnWriterComplete;

            this.runnableUnitDelegate = this.Update;
            this.subscriber.Register(this.runnableUnitDelegate);

            this.Pipeline.OnTransportActive(this);
        }