Пример #1
0
        void WaitForConnectionCallBack(IAsyncResult Result)
        {
            try
            {
                // Get the pipe
                NamedPipeServerStream pipeServer = (NamedPipeServerStream)(Result.AsyncState);
                TempServer = pipeServer;
                // End waiting for the connection
                pipeServer.EndWaitForConnection(Result);

                if (!RunningToken.IsCancellationRequested)
                {
                    #region NPContract
                    var                       Receiver     = SerializationContext.Default.GetSerializer <NPCallContract>();
                    NPCallContract            ClientCall   = Receiver.Unpack(pipeServer);
                    NPResultContract <object> ClientResult = new NPResultContract <object>();
                    if (ClientCall.VerifyMessage == VerifyMessage)
                    {
                        switch (ClientCall.Type)
                        {
                        case NPCalType.Get:
                            if (Command.ContainsKey(ClientCall.MethodCommand))
                            {
                                try
                                {
                                    ClientResult.ObjectResult = (Command[ClientCall.MethodCommand])(ClientCall.Argruments);
                                    ClientResult.Type         = NPResultType.Complete;
                                }
                                catch
                                {
                                    ClientResult.Type = NPResultType.InvalidOperationException;
                                }
                            }
                            else
                            {
                                ClientResult.Type = NPResultType.MissingCommandException;
                            }
                            break;
                        }
                    }
                    else
                    {
                        ClientResult.Type = NPResultType.VerifyMessageException;
                    }
                    var Sender = SerializationContext.Default.GetSerializer <NPResultContract <object> >();
                    Sender.Pack(pipeServer, ClientResult);
                    pipeServer.Dispose();
                    #endregion
                    #region CleanCode
                    if (pipeServer != null)
                    {
                        pipeServer.Dispose();
                        pipeServer = null;
                    }

                    if (Server != null)
                    {
                        Server.Dispose();
                        Server = null;
                    }
                    #endregion
                    #region Recursively wait for the connection
                    pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
                    Server     = pipeServer;


                    if (RunningToken != null && !RunningToken.IsCancellationRequested)
                    {
                        pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), Server);
                    }
                    #endregion
                }
            }
            catch
            {
                //Cannot access close pipe
                //Happen when stop
            }
        }
Пример #2
0
        public o Get <o>(string Command, params object[] argruments)
        {
            //Check argrument
            foreach (object obj in argruments)
            {
                Type ObjectType         = obj.GetType();
                bool IsHaveDataContract = Attribute.IsDefined(ObjectType, typeof(DataContractAttribute));
                if (!IsHaveDataContract && !ObjectType.IsSerializable)
                {
                    throw new ArgumentException("'" + ObjectType.Name + "' is not serializable type");
                }
            }


            o                     Result     = default(o);
            NPResultType          ResultType = NPResultType.Complete;
            NamedPipeClientStream pipeClient = null;

            try
            {
                pipeClient = new NamedPipeClientStream(ServerName.Length == 0 ? "." : ServerName, PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
                pipeClient.Connect(500);
                var            Sender     = SerializationContext.Default.GetSerializer <NPCallContract>();
                NPCallContract ClientCall = new NPCallContract();
                ClientCall.VerifyMessage = VerifyMessage;
                ClientCall.MethodCommand = Command;
                ClientCall.Type          = NPCalType.Get;
                ClientCall.Argruments    = argruments;
                Sender.Pack(pipeClient, ClientCall);

                var Receiver = SerializationContext.Default.GetSerializer <NPResultContract <o> >();
                NPResultContract <o> ClientResult = Receiver.Unpack(pipeClient);
                switch (ClientResult.Type)
                {
                case NPResultType.Complete:
                    Result     = (o)ClientResult.ObjectResult;
                    ResultType = NPResultType.Complete;
                    break;

                case NPResultType.InvalidOperationException:
                case NPResultType.MissingCommandException:
                case NPResultType.TimeoutException:
                case NPResultType.VerifyMessageException:
                    ResultType = ClientResult.Type;
                    break;
                }
            }
            catch (TimeoutException)
            {
                ResultType = NPResultType.TimeoutException;
            }
            finally
            {
                pipeClient.Close();
                pipeClient.Dispose();
            }

            if (ResultType == NPResultType.TimeoutException)
            {
                throw new TimeoutException("Missing '" + PipeName + "' server");
            }
            else if (ResultType == NPResultType.VerifyMessageException)
            {
                throw new UnauthorizedAccessException("Verify Message Incurrect");
            }
            else if (ResultType == NPResultType.MissingCommandException)
            {
                throw new MissingMethodException("Missing Command '" + Command + "'");
            }
            else if (ResultType == NPResultType.InvalidOperationException)
            {
                throw new InvalidOperationException("Somethings error on command '" + Command + "'");
            }
            return(Result);
        }