Beispiel #1
0
        public static void HandleServerConnection(object client)
        {
            TcpClient     TCPClient        = (TcpClient)client;
            NetworkStream ClientDataStream = TCPClient.GetStream();
            UTF8Encoding  encoder          = new UTF8Encoding();
            int           CurrentMessage_BytesRead;

            byte[] message = new byte[UInt16.MaxValue * 16];
            while (true)
            {
                CurrentMessage_BytesRead = 0;

                try
                {
                    // READ INPUT
                    CurrentMessage_BytesRead = ClientDataStream.Read(message, 0, ushort.MaxValue * 16);
                }
                catch
                {
                    // EXCEPTION
                    break;
                }

                if (CurrentMessage_BytesRead == 0)
                {
                    Console.WriteLine("Connection closed.");
                    ClientDataStream.Close();
                    break;
                }
                // RECIEVED
                Message Received = BeeperBuilder.BuildMessage(encoder.GetString(message, 0, CurrentMessage_BytesRead));
                Console.WriteLine(BeeperBuilder.BuildMessageOutput(Received));
            }

            TCPClient.Close();
            Disconnected = true;
        }
Beispiel #2
0
        private void HandleClientConnection(object client)
        {
            TcpClient     TCPClient        = (TcpClient)client;
            NetworkStream ClientDataStream = TCPClient.GetStream();
            UTF8Encoding  encoder          = new UTF8Encoding();

            l.WriteLine(l.VERBOSE, "Client thread started for " + TCPClient.GetHashCode());
            byte[] message = new byte[UInt16.MaxValue * 16];

            l.WriteLine(l.INFO, "A client is connecting...");
            l.WriteLine(l.VERBOSE, "Awaiting introduction message of " + TCPClient.GetHashCode());
            Message IntroductionMessage = null;

            try {
                byte[] Introduction_Bytes     = new byte[ushort.MaxValue];
                int    Introduction_BytesRead = ClientDataStream.Read(Introduction_Bytes, 0, ushort.MaxValue * 16);
                IntroductionMessage = BeeperBuilder.BuildMessage(encoder.GetString(Introduction_Bytes, 0, Introduction_BytesRead));
            } catch (Exception e)
            {
                l.WriteLine(l.ERROR, "An exception occured! Oh no!");
                throw new BeeperCrash(e.Message);
            }
            if (IntroductionMessage == null)
            {
                l.WriteLine(l.INFO, "Connection failed. Reason: User error");
                l.WriteLine(l.VERBOSE, "Introduction message invalid. Disconnecting " + TCPClient.GetHashCode() + "...");
                ClientDataStream.Close();
                TCPClient.Close();
                return;
            }
            else
            {
                if (IntroductionMessage.AreContentsNull())
                {
                    l.WriteLine(l.INFO, "Connection failed. Reason: User error");
                    l.WriteLine(l.VERBOSE, "Introduction message null. Disconnecting " + TCPClient.GetHashCode() + "...");
                    ClientDataStream.Close();
                    TCPClient.Close();
                    return;
                }
                else if (IntroductionMessage.Type != "INTRO")
                {
                    l.WriteLine(l.INFO, "Connection failed. Reason: User error");
                    l.WriteLine(l.VERBOSE, "Did not send introduction for first contact. Disconnecting " + TCPClient.GetHashCode() + "...");
                    ClientDataStream.Close();
                    TCPClient.Close();
                    return;
                }
                else
                {
                    l.WriteLine(l.VERBOSE, "User introduction is good. Connecting to " + TCPClient.GetHashCode());
                    Clients.Add(TCPClient, IntroductionMessage.Sender);
                    l.WriteLine(l.VERBOSE, "User connected: " + TCPClient.GetHashCode() + ". Switching from hashcode identification to username identification.");
                    l.WriteLine(l.INFO, "User " + IntroductionMessage.Sender.DisplayName + " (" + IntroductionMessage.Sender.Username + ") connected");
                    l.WriteLine(l.VERBOSE, IntroductionMessage.Sender.DisplayName + ". Sending verification response.");
                    byte[] buffer = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(GetIntroMessage()));
                    ClientDataStream.Write(buffer, 0, buffer.Length);
                    l.WriteLine(l.VERBOSE, IntroductionMessage.Sender.DisplayName + ": Response sent.");
                    DistributeMessage(new Message(ServerUser, "MESSAGE_STANDARD", IntroductionMessage.Sender.DisplayName + " connected."));
                }
            }

            int CurrentMessage_BytesRead;

            while (true)
            {
                CurrentMessage_BytesRead = 0;

                try
                {
                    // READ INPUT
                    CurrentMessage_BytesRead = ClientDataStream.Read(message, 0, ushort.MaxValue * 16);
                }
                catch
                {
                    // EXCEPTION
                    break;
                }

                if (CurrentMessage_BytesRead == 0)
                {
                    DisconnectUser(TCPClient);
                }
                // RECIEVED
                String ReceivedJSON = "";
                try
                {
                    ReceivedJSON = encoder.GetString(message, 0, CurrentMessage_BytesRead);
                    l.WriteLine(l.DEBUG, ReceivedJSON);
                    Message Received = BeeperBuilder.BuildMessage(ReceivedJSON);
                    if (!Received.AreContentsNull())
                    {
                        if (Received.Type == "MESSAGE_STANDARD")
                        {
                            DistributeMessage(Received);
                            l.WriteLine(l.INFO, BeeperBuilder.BuildMessageOutput(Received));
                        }
                        else if (Received.Type == "DISCONNECT")
                        {
                            DisconnectUser(TCPClient);
                            return;
                        }
                        else if (Received.Type == "COMMAND")
                        {
                            int CommandResult = Commander.HandleCommand(new Command(Received.Sender, ((Received.Content.Split(' ').Length == 1) ? Received.Content : Received.Content.Split(' ')[0]), ((Received.Content.Split(' ').Length == 1) ? new String[0] : Received.Content.Split(' ')), Received.Sent, TCPClient));
                            switch (CommandResult)
                            {
                            case (int)CommandStatus.NonExistent:
                                SendMessage(ClientDataStream, "Command is unknown or inaccessible.");
                                break;

                            case (int)CommandStatus.ExecutionError:
                                SendMessage(ClientDataStream, "The command handler had an issue executing the command.");
                                break;

                            case (int)CommandStatus.ProcessingError:
                                break;

                            case (int)CommandStatus.InvalidResult:
                                break;

                            case (int)CommandStatus.KillThread:
                                return;

                            default:
                                // OK.
                                break;
                            }
                        }
                    }
                } catch (Exception e)
                {
                    l.WriteLine(l.ERROR, "Hold your horses! The JSON is invalid! This message can't be displayed!");
                    l.WriteLine(l.ERROR, ReceivedJSON);
                    SendMessage(ClientDataStream, new Message(ServerUser, "ERROR", "Your message cannot be displayed! " + e.Message, DateTime.Now));
                }
            }

            TCPClient.Close();
        }