示例#1
0
        /// <summary>
        ///     The function called when a client connects to the named pipe. Note: This method is called on a non-UI thread.
        /// </summary>
        /// <param name="iAsyncResult"></param>
        public static void NamedPipeServerConnectionCallback(IAsyncResult iAsyncResult)
        {
            try
            {
                // End waiting for the connection
                _namedPipeServerStream.EndWaitForConnection(iAsyncResult);

                // Read data and prevent access to _namedPipeXmlPayload during threaded operations
                lock (_namedPiperServerThreadLock)
                {
                    // Read data from client
                    //var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload));
                    IFormatter f = new BinaryFormatter();
                    _namedPipePayload = (NamedPipePayload)(f.Deserialize(_namedPipeServerStream));

                    if (_namedPipePayload.SignalQuit)
                    {
                        return;
                    }

                    //Console.WriteLine("Got Authenticaiton Response.");

                    // _namedPipeXmlPayload contains the data sent from the other instance
                    //Console.WriteLine(_namedPipePayload.Arguments);
                    Authentication.ProcessAuthenticationResponse(_namedPipePayload.Arguments);
                }
            }
            catch (ObjectDisposedException)
            {
                // EndWaitForConnection will exception when someone closes the pipe before connection made
                // In that case we dont create any more pipes and just return
                // This will happen when app is closing and our pipe is closed/disposed
                return;
            }
            catch (Exception)
            {
                // ignored
            }
            finally
            {
                // Close the original pipe (we will create a new one each time)
                _namedPipeServerStream.Dispose();
            }

            // Create a new pipe for next connection
            NamedPipeServerCreateServer();
        }
示例#2
0
        /// <summary>
        /// Uses a named pipe client to send the currently parsed options to an already running instance.
        /// </summary>
        /// <param name="namedPipePayload"></param>
        public static void SendNamedPipeClient(NamedPipePayload namedPipePayload)
        {
            try
            {
                using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream("test-pipe"))
                {
                    namedPipeClient.Connect();

                    IFormatter f = new BinaryFormatter();
                    f.Serialize(namedPipeClient, namedPipePayload);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
示例#3
0
        /// <summary>
        ///     Uses a named pipe to send the currently parsed options to an already running instance.
        /// </summary>
        /// <param name="namedPipePayload"></param>
        public static void NamedPipeClientSendOptions(NamedPipePayload namedPipePayload)
        {
            try
            {
                using (var namedPipeClientStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
                {
                    namedPipeClientStream.Connect(3000); // Maximum wait 3 seconds
                    IFormatter f = new BinaryFormatter();
                    f.Serialize(namedPipeClientStream, namedPipePayload);

                    using (StreamReader sr = new StreamReader(namedPipeClientStream))
                    {
                        while (true)
                        {
                            string buffer;
                            try
                            {
                                buffer = sr.ReadLine();
                            }
                            catch
                            {
                                //read error has occurred
                                break;
                            }

                            //client has disconnected
                            if (buffer.Length == 0)
                            {
                                break;
                            }

                            //Console.WriteLine(buffer);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // Error connecting or sending
            }
        }