public override ChanneledPacket HandleIncomingMessagePoll(byte[] buffer, int length, out bool hasMore) { ReliableSequencedPacket packet = new ReliableSequencedPacket(); packet.Read(buffer, length); _receiveSequencer.Push(packet); // Set the ack if (_sendSequencer.HasMessage(packet.AckSequence) && _sendSequencer.Peek(packet.AckSequence).ExplicitResponse != ExplicitResponseState.Ack) { packet.ExplicitResponse = ExplicitResponseState.Ack; OnMessageAck(packet.AckSequence); } for (ushort i = packet.AckSequence; _sendSequencer.HasMessage(i) && _sendSequencer.Peek(i).ExplicitResponse == ExplicitResponseState.Ack; i++) { _lowestAckedMessage = i; } if (_receiveSequencer.HasMessage((ushort)(_lastReceivedSequenceNumber + 1))) { hasMore = _receiveSequencer.HasMessage((ushort)(_lastReceivedSequenceNumber + 2)); return(_receiveSequencer.Peek(++_lastReceivedSequenceNumber)); } else { hasMore = false; return(null); } }
public override ChanneledPacket CreateOutgoingMessage(byte[] payload, int offset, int length) { ReliableSequencedPacket message = new ReliableSequencedPacket(ChannelId, ++_lastOutboundSequenceNumber, _lastReceivedSequenceNumber, payload, offset, length); _sendSequencer.Push(message); return(message); }
public void TestReliableSequencedChannelReceiveOutOfSequence() { ReliableSequencedChannel sender = new ReliableSequencedChannel(0); ReliableSequencedChannel receiver = new ReliableSequencedChannel(0); RemotePeer firstPeer; ChanneledPacket firstPacket; { // Create first packet with sequence 1 firstPeer = new RemotePeer();// (new IPEndPoint(IPAddress.Any, 5670)); ulong sessionId = (ulong)0; firstPeer.ChallengeResult = sessionId; byte[] payload = new byte[0]; firstPacket = sender.CreateOutgoingMessage(payload, 0, payload.Length); } { Random random = new Random(0); foreach (int i in Enumerable.Range(1, 100).OrderBy(x => random.Next())) { RemotePeer fakePeer = new RemotePeer(); // (new IPEndPoint(IPAddress.Any, 5670)); ulong sessionId = (ulong)i; fakePeer.ChallengeResult = sessionId; byte[] payload = new byte[random.Next(0, 1024)]; random.NextBytes(payload); ChanneledPacket sent = sender.CreateOutgoingMessage(payload, 0, payload.Length); byte[] buffer = new byte[5000]; int oSize = sent.Write(buffer, fakePeer); ReliableSequencedPacket incPacket = (ReliableSequencedPacket)receiver.HandleIncomingMessagePoll(buffer, oSize, out bool hasMore); Assert.IsNull(incPacket); } } { byte[] buffer = new byte[5000]; int oSize = firstPacket.Write(buffer, firstPeer); ReliableSequencedPacket incPacket = (ReliableSequencedPacket)receiver.HandleIncomingMessagePoll(buffer, oSize, out bool hasMore); Assert.NotNull(incPacket); Assert.IsTrue(incPacket.Payload.Length == 0); Assert.IsTrue(hasMore); } { Random random = new Random(0); foreach (int i in Enumerable.Range(1, 100).OrderBy(x => random.Next())) { ulong sessionId = (ulong)i; byte[] payload = new byte[random.Next(0, 1024)]; random.NextBytes(payload); ReliableSequencedPacket incPacket = (ReliableSequencedPacket)receiver.HandlePoll(); Assert.NotNull(incPacket); CollectionAssert.AreEqual(incPacket.Payload, payload); } } }
public void TestReliableSequencedChannelReceiveInSequence() { ReliableSequencedChannel sender = new ReliableSequencedChannel(0); ReliableSequencedChannel receiver = new ReliableSequencedChannel(0); Random random = new Random(0); for (int i = 0; i < 100000; i++) { RemotePeer fakePeer = new RemotePeer();// new IPEndPoint(IPAddress.Any, 5670)); ulong sessionId = RandomUtils.GetULong(false); fakePeer.ChallengeResult = sessionId; int payLength = random.Next(0, 1024); byte[] payload = new byte[payLength]; random.NextBytes(payload); ChanneledPacket sent = sender.CreateOutgoingMessage(payload, 0, payload.Length); byte[] buffer = new byte[5000]; int oSize = sent.Write(buffer, fakePeer); ReliableSequencedPacket incPacket = (ReliableSequencedPacket)receiver.HandleIncomingMessagePoll(buffer, oSize, out bool hasMore); Assert.IsNotNull(incPacket, "it:" + i); Assert.AreEqual(payLength, incPacket.Payload.Length); CollectionAssert.AreEqual(payload, incPacket.Payload); } }