示例#1
0
        private void RunSendingServer(string pipeName, IpcCommands command, Func <object> function)
        {
            _namedPipeServer = new NamedPipeServerStream(NamedPipe.GetUserPipeName(pipeName), PipeDirection.Out, 2, PipeTransmissionMode.Message,
                                                         PipeOptions.Asynchronous);

            AsyncCallback ac = null;

            ac = o =>
            {
                NamedPipeServerStream s = (NamedPipeServerStream)o.AsyncState;
                try
                {
                    if (disposedValue)
                    {
                        return;
                    }
                    s.EndWaitForConnection(o);
                    object data = function.Invoke();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.WriteByte((byte)command);
                        if (data != null)
                        {
                            BinaryFormatter bf = new BinaryFormatter();
                            bf.Serialize(ms, data);
                        }
                        ms.Seek(0, SeekOrigin.Begin);

                        ms.CopyTo(s);
                        s.Flush();
                        s.WaitForPipeDrain();
                    }

                    s.Disconnect();
                    s.BeginWaitForConnection(ac, s);
                }
                catch (Exception)
                {
                    s.Dispose();
                    RunSendingServer(pipeName, command, function);
                }
            };
            _namedPipeServer.BeginWaitForConnection(ac, _namedPipeServer);
        }
示例#2
0
        private void RunReceivingServer(string pipeName, IMessageProcessor messageProcessor)
        {
            PipeSecurity pipeSecurity = new PipeSecurity();

            pipeSecurity.SetAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

            _namedPipeServer = new NamedPipeServerStream(NamedPipe.GetUserPipeName(pipeName), PipeDirection.In, 1, PipeTransmissionMode.Message,
                                                         PipeOptions.Asynchronous, 0, 0, pipeSecurity);

            AsyncCallback ac = null;

            ac = o =>
            {
                if (disposedValue)
                {
                    return;
                }
                NamedPipeServerStream server = (NamedPipeServerStream)o.AsyncState;
                try
                {
                    server.EndWaitForConnection(o);

                    object data = NamedPipe.ReadMessages(server, out IpcCommands command);
                    messageProcessor.ProcessMessages(command, data);
                    server.Disconnect();

                    server.BeginWaitForConnection(ac, server);
                }
                catch (Exception e)
                {
                    Logging.LogException(e);
                    RunReceivingServer(pipeName, messageProcessor);
                }
            };
            _namedPipeServer.BeginWaitForConnection(ac, _namedPipeServer);
        }