Пример #1
0
            /// <summary>
            /// Runs a background thread, monitoring for new connections to the server.
            /// </summary>
            private void ServerMain()
            {
                while (PipeServer.ThreadState != System.Threading.ThreadState.AbortRequested)
                {
                    PipeSecurity security = new PipeSecurity();
                    security.AddAccessRule(new PipeAccessRule(
                                               WindowsIdentity.GetCurrent().User,
                                               PipeAccessRights.FullControl,
                                               AccessControlType.Allow));

                    using (NamedPipeServerStream server = new NamedPipeServerStream(InstanceID,
                                                                                    PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous,
                                                                                    128, 128, security))
                    {
                        ServerAsyncInfo async = new ServerAsyncInfo();
                        async.Server     = server;
                        async.WaitHandle = new AutoResetEvent(false);
                        IAsyncResult result = server.BeginWaitForConnection(WaitForConnection, async);

                        //Wait for the operation to complete.
                        if (result.AsyncWaitHandle.WaitOne())
                        {
                            //It completed. Wait for the processing to complete.
                            async.WaitHandle.WaitOne();
                        }
                    }
                }
            }
        private void BeginWaitForConnection()
        {
            NamedPipeServerStream pipeServer =
                new NamedPipeServerStream(guid, PipeDirection.In, 1,
                                          PipeTransmissionMode.Message, PipeOptions.Asynchronous);

            ServerAsyncInfo async = new ServerAsyncInfo();

            async.Server = pipeServer;

            IAsyncResult result = pipeServer.BeginWaitForConnection(Connection, async);
        }
        private void Connection(IAsyncResult result)
        {
            ServerAsyncInfo async = (ServerAsyncInfo)result.AsyncState;

            try
            {
                async.Server.EndWaitForConnection(result);

                //Process the connection if the server was successfully connected.
                if (async.Server.IsConnected)
                {
                    //Read the message from the secondary instance
                    MemoryStream ms     = new MemoryStream();
                    byte[]       buffer = new byte[8192];
                    do
                    {
                        int lastRead = async.Server.Read(buffer, 0, buffer.Length);
                        ms.Write(buffer, 0, lastRead);
                    }while (!async.Server.IsMessageComplete);

                    BinaryFormatter bf = new BinaryFormatter();
                    ms.Position = 0;
                    string[] args = (string[])bf.Deserialize(ms);

                    //Let the event handler process the message.
                    if (OnNextInstance != null)
                    {
                        OnNextInstance(args);
                    }
                }

                async.Server.Close();
                BeginWaitForConnection();
            }
            catch (Exception m)
            {
                Logger.Instance.Error(m);
            }
        }
Пример #4
0
            /// <summary>
            /// Waits for new connections to be made to the server.
            /// </summary>
            /// <param name="result"></param>
            private void WaitForConnection(IAsyncResult result)
            {
                ServerAsyncInfo async = (ServerAsyncInfo)result.AsyncState;

                try
                {
                    //We're done waiting for the connection
                    async.Server.EndWaitForConnection(result);

                    //Process the connection if the server was successfully connected.
                    if (async.Server.IsConnected)
                    {
                        //Read the message from the secondary instance
                        byte[]   buffer      = new byte[8192];
                        string[] commandLine = null;
                        string   message     = string.Empty;

                        do
                        {
                            int lastRead = async.Server.Read(buffer, 0, buffer.Length);
                            message += Encoding.UTF8.GetString(buffer, 0, lastRead);
                        }while (!async.Server.IsMessageComplete);

                        //Let the event handler process the message.
                        OnNextInstance(this, new NextInstanceEventArgs(commandLine, message));
                    }
                }
                catch (ObjectDisposedException)
                {
                }
                finally
                {
                    //Reset the wait event
                    async.WaitHandle.Set();
                }
            }
Пример #5
0
 private void ServerMain()
 {
     while (pipeServer.ThreadState != System.Threading.ThreadState.AbortRequested)
        {
     PipeSecurity security = new PipeSecurity();
     security.AddAccessRule(new PipeAccessRule(
      WindowsIdentity.GetCurrent().User,
      PipeAccessRights.FullControl, AccessControlType.Allow));
     using (NamedPipeServerStream server = new NamedPipeServerStream(instanceID,
      PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous,
      128, 128, security))
     {
      ServerAsyncInfo async = new ServerAsyncInfo();
      async.Server = server;
      async.WaitHandle = new AutoResetEvent(false);
      IAsyncResult result = server.BeginWaitForConnection(WaitForConnection, async);
      if (result.AsyncWaitHandle.WaitOne())
       async.WaitHandle.WaitOne();
     }
        }
 }