Exemplo n.º 1
0
        public void OnReceivePacket(DatagramPacket datagramPacket)
        {
            byte[] dpData = datagramPacket.Data;

            int sType          = PacketCheck.CheckSType(datagramPacket);
            int remoteClientId = BitConverter.ToInt32(dpData, 9);

            if (sType == PacketType.PingPacket)
            {
                PingPacket pingPacket = new PingPacket(datagramPacket);
                SendPingPacket2(pingPacket.PingId, datagramPacket.Host, datagramPacket.Port);
                CurrentSpeed = pingPacket.DownloadSpeed * 1024;
            }
            else if (sType == PacketType.PingPacket2)
            {
                PingPacket2 pingPacket2 = new PingPacket2(datagramPacket);
                LastReceivePingTime = DateTimeExtensions.CurrentTimeMillis();
                long?t = PingTable[pingPacket2.PingId];
                if (t != null)
                {
                    PingDelay = (int)(DateTimeExtensions.CurrentTimeMillis() - t);
                    string protocal = "udp";
                    Console.WriteLine("delay_" + protocal + " " + PingDelay + "ms " + datagramPacket.Host + ":" + datagramPacket.Port);
                }
            }
        }
Exemplo n.º 2
0
 void SendPacket(DatagramPacket dp)
 {
     Conn.MyClientControl.SendPacket(dp);
 }
Exemplo n.º 3
0
 void Send(DatagramPacket dp)
 {
     SendPacket(dp);
 }
Exemplo n.º 4
0
        public Route(int mode, int port = 150)
        {
            LocalClientId = Ran.Next();
            Mode = mode;

            if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient();
            }
            else if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient(port);
            }

            MyClientManager = new ClientManager(this);
            Task.Factory.StartNew(() => {
                while (true)
                {
                    try
                    {
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        byte[] data = MyUdpClient.Receive(ref RemoteIpEndPoint);
                        DatagramPacket datagramPacket = new DatagramPacket(data, data.Length);
                        PacketBuffer.Add(datagramPacket);
                    }
                    catch (Exception e)
                    {
                        Thread.Sleep(1);
                        continue;
                    }
                }
            });

            Task.Factory.StartNew(() => {
                while (true)
                {
                    DatagramPacket datagramPacket = PacketBuffer.Take();
                    if (datagramPacket == null) continue;

                    byte[] data = datagramPacket.Data;
                    if (data.Length < 4)
                    {
                        return;
                    }

                    int sType = PacketCheck.CheckSType(datagramPacket);
                    int connectId = BitConverter.ToInt32(data, 4);
                    int remoteClientId = BitConverter.ToInt32(data, 8);

                    if (ClosedTable.Contains(connectId) && connectId != 0) continue;

                    ClientControl clientControl;
                    if (sType == PacketType.PingPacket || sType == PacketType.PingPacket2)
                    {
                        if (mode == ClientMode)
                        {
                            string key = datagramPacket.Host + ":" + datagramPacket.Port;
                            int simClientId = Math.Abs(key.GetHashCode());
                            clientControl = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                        else if (mode == ServerMode)
                        {
                            clientControl = MyClientManager.GetClientControl(remoteClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                    }
                    else
                    {
                        if (mode == ClientMode)
                        {
                            if (!SettedTable.Contains(remoteClientId))
                            {
                                string key = datagramPacket.Host + ":" + datagramPacket.Port;
                                int simClientId = Math.Abs(key.GetHashCode());
                                clientControl = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);

                                if (clientControl.ClientIdReal == -1)
                                {
                                    clientControl.ClientIdReal = remoteClientId;
                                }
                                else if (clientControl.ClientIdReal != remoteClientId)
                                {
                                    clientControl.UpdateClientId(remoteClientId);
                                }

                                SettedTable.Add(remoteClientId);
                            }
                        }
                        else if (mode == ServerMode)
                        {
                            GetConnection2(datagramPacket.Host, datagramPacket.Port, connectId, remoteClientId);
                        }

                        ConnectionUDP conn = ConnTable[connectId];
                        if (conn != null)
                        {
                            conn.MyRecevier.OnReceivePacket(datagramPacket);
                            if (sType == PacketType.DataPacket)
                            {
                                TrafficEvent e = new TrafficEvent("", Ran.Next(), datagramPacket.Data.Length, TrafficEvent.Download);
                                FireEvent(e);
                            }
                        }
                    }
                }
            });
        }
Exemplo n.º 5
0
 public void SendPacket(DatagramPacket datagramPacket)
 {
     MyUdpClient.Send(datagramPacket.Data, datagramPacket.Bytes);
 }
Exemplo n.º 6
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");
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public Route(int mode, int port = 150)
        {
            LocalClientId = Ran.Next();
            Mode          = mode;

            if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient();
            }
            else if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient(port);
            }

            MyClientManager = new ClientManager(this);
            Task.Factory.StartNew(() => {
                while (true)
                {
                    try
                    {
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        byte[] data = MyUdpClient.Receive(ref RemoteIpEndPoint);
                        DatagramPacket datagramPacket = new DatagramPacket(data, data.Length);
                        PacketBuffer.Add(datagramPacket);
                    }
                    catch (Exception e)
                    {
                        Thread.Sleep(1);
                        continue;
                    }
                }
            });

            Task.Factory.StartNew(() => {
                while (true)
                {
                    DatagramPacket datagramPacket = PacketBuffer.Take();
                    if (datagramPacket == null)
                    {
                        continue;
                    }

                    byte[] data = datagramPacket.Data;
                    if (data.Length < 4)
                    {
                        return;
                    }

                    int sType          = PacketCheck.CheckSType(datagramPacket);
                    int connectId      = BitConverter.ToInt32(data, 4);
                    int remoteClientId = BitConverter.ToInt32(data, 8);

                    if (ClosedTable.Contains(connectId) && connectId != 0)
                    {
                        continue;
                    }

                    ClientControl clientControl;
                    if (sType == PacketType.PingPacket || sType == PacketType.PingPacket2)
                    {
                        if (mode == ClientMode)
                        {
                            string key      = datagramPacket.Host + ":" + datagramPacket.Port;
                            int simClientId = Math.Abs(key.GetHashCode());
                            clientControl   = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                        else if (mode == ServerMode)
                        {
                            clientControl = MyClientManager.GetClientControl(remoteClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                    }
                    else
                    {
                        if (mode == ClientMode)
                        {
                            if (!SettedTable.Contains(remoteClientId))
                            {
                                string key      = datagramPacket.Host + ":" + datagramPacket.Port;
                                int simClientId = Math.Abs(key.GetHashCode());
                                clientControl   = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);

                                if (clientControl.ClientIdReal == -1)
                                {
                                    clientControl.ClientIdReal = remoteClientId;
                                }
                                else if (clientControl.ClientIdReal != remoteClientId)
                                {
                                    clientControl.UpdateClientId(remoteClientId);
                                }

                                SettedTable.Add(remoteClientId);
                            }
                        }
                        else if (mode == ServerMode)
                        {
                            GetConnection2(datagramPacket.Host, datagramPacket.Port, connectId, remoteClientId);
                        }

                        ConnectionUDP conn = ConnTable[connectId];
                        if (conn != null)
                        {
                            conn.MyRecevier.OnReceivePacket(datagramPacket);
                            if (sType == PacketType.DataPacket)
                            {
                                TrafficEvent e = new TrafficEvent("", Ran.Next(), datagramPacket.Data.Length, TrafficEvent.Download);
                                FireEvent(e);
                            }
                        }
                    }
                }
            });
        }
Exemplo n.º 8
0
 public void SendPacket(DatagramPacket datagramPacket)
 {
     MyUdpClient.Send(datagramPacket.Data, datagramPacket.Bytes);
 }
Exemplo n.º 9
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");
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        public DatagramPacket GetPacket(int connectId)
        {
            DatagramPacket datagramPacket = DpBuffer.Take();

            return(datagramPacket);
        }
Exemplo n.º 11
0
 public static int CheckSType(DatagramPacket dp) => BitConverter.ToInt16(dp.Data, 2);
Exemplo n.º 12
0
 public static int CheckVer(DatagramPacket dp) => BitConverter.ToInt16(dp.Data, 0);
Exemplo n.º 13
0
 public void SendPacket(DatagramPacket datagramPacket)
 {
     MyRoute.SendPacket(datagramPacket);
 }