コード例 #1
0
ファイル: Program.cs プロジェクト: baominxing/Learning
        private static void ServerThread(object data)
        {
            NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads);

            int threadId = Thread.CurrentThread.ManagedThreadId;

            // Wait for a client to connect
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected on thread[{0}].", threadId);
            try
            {
                // Read the request from the client. Once the client has
                // written to the pipe its security token will be available.

                StreamString ss = new StreamString(pipeServer);

                // Verify our identity to the connected client using a
                // string that the client anticipates.

                ss.WriteString("I am the one true server!");
                string filename = ss.ReadString();

                if (!File.Exists(filename))
                {
                    using (File.Create(filename))
                    {
                    }
                }

                // Read in the contents of the file while impersonating the client.1
                ReadFileToStream fileReader = new ReadFileToStream(ss, filename);

                // Display the name of the user we are impersonating.
                Console.WriteLine("Reading file: {0} on thread[{1}] as user: {2}.",
                                  filename, threadId, pipeServer.GetImpersonationUserName());
                pipeServer.RunAsClient(fileReader.Start);
            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            pipeServer.Close();
        }
コード例 #2
0
ファイル: PipeServer.cs プロジェクト: ukrop73/uwpipc
        public void start(CancellationToken cancellationToken)
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            Console.WriteLine(String.Format("PipeServer starting[{0}]...", threadId));

start:

            NamedPipeServerStream pipeServer = null;

            try
            {
                pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.InOut, MaxPipes, PipeTransmissionMode.Byte, PipeOptions.None);

                /*PipeSecurity ps = pipeServer.GetAccessControl();
                 * Log.Information("ps={0}", ps.AccessRuleType.FullName);
                 * //ps.SetAccessRuleProtection(false, false);
                 * foreach (PipeAccessRule r in ps.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                 * {
                 *  Log.Information("PipeAccessRule={0}/{1}/{2}", r.AccessControlType, r.PipeAccessRights, r.IdentityReference);
                 * }
                 * //pipeServer.SetAccessControl(ps);
                 * WindowsIdentity identity = WindowsIdentity.GetCurrent();
                 * WindowsPrincipal principal = new WindowsPrincipal(identity);
                 * Log.Information("identity/principal={0}/{1}", identity.User, principal);
                 * /*if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                 * {
                 *  // Allow the Administrators group full access to the pipe.
                 *  ps.AddAccessRule(new PipeAccessRule("LAPTOP-4GA1U2Q9\\oleks",
                 *      PipeAccessRights.FullControl, AccessControlType.Allow));
                 *  //new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).Translate(typeof(NTAccount)),
                 *  //    PipeAccessRights.FullControl, AccessControlType.Allow));
                 * }
                 * else
                 * {
                 *  // Allow current user read and write access to the pipe.
                 *  ps.AddAccessRule(new PipeAccessRule(
                 *      WindowsIdentity.GetCurrent().User,
                 *      PipeAccessRights.ReadWrite, AccessControlType.Allow));
                 * }
                 *
                 * pipeServer.SetAccessControl(ps);
                 * //PipesAclExtensions.SetAccessControl(pipeServer, ps);*/
            }
            catch (Exception exception)
            {
                Console.WriteLine(String.Format("PipeServer[{0}] Error: {1}", threadId, exception.Message));
                return;
            }

            cancellationToken.Register(() => pipeServer.Close());

            // Wait for a client to connect
            try
            {
                pipeServer.WaitForConnection();
            }
            catch (Exception exception)
            {
                Console.WriteLine(String.Format("PipeServer[{0}] Error: {1}", threadId, exception.Message));
                return;
            }

            Console.WriteLine(String.Format("PipeServer[{0}] Client connected.", threadId));
            stateEvent?.Invoke(this);

            try
            {
                StreamString ss = new StreamString(pipeServer);
                ss.WriteString(Message.PhraseHello, false);
                Console.WriteLine(String.Format("PipeServer[{0}] Hello Phrase sent.", threadId));

                Message messageIn;
                string  result;

                while (!cancellationToken.IsCancellationRequested)
                {
                    messageIn = ss.ReadString();
                    Console.WriteLine(String.Format("PipeServer[{0}] messageIn={1}", threadId, messageIn));

                    if (messageIn.isBye() || messageIn.isEnd())
                    {
                        break;
                    }

                    if (messageIn.isError())
                    {
                        ss.WriteString(messageIn.getError());
                        break;
                    }

                    result = objectToJson("testcommand", "Ok.");
                    if (ss.WriteString(result) == 0)
                    {
                        result = objectToJson("testcommand", "Too long result. Please try to use filter.");
                        ss.WriteString(result);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(String.Format("PipeServer[{0}] Error: {1}", threadId, exception.Message));
            }

            try
            {
                pipeServer.Close();
            }
            catch (Exception exception)
            {
                Console.WriteLine(String.Format("PipeServer[{0}] Error: {1}", threadId, exception.Message));
            }

            Console.WriteLine(String.Format("PipeServer[{0}] done.", threadId));
            goto start;
        }