Ejemplo n.º 1
0
        /// <summary>
        /// Handle incoming packets related to connection setup
        /// </summary>
        /// <param name="message">The message to handle</param>
        /// <returns>A PacketResponse indicating if the message was consumed</returns>
        public PacketResponse HandlePacket(Message message)
        {
            switch (message.MessageType)
            {
            case MessageType.HeartbeatRequest:
                var msg = new Message(MessageType.Heartbeat, message.Sender)
                {
                    DataAsString = Dns.GetHostName()
                };
                Outbox.SendUnencryptedMessage(msg);
                if (!ConnectedPeers.ContainsKey(message.Sender))
                {
                    ConnectedPeers.Add(message.Sender, message.DataAsString);
                }
                return(PacketResponse.Accept);

            case MessageType.Heartbeat:
                if (!ConnectedPeers.ContainsKey(message.Sender))
                {
                    ConnectedPeers.Add(message.Sender, message.DataAsString);
                }
                return(PacketResponse.Accept);

            case MessageType.EncryptionPacket:
                SecurityManager.GetInstance().SetKeysFromPacket(message.Data);
                Inbox.GetInstance().UseEncryption = true;
                ConnectionDone = true;

                return(PacketResponse.AcceptAndFinish);

            default:
                return(PacketResponse.Decline);
            }
        }