Exemplo n.º 1
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            memoryStream = GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            LocalConn           = localConn;
            RemoteConn          = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            kcp = Kcp.KcpCreate(RemoteConn, new IntPtr(LocalConn));

            SetOutput();
            Kcp.KcpNodelay(kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(kcp, 256, 256);
            Kcp.KcpSetmtu(kcp, 470);
            lastRecvTime = kService.TimeNow;
            createTime   = kService.TimeNow;
            Accept();
        }
Exemplo n.º 2
0
        public void HandleConnnect(uint remoteConn)
        {
            if (isConnected)
            {
                return;
            }

            RemoteConn = remoteConn;

            kcp = Kcp.KcpCreate(RemoteConn, new IntPtr(LocalConn));
            SetOutput();
            Kcp.KcpNodelay(kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(kcp, 256, 256);
            Kcp.KcpSetmtu(kcp, 470);

            isConnected  = true;
            lastRecvTime = GetService().TimeNow;

            HandleSend();
        }
Exemplo n.º 3
0
        public void Update()
        {
            if (IsDisposed)
            {
                return;
            }

            uint timeNow = GetService().TimeNow;

            // 如果还没连接上,发送连接请求
            if (!isConnected)
            {
                // 10秒没连接上则报错
                if (timeNow - createTime > 10 * 1000)
                {
                    OnError(ErrorCode.ERR_KcpCantConnect);
                    return;
                }

                if (timeNow - lastRecvTime < 500)
                {
                    return;
                }

                switch (ChannelType)
                {
                case ChannelType.Accept:
                    Accept();
                    break;

                case ChannelType.Connect:
                    Connect();
                    break;
                }

                return;
            }

            // 超时断开连接
            //if (timeNow - lastRecvTime > 40 * 1000)
            //{
            //	OnError(ErrorCode.ERR_KcpChannelTimeout);
            //	return;
            //}

            try
            {
                Kcp.KcpUpdate(kcp, timeNow);
            }
            catch (Exception e)
            {
                Log.Error(e);
                OnError(ErrorCode.ERR_SocketError);
                return;
            }


            if (kcp != IntPtr.Zero)
            {
                uint nextUpdateTime = Kcp.KcpCheck(kcp, timeNow);
                GetService().AddToUpdateNextTime(nextUpdateTime, Id);
            }
        }