public void SendCommand(CommandParameterPair command)
 {
     try
     {
         byte[] comm = Encoding.UTF8.GetBytes(EncodeMessage(command));
         Stream.Write(BitConverter.GetBytes(comm.Length));
         Stream.Write(comm);
     }
     catch (Exception)
     {
         throw new SocketException();
     }
 }
Beispiel #2
0
        public void SendCommand(CommandParameterPair command)
        {
            if (command.ParameterLength == 0)
            {
                byte[] fullMessage = Encoding.UTF8.GetBytes(command.Command);
                Stream.Write(BitConverter.GetBytes(fullMessage.Length), 0, 4);
                Stream.Write(fullMessage, 0, fullMessage.Length);
                return;
            }
            string smessage = EncodeMessage(command);

            byte[] byteMessage = Encoding.UTF8.GetBytes(smessage);
            Stream.Write(BitConverter.GetBytes(byteMessage.Length), 0, 4);
            Stream.Write(byteMessage, 0, byteMessage.Length);
        }
        public string EncodeMessage(CommandParameterPair pair)
        {
            if (pair.ParameterLength == 0)
            {
                return(pair.Command);
            }
            var builder = new StringBuilder();

            builder.Append(String.Format("{0} ", pair.Command));
            foreach (string i in pair.Parameters)
            {
                builder.Append(String.Format("{0}&", Uri.EscapeDataString(i)));
            }
            return(builder.ToString().Substring(0, builder.Length - 1));
        }
Beispiel #4
0
 public CommandParameterPair RecieveCommand()
 {
     while (true)
     {
         int messageLength;
         using (var stream = new MemoryStream())
         {
             while (stream.Length != 4)
             {
                 var buffer        = new byte[4 - stream.Length];
                 int bytesRecieved = Stream.Read(buffer, 0, 4);
                 stream.Write(buffer, 0, bytesRecieved);
                 if (!CheckIfConnected())
                 {
                     return(null);
                 }
             }
             messageLength = BitConverter.ToInt32(stream.ToArray(), 0);
         }
         using (var stream = new MemoryStream())
         {
             while (stream.Length != messageLength)
             {
                 var buffer        = stream.Length - messageLength > 512 ? new byte[512] : new byte[512 - (messageLength - stream.Length)];
                 int bytesRecieved = Stream.Read(buffer, 0, buffer.Length);
                 stream.Write(buffer, 0, bytesRecieved);
                 if (!CheckIfConnected())
                 {
                     return(null);
                 }
             }
             CommandParameterPair pair = DecodeMessage(Encoding.UTF8.GetString(stream.ToArray()));
             if (pair == null)
             {
                 SendInvalid("The command was not formatted correctly");
                 continue;
             }
             return(pair);
         }
     }
 }
Beispiel #5
0
        public void SendInvalid(string message)
        {
            var pair = new CommandParameterPair("INVOP", Uri.EscapeDataString(message));

            SendCommand(pair);
        }