示例#1
0
 public void Disconnect(string sReason)
 {
     Debug.LogFormat("NetUdpSocket Disconnect:{0}", sReason);
     if (sReason != "Server")
     {
         Kcp_DisConnect msg = new Kcp_DisConnect();
         msg.Type = (int)Kcp_Net_Type.DscNormal;
         KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.DisConnected);
         SocketSend(pack);
     }
     isConnected   = false;
     isConnectting = false;
     try
     {
         //关闭线程
         ConnectReceiveThread_Stop();
         //最后关闭socket
         if (socket != null)
         {
             socket.Close();
         }
     }
     catch (Exception e)
     {
         Debug.LogErrorFormat("Disconnect:{0}", e);
     }
     finally
     {
         lock (netEventList)
         {
             netEventList.Enqueue(NetworkEvent.disconnected);
         }
     }
     //Debug.Log("继续运行==============================>");
 }
示例#2
0
        //==========Rcp核心算法======================================================================
        public void Input(byte[] buffer, int bufSize)
        {
            //List<NetPack> packs = DecodeBuffer(recvData, recvLen);
            //for (int i = 0; i < packs.Count; ++i)
            //    Input(packs[i]);
            int        offset1    = 0;
            int        restCount2 = bufSize;// 剩余缓存大小
            KcpNetPack pack       = new KcpNetPack();

            if (!pack.FillPack(buffer, ref offset1, ref restCount2))
            {
                return;
            }

            Kcp_MsgID msgID = (Kcp_MsgID)pack.HeadID;

            if (msgID == Kcp_MsgID.NotAckIndex)
            {
                Kcp_NotAckIndex msg = Kcp_NotAckIndex.Parser.ParseFrom(pack.BodyBuffer.Bytes);
                Debug.Log("回复对方缺失的序号:" + msg.Startindex + "-" + msg.Endindex + ",对方已确认接收成功的Ack序号:" + _sendAckIdx);
                if (_sendAckIdx >= msg.Endindex)
                {
                    return;
                }
                int iStartIndex = Math.Max(msg.Startindex, _sendAckIdx + 1);
                if (iStartIndex > msg.Endindex)
                {
                    return;
                }
                _reSendTime++;
                if (_lastReSendIdx != iStartIndex || _reSendTime > RE_SEND_ACK_TIME)
                {
                    _lastReSendIdx = iStartIndex;
                    _reSendTime    = 0;
                    RcpPackSendData(msg.Endindex, msg.Endindex - iStartIndex + 1);
                }
                _lastSendNotAckTime++;
                if (_lastSendNotAckIdx != iStartIndex || _lastSendNotAckTime > RE_SEND_NOT_ACK_TIME)
                {
                    _lastSendNotAckIdx  = iStartIndex;
                    _lastSendNotAckTime = 0;
                    UpCacheFSTime();
                }
            }
            else if (msgID == Kcp_MsgID.FsInfoS || msgID == Kcp_MsgID.SynAck)
            {
                Kcp_FsPackDatas msg = Kcp_FsPackDatas.Parser.ParseFrom(pack.BodyBuffer.Bytes);
                if (msg.Ackindex > _sendAckIdx)
                {
                    _sendAckIdx = msg.Ackindex;
                }
                if (msg.Sendindex > _sendNewIdx)
                {
                    _sendNewIdx = msg.Sendindex;
                }
                //if (msgID == Kcp_MsgID.FS_INFO_S)
                //    Debug.Log("最新的已ACK序号确认==>" + _lastSendAckIdx + ",对方已确认接收成功的Ack序号:" + _sendAckIdx + "," + msg.ackindex + ",对方最新的发送序号:" + _sendNewIdx + "," + msg.sendindex);
                //else
                //    Debug.Log("同步ACK状态==========>" + _lastSendAckIdx + ",对方已确认接收成功的Ack序号:" + _sendAckIdx + "," + msg.ackindex + ",对方最新的发送序号:" + _sendNewIdx + "," + msg.sendindex);
                for (int idx = _lastSendAckIdx; idx <= _sendAckIdx; idx++)
                {
                    _sendPacks.Remove(idx);//清理内存
                }
                _lastSendAckIdx = _sendAckIdx;

                int iMinIndex = -1;
                for (int i = 0; i < msg.Fspackdata.Count; i++)
                {
                    Kcp_OneFsPackData fsPackData = msg.Fspackdata[i];
                    //Debug.Log("*************=1==>" + fsPackData.index);
                    if (_recvPacks.ContainsKey(fsPackData.Index))
                    {
                        continue;
                    }
                    if (_recvAckIdx >= fsPackData.Index)
                    {
                        continue;
                    }
                    //Debug.Log("*************=2==>" + fsPackData.index);
                    int offset    = 0;
                    int restCount = 1024;// 剩余缓存大小
                    if (curPack2 == null)
                    {
                        curPack2 = new KcpNetPack();
                    }
                    if (curPack2.FillPack(fsPackData.Fsdata.ToByteArray(), ref offset, ref restCount))
                    {
                        if (fsPackData.Index < iMinIndex || iMinIndex == -1)
                        {
                            iMinIndex = fsPackData.Index;
                        }
                        _recvPacks[fsPackData.Index] = curPack2;
                        curPack2 = null;
                    }
                    else
                    {
                        RcpPackSendData(fsPackData.Index, 2);
                        Debug.LogErrorFormat("Input err: {0} ", fsPackData.Index);
                    }
                }
                CheckPreorderLose(iMinIndex);
                DownCacheFSTime();
            }
            else if (msgID == Kcp_MsgID.Connected || msgID == Kcp_MsgID.ReConnected)  //暂时只有一次握手
            {
                Kcp_Connect msg = Kcp_Connect.Parser.ParseFrom(pack.BodyBuffer.Bytes);
                if (_quickConnect != msg.Quickconnect)  //防重复
                {
                    lock (netEventList)
                    {
                        netEventList.Enqueue(NetworkEvent.connected);
                    }
                    _quickConnect = msg.Quickconnect;
                }
                isConnected     = true;
                isConnectting   = false;
                _tryConnectTime = 0;
                Debug.Log("链接成功:" + _quickConnect);
            }
            else if (msgID == Kcp_MsgID.ConnectedFail)  //暂时只有一次握手
            {
                lock (netEventList)
                {
                    netEventList.Enqueue(NetworkEvent.connectFail);
                }
                isConnected   = false;
                isConnectting = false;
                Debug.Log("链接失败:CONNECTED_FAIL:");
            }
            else if (msgID == Kcp_MsgID.DisConnected)  //暂时只有一次挥手
            {
                Kcp_DisConnect msg = Kcp_DisConnect.Parser.ParseFrom(pack.BodyBuffer.Bytes);
                if (msg.Type == (int)Kcp_Net_Type.DscErrAddr)
                {
                    Debug.Log("链接断开:DSC_ERR_ADDR:" + _quickConnect);
                    isConnectting = true;       //快速重连/身份重新认证/更新IP地址
                }
                else
                {
                    Disconnect("Server");
                }
                //Debug.Log("继续运行2==============================>");
            }
            else if (msgID == Kcp_MsgID.PingIndex)
            {
                Kcp_Ping msg = Kcp_Ping.Parser.ParseFrom(pack.BodyBuffer.Bytes);
                if (msg.Index == _pingIdx)
                {
                    Ping = System.Environment.TickCount - _lastPingTime;
                }
                UpdateNetInfo("");
                //Debug.Log("Ping==============================>" + Ping);
                _canNextPing = true;
            }
            else
            {
                Debug.LogErrorFormat("Input err2: {0} ", msgID);
                return;
            }
            SetHadCheck("Input");
        }