Ejemplo n.º 1
0
        /// <summary>
        /// Reads a response from the stream to CWSRestart
        /// </summary>
        /// <returns>A tuple containting the Command and the content of the response. Can be null if no response was read</returns>
        private Tuple <Commands.Command, string> readResponse()
        {
            if (tryConnect())
            {
                StreamReader reader = new StreamReader(client, System.Text.Encoding.UTF8, true, 2048, true);

                string response = reader.ReadLine();

                if (response == null)
                {
                    return(null);
                }

                string[] messages = response.Split(new string[] { " " }, 3, StringSplitOptions.None);

                if (messages.Count() == 2 || messages.Count() == 3)
                {
                    Commands.Action  a = (Commands.Action)Enum.Parse(typeof(Commands.Action), messages[0]);
                    Commands.Command c = (Commands.Command)Enum.Parse(typeof(Commands.Command), messages[1]);

                    string message = (messages.Count() == 3) ? messages[2] : "";

                    switch (a)
                    {
                    case Commands.Action.POST:
                        reader.Close();
                        return(new Tuple <Commands.Command, string>(c, message));

                    default:
                        reader.Close();
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    reader.Close();
                    throw new NotImplementedException();
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send the given command with the given content and method to CWSRestart
        /// </summary>
        /// <param name="command"></param>
        /// <param name="content"></param>
        /// <param name="action"></param>
        /// <returns>True if the command was sent succesful, otherwise false</returns>
        private bool sendCommand(Commands.Command command, String content, Commands.Action action)
        {
            try
            {
                if (tryConnect())
                {
                    StreamWriter writer = new StreamWriter(client, System.Text.Encoding.UTF8, 2048, true);

                    string message = String.Format("{0} {1} {2}", action, command, content);
                    writer.WriteLine(message);
                    writer.Close();
                    return(true);
                }
                return(false);
            }
            catch (IOException)
            {
                disconnectClient();
                return(false);
            }
        }