Example #1
0
        public void TestWrap()
        {
            var sequencer = new Sequencer(2);

            Assert.That(sequencer.Next(), Is.EqualTo(1));
            Assert.That(sequencer.Next(), Is.EqualTo(2));
            Assert.That(sequencer.Next(), Is.EqualTo(3));
            Assert.That(sequencer.Next(), Is.EqualTo(0),
                        "2 bit sequencer should wrap after 4 numbers");
        }
Example #2
0
        public void ItShouldStartOverAfterLastSequenceNumber()
        {
            var sequencer = new Sequencer(2);

            Assert.That(sequencer.Next(), Is.EqualTo(0));
            Assert.That(sequencer.Next(), Is.EqualTo(1));
            Assert.That(sequencer.Next(), Is.EqualTo(2));
            Assert.That(sequencer.Next(), Is.EqualTo(3));
            Assert.That(sequencer.Next(), Is.EqualTo(0),
                        "2 bit sequencer should wrap after 4 numbers");
        }
Example #3
0
        /// <summary>
        /// Sends a message, but notify when it is delivered or lost
        /// </summary>
        /// <typeparam name="T">type of message to send</typeparam>
        /// <param name="message">message to send</param>
        /// <param name="token">a arbitrary object that the sender will receive with their notification</param>
        public void SendNotify <T>(T message, object token, int channelId = Channel.Unreliable)
        {
            if (sendWindow.Count == WINDOW_SIZE)
            {
                NotifyLost?.Invoke(this, token);
                return;
            }

            using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
            {
                var notifyPacket = new NotifyPacket
                {
                    Sequence        = (ushort)sequencer.Next(),
                    ReceiveSequence = receiveSequence,
                    AckMask         = receiveMask
                };

                sendWindow.Enqueue(new PacketEnvelope
                {
                    Sequence = notifyPacket.Sequence,
                    Token    = token
                });

                MessagePacker.Pack(notifyPacket, writer);
                MessagePacker.Pack(message, writer);
                NetworkDiagnostics.OnSend(message, channelId, writer.Length, 1);
                Send(writer.ToArraySegment(), channelId);
                lastNotifySentTime = Time.unscaledTime;
            }
        }
Example #4
0
        public void TestNext()
        {
            var sequencer = new Sequencer(3);

            Assert.That(sequencer.Next(), Is.EqualTo(1));
        }
Example #5
0
        public void SequencerStartAt0()
        {
            var sequencer = new Sequencer(3);

            Assert.That(sequencer.Next(), Is.EqualTo(0));
        }