public override void f_INIT() { Tracer.WriteLine("J{0}_{1} {2} -> INIT", this.f_getId(), this.Type, this.GetType().Name); try { //// Add an endpoint so the client can connect, this is local-host only: server.AddProtocol(RpcProtseq.ncalrpc, codeAPI, 100); //// If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\" + codeAPI, 25); ////// 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, @"18081", 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 += f_server_OnExecute; //_server.OnExecute += delegate (IRpcClientInfo client, byte[] bytes) { return new byte[0]; }; // Start Listening server.StartListening(); } catch (Exception ex) { Console.Error.WriteLine(ex); } }
public void TestPerformanceOnLocalRpc() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { return arg; }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest")) { client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); client.Execute(new byte[0]); byte[] bytes = new byte[512]; new Random().NextBytes(bytes); Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 0; i < 10000; i++) client.Execute(bytes); timer.Stop(); Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), "ncalrpc-timming"); } } }
public void active(string endpoint) { // The client and server must agree on the interface id to use: var iid = new Guid("{78323803-786f-4f7b-908d-b2e89c41d45f}"); // Create the server instance, adjust the defaults to your needs. source = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false); try { // Add an endpoint so the client can connect, this is local-host only: source.AddProtocol(RpcProtseq.ncalrpc, endpoint, 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 source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE); source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE); source.OnExecute += executeDispatch; source.OnExecuteAsync += executeAsyncDispatch; source.StartListening(); } catch (Exception e) { throw; } }
public void TestPerformanceOnLocalRpc() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { return(arg); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest")) { client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); client.Execute(new byte[0]); byte[] bytes = new byte[512]; new Random().NextBytes(bytes); Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 0; i < 10000; i++) { client.Execute(bytes); } timer.Stop(); Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), "ncalrpc-timming"); } } }
static void Main(string[] args) { var iid = Guid.NewGuid(); using (var server = new RpcServerApi(iid, 1234, -1, true)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.StartListening(); Console.WriteLine("Is listening"); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { string tmp = ""; return(new byte[0]); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { // client.AuthenticateAs(RpcClientApi.Self); byte[] response = client.Execute(new byte[0]); } Console.ReadLine(); string s = ""; } }
public void TestNestedClientImpersonate() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(false, client.IsImpersonating); using (client.Impersonate()) { Assert.AreEqual(true, client.IsImpersonating); using (client.Impersonate()) Assert.AreEqual(true, client.IsImpersonating); //does not dispose, we are still impersonating Assert.AreEqual(true, client.IsImpersonating); } Assert.AreEqual(false, client.IsImpersonating); return(arg); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Self); client.Execute(new byte[0]); } } }
public void TestClientAbandon() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { return arg; }; { RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"); client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); client.Execute(new byte[0]); client = null; } GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); server.StopListening(); } }
public void TestUnregisterListener() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); RpcServerApi.RpcExecuteHandler handler = delegate(IRpcClientInfo client, byte[] arg) { return(arg); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest")) { client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); server.OnExecute += handler; client.Execute(new byte[0]); server.OnExecute -= handler; try { client.Execute(new byte[0]); Assert.Fail(); } catch (RpcException) { } } } }
public void TestClientAbandon() { var iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { return(arg); }; { RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"); client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); client.Execute(new byte[0]); client = null; } GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); server.StopListening(); } }
public void TestClientOnNamedPipe() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(0, arg.Length); Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_WINNT, client.AuthenticationLevel); Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, client.ProtectionLevel); Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType); Assert.AreEqual(new byte[0], client.ClientAddress); Assert.AreEqual(0, client.ClientPid.ToInt32()); Assert.AreEqual(String.Empty, client.ClientPrincipalName); Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, client.ClientUser.Name); Assert.AreEqual(true, client.IsClientLocal); Assert.AreEqual(true, client.IsAuthenticated); Assert.AreEqual(false, client.IsImpersonating); using (client.Impersonate()) Assert.AreEqual(true, client.IsImpersonating); Assert.AreEqual(false, client.IsImpersonating); return arg; }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Self); client.Execute(new byte[0]); } } }
public void TestUnregisterListener() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); RpcServerApi.RpcExecuteHandler handler = delegate(IRpcClientInfo client, byte[] arg) { return arg; }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest")) { client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT); server.OnExecute += handler; client.Execute(new byte[0]); server.OnExecute -= handler; try { client.Execute(new byte[0]); Assert.Fail(); } catch (RpcException) { } } } }
private static void Main() { // 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 += (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.ForegroundColor = ConsoleColor.Yellow; Console.Error.WriteLine(ex); Console.ResetColor(); } // Wait until you are done... Console.WriteLine("Server is listening. Press [Enter] to exit."); Console.ReadLine(); } }
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); }); }
public void Start(IEnumerable <byte> ipAddress = null) { if (ipAddress == null) { ipAddress = _ipAdrHelper.GetIpv4Address(); } _ipAddress = new IpAddress(DateTime.UtcNow, _serviceFlag, ipAddress.ToArray(), ushort.Parse(PortsHelper.GetPort(_network))); PeersStore.Instance().SetMyIpAddress(_ipAddress); var iid = Interop.Constants.InterfaceId; var instance = PeersStore.Instance(); var port = PortsHelper.GetPort(_network); // _server = new RpcServerApi(iid, 1234, -1, true); // _server.AddProtocol(RpcProtseq.ncacn_ip_tcp, port, 5); var name = (new IPAddress(_ipAddress.Ipv6)).MapToIPv4().ToString(); // USE QUEUE. _server = new RpcServerApi(iid, 1234, -1, true); _server.AddProtocol(RpcProtseq.ncalrpc, name, 5); _server.StartListening(); _server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { var message = _messageParser.Parse(arg); var connectedPeer = _p2pNetworkConnector.GetPeer(message.MessageHeader.Ipv6); if (NewMessageEvent != null) { NewMessageEvent(this, new StringEventArgs(message.GetCommandName())); } var response = _messageCoordinator.Receive(message, connectedPeer, _p2pNetworkConnector); if (response == null) { return(new byte[0]); } return(response.Serialize()); }; if (StartNodeEvent != null) { StartNodeEvent(this, EventArgs.Empty); } }
private void ServerLoop() { Guid iid = new Guid(Constants.ACWatchDogInteropId); using (RpcServerApi server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false)) { // Add an endpoint so the client can connect, this is local-host only: server.AddProtocol(RpcProtseq.ncalrpc, Constants.ACWatchDogInteropEndpoint, 100); // Add the types of authentication we will accept server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE); // Subscribe the code to handle requests on this event: server.OnExecute += Server_OnExecute; // Start Listening server.StartListening(); @continue = true; while (@continue) { Thread.Sleep(100); } server.StopListening(); } }
public void TestClientOnAnonymousPipe() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(0, arg.Length); Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_NONE, client.AuthenticationLevel); Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE, client.ProtectionLevel); Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType); Assert.AreEqual(new byte[0], client.ClientAddress); Assert.AreEqual(0, client.ClientPid.ToInt32()); Assert.AreEqual(null, client.ClientPrincipalName); Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetAnonymous().Name, client.ClientUser.Name); Assert.AreEqual(true, client.IsClientLocal); Assert.AreEqual(false, client.IsAuthenticated); Assert.AreEqual(false, client.IsImpersonating); bool failed = false; try { client.Impersonate().Dispose(); } catch (UnauthorizedAccessException) { failed = true; } Assert.AreEqual(true, failed); return(arg); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Anonymous); client.Execute(new byte[0]); } } }
static void ReversePingTest(RpcProtseq protocol, string[] hostNames, string endpoint, RpcAuthentication auth) { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Array.Reverse(arg); return(arg); }; server.AddProtocol(protocol, endpoint, 5); server.AddAuthentication(auth); server.StartListening(); byte[] input = Encoding.ASCII.GetBytes("abc"); byte[] expect = Encoding.ASCII.GetBytes("cba"); foreach (string hostName in hostNames) { using (RpcClientApi client = new RpcClientApi(iid, protocol, hostName, endpoint)) { client.AuthenticateAs(null, auth == RpcAuthentication.RPC_C_AUTHN_NONE ? RpcClientApi.Anonymous : RpcClientApi.Self, auth == RpcAuthentication.RPC_C_AUTHN_NONE ? RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE : RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, auth); Assert.AreEqual(expect, client.Execute(input)); } } } }
public void TestClientOnNamedPipe() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(0, arg.Length); Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_WINNT, client.AuthenticationLevel); Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, client.ProtectionLevel); Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType); Assert.AreEqual(new byte[0], client.ClientAddress); Assert.AreEqual(0, client.ClientPid.ToInt32()); Assert.AreEqual(String.Empty, client.ClientPrincipalName); Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, client.ClientUser.Name); Assert.AreEqual(true, client.IsClientLocal); Assert.AreEqual(true, client.IsAuthenticated); Assert.AreEqual(false, client.IsImpersonating); using (client.Impersonate()) Assert.AreEqual(true, client.IsImpersonating); Assert.AreEqual(false, client.IsImpersonating); return(arg); }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Self); client.Execute(new byte[0]); } } }
static void ReversePingTest(RpcProtseq protocol, string[] hostNames, string endpoint, RpcAuthentication auth) { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Array.Reverse(arg); return arg; }; server.AddProtocol(protocol, endpoint, 5); server.AddAuthentication(auth); server.StartListening(); byte[] input = Encoding.ASCII.GetBytes("abc"); byte[] expect = Encoding.ASCII.GetBytes("cba"); foreach (string hostName in hostNames) { using (RpcClientApi client = new RpcClientApi(iid, protocol, hostName, endpoint)) { client.AuthenticateAs(null, auth == RpcAuthentication.RPC_C_AUTHN_NONE ? RpcClientApi.Anonymous : RpcClientApi.Self, auth == RpcAuthentication.RPC_C_AUTHN_NONE ? RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE : RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, auth); Assert.AreEqual(expect, client.Execute(input)); } } } }
public void TestNestedClientImpersonate() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(false, client.IsImpersonating); using (client.Impersonate()) { Assert.AreEqual(true, client.IsImpersonating); using (client.Impersonate()) Assert.AreEqual(true, client.IsImpersonating); //does not dispose, we are still impersonating Assert.AreEqual(true, client.IsImpersonating); } Assert.AreEqual(false, client.IsImpersonating); return arg; }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Self); client.Execute(new byte[0]); } } }
public void TestClientOnAnonymousPipe() { Guid iid = Guid.NewGuid(); using (RpcServerApi server = new RpcServerApi(iid)) { server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5); server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE); server.StartListening(); server.OnExecute += delegate(IRpcClientInfo client, byte[] arg) { Assert.AreEqual(0, arg.Length); Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_NONE, client.AuthenticationLevel); Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE, client.ProtectionLevel); Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType); Assert.AreEqual(new byte[0], client.ClientAddress); Assert.AreEqual(0, client.ClientPid.ToInt32()); Assert.AreEqual(null, client.ClientPrincipalName); Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetAnonymous().Name, client.ClientUser.Name); Assert.AreEqual(true, client.IsClientLocal); Assert.AreEqual(false, client.IsAuthenticated); Assert.AreEqual(false, client.IsImpersonating); bool failed = false; try { client.Impersonate().Dispose(); } catch (UnauthorizedAccessException) { failed = true; } Assert.AreEqual(true, failed); return arg; }; using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename")) { client.AuthenticateAs(RpcClientApi.Anonymous); client.Execute(new byte[0]); } } }
static void Main(string[] args) { // The client and server must agree on the interface id to use: //var iid = new Guid("{0092F74D-0EA7-4667-A89F-A04C64244031}"); var iid = new Guid("{78323803-786f-4f7b-908d-b2e89c41d45f}"); // 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, "12345", 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()) { Console.WriteLine("Received'{0}' from {1} ...", bytes.Length, client.ClientUser.Name); byte[] response; var memoryStream = new MemoryStream(bytes); RpcProto.Request message; try { message = ProtoBuf.Serializer.Deserialize <RpcProto.Request>(memoryStream); } catch (Exception e) { Console.WriteLine(e); throw e; } switch (message.method) { case RpcProto.RequestType.GetCustomers: var r = message.post; Console.WriteLine("Received '{0}'", r); var rpn = new RpcProto.GetCustomerResponse() { result = 0, }; response = toBytes(rpn); break; default: response = new byte[0]; break; } return(response); } }; server.OnExecuteAsync += delegate(IRpcClientInfo client, IntPtr pAsyncState, byte[] bytes) { //Impersonate the caller: using (client.Impersonate()) { Console.WriteLine("Received'{0}' from {1} ...", bytes.Length, client.ClientUser.Name); byte[] response; var memoryStream = new MemoryStream(bytes); RpcProto.Request message; try { message = ProtoBuf.Serializer.Deserialize <RpcProto.Request>(memoryStream); } catch (Exception e) { Console.WriteLine(e); throw e; } switch (message.method) { case RpcProto.RequestType.PostCustomer: var r1 = message.post; Console.WriteLine("Received '{0}'", r1); var rpn1 = new RpcProto.PostCustomerResponse() { result = 0 }; response = toBytes(rpn1); break; case RpcProto.RequestType.DeleteCustomer: { } var r2 = message.delete; Console.WriteLine("Received '{0}'", r2); var rpn2 = new RpcProto.DeleteCustomerResponse() { result = 0 }; response = toBytes(rpn2); break; default: response = new byte[0]; break; } return(response); } }; // Start Listening server.StartListening(); } catch (Exception ex) { Console.Error.WriteLine(ex); } // Wait until you are done... Console.WriteLine("Press [Enter] to exit..."); Console.ReadLine(); } }
public Win32RpcServer AddProtocol(string protocol, string endpoint, uint maxCallLimit) { _server.AddProtocol(Win32RpcClient.Parse(protocol), endpoint, maxCallLimit); return(this); }
static void Main(string[] args) { // The client and server must agree on the interface id to use: var iid = new Guid("{f4db45dc-0dcb-4003-b680-56c40f6cb6a8}"); // 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, "1234", 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 ) )); } }; server.OnExecuteAsync += delegate(IRpcClientInfo client, IntPtr pAsyncState, byte[] bytes) { //Impersonate the caller: using (client.Impersonate()) { var reqBody = Encoding.UTF8.GetString(bytes); Console.Write("Received (async) '{0}' from {1} ...", reqBody, client.ClientUser.Name); Thread.Sleep(3 * 1000); Console.WriteLine("Wake up!"); 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(); } }
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(); } }