示例#1
0
        private void Recv(string msg)
        {
            // 解析包Token
            Packet packet = JsonUtility.Decode <Packet>(msg);

            if (packet.ack == 0)
            {
                // 收到新的数据包后发送回包
                Send(packet, false, true);

                bool isNew = true; // 判断是否新的数据包

                // 接收到的序号
                if (packet.sn >= RecvSN + 1)
                {
                    // 说明:
                    // 1.序号等于现有序号加一说明没有跳包正常接收.
                    // 2.序号大于现有序号加一说明存在丢包,把丢失的包信息保存起来.

                    // 添加丢失的包序号
                    long recv   = RecvSN + 1;
                    long recvsn = packet.sn;
                    for (long i = recv; i < recvsn; i++)
                    {
                        LostRecv.Add(i);
                    }
                    // 当前接收到的包的序号
                    RecvSN = packet.sn;
                }
                else
                {
                    // 说明:当回包验证没法完成时,客户端会重新发包,这时如果已经处理了该包则不用再次处理!

                    // 接收到的序号小于等于现有序号表示该数据包已经接收过或上回丢失的数据包。
                    long recv = -1;
                    recv = LostRecv.Find((it) => it == packet.sn);
                    // 没有找到丢失的包序号则判定该包是客户端重复发送的数据包
                    if (recv == -1)
                    {
                        isNew = false;
                    }
                }

                if (isNew)
                {
                    // 处理新包
                    Packet newPacket = PakcetFactory.Create(packet.token, msg);
                    if (newPacket != null)
                    {
                        PacketProcess.Process(newPacket);
                    }
                }
            }
            else
            {
                // 收到回包后删除发送成功的数据包
                BeenSent.RemoveAll((it) => it.sn == packet.sn);
            }
        }
示例#2
0
        public PiplineClient(IPEndPoint sendPoint, IPEndPoint recvPoint, float timeOut = 1.0f)
        {
            mRecvPoint = recvPoint;
            mSendPoint = sendPoint;
            SendClient = new UdpClient();
            RecvClient = new UdpClient(mRecvPoint);

            RecvMsgs = new Queue <string>();

            LostRecv = new List <long>();
            BeenSent = new List <Packet>();

            PakcetFactory = new PacketFactory();
            PacketProcess = new PacketProcess();

            TimeOut = timeOut;

            // 开始接收数据
            RecvClient.BeginReceive(CallBackRecvive, RecvClient);
        }