Example #1
0
        private Packet GetPingPacket(FalconPeer peerToBorrowPacketFrom)
        {
            var pingPacket = peerToBorrowPacketFrom.BorrowPacketFromPool();

            pingPacket.WriteByte((byte)FalconTestMessageType.Ping);
            return(pingPacket);
        }
Example #2
0
        private void ProcessReceivedPacket(Packet packet)
        {
            IPEndPoint sender;
            FalconPeer peer = peerProcessingReceivedPacketsFor;

            if (!peer.TryGetPeerIPEndPoint(packet.PeerId, out sender))
            {
                Debug.WriteLine("Failed to find IPEndPoint of peer packet received from!");
                //Assert.Fail("Failed to find IPEndPoint of peer packet received from!");
                return;
            }

            if (packet.BytesWritten == 0)
            {
                Debug.WriteLine("Empty packet!?");
                return;
            }

            var type = (FalconTestMessageType)packet.ReadByte();

            switch (type)
            {
            case FalconTestMessageType.Ping:
            {
                Debug.WriteLine("Ping received from: {0}, sending pong...", sender);
                var pongPacket = peer.BorrowPacketFromPool();
                pongPacket.WriteByte((byte)FalconTestMessageType.Pong);
                peer.EnqueueSendTo(packet.PeerId, SendOptions.ReliableInOrder, pongPacket);
            }
            break;

            case FalconTestMessageType.Pong:
            {
                Debug.WriteLine("Pong received from: {0}!", sender);
                if (replyReceived != null)
                {
                    replyReceived(sender, packet);
                }
            }
            break;

            case FalconTestMessageType.RandomBytes:
            {
                var opts = (SendOptions)packet.ReadByte();
                //Assert.IsTrue(Enum.IsDefined(typeof(SendOptions), opts), "Invalid SendOptions");
                if (!Enum.IsDefined(typeof(SendOptions), opts))
                {
                    Debug.WriteLine("Invalid SendOptions");
                }
                var length = packet.ReadUInt16();
                Debug.WriteLine(" -> RandomBytes received from: {0}, on channel: {1}, purported length: {2}, actual: {3}", sender, opts, length, packet.BytesRemaining);
                var bytes = packet.ReadBytes(length);

                var reply = peer.BorrowPacketFromPool();
                reply.WriteByte((byte)FalconTestMessageType.RandomBytesReply);
                reply.WriteUInt16((ushort)bytes.Length);
                reply.WriteBytes(bytes);

                peer.EnqueueSendTo(packet.PeerId, opts, reply);
            }
            break;

            case FalconTestMessageType.RandomBytesReply:
            {
                Debug.WriteLine(" <- RandomBytesReply received from: {0}", sender);
                if (replyReceived != null)
                {
                    replyReceived(sender, packet);
                }
            }
            break;

            default:
            {
                //Assert.Fail("Unhandeled FalconTestMessagePacketType: " + type.ToString());
                Debug.WriteLine("Unhandeled FalconTestMessagePacketType: " + type.ToString());
            }
            break;
            }
        }