Ejemplo n.º 1
0
        public void GetMessages(IEventAggregator _eventAggregator)
        {
            ResponseHandler._eventAggregator = _eventAggregator;
            String response = "";

            // Bytes Array to receive Server Response.
            Byte[] data = new Byte[512];

            Task.Run(async() =>
            {
                // Read the Tcp Server Response Bytes.
                try
                {
                    Int32 bytes;
                    while ((bytes = await networkStream.ReadAsync(data, 0, data.Length)) != 0)
                    {
                        //Int32 bytes = await networkStream.ReadAsync(data, 0, data.Length);
                        response = System.Text.Encoding.UTF8.GetString(data, 0, bytes);
                        _eventAggregator?.GetEvent <UpdateUserConsole>().Publish(response);

                        if (response.Contains("BYE"))
                        {
                            ResponseHandler.Bye();
                            Disconnect();
                            break;
                        }

                        var responseTokens = response.Split(Environment.NewLine);
                        responseTokens     = responseTokens.Where(i => i != "").ToArray();


                        if (responseTokens.Last().Split(' ')[0] == outgoingTag)
                        {
                            //Here we can handle a server result (it means that the command execution is finished
                            //on server side and we can start handle its result on the server).
                            wholeServerResponse += response;
                            ResponseHandler.HandleResponse(outgoingCommand, response);
                            wholeServerResponse += "";
                        }
                        else if (response.Split(' ')[0] == "*" || response.Split(' ')[0] == "+")
                        {
                            //Over here we can add a condition for a currently occuring command
                            //for an instance, sending encryption keys if the server response is "+",
                            //or when we want to read the information a bit by a bit.
                            wholeServerResponse += response;
                        }
                        else
                        {
                            //Illegal server response. Should usually not be the case.
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("You are now disconnected from the server.");
                    Disconnect();
                    ResponseHandler.Bye();
                    //System.Windows.MessageBox.Show(ex.Message);
                    //break;
                }
            });
        }