Example #1
0
        static void Main()
        {
            using (Mutex mutex = new Mutex(false, @"Global\simpledlnaguilock")) {
            #if !DEBUG
            if (!mutex.WaitOne(0, false)) {
              using (var pipe = new NamedPipeClientStream(".", "simpledlnagui", PipeDirection.Out)) {
            try {
              pipe.Connect(10000);
              pipe.WriteByte(1);
            }
            catch (Exception) {
            }
            return;
              }
            }
            GC.Collect();
            #endif

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            using (var main = new FormMain()) {
              try {
            Application.Run(main);
              }
              catch (Exception ex) {
            log4net.LogManager.GetLogger(typeof(Program)).Fatal("Encountered fatal unhandled exception", ex);
            throw;
              }
            }
              }
        }
Example #2
0
    private static void Main()
    {
      using (var mutex = new Mutex(false, @"Global\simpledlnaguilock")) {
#if !DEBUG
        if (!mutex.WaitOne(0, false)) {
          using (var pipe = new NamedPipeClientStream(
              ".", "simpledlnagui", PipeDirection.Out)) {
            try {
              pipe.Connect(10000);
              pipe.WriteByte(1);
            }
            catch (Exception) {
            }
            return;
          }
        }
        GC.Collect();
#endif

        using (var main = new FormMain()) {
          try {
            Application.Run(main);
          }
          catch (Exception ex) {
            log4net.LogManager.GetLogger(typeof(Program)).Fatal(
              "Encountered fatal unhandled exception", ex);
            MessageBox.Show(
              string.Format(
                "Encountered an unhandled error. Will exit now.\n\n{0}\n{1}",
                ex.Message, ex.StackTrace),
              "Error",
              MessageBoxButtons.OK,
              MessageBoxIcon.Error
            );
            throw;
          }
        }
      }
    }
        private static void SendDataMessage(NamedPipeClientStream pipe, String xml)
        {
            try
            {
                // Write data length, string and null
                _logger.LogMsg(Level.Debug, String.Format("Getting byte array from XML string for sending (data pipe)."));
                {
                    // Write data length to stream. This is a 32-bit integer in big endian byte order.
                    int data_len = xml.Length + 1;
                    byte[] ar_bytes = BitConverter.GetBytes(data_len);
                    for (int ii = 0; ii < 4; ii++)
                    {
                        pipe.WriteByte(ar_bytes[3 - ii]);
                    }

                    Debug.WriteLine(String.Format("Writing XML to stream: {0} chars", xml.Length.ToString()));
                    Byte[] xml_buf = GetBytes(xml);
                    for (int ii = 0; ii < xml_buf.Length; ii++)
                    {
                        if (xml_buf[ii] != 0x00)
                        {
                            pipe.WriteByte(xml_buf[ii]);
                        }
                    }
                    Debug.WriteLine("Writing 0x00 to stream.");
                    pipe.WriteByte(0x00);

                    pipe.Flush();

                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("Error in SendDateMessage(): " + ex.Message);
                throw ex;
            }
        }
        private static void SendCommandMessage(NamedPipeClientStream pipe, String xml)
        {
            try
            {
                // Write data length (4 byte integer in big-endian byte order), xml message (string) and final null byte.
                _logger.LogMsg(Level.Debug, String.Format("Sending Command Response message:\r\n{0}", xml));

                int data_len = xml.Length + 1;
                byte[] ar_bytes = BitConverter.GetBytes(data_len);
                //Array.Reverse(ar_bytes);

                // Write the length out in reverse (big-endian byte order).
                for (int ii = 0; ii < 4; ii++)
                {
                    pipe.WriteByte(ar_bytes[3-ii]);
                }
                // Write the XML string 1 byte at a time - no nulls.
                Debug.WriteLine(String.Format("Writing XML to stream: total {0} chars", xml.Length.ToString()));
                Byte[] xml_buf = GetBytes(xml);
                for (int ii = 0; ii < xml_buf.Length; ii++)
                {
                    if (xml_buf[ii] != 0x00)
                    {
                        pipe.WriteByte(xml_buf[ii]);
                    }
                }
                // Write the final null byte that QlikView is expecting.
                pipe.WriteByte(0x00);

                // TO DO: is this Flush() necessary?
                pipe.Flush();

                // Wait for QlikView to read from the pipe.
               // _logger.LogMsg(Level.Debug, "Waiting for pipe drain on command pipe.");
               // pipe.WaitForPipeDrain();
               // _logger.LogMsg(Level.Debug, "Pipe drain complete.");

            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("Error in ReadNextCommandMessage(): " + ex.Message);
                throw ex;
            }
        }
            protected void ProcessReadFromLog(NamedPipeClientStream pipe, CancellationToken token)
            {
                //Just doing this to clear the stream for the next command.
                ReadToEnd(pipe);
                string path = Path.Combine(VM_SelfManager.LogPath, VMName + ".log");
                FileStream Log = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

                // This is only complicated in order to handle cases of WAY oversized log files.
                int reps = (int)(Log.Length / int.MaxValue);
                byte[] data = new byte[(reps==0 ? (int)(Log.Length % int.MaxValue) : int.MaxValue)];

                //read the log
                Log.Seek(0, SeekOrigin.Begin);
                for (int i = 0; i <= reps; i++)
                {
                    Log.Read(data, 0, (i == reps ? (int) (Log.Length%int.MaxValue) : int.MaxValue));
                    //write it immediately so we can grab the next set if needed...
                    pipe.Write(data, 0, (i == reps ? (int) (Log.Length%int.MaxValue) : int.MaxValue));
                }
                pipe.WriteByte(0x1A); // writes an 'end of stream' character so the client knows that all has been sent.
            }
        private Response GetResponse(Request request)
        {
            _pipeClient = CreateClientPipe();
            TryConnect(_pipeClient);

            _pipeClient.WriteByte((byte)request);
            _pipeClient.WriteByte((byte)_pos);
            _pipeClient.WaitForPipeDrain();

            Response response = (Response)_pipeClient.ReadByte();
            return response;
        }
Example #7
0
        public static void Main(string[] Args)
        {
            if (Args.Length > 0)
            {
                if (Args[0] == "spawnclient")
                {
                    NamedPipeClientStream pipeClient =
                        new NamedPipeClientStream(".", "testpipe",
                            PipeDirection.InOut, PipeOptions.None,
                            TokenImpersonationLevel.Impersonation);

                    Console.WriteLine("Connecting to server...\n");
                    pipeClient.Connect();
                    SampleModel.SampleObject obj = new SampleObject() { PDFURL = "star" };
                    BinaryFormatter bf = new BinaryFormatter();

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bf.Serialize(ms, obj);
                        pipeClient.WriteByte(ms.ToArray());
                        //pipeClient.Write(, 0, ms.ToArray().Length);

                    }

                    //StreamWriter sw = new StreamWriter(pipeClient);
                    //sw.WriteLine("시작");
                    //sw.Flush();
                    //StreamString ss = new StreamString(pipeClient);
                    //// Validate the server's signature string
                    //if (ss.ReadString() == "I am the one true server!")
                    //{
                    //    // The client security token is sent with the first write.
                    //    // Send the name of the file whose contents are returned
                    //    // by the server.
                    //    ss.WriteString("c:\\textfile.txt");

                    //    // Print the file to the screen.
                    //    Console.Write(ss.ReadString());
                    //}
                    //else
                    //{
                    //    Console.WriteLine("Server could not be verified.");
                    //}
                    //pipeClient.Close();
                    // Give the client process some time to display results before exiting.

                    Thread.Sleep(4000);
                    pipeClient.Close();
                }
            }
            else
            {
                Console.WriteLine("\n*** Named pipe client stream with impersonation example ***\n");
                StartClients();
            }
        }