Exemplo n.º 1
0
        internal void SendPacket(int numBytes, NetEndPoint target, int numMessages, out bool connectionReset)
        {
            connectionReset = false;

            // simulate loss
            float loss = m_configuration.m_loss;

            if (loss > 0.0f)
            {
                if ((float)MWCRandom.Instance.NextDouble() < loss)
                {
                    LogVerbose("Sending packet " + numBytes + " bytes - SIMULATED LOST!");
                    return;                     // packet "lost"
                }
            }

            m_statistics.PacketSent(numBytes, numMessages);

            // simulate latency
            float m = m_configuration.m_minimumOneWayLatency;
            float r = m_configuration.m_randomOneWayLatency;

            if (m == 0.0f && r == 0.0f)
            {
                ActuallySendPacket(m_sendBuffer, numBytes, target, out connectionReset);

                if (m_configuration.m_duplicates > 0.0f && MWCRandom.Instance.NextDouble() < m_configuration.m_duplicates)
                {
                    ActuallySendPacket(m_sendBuffer, numBytes, target, out connectionReset);                     // send it again!
                }
                return;
            }

            int num = 1;

            if (m_configuration.m_duplicates > 0.0f && MWCRandom.Instance.NextSingle() < m_configuration.m_duplicates)
            {
                num++;
            }

            float delay = 0;

            for (int i = 0; i < num; i++)
            {
                delay = m_configuration.m_minimumOneWayLatency + (MWCRandom.Instance.NextSingle() * m_configuration.m_randomOneWayLatency);

                // Enqueue delayed packet
                DelayedPacket p = new DelayedPacket();
                p.Target = target;
                p.Data   = new byte[numBytes];
                Buffer.BlockCopy(m_sendBuffer, 0, p.Data, 0, numBytes);
                p.DelayedUntil = NetTime.Now + delay;

                m_delayedPackets.Add(p);
            }

            // LogVerbose("Sending packet " + numBytes + " bytes - delayed " + NetTime.ToReadable(delay));
        }
		internal void SendPacket(int numBytes, IPEndPoint target, int numMessages, out bool connectionReset)
		{
			connectionReset = false;

			// simulate loss
			float loss = m_configuration.m_loss;
			if (loss > 0.0f)
			{
				if ((float)MWCRandom.Instance.NextDouble() < loss)
				{
					LogVerbose("Sending packet " + numBytes + " bytes - SIMULATED LOST!");
					return; // packet "lost"
				}
			}

			m_statistics.PacketSent(numBytes, numMessages);

			// simulate latency
			float m = m_configuration.m_minimumOneWayLatency;
			float r = m_configuration.m_randomOneWayLatency;
			if (m == 0.0f && r == 0.0f)
			{
				// no latency simulation
				// LogVerbose("Sending packet " + numBytes + " bytes");
				bool wasSent = ActuallySendPacket(m_sendBuffer, numBytes, target, out connectionReset);
				// TODO: handle wasSent == false?

				if (m_configuration.m_duplicates > 0.0f && MWCRandom.Instance.NextDouble() < m_configuration.m_duplicates)
					ActuallySendPacket(m_sendBuffer, numBytes, target, out connectionReset); // send it again!

				return;
			}

			int num = 1;
			if (m_configuration.m_duplicates > 0.0f && MWCRandom.Instance.NextSingle() < m_configuration.m_duplicates)
				num++;

			float delay = 0;
			for (int i = 0; i < num; i++)
			{
				delay = m_configuration.m_minimumOneWayLatency + (MWCRandom.Instance.NextSingle() * m_configuration.m_randomOneWayLatency);

				// Enqueue delayed packet
				DelayedPacket p = new DelayedPacket();
				p.Target = target;
				p.Data = new byte[numBytes];
				Buffer.BlockCopy(m_sendBuffer, 0, p.Data, 0, numBytes);
				p.DelayedUntil = NetTime.Now + delay;

				m_delayedPackets.Add(p);
			}

			// LogVerbose("Sending packet " + numBytes + " bytes - delayed " + NetTime.ToReadable(delay));
		}
Exemplo n.º 3
0
        private void DelayPacket(byte[] data, int length, NetworkEndPoint remoteEP, float delay)
        {
            DelayedPacket pk = new DelayedPacket();

            pk.Data = new byte[length];
            Buffer.BlockCopy(data, 0, pk.Data, 0, length);
            pk.Recipient = remoteEP;

            double now = NetTime.Now;

            pk.DelayedUntil = now + delay;
            m_delayedPackets.Add(pk);
        }
        private void DelayPacket(byte[] data, int length, IPEndPoint remoteEP, float delay)
        {
#if DEBUG
            if (Thread.CurrentThread != m_heartbeatThread)
            {
                throw new Exception("Threading error; should be heartbeat thread. Please check callstack!");
            }
#endif
            DelayedPacket pk = new DelayedPacket();
            pk.Data = new byte[length];
            Buffer.BlockCopy(data, 0, pk.Data, 0, length);
            pk.Recipient = remoteEP;

            double now = NetTime.Now;
            pk.DelayedUntil = now + delay;
            m_delayedPackets.Add(pk);
        }
        private void SendDelayedPackets()
        {
            if (DelayedPackets.Count == 0)
            {
                return;
            }

            TimeSpan now = NetTime.Now;

            // reverse-for so elements can be removed without breaking loop
            for (int i = DelayedPackets.Count; i-- > 0;)
            {
                DelayedPacket p = DelayedPackets[i];
                if (now > p.DelayedUntil)
                {
                    ActuallySendPacket(p.Data, p.Data.Length, p.Target);
                    DelayedPackets.RemoveAt(i);
                }
            }
        }
Exemplo n.º 6
0
        private void DelayPacket(byte[] data, int length, IPEndPoint remoteEP, float delay)
        {
            #if DEBUG
            if (Thread.CurrentThread != m_heartbeatThread)
                throw new Exception("Threading error; should be heartbeat thread. Please check callstack!");
            #endif
            DelayedPacket pk = new DelayedPacket();
            pk.Data = new byte[length];
            Buffer.BlockCopy(data, 0, pk.Data, 0, length);
            pk.Recipient = remoteEP;

            double now = NetTime.Now;
            pk.DelayedUntil = now + delay;
            m_delayedPackets.Add(pk);
        }