Exemple #1
0
 public static void FireEvent(TrafficEvent e)
 {
     foreach (TrafficListener listener in ListenerList)
     {
         int type = e.Type;
         if (type == TrafficEvent.Download)
         {
             listener.TrafficDownload(e);
         }
         else if (type == TrafficEvent.Upload)
         {
             listener.TrafficUpload(e);
         }
     }
 }
Exemple #2
0
        void SendDataPacket(DataPacket dataPacket, bool resend, bool twice, bool block)
        {
            lock (Conn.MyClientControl.SynLock)
            {
                long startTime = DateTimeExtensions.NanoTime();
                long t1        = DateTimeExtensions.CurrentTimeMillis();

                Conn.MyClientControl.OnSendDataPacket(Conn);

                int timeId = Conn.MyClientControl.GetCurrentTimeId();
                dataPacket.Create(timeId);

                SendRecord currentRecord = Conn.MyClientControl.GetSendRecord(timeId);
                if (!resend)
                {
                    dataPacket.FirstSendTimeId = timeId;
                    dataPacket.FirstSendTime   = DateTimeExtensions.CurrentTimeMillis();
                    currentRecord.AddSentFirst(dataPacket.Data.Length);
                    currentRecord.AddSent(dataPacket.Data.Length);
                }
                else
                {
                    SendRecord record = Conn.MyClientControl.GetSendRecord(dataPacket.FirstSendTimeId);
                    record.AddResent(dataPacket.Data.Length);
                    currentRecord.AddSent(dataPacket.Data.Length);
                }

                //try
                SendSum  += 1;
                Sum      += 1;
                UnAckMax += 1;

                Send(dataPacket.MyDatagramPacket);

                if (twice)
                {
                    Send(dataPacket.MyDatagramPacket);
                }
                if (block)
                {
                    Conn.MyClientControl.SendSleep(startTime, dataPacket.Data.Length);
                }
                TrafficEvent e = new TrafficEvent("", (long)Ran.Next(), dataPacket.Data.Length, TrafficEvent.Upload);
                Route.FireEvent(e);
            }
        }
Exemple #3
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);
                            }
                        }
                    }
                }
            });
        }