static void Main(string[] args)
        {
            // The client and server must agree on the interface id to use:
            var iid = new Guid("{1B617C4B-BF68-4B8C-AE2B-A77E6A3ECEC5}");
            
            // Create the server instance, adjust the defaults to your needs.
            using (var server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false))
            {
                try
                {
                    // Add an endpoint so the client can connect, this is local-host only:
                    server.AddProtocol(RpcProtseq.ncalrpc, "RpcExampleClientServer", 100);

                    // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                    // server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"8080", 25);

                    // Add the types of authentication we will accept
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                    // Subscribe the code to handle requests on this event:
                    server.OnExecute +=
                        delegate(IRpcClientInfo client, byte[] bytes)
                            {
                                //Impersonate the caller:
                                using (client.Impersonate())
                                {
                                    var reqBody = Encoding.UTF8.GetString(bytes);
                                    Console.WriteLine("Received '{0}' from {1}", reqBody, client.ClientUser.Name);

                                    return Encoding.UTF8.GetBytes(
                                        String.Format(
                                            "Hello {0}, I received your message '{1}'.",
                                            client.ClientUser.Name,
                                            reqBody
                                            )
                                        );
                                }
                            };

                    // Start Listening 
                    server.StartListening();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }

                // Wait until you are done...
                Console.WriteLine("Press [Enter] to exit...");
                Console.ReadLine();
            }
        }
Example #2
0
        static BuildScript()
        {
            server = new Lazy<RpcServerApi>(() => {
                var result = new RpcServerApi(iid);
                result.AddProtocol(RpcProtseq.ncacn_np, pipeName, 5);
                //Authenticate via WinNT
                // server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);

                //Start receiving calls
                result.StartListening();
                return result;
            });
        }
Example #3
0
        protected void ExecuteTool() {
            try {
                if ((((ushort)GetKeyState(0x91)) & 0xffff) != 0) {
                    Debugger.Break();
                }
                if (skip) {
                    return;
                }

                if (StartMessage.Is()) {
                    Messages.Enqueue( new BuildMessage( StartMessage));
                }

                Guid iid = Guid.NewGuid();
                string pipeName = @"\pipe\ptk_{0}_{1}".format(Process.GetCurrentProcess().Id, Index);
                Result = true;

                using (var server = new RpcServerApi(iid)) {
                    string currentProjectName = string.Empty;
                    //Allow up to 5 connections over named pipes
                    server.AddProtocol(RpcProtseq.ncacn_np, pipeName, 5);
                    //Authenticate via WinNT
                    // server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);

                    //Start receiving calls
                    server.StartListening();
                    //When a call comes, do the following:
                    server.OnExecute +=
                        (client, arg) => {
                            // deserialize the message object and replay thru this logger. 
                            var message = BuildMessage.DeserializeFromString(arg.ToUtf8String());
                            // var message = JsonSerializer.DeserializeFromString<BuildMessage>(arg.ToUtf8String());
                            if (!filters.Any(each => each.IsMatch(message.Message))) {
                                Messages.Enqueue(message);
                            }
                            if (cancellationTokenSource.IsCancellationRequested) {
                                return new byte[1] {
                                    0x01
                                };
                            }

                            return new byte[0];
                        };

                    foreach (var project in projectFiles) {
                        if (cancellationTokenSource.IsCancellationRequested) {
                            Result = false;
                            return;
                        }

                        currentProjectName = project;
                        if (ProjectStartMessage.Is()) {
                            Messages.Enqueue(new BuildMessage(ProjectStartMessage));
                        }

                        try {
                            // no logo, thanks.
                            var parameters = " /nologo";

                            // add properties lines.
                            if (!Properties.IsNullOrEmpty()) {
                                parameters = parameters + " /p:" + Properties.Select(each => each.ItemSpec).Aggregate((c, e) => c + ";" + e);
                            }

                            parameters = parameters + @" /noconsolelogger ""/logger:ClrPlus.Scripting.MsBuild.Building.Logger,{0};{1};{2}"" ""{3}""".format(Assembly.GetExecutingAssembly().Location, pipeName, iid, project);
                            if ((((ushort)GetKeyState(0x91)) & 0xffff) != 0) {
                                Debugger.Break();
                            }

                            var proc = AsyncProcess.Start(
                                new ProcessStartInfo(MSBuildUtility.MsbuildExe.Path, parameters) {
                                    WindowStyle = ProcessWindowStyle.Normal
                                    ,
                                }, _environment);

                            while (!proc.WaitForExit(20)) {
                                if (cancellationTokenSource.IsCancellationRequested) {
                                    proc.Kill();
                                }
                            }

                            // StdErr = proc.StandardError.Where(each => each.Is()).Select(each => (ITaskItem)new TaskItem(each)).ToArray();
                            // StdOut = proc.StandardOutput.Where(each => each.Is()).Select(each => (ITaskItem)new TaskItem(each)).ToArray();
                            if (proc.ExitCode != 0) {
                                Result = false;
                                return;
                            }
                            ;
                        } catch (Exception e) {
                            Messages.Enqueue( new BuildMessage( "{0},{1},{2}".format(e.GetType().Name, e.Message, e.StackTrace)) {
                                EventType = "BuildError",
                            });

                            Result = false;
                            return;
                        }

                        if (ProjectEndMessage.Is()) {
                            Messages.Enqueue(new BuildMessage (ProjectEndMessage));
                        }
                    }
                }

                if (EndMessage.Is()) {
                    Messages.Enqueue(new BuildMessage(EndMessage));
                }
            } catch (Exception e) {
                Messages.Enqueue(new BuildMessage("{0},{1},{2}".format(e.GetType().Name, e.Message, e.StackTrace)) {
                    EventType = "BuildError",
                });
            } finally {
                Completed.Set();
            }
        }
 public Win32RpcServer(Guid iid, IRpcServerStub implementation)
     : base(implementation)
 {
     _server = new RpcServerApi(iid);
 }