Exemple #1
0
        private void OnRecive(IAsyncResult ar)
        {
            if (client == null)
            {
                return;
            }

            try
            {
                byte[] buf = client.EndReceive(ar, ref remoteEndPoint);

                if (buf == null || buf.Length <= 0)
                {
                    Debug.LogError("Recive buff is null");
                    return;
                }

                if (buf.Length < KCP.IKCP_OVERHEAD)
                {
                    return;
                }

                UInt32 ts   = 0;
                UInt32 conv = 0;
                byte   cmd  = 0;

                KCP.ikcp_decode32u(buf, 0, ref conv);

                if (this.conv != conv)
                {
                    return;
                }

                KCP.ikcp_decode8u(buf, 4, ref cmd);

                if (cmd == KCP.IKCP_CMD_ACK)
                {
                    //拦截ack包中的时间戳,作为ping值计算
                    KCP.ikcp_decode32u(buf, 8, ref ts);
                    App.Trigger(EventName.Ping, (SystemTime.Clock() - ts));
                }

                //推进kcp处理
                channel.Input(buf);
                client.BeginReceive(OnRecive, null);
            }
            catch (Exception)
            {
                Debug.LogError("host is closed.");
                Dispose();
            }
        }