Esempio n. 1
0
        private void ReceivePingAnswer(object sender, SocketPacketEventArgs e)
        {
            if (TimeHelper.PingPacketSent == TimeHelper.PingPacketRecv &&
                TimeHelper.PingPacketRecv >= Parameters.PING_NUMBER_PACKET)
            {
                if (TimeHelper.PingPacketRecv > 2)
                {
                    // remove max and min
                    TimeHelper.PingPacketNfo = TimeHelper.PingPacketNfo.OrderBy(ts => ts.Value).ToDictionary(ts => ts.Key, ts => ts.Value);
                    uint lastPacketID  = TimeHelper.PingPacketNfo.Keys.Last();
                    uint firstPacketID = TimeHelper.PingPacketNfo.Keys.First();

                    TimeHelper.PingPacketNfo.Remove(lastPacketID);
                    TimeHelper.PingPacketNfo.Remove(firstPacketID);
                }

                // get average
                ulong timestampSum = (ulong)TimeHelper.PingPacketNfo.Sum(ts => ts.Value);
                uint  avgTS        = (uint)timestampSum / (uint)TimeHelper.PingPacketNfo.Count;

                // divide by two because currently we get the time for the packet to go to the server AND comeback
                uint ping = avgTS / 2;
                TimeHelper.CurrentPing = ping;
                Console.WriteLine($"Current Ping : {ping}");

                TimeHelper.PingPacketNfo.Clear();

                // Now sync clock with the server's one
                PacketSyncClockRequest packetSync = new PacketSyncClockRequest();
                m_udpConnection.SendPacket(packetSync);
            }
            else
            {
                // If there isn't enough ping info to estimate current ping, send another ping request
                PacketPingRequest ping = new PacketPingRequest();
                TimeHelper.PingPacketNfo.Add(ping.Id, 0);
                m_udpConnection.SendPacket(ping);
            }
        }
Esempio n. 2
0
        private void InitUdp(object sender, SocketPacketEventArgs e)
        {
            IPAddress addr;

            if (IPAddress.TryParse((e.Packet as PacketClientConnected).AddrUdp, out addr))
            {
                Console.WriteLine("UDP addr : " + addr.ToString() + " | port : " + (e.Packet as PacketClientConnected).PortUdp);
                m_udpConnection = new DeusUdpConnection(new IPEndPoint(addr, (int)(e.Packet as PacketClientConnected).PortUdp));

                PacketConnectedUdpAnswer feedback = new PacketConnectedUdpAnswer(m_playerName);
                m_udpConnection.SendPacket(feedback);


                PacketPingRequest ping = new PacketPingRequest();
                TimeHelper.PingPacketNfo.Add(ping.Id, 0);
                m_udpConnection.SendPacket(ping);
            }
            else
            {
                throw new Exception("Cannot parse IPAdress, abort the UDP connection...");
            }
        }