Example #1
0
        static void Main(string[] args)
        {
            InitializeUser();

            while (true)
            {
                // ACCEPT COMMANDS
                Console.Write("> ");
                String Line = Console.ReadLine();
                if (Line == "exit")
                {
                    break;
                }
                else if (Line == "connect")
                {
                    Connect();
                    Console.WriteLine("Connection trial sent.");
                }
                else if (Line == "jsontest")
                {
                    Console.WriteLine("Creating intro message...");
                    Message IntroductoryMessage = new Message(User, "INTRO", "Hello, Beeper World!", DateTime.Now);
                    Console.WriteLine("Message JSON: " + BeeperBuilder.BuildMessageJSON(IntroductoryMessage));
                }
                Console.WriteLine();
            }
        }
Example #2
0
 public void SendMessage(NetworkStream ReceivingStream, Message Message)
 {
     try {
         UTF8Encoding encoder = new UTF8Encoding();
         byte[]       buffer  = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(Message));
         ReceivingStream.Write(buffer, 0, buffer.Length);
         ReceivingStream.Flush();
     }
     catch {
         l.WriteLine(l.ERROR, "Failed to send message.");
     }
 }
Example #3
0
 public void SendMessage(NetworkStream ReceivingStream, String Message)
 {
     try
     {
         UTF8Encoding encoder = new UTF8Encoding();
         byte[]       buffer  = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(new Message(ServerUser, "MESSAGE_STANDARD", Message, DateTime.Now)));
         ReceivingStream.Write(buffer, 0, buffer.Length);
         ReceivingStream.Flush();
     }
     catch {
         l.WriteLine(l.ERROR, "Failed to send message.");
     }
 }
Example #4
0
 public void DistributeMessage(String Message)
 {
     foreach (TcpClient ReceivingClient in Clients.Keys)
     {
         if (!Terminated)
         {
             try {
                 NetworkStream ReceivingStream = ReceivingClient.GetStream();
                 UTF8Encoding  encoder         = new UTF8Encoding();
                 byte[]        buffer          = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(new Message(BeeperBuilder.CleanseUserForDistribution(ServerUser), "MESSAGE_STANDARD", Message, DateTime.Now)));
                 ReceivingStream.Write(buffer, 0, buffer.Length);
                 ReceivingStream.Flush();
             }
             catch { }
         }
     }
 }
Example #5
0
 public void DistributeMessage(Message Message)
 {
     foreach (TcpClient ReceivingClient in Clients.Keys)
     {
         if (!Terminated)
         {
             try
             {
                 NetworkStream ReceivingStream = ReceivingClient.GetStream();
                 UTF8Encoding  encoder         = new UTF8Encoding();
                 byte[]        buffer          = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(Message));
                 ReceivingStream.Write(buffer, 0, buffer.Length);
                 ReceivingStream.Flush();
             }
             catch { }
         }
     }
 }
Example #6
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();
        }
Example #7
0
        public static void Connect()
        {
            Console.WriteLine("TCP Client started");
            TcpClient client = new TcpClient();

            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 40613);

            Console.WriteLine("Server endpoint set.");

            Console.WriteLine("Connecting to endpoint...");
            try
            {
                client.Connect(serverEndPoint);
            } catch (Exception e)
            {
                Console.WriteLine("Cannot connect to endpoint: " + e.Message);
            }
            Console.WriteLine("Connected to endpoint.");

            Console.WriteLine("Looking for stream...");
            NetworkStream clientStream = client.GetStream();

            Console.WriteLine("Stream found.");

            Console.WriteLine("Building introductory message...");
            Message IntroductoryMessage = new Message();

            IntroductoryMessage.Sender  = User;
            IntroductoryMessage.Sent    = DateTime.Now;
            IntroductoryMessage.Type    = "INTRO";
            IntroductoryMessage.Content = "Hello, Beeper Server!";
            Console.WriteLine("Introductory message built.");

            Console.WriteLine("Initializing encoder...");
            ASCIIEncoding encoder = new ASCIIEncoding();

            Console.WriteLine("Encoder initialized.");
            Console.WriteLine("Serializing Message.");
            Console.WriteLine("Encoding message...");
            byte[] buffer = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(IntroductoryMessage));
            Console.WriteLine("Message encoded.");

            Console.WriteLine("Writing to stream...");
            clientStream.Write(buffer, 0, buffer.Length);
            Console.WriteLine("Written to buffer...");
            clientStream.Flush();
            Console.WriteLine("Written to stream.");
            Console.WriteLine("Starting stream reading...");

            Thread ReceivingThread = new Thread(new ParameterizedThreadStart(HandleServerConnection));

            ReceivingThread.Start(client);
            while (!Disconnected)
            {
                Console.Write("Server > ");
                String Data        = Console.ReadLine();
                String MessageType = "MESSAGE_STANDARD";
                if (Data.Substring(0, 2) == "::")
                {
                    MessageType = "COMMAND";
                    Data        = Data.Remove(0, 2);
                }
                Console.WriteLine(BeeperBuilder.BuildMessageJSON(new Message(User, MessageType, Data, DateTime.Now)));
                byte[] sendbuffer = encoder.GetBytes(BeeperBuilder.BuildMessageJSON(new Message(User, MessageType, Data, DateTime.Now)));
                clientStream.Write(sendbuffer, 0, sendbuffer.Length);
                clientStream.Flush();
            }
        }