Esempio n. 1
0
        private void ReRecvAckIdx(int notAckIdx)
        {
            int iEndNotAckIdx = notAckIdx;

            for (int i = 1; i < MAX_DIS_CONNECTED_FS_NUM; i++)
            {
                if ((notAckIdx + i) > _sendNewIdx)
                {
                    break;                                          //大于对方最新的发送序号
                }
                if (_recvPacks.ContainsKey(notAckIdx + i))
                {
                    break;                                          //已经有数据了
                }
                iEndNotAckIdx++;
            }
            Debug.Log("请求对方重发非ACK序号:" + notAckIdx + "-" + iEndNotAckIdx);
            Kcp_NotAckIndex msg = new Kcp_NotAckIndex();

            msg.Startindex = notAckIdx;
            msg.Endindex   = iEndNotAckIdx;

            KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.NotAckIndex);

            SocketSend(pack);
            _lastRecvNotAckTime++;
            if (_lastRecvNotAckIdx != notAckIdx || _lastRecvNotAckTime > RE_RECV_NOT_ACK_TIME)
            {
                _lastRecvNotAckIdx  = notAckIdx;
                _lastRecvNotAckTime = 0;
                UpCacheFSTime();
            }
        }
Esempio n. 2
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("继续运行==============================>");
 }
Esempio n. 3
0
        private void RcpPackSendData(int iSendIdx, int iRange)//包含了iSendIdx的iRange个包
        {
            if (!_sendPacks.ContainsKey(iSendIdx))
            {
                return;
            }
            iRange = Math.Min(iRange, iSendIdx - _sendAckIdx);
            if (iRange <= 0)
            {
                return;
            }
            // Debug.Log("RcpPackSendData=========>" + iSendIdx + ",iRange:" + iRange);
            int iTime     = iRange / MAX_DATA_CACHE + 1;
            int iStartIdx = iSendIdx - iRange;

            for (int i = 1; i <= iTime; i++)
            {
                int iSendIdx2, iRange2;
                if (iTime == i)
                {
                    iSendIdx2 = iSendIdx;
                    iRange2   = Math.Min(iRange - (i - 1) * MAX_DATA_CACHE, MAX_DATA_CACHE);
                    if (iRange2 <= 0)
                    {
                        return;
                    }
                }
                else
                {
                    iSendIdx2 = iStartIdx + MAX_DATA_CACHE * i;
                    iRange2   = MAX_DATA_CACHE;
                }
                Kcp_FsPackDatas msg = new Kcp_FsPackDatas();
                msg.Ackindex  = _recvAckIdx;
                msg.Sendindex = _sendIdx;
                for (int idx = (iSendIdx2 - iRange2 + 1); idx <= iSendIdx2; idx++)
                {
                    if (idx < 0)
                    {
                        continue;
                    }
                    if (!_sendPacks.ContainsKey(idx))
                    {
                        continue;
                    }
                    // Debug.Log("RcpPackSendData===>" + idx);
                    Kcp_OneFsPackData fsPackData = new Kcp_OneFsPackData();
                    fsPackData.Index  = idx;
                    fsPackData.Fsdata = ByteString.CopyFrom(_sendPacks[idx]);
                    msg.Fspackdata.Add(fsPackData);
                }
                //Debug.Log("RcpPackSendData=========>总序号:" + iSendIdx + ",总范围:" + iRange + ",序号" + iSendIdx2 + ",范围:" + iRange2 );
                KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.FsInfoS);
                SocketSend(pack);
            }
        }
Esempio n. 4
0
        /*public void Flush()
         * {
         *  if (_sendAckIdx < _sendIdx && _sendAckIdx > -1)
         *  {
         *      RcpPackSendData(_sendIdx, _range);
         *  }
         *  SetHadCheck();
         * }*/

        private void SynAckStatus()
        {
            if ((System.Environment.TickCount - _lastCheckTime > SYN_ACK_TIME && !_isSynAck) || System.Environment.TickCount - _lastSendTime > MUST_SYN_ACK_TIME)
            {
                //Debug.Log("同步Ack");
                _isSynAck = true;
                Kcp_FsPackDatas msg = new Kcp_FsPackDatas();
                msg.Ackindex  = _recvAckIdx;
                msg.Sendindex = _sendIdx;
                KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.SynAck);
                SocketSend(pack);
            }
        }
Esempio n. 5
0
 public void OnUpdate()
 {
     if (isConnectting)
     {
         if (System.Environment.TickCount - _lastCheckFailTime > (CONNECTED_FAIL_TIME * (_tryConnectTime + 1)))
         {
             _tryConnectTime++;
             if (_tryConnectTime < MAX_CONNECT_TIME)
             {
                 ConnectReceiveThread_Stop();//必须先关接收数据线程,再开.不然无法接收数据(待不需要关线程方案)
                 Kcp_Connect msg = new Kcp_Connect();
                 msg.Quickconnect = _quickConnect;
                 KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.Connected);
                 SetHadCheck("ReConnect");
                 var data = pack.Assembly();
                 socket.SendTo(data.Bytes, data.Length, SocketFlags.None, ipEnd);
                 _lastCheckFailTime = System.Environment.TickCount;
                 Debug.LogErrorFormat("[链接失败]第{0}次尝试重新连接", _tryConnectTime);
                 ConnectReceiveThread_Start();
             }
             else
             {
                 lock (netEventList)
                 {
                     netEventList.Enqueue(NetworkEvent.connectFail);
                 }
                 Debug.LogErrorFormat("[失败]第{0}次连接", _tryConnectTime);
                 isConnectting   = false;
                 _tryConnectTime = 0;
             }
         }
     }
     lock (netEventList)
     {
         while (netEventList.Count > 0)
         {
             var e = netEventList.Dequeue();
             listener.OnEvent(e);
         }
     }
     Check();
 }
Esempio n. 6
0
 private void TestPing()
 {
     if (OBTAIN_PING)
     {
         if (_canNextPing && System.Environment.TickCount - _lastPingTime > PING_CHECK_TIME)
         {
             _pingIdx++;
             Kcp_Ping msg = new Kcp_Ping();
             msg.Index = _pingIdx;
             KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.PingIndex);
             SocketSend(pack);
             _lastPingTime = System.Environment.TickCount;
             _canNextPing  = false;
         }
         else if (!_canNextPing && System.Environment.TickCount - _lastPingTime > PING_CHECK_TIME * 3)
         {
             _canNextPing = true;    //恰好是测试ping丢包时候,这里做个超时重测功能
         }
     }
 }
Esempio n. 7
0
        public void ConnectKCP(string host, int iport)
        {
            mHUDFPS = Facade.Instance.GetManager <HUDFPS>(FacadeConfig.ChildSystem_HUDFPS);
            _range  = BASE_DATA_CACHE;
            ipEnd   = new IPEndPoint(IPAddress.Parse(host), iport);
            socket  = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //socket.Connect(ipEnd);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

            serverEnd = (EndPoint)sender;
            Kcp_Connect msg = new Kcp_Connect();

            msg.Quickconnect = _quickConnect;
            KcpNetPack pack = KcpNetPack.SerializeToPack(msg, (ushort)Kcp_MsgID.Connected);

            SetHadCheck("Connect");
            var data = pack.Assembly();

            socket.SendTo(data.Bytes, data.Length, SocketFlags.None, ipEnd);
            isConnectting      = true;
            isConnected        = false;
            _lastCheckFailTime = System.Environment.TickCount;
            ConnectReceiveThread_Start();
        }