Пример #1
0
        public void TestScenario()
        {
            NetQueue <int> queue = new NetQueue <int>(4);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            int[] arr = queue.ToArray();
            if (arr.Length != 3)
            {
                throw new Exception("NetQueue.ToArray failure");
            }
            if (arr[0] != 1 || arr[1] != 2 || arr[2] != 3)
            {
                throw new Exception("NetQueue.ToArray failure");
            }

            bool ok;
            int  a;

            if (queue.Contains(4))
            {
                throw new Exception("NetQueue Contains failure");
            }

            if (!queue.Contains(2))
            {
                throw new Exception("NetQueue Contains failure 2");
            }

            if (queue.Count != 3)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 1)
            {
                throw new Exception("NetQueue failure");
            }

            if (queue.Count != 2)
            {
                throw new Exception("NetQueue failed");
            }

            queue.EnqueueFirst(42);
            if (queue.Count != 3)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 42)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 2)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 3)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == true)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == true)
            {
                throw new Exception("NetQueue failed");
            }

            queue.Enqueue(78);
            if (queue.Count != 1)
            {
                throw new Exception("NetQueue failed");
            }

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 78)
            {
                throw new Exception("NetQueue failed");
            }

            queue.Clear();
            if (queue.Count != 0)
            {
                throw new Exception("NetQueue.Clear failed");
            }

            int[] arr2 = queue.ToArray();
            if (arr2.Length != 0)
            {
                throw new Exception("NetQueue.ToArray failure");
            }
        }
Пример #2
0
        public static void Run()
        {
            {
                var queue = new NetQueue <int>(4);

                queue.Enqueue(1);
                queue.Enqueue(2);
                queue.Enqueue(3);

                var arr = new int[queue.Count];
                queue.CopyTo(arr);
                if (arr.Length != 3)
                {
                    throw new Exception("NetQueue.CopyTo failure");
                }
                if (arr[0] != 1 || arr[1] != 2 || arr[2] != 3)
                {
                    throw new Exception("NetQueue.CopyTo failure");
                }

                bool ok;
                if (queue.Contains(4))
                {
                    throw new Exception("NetQueue Contains failure");
                }

                if (!queue.Contains(2))
                {
                    throw new Exception("NetQueue Contains failure 2");
                }

                if (queue.Count != 3)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out int a);
                if (ok == false || a != 1)
                {
                    throw new Exception("NetQueue failure");
                }

                if (queue.Count != 2)
                {
                    throw new Exception("NetQueue failed");
                }

                queue.EnqueueFirst(42);
                if (queue.Count != 3)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == false || a != 42)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == false || a != 2)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == false || a != 3)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == true)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == true)
                {
                    throw new Exception("NetQueue failed");
                }

                queue.Enqueue(78);
                if (queue.Count != 1)
                {
                    throw new Exception("NetQueue failed");
                }

                ok = queue.TryDequeue(out a);
                if (ok == false || a != 78)
                {
                    throw new Exception("NetQueue failed");
                }

                queue.Clear();
                if (queue.Count != 0)
                {
                    throw new Exception("NetQueue.Clear failed");
                }

                int[] arr2 = new int[queue.Count];
                queue.CopyTo(arr2);
                if (arr2.Length != 0)
                {
                    throw new Exception("NetQueue.CopyTo failure");
                }
            }

            {
                var queue = new NetQueue <int>(8);
                queue.Enqueue(1);
                queue.Enqueue(2);
                queue.Enqueue(3);
                queue.EnqueueFirst(4);

                List <int> destination = new();
                int        drained     = queue.TryDrain(destination);

                if (destination.Count != drained)
                {
                    throw new Exception("NetQueue.TryDrain drained incorrect amount");
                }
            }

            Console.WriteLine("NetQueue tests OK");
        }
Пример #3
0
        public static void Run()
        {
            NetQueue<int> queue = new NetQueue<int>(4);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            int[] arr = queue.ToArray();
            if (arr.Length != 3)
                throw new Exception("NetQueue.ToArray failure");
            if (arr[0] != 1 || arr[1] != 2 || arr[2] != 3)
                throw new Exception("NetQueue.ToArray failure");

            bool ok;
            int a;

            if (queue.Contains(4))
                throw new Exception("NetQueue Contains failure");

            if (!queue.Contains(2))
                throw new Exception("NetQueue Contains failure 2");

            if (queue.Count != 3)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 1)
                throw new Exception("NetQueue failure");

            if (queue.Count != 2)
                throw new Exception("NetQueue failed");

            queue.EnqueueFirst(42);
            if (queue.Count != 3)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 42)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 2)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 3)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == true)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == true)
                throw new Exception("NetQueue failed");

            queue.Enqueue(78);
            if (queue.Count != 1)
                throw new Exception("NetQueue failed");

            ok = queue.TryDequeue(out a);
            if (ok == false || a != 78)
                throw new Exception("NetQueue failed");

            queue.Clear();
            if (queue.Count != 0)
                throw new Exception("NetQueue.Clear failed");

            int[] arr2 = queue.ToArray();
            if (arr2.Length != 0)
                throw new Exception("NetQueue.ToArray failure");

            Console.WriteLine("NetQueue tests OK");
        }
Пример #4
0
        internal void Heartbeat(double now, uint frameCounter)
        {
            m_peer.VerifyNetworkThread();

            NetException.Assert(m_status != NetConnectionStatus.InitiatedConnect && m_status != NetConnectionStatus.RespondedConnect);

            if ((frameCounter % m_infrequentEventsSkipFrames) == 0)
            {
                if (now > m_timeoutDeadline)
                {
                    //
                    // connection timed out
                    //
                    m_peer.LogVerbose("Connection timed out at " + now + " deadline was " + m_timeoutDeadline);
                    ExecuteDisconnect("Connection timed out", true);
                    return;
                }

                // send ping?
                if (m_status == NetConnectionStatus.Connected)
                {
                    if (now > m_sentPingTime + m_peer.m_configuration.m_pingInterval)
                    {
                        SendPing();
                    }

                    // handle expand mtu
                    MTUExpansionHeartbeat(now);
                }

                if (m_disconnectRequested)
                {
                    ExecuteDisconnect(m_disconnectMessage, m_disconnectReqSendBye);
                    return;
                }
            }

            bool connectionReset;             // TODO: handle connection reset

            //
            // Note: at this point m_sendBufferWritePtr and m_sendBufferNumMessages may be non-null; resends may already be queued up
            //

            byte[] sendBuffer = m_peer.m_sendBuffer;
            int    mtu        = m_currentMTU;

            if ((frameCounter % m_messageCoalesceFrames) == 0)             // coalesce a few frames
            {
                //
                // send ack messages
                //
                while (m_queuedOutgoingAcks.Count > 0)
                {
                    int acks = (mtu - (m_sendBufferWritePtr + 5)) / 3;                     // 3 bytes per actual ack
                    if (acks > m_queuedOutgoingAcks.Count)
                    {
                        acks = m_queuedOutgoingAcks.Count;
                    }

                    NetException.Assert(acks > 0);

                    m_sendBufferNumMessages++;

                    // write acks header
                    sendBuffer[m_sendBufferWritePtr++] = (byte)NetMessageType.Acknowledge;
                    sendBuffer[m_sendBufferWritePtr++] = 0;       // no sequence number
                    sendBuffer[m_sendBufferWritePtr++] = 0;       // no sequence number
                    int len = (acks * 3) * 8;                     // bits
                    sendBuffer[m_sendBufferWritePtr++] = (byte)len;
                    sendBuffer[m_sendBufferWritePtr++] = (byte)(len >> 8);

                    // write acks
                    for (int i = 0; i < acks; i++)
                    {
                        NetTuple <NetMessageType, int> tuple;
                        m_queuedOutgoingAcks.TryDequeue(out tuple);

                        //m_peer.LogVerbose("Sending ack for " + tuple.Item1 + "#" + tuple.Item2);

                        sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item1;
                        sendBuffer[m_sendBufferWritePtr++] = (byte)tuple.Item2;
                        sendBuffer[m_sendBufferWritePtr++] = (byte)(tuple.Item2 >> 8);
                    }

                    if (m_queuedOutgoingAcks.Count > 0)
                    {
                        // send packet and go for another round of acks
                        NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
                        m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndPoint, m_sendBufferNumMessages, out connectionReset);
                        m_statistics.PacketSent(m_sendBufferWritePtr, 1);
                        m_sendBufferWritePtr    = 0;
                        m_sendBufferNumMessages = 0;
                    }
                }

                //
                // Parse incoming acks (may trigger resends)
                //
                NetTuple <NetMessageType, int> incAck;
                while (m_queuedIncomingAcks.TryDequeue(out incAck))
                {
                    //m_peer.LogVerbose("Received ack for " + acktp + "#" + seqNr);
                    NetSenderChannelBase chan = m_sendChannels[(int)incAck.Item1 - 1];

                    // If we haven't sent a message on this channel there is no reason to ack it
                    if (chan == null)
                    {
                        continue;
                    }

                    chan.ReceiveAcknowledge(now, incAck.Item2);
                }
            }

            //
            // send queued messages
            //
            if (m_peer.m_executeFlushSendQueue)
            {
                for (int i = m_sendChannels.Length - 1; i >= 0; i--)                    // Reverse order so reliable messages are sent first
                {
                    var channel = m_sendChannels[i];
                    NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
                    if (channel != null)
                    {
                        channel.SendQueuedMessages(now);
                        if (channel.NeedToSendMessages())
                        {
                            m_peer.m_needFlushSendQueue = true;                             // failed to send all queued sends; likely a full window - need to try again
                        }
                    }
                    NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
                }
            }

            //
            // Put on wire data has been written to send buffer but not yet sent
            //
            if (m_sendBufferWritePtr > 0)
            {
                m_peer.VerifyNetworkThread();
                NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
                m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndPoint, m_sendBufferNumMessages, out connectionReset);
                m_statistics.PacketSent(m_sendBufferWritePtr, m_sendBufferNumMessages);
                m_sendBufferWritePtr    = 0;
                m_sendBufferNumMessages = 0;
            }
        }