private static void ClientConnectionCallback(RemoteSessionNamedPipeServer pipeServer) { // Create server mediator object and begin remote session with client. NamedPipeProcessMediator.Run( string.Empty, pipeServer); }
/// <summary> /// Creates the custom named pipe server with the given pipename. /// </summary> /// <param name="pipeName">The name of the pipe to create.</param> public static void CreateCustomNamedPipeServer(string pipeName) { lock (s_syncObject) { if (_customNamedPipeServer != null && !_customNamedPipeServer.IsDisposed) { if (pipeName == _customNamedPipeServer.PipeName) { // we shouldn't recreate the server object if we're using the same pipeName return; } // Dispose of the current pipe server so we can create a new one with the new pipeName _customNamedPipeServer.Dispose(); } if (!Platform.IsWindows) { int maxNameLength = (Platform.IsLinux ? _maxPipePathLengthLinux : _maxPipePathLengthMacOS) - Path.GetTempPath().Length; if (pipeName.Length > maxNameLength) { throw new InvalidOperationException( string.Format( RemotingErrorIdStrings.CustomPipeNameTooLong, maxNameLength, pipeName, pipeName.Length)); } } try { try { _customNamedPipeServer = new RemoteSessionNamedPipeServer(pipeName); } catch (IOException) { // Expected when named pipe server for this process already exists. // This can happen if process has multiple AppDomains hosting PowerShell (SMA.dll). return; } // Listener ended callback, used to create listening new pipe server. _customNamedPipeServer.ListenerEnded += OnCustomNamedPipeServerEnded; // Start the pipe server listening thread, and provide client connection callback. _customNamedPipeServer.StartListening(ClientConnectionCallback); } catch (Exception) { _customNamedPipeServer = null; } } }
private static void CreateAppDomainUnloadHandler() { // Subscribe to the app domain unload event. AppDomain.CurrentDomain.DomainUnload += (sender, args) => { IPCNamedPipeServerEnabled = false; RemoteSessionNamedPipeServer namedPipeServer = IPCNamedPipeServer; if (namedPipeServer != null) { try { // Terminate the IPC thread. namedPipeServer.Dispose(); } catch (ObjectDisposedException) { } catch (Exception) { // Don't throw an exception on the app domain unload event thread. } } }; }
/// <summary> /// Creates the process named pipe server object singleton and /// starts the client listening thread. /// </summary> internal static void CreateIPCNamedPipeServerSingleton() { lock (s_syncObject) { if (!IPCNamedPipeServerEnabled) { return; } if (IPCNamedPipeServer == null || IPCNamedPipeServer.IsDisposed) { try { try { IPCNamedPipeServer = CreateRemoteSessionNamedPipeServer(); } catch (IOException) { // Expected when named pipe server for this process already exists. // This can happen if process has multiple AppDomains hosting PowerShell (SMA.dll). return; } // Listener ended callback, used to create listening new pipe server. IPCNamedPipeServer.ListenerEnded += OnIPCNamedPipeServerEnded; // Start the pipe server listening thread, and provide client connection callback. IPCNamedPipeServer.StartListening(ClientConnectionCallback); } catch (Exception e) { CommandProcessorBase.CheckForSevereException(e); IPCNamedPipeServer = null; } } } }
private static void CreateProcessExitHandler() { AppDomain.CurrentDomain.ProcessExit += (sender, args) => { IPCNamedPipeServerEnabled = false; RemoteSessionNamedPipeServer namedPipeServer = IPCNamedPipeServer; if (namedPipeServer != null) { try { // Terminate the IPC thread. namedPipeServer.Dispose(); } catch (ObjectDisposedException) { // Ignore if object already disposed. } catch (Exception) { // Don't throw an exception on the app domain unload event thread. } } }; }