示例#1
0
 public static int Command(this ITestableNetworkStream netStream, char commandkey)
 {
     try
     {
         if (netStream.CanWrite && Commands.ContainsKey(commandkey))
         {
             System.Diagnostics.Debug.WriteLine("\t\tTransmit Command('{0}'): ASC({1} --> 0x{2})", commandkey, Commands[commandkey].Key, BitConverter.ToString(Noxon.IntToByteArray(Commands[commandkey].Key)));
             netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int));
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     catch (System.IO.IOException e)
     {
         System.Diagnostics.Debug.WriteLine("\t\tTransmit Command() failed ({0})", e.Message);
         Close();
         Open();
         if (netStream != null && netStream.CanWrite)
         {
             netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int));
         }
         return(0);
     }
     catch
     {
         return(-1);
     }
 }
        public async static Task <int> CommandAsync(this NetworkStream netStream, char commandkey)
        {
            try
            {
                if (netStream.CanWrite && Noxon.Commands.ContainsKey(commandkey))
                {
                    System.Diagnostics.Debug.WriteLine("\t\tTransmit CommandAsync('{0}'): ASC({1} --> 0x{2})", commandkey, Noxon.Commands[commandkey].Key, BitConverter.ToString(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key)));
                    if (Properties.Settings.Default.LogCommands)
                    {
                        Program.FormShow.Log(Form1.ParsedElementsWriter, Form1.StdOut, new XElement("CommandAsync", commandkey));
                    }
                    await netStream.WriteAsync(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key), 0, sizeof(int));

                    return(0);
                }
                else
                {
                    return(-1);
                }
            }
            catch (System.IO.IOException e)
            {
                System.Diagnostics.Debug.WriteLine("\t\tTransmit CommandAsync() failed ({0})", e.Message);
                Noxon.Close();
                Task <bool> isOPen = NoxonAsync.OpenAsync();
                await       isOPen;
                if (netStream != null && netStream.CanWrite)
                {
                    await netStream.WriteAsync(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key), 0, sizeof(int));
                }
                return(0);
            }
            catch
            {
                return(-1);
            }
        }