static void ServerThreadLoop(object mrevent) { ManualResetEvent resetEvent = (ManualResetEvent)mrevent; var host = new ServerHost(); SingleonIPCService.Instance.OnMessageRecieved += IPCService_OnMessageRecieved; BindingScheme schemes = BindingScheme.NAMED_PIPE; foreach (var sch in _options.BindingScheme) { switch (sch) { case 't': schemes |= BindingScheme.TCP; break; case 'p': schemes |= BindingScheme.NAMED_PIPE; break; } } host.Start(SingleonIPCService.Instance, resetEvent, new ServerOptions(_options.ServerName, new Dictionary <string, string>() { { "Description", "Report CPU and RAM usage" } }) { Scheme = schemes, EnableDiscovery = true }); }
internal static string GetEndpointAddress(this BindingScheme scheme, IConnectionOptions options, bool isMex = false) { string transport = string.Empty; string serverHostName = options.HostName; switch (scheme) { case BindingScheme.NAMED_PIPE: transport = "net.pipe"; break; case BindingScheme.TCP: transport = "net.tcp"; serverHostName = $"{options.HostName}:{options.TCPPort}"; break; } if (isMex) { return($"{transport}://{serverHostName}/Codex/{options.ProcessID}/mex"); } else { return($"{transport}://{serverHostName}/Codex/{options.ProcessID}/IPCService"); } }
internal static Binding GetBinding(this BindingScheme scheme, IConnectionOptions options) { Binding binding = null; switch (scheme) { case BindingScheme.TCP: { binding = new NetTcpBinding(SecurityMode.None); NetTcpBinding tcpBinding = ((NetTcpBinding)binding); tcpBinding.MaxBufferPoolSize = Constants.MAX_MSG_SIZE; tcpBinding.MaxReceivedMessageSize = Constants.MAX_MSG_SIZE; break; } case BindingScheme.NAMED_PIPE: { binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); NetNamedPipeBinding npBinding = ((NetNamedPipeBinding)binding); npBinding.MaxBufferPoolSize = Constants.MAX_MSG_SIZE; npBinding.MaxReceivedMessageSize = Constants.MAX_MSG_SIZE; break; } } if (binding != null) { binding.CloseTimeout = options.CloseTimeout; binding.OpenTimeout = options.OpenTimeout; binding.ReceiveTimeout = options.ReceiveTimeout; binding.SendTimeout = options.SendTimeout; } return(binding); }
/// <summary> /// Start the IPC server for this process. /// </summary> /// <param name="resetEvent">Reset event to gracefully shutdown the server.</param> /// <param name="processID">Base address for the process.</param> /// <param name="options">Connections options for the server.</param> /// <param name="scheme">Connection schemes that can be used in the server, this is a flags enum so multiple schemes can be provided.</param> public void Start(ManualResetEvent resetEvent, string processID, ConnectionOptions options, BindingScheme scheme) { List<Uri> baseAddresses = new List<Uri>(); if (scheme.IsBindingScheme(BindingScheme.NAMED_PIPE)) { baseAddresses.Add(new Uri(BindingScheme.NAMED_PIPE.GetEndpointAddress(processID, options, false))); } if (scheme.IsBindingScheme(BindingScheme.TCP)) { baseAddresses.Add(new Uri(BindingScheme.TCP.GetEndpointAddress(processID, options, false))); } if (scheme.IsBindingScheme(BindingScheme.HTTP)) { baseAddresses.Add(new Uri(BindingScheme.HTTP.GetEndpointAddress(processID, options, false))); } using (var host = new ServiceHost(IPCService.Instance, baseAddresses.ToArray())) { // Check to see if the service host already has a ServiceMetadataBehavior var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); // If not, add one if (smb == null) { smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb); } // Check to see if the service host already has a ServiceDebugBehavior var sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>(); // If not, add one if (sdb == null) { sdb = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }; host.Description.Behaviors.Add(sdb); } // Setup the bindings if (scheme.IsBindingScheme(BindingScheme.TCP)) { var tcpBinding = (NetTcpBinding)BindingScheme.TCP.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), tcpBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), tcpBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), BindingScheme.TCP.GetEndpointAddress(processID, options, true)); } if (scheme.IsBindingScheme(BindingScheme.NAMED_PIPE)) { var namedPipeBinding = (NetNamedPipeBinding)BindingScheme.NAMED_PIPE.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), namedPipeBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), namedPipeBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), BindingScheme.NAMED_PIPE.GetEndpointAddress(processID, options, true)); } if (scheme.IsBindingScheme(BindingScheme.HTTP)) { var httpBinding = (NetHttpBinding)BindingScheme.HTTP.GetBinding(options); host.AddServiceEndpoint(typeof(IIPC), httpBinding, ""); host.AddServiceEndpoint(typeof(IIPCDuplex), httpBinding, ""); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), BindingScheme.HTTP.GetEndpointAddress(processID, options, true)); } host.Open(); Trace.WriteLine("Service up and running at:"); foreach (var ea in host.Description.Endpoints) { Trace.WriteLine(ea.Address); } resetEvent.WaitOne(); host.Close(); } }
internal static bool IsBindingScheme(this BindingScheme scheme, BindingScheme schemeToCheck) { return((scheme & schemeToCheck) == schemeToCheck); }
/// <summary> /// Gets a duplex client /// </summary> /// <param name="processID">Process identifier for the server on the host machine.</param> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCDuplexClient GetDuplexClient(InstanceContext context, string processID, ConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return new IPCDuplexClient(context, scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(processID, options, false))); }
/// <summary> /// Gets a basic request response client /// </summary> /// <param name="processID">Process identifier for the server on the host machine.</param> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCClient GetClient(string processID, ConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return new IPCClient(scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(processID, options, false))); }
/// <summary> /// Gets a duplex client /// </summary> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCDuplexClient GetDuplexClient(InstanceContext context, IConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return(new IPCDuplexClient(context, scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(options, false)))); }
/// <summary> /// Gets a basic request response client /// </summary> /// <param name="options">Connection Options</param> /// <returns>Client object.</returns> public static IPCClient GetClient(IConnectionOptions options, BindingScheme scheme = BindingScheme.NAMED_PIPE) { return(new IPCClient(scheme.GetBinding(options), new EndpointAddress(scheme.GetEndpointAddress(options, false)))); }
public static bool IsBindingScheme(this BindingScheme scheme, BindingScheme schemeToCheck) { return (scheme & schemeToCheck) == schemeToCheck; }