Exemple #1
0
        public static void VerifiedRecieve(ClientInstance instance, byte[] bytes)
        {
            // Random CryptographicException that i am not able to replicate
            // TODO Investigate reason for exception.
            string message = "";

            try
            {
                message = Encryption.DecryptMessageData(bytes);
            }
            catch (CryptographicException)
            {
                int    index         = ServerMessage.FindClientKeysIndex(instance.client);
                string serverMessage = String.Format("{0} Was kicked due to a cryptography error.", ServerHandler.activeClients[index].Username);
                ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, serverMessage);
                instance.client.Close();
            }

            string messageFormatted = Common.ReturnEndOfStream(message);

            byte[] messageBytes = Encoding.ASCII.GetBytes(messageFormatted);

            List <MessageFormat> messageList;

            switch (Common.ReturnMessageType(messageBytes))
            {
            case MessageTypes.MESSAGE:
                int index = ServerMessage.FindClientKeysIndex(instance.client);

                // Error when deserializing message.
                // Could mean corrupt or icorrect data has been transmitted.
                try
                {
                    messageList = Serialization.DeserializeMessageFormat(messageFormatted);

                    List <MessageReplyFormat> replyFormat = new();
                    replyFormat.Add(new MessageReplyFormat
                    {
                        MessageType   = MessageTypes.MESSAGEREPLY,
                        Message       = messageList[0].Message,
                        ID            = ServerHandler.activeClients[index].ID,
                        Username      = ServerHandler.activeClients[index].Username,
                        UsernameColor = ServerHandler.activeClients[index].UsernameColor,
                    });

                    ConsoleOutput.RecievedMessageReplyFormat(replyFormat, ServerHandler.activeClients[index].ChannelID);


                    // Encrypts the message and sends it to all clients.
                    RepeatToAllClientsInChannel(replyFormat, instance);
                }
                catch (JsonException)
                {
                    string serverMessage = String.Format("({0}) {1} Was kicked due to an invalid message.", ServerHandler.activeClients[index].ID, ServerHandler.activeClients[index].Username);
                    ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, serverMessage);
                    instance.client.Close();
                }
                break;

            case MessageTypes.DATAREQUEST:
                List <DataRequestFormat> dataList = Serialization.DeserializeDataRequestFormat(messageFormatted);
                CommandHandler.RecievedDataRequest(instance, dataList);
                break;
            }
        }
Exemple #2
0
 public static void Execute()
 {
     ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, Commands.CommandArguments[0]);
 }