Пример #1
0
        void SendCloseStreamPacket()
        {
            CloseStreamPacket closeStreamPacket = new CloseStreamPacket(Conn.ConnectId, Conn.MyRoute.LocalClientId, Sequence);

            closeStreamPacket.DstHost = DstHost;
            closeStreamPacket.DstPort = DstPort;

            //try
            Send(closeStreamPacket.MyDatagramPacket);
            Send(closeStreamPacket.MyDatagramPacket);
        }
Пример #2
0
        public void OnReceivePacket(DatagramPacket datagramPacket)
        {
            DataPacket dataPacket;

            if (datagramPacket != null)
            {
                if (Conn.IsConnected)
                {
                    int ver   = PacketCheck.CheckVer(datagramPacket);
                    int sType = PacketCheck.CheckSType(datagramPacket);

                    if (ver == RUDPConfig.ProtocalVer)
                    {
                        Conn.Live();
                        if (sType == PacketType.DataPacket)
                        {
                            dataPacket = new DataPacket(datagramPacket);
                            int timeId = dataPacket.TimeId;

                            SendRecord record = Conn.MyClientControl.SendRecordTableRemote[timeId];
                            if (record == null)
                            {
                                record        = new SendRecord();
                                record.TimeId = timeId;
                                Conn.MyClientControl.SendRecordTableRemote.Add(timeId, record);
                            }
                            record.AddSent(dataPacket.Data.Length);

                            if (timeId > CurrentRemoteTimeId)
                            {
                                CurrentRemoteTimeId = timeId;
                            }

                            int sequence = dataPacket.Sequence;

                            Conn.MySender.SendAckDelay(dataPacket.Sequence);
                            if (sequence > LastRead)
                            {
                                lock (AvailOb)
                                {
                                    ReceiveTable.Add(sequence, dataPacket);
                                    if (ReceiveTable.ContainsKey(LastRead + 1))
                                    {
                                        Monitor.Pulse(AvailOb);
                                    }
                                }
                            }
                        }
                        else if (sType == PacketType.AckListPacket)
                        {
                            AckListPacket ackListPacket = new AckListPacket(datagramPacket);

                            int lastRead3 = ackListPacket.LastRead;
                            if (lastRead3 > LastRead2)
                            {
                                LastRead2 = lastRead3;
                            }

                            List <int> ackList = ackListPacket.AckList;
                            foreach (int sequence in ackList)
                            {
                                Conn.MySender.RemoveSentAck(sequence);
                            }

                            SendRecord rc1 = Conn.MyClientControl.GetSendRecord(ackListPacket.R1);
                            if (rc1 != null && ackListPacket.S1 > rc1.AckedSize)
                            {
                                rc1.AckedSize = ackListPacket.S1;
                            }

                            SendRecord rc2 = Conn.MyClientControl.GetSendRecord(ackListPacket.R2);
                            if (rc2 != null && ackListPacket.S2 > rc2.AckedSize)
                            {
                                rc2.AckedSize = ackListPacket.S2;
                            }

                            SendRecord rc3 = Conn.MyClientControl.GetSendRecord(ackListPacket.R3);
                            if (rc3 != null && ackListPacket.S3 > rc3.AckedSize)
                            {
                                rc3.AckedSize = ackListPacket.S3;
                            }

                            if (CheckWin())
                            {
                                Conn.MySender.Play();
                            }
                        }
                        else if (sType == PacketType.CloseStreamPacket)
                        {
                            CloseStreamPacket closeStreamPacket = new CloseStreamPacket(datagramPacket);
                            ReceviedClose = true;
                            int n = closeStreamPacket.CloseOffset;
                            CloseRemoteStream(n);
                        }
                        else if (sType == PacketType.CloseConnPacket)
                        {
                            CloseConnPacket closeConnPacket = new CloseConnPacket(datagramPacket);
                            Conn.CloseRemote();
                        }
                        else
                        {
                            throw new Exception("Unknown packet type");
                        }
                    }
                }
            }
        }
Пример #3
0
        public void OnReceivePacket(DatagramPacket datagramPacket)
        {
            DataPacket dataPacket;
            if (datagramPacket != null)
            {
                if (Conn.IsConnected)
                {
                    int ver = PacketCheck.CheckVer(datagramPacket);
                    int sType = PacketCheck.CheckSType(datagramPacket);

                    if (ver == RUDPConfig.ProtocalVer)
                    {
                        Conn.Live();
                        if (sType == PacketType.DataPacket)
                        {
                            dataPacket = new DataPacket(datagramPacket);
                            int timeId = dataPacket.TimeId;

                            SendRecord record = Conn.MyClientControl.SendRecordTableRemote[timeId];
                            if (record == null)
                            {
                                record = new SendRecord();
                                record.TimeId = timeId;
                                Conn.MyClientControl.SendRecordTableRemote.Add(timeId, record);
                            }
                            record.AddSent(dataPacket.Data.Length);

                            if (timeId > CurrentRemoteTimeId)
                            {
                                CurrentRemoteTimeId = timeId;
                            }

                            int sequence = dataPacket.Sequence;

                            Conn.MySender.SendAckDelay(dataPacket.Sequence);
                            if (sequence > LastRead)
                            {
                                lock (AvailOb)
                                {
                                    ReceiveTable.Add(sequence, dataPacket);
                                    if (ReceiveTable.ContainsKey(LastRead + 1))
                                    {
                                        Monitor.Pulse(AvailOb);
                                    }
                                }
                            }
                        }
                        else if (sType == PacketType.AckListPacket)
                        {
                            AckListPacket ackListPacket = new AckListPacket(datagramPacket);

                            int lastRead3 = ackListPacket.LastRead;
                            if (lastRead3 > LastRead2)
                            {
                                LastRead2 = lastRead3;
                            }

                            List<int> ackList = ackListPacket.AckList;
                            foreach (int sequence in ackList)
                            {
                                Conn.MySender.RemoveSentAck(sequence);
                            }

                            SendRecord rc1 = Conn.MyClientControl.GetSendRecord(ackListPacket.R1);
                            if (rc1 != null && ackListPacket.S1 > rc1.AckedSize)
                            {
                                rc1.AckedSize = ackListPacket.S1;
                            }

                            SendRecord rc2 = Conn.MyClientControl.GetSendRecord(ackListPacket.R2);
                            if (rc2 != null && ackListPacket.S2 > rc2.AckedSize)
                            {
                                rc2.AckedSize = ackListPacket.S2;
                            }

                            SendRecord rc3 = Conn.MyClientControl.GetSendRecord(ackListPacket.R3);
                            if (rc3 != null && ackListPacket.S3 > rc3.AckedSize)
                            {
                                rc3.AckedSize = ackListPacket.S3;
                            }

                            if (CheckWin())
                            {
                                Conn.MySender.Play();
                            }
                        }
                        else if (sType == PacketType.CloseStreamPacket)
                        {
                            CloseStreamPacket closeStreamPacket = new CloseStreamPacket(datagramPacket);
                            ReceviedClose = true;
                            int n = closeStreamPacket.CloseOffset;
                            CloseRemoteStream(n);
                        }
                        else if (sType == PacketType.CloseConnPacket)
                        {
                            CloseConnPacket closeConnPacket = new CloseConnPacket(datagramPacket);
                            Conn.CloseRemote();
                        }
                        else
                        {
                            throw new Exception("Unknown packet type");
                        }
                    }
                }
            }
        }