internal ServerSteppablePipelineDriver(PowerShell powershell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse, ServerSteppablePipelineSubscriber eventSubscriber, PSDataCollection<object> powershellInput) { this.localPowerShell = powershell; this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.noInput = noInput; this.addToHistory = addToHistory; this.eventSubscriber = eventSubscriber; this.powershellInput = powershellInput; this.input = new PSDataCollection<object>(); this.inputEnumerator = this.input.GetEnumerator(); this.input.ReleaseOnEnumeration = true; this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, null); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); this.dsHandler.InputEndReceived += new EventHandler(this.HandleInputEndReceived); this.dsHandler.InputReceived += new EventHandler<RemoteDataEventArgs<object>>(this.HandleInputReceived); this.dsHandler.StopPowerShellReceived += new EventHandler(this.HandleStopReceived); this.dsHandler.HostResponseReceived += new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived); this.dsHandler.OnSessionConnected += new EventHandler(this.HandleSessionConnected); if (rsToUse == null) { throw PSTraceSource.NewInvalidOperationException("RemotingErrorIdStrings", "NestedPipelineMissingRunspace", new object[0]); } this.localPowerShell.Runspace = rsToUse; eventSubscriber.SubscribeEvents(this); this.stateOfSteppablePipeline = PSInvocationState.NotStarted; }
private void RegisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler) { dsHandler.InputEndReceived += HandleInputEndReceived; dsHandler.InputReceived += HandleInputReceived; dsHandler.StopPowerShellReceived += HandleStopReceived; dsHandler.HostResponseReceived += HandleHostResponseReceived; dsHandler.OnSessionConnected += HandleSessionConnected; }
private void UnregisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler) { dsHandler.InputEndReceived -= new EventHandler(this.HandleInputEndReceived); dsHandler.InputReceived -= new EventHandler <RemoteDataEventArgs <object> >(this.HandleInputReceived); dsHandler.StopPowerShellReceived -= new EventHandler(this.HandleStopReceived); dsHandler.HostResponseReceived -= new EventHandler <RemoteDataEventArgs <RemoteHostResponse> >(this.HandleHostResponseReceived); dsHandler.OnSessionConnected -= new EventHandler(this.HandleSessionConnected); }
internal void DispatchMessageToPowerShell(RemoteDataObject <PSObject> rcvdData) { ServerPowerShellDataStructureHandler associatedPowerShellDataStructureHandler = this.GetAssociatedPowerShellDataStructureHandler(rcvdData.PowerShellId); if (associatedPowerShellDataStructureHandler != null) { associatedPowerShellDataStructureHandler.ProcessReceivedData(rcvdData); } }
private void HandleRemoveAssociation(object sender, EventArgs e) { ServerPowerShellDataStructureHandler handler = sender as ServerPowerShellDataStructureHandler; lock (this.associationSyncObject) { this.associatedShells.Remove(handler.PowerShellId); } this.transportManager.RemoveCommandTransportManager(handler.PowerShellId); }
/// <summary> /// Dispatch the message to the associated powershell data structure handler. /// </summary> /// <param name="rcvdData">Message to dispatch.</param> internal void DispatchMessageToPowerShell(RemoteDataObject <PSObject> rcvdData) { ServerPowerShellDataStructureHandler dsHandler = GetAssociatedPowerShellDataStructureHandler(rcvdData.PowerShellId); // if data structure handler is not found, then association has already been // removed, discard message if (dsHandler != null) { dsHandler.ProcessReceivedData(rcvdData); } }
internal ServerPowerShellDataStructureHandler GetAssociatedPowerShellDataStructureHandler(Guid clientPowerShellId) { ServerPowerShellDataStructureHandler handler = null; lock (this.associationSyncObject) { if (!this.associatedShells.TryGetValue(clientPowerShellId, out handler)) { handler = null; } } return(handler); }
/// <summary> /// Remove the association of the powershell from the runspace pool. /// </summary> /// <param name="sender">Sender of this event.</param> /// <param name="e">Unused.</param> private void HandleRemoveAssociation(object sender, EventArgs e) { Dbg.Assert(sender is ServerPowerShellDataStructureHandler, @"sender of the event must be ServerPowerShellDataStructureHandler"); ServerPowerShellDataStructureHandler dsHandler = sender as ServerPowerShellDataStructureHandler; lock (_associationSyncObject) { _associatedShells.Remove(dsHandler.PowerShellId); } // let session transport manager remove its association of command transport manager. _transportManager.RemoveCommandTransportManager(dsHandler.PowerShellId); }
internal ServerPowerShellDataStructureHandler CreatePowerShellDataStructureHandler(Guid instanceId, Guid runspacePoolId, RemoteStreamOptions remoteStreamOptions, PowerShell localPowerShell) { AbstractServerTransportManager transportManager = this.transportManager; if (instanceId != Guid.Empty) { transportManager = this.transportManager.GetCommandTransportManager(instanceId); } ServerPowerShellDataStructureHandler handler = new ServerPowerShellDataStructureHandler(instanceId, runspacePoolId, remoteStreamOptions, transportManager, localPowerShell); lock (this.associationSyncObject) { this.associatedShells.Add(handler.PowerShellId, handler); } handler.RemoveAssociation += new EventHandler(this.HandleRemoveAssociation); return handler; }
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse) { this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.localPowerShell = powershell; this.extraPowerShell = extraPowerShell; this.localPowerShellOutput = new PSDataCollection<PSObject>(); this.noInput = noInput; this.addToHistory = addToHistory; this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, this.localPowerShell); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); if (!noInput) { this.input = new PSDataCollection<object>(); this.input.ReleaseOnEnumeration = true; this.input.IdleEvent += new EventHandler<EventArgs>(this.HandleIdleEvent); } this.RegisterPipelineOutputEventHandlers(this.localPowerShellOutput); if (this.localPowerShell != null) { this.RegisterPowerShellEventHandlers(this.localPowerShell); this.datasent[0] = false; } if (extraPowerShell != null) { this.RegisterPowerShellEventHandlers(extraPowerShell); this.datasent[1] = false; } this.RegisterDataStructureHandlerEventHandlers(this.dsHandler); if (rsToUse != null) { this.localPowerShell.Runspace = rsToUse; if (extraPowerShell != null) { extraPowerShell.Runspace = rsToUse; } } else { this.localPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell != null) { extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } } }
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, System.Management.Automation.RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse) { this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.localPowerShell = powershell; this.extraPowerShell = extraPowerShell; this.localPowerShellOutput = new PSDataCollection <PSObject>(); this.noInput = noInput; this.addToHistory = addToHistory; this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, this.remoteStreamOptions, this.localPowerShell); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); if (!noInput) { this.input = new PSDataCollection <object>(); this.input.ReleaseOnEnumeration = true; this.input.IdleEvent += new EventHandler <EventArgs>(this.HandleIdleEvent); } this.RegisterPipelineOutputEventHandlers(this.localPowerShellOutput); if (this.localPowerShell != null) { this.RegisterPowerShellEventHandlers(this.localPowerShell); this.datasent[0] = false; } if (extraPowerShell != null) { this.RegisterPowerShellEventHandlers(extraPowerShell); this.datasent[1] = false; } this.RegisterDataStructureHandlerEventHandlers(this.dsHandler); if (rsToUse != null) { this.localPowerShell.Runspace = rsToUse; if (extraPowerShell != null) { extraPowerShell.Runspace = rsToUse; } } else { this.localPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell != null) { extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } } }
/// <summary> /// Get the associated powershell data structure handler for the specified /// powershell id /// </summary> /// <param name="clientPowerShellId">powershell id for the /// powershell data structure handler</param> /// <returns>ServerPowerShellDataStructureHandler</returns> internal ServerPowerShellDataStructureHandler GetAssociatedPowerShellDataStructureHandler (Guid clientPowerShellId) { ServerPowerShellDataStructureHandler dsHandler = null; lock (_associationSyncObject) { bool success = _associatedShells.TryGetValue(clientPowerShellId, out dsHandler); if (!success) { dsHandler = null; } } return(dsHandler); }
internal ServerPowerShellDataStructureHandler CreatePowerShellDataStructureHandler(Guid instanceId, Guid runspacePoolId, RemoteStreamOptions remoteStreamOptions, PowerShell localPowerShell) { AbstractServerTransportManager transportManager = this.transportManager; if (instanceId != Guid.Empty) { transportManager = this.transportManager.GetCommandTransportManager(instanceId); } ServerPowerShellDataStructureHandler handler = new ServerPowerShellDataStructureHandler(instanceId, runspacePoolId, remoteStreamOptions, transportManager, localPowerShell); lock (this.associationSyncObject) { this.associatedShells.Add(handler.PowerShellId, handler); } handler.RemoveAssociation += new EventHandler(this.HandleRemoveAssociation); return(handler); }
internal ServerPowerShellDataStructureHandler CreatePowerShellDataStructureHandler( ServerPowerShellDriver driver) { using (ServerRunspacePoolDataStructureHandler.tracer.TraceMethod()) { AbstractServerTransportManager transportManager = (AbstractServerTransportManager)this.transportManager; if (driver.InstanceId != Guid.Empty) { transportManager = this.transportManager.GetCommandTransportManager(driver.InstanceId); } ServerPowerShellDataStructureHandler structureHandler = new ServerPowerShellDataStructureHandler(driver, transportManager); lock (this.associationSyncObject) this.associatedShells.Add(structureHandler.PowerShellId, structureHandler); structureHandler.RemoveAssociation += new EventHandler(this.HandleRemoveAssociation); return(structureHandler); } }
/// <summary> /// Returns the currently active PowerShell datastructure handler. /// </summary> /// <returns> /// ServerPowerShellDataStructureHandler if one is present, null otherwise. /// </returns> internal ServerPowerShellDataStructureHandler GetPowerShellDataStructureHandler() { lock (_associationSyncObject) { if (_associatedShells.Count > 0) { foreach (object o in _associatedShells.Values) { ServerPowerShellDataStructureHandler result = o as ServerPowerShellDataStructureHandler; if (result != null) { return(result); } } } } return(null); }
internal ServerPowerShellDataStructureHandler GetPowerShellDataStructureHandler() { lock (this.associationSyncObject) { if (this.associatedShells.Count > 0) { foreach (object obj2 in this.associatedShells.Values) { ServerPowerShellDataStructureHandler handler = obj2 as ServerPowerShellDataStructureHandler; if (handler != null) { return(handler); } } } } return(null); }
/// <summary> /// Creates a powershell data structure handler from this runspace pool. /// </summary> /// <param name="instanceId">Powershell instance id.</param> /// <param name="runspacePoolId">Runspace pool id.</param> /// <param name="remoteStreamOptions">Remote stream options.</param> /// <param name="localPowerShell">Local PowerShell object.</param> /// <returns>ServerPowerShellDataStructureHandler.</returns> internal ServerPowerShellDataStructureHandler CreatePowerShellDataStructureHandler( Guid instanceId, Guid runspacePoolId, RemoteStreamOptions remoteStreamOptions, PowerShell localPowerShell) { // start with pool's transport manager. AbstractServerTransportManager cmdTransportManager = _transportManager; if (instanceId != Guid.Empty) { cmdTransportManager = _transportManager.GetCommandTransportManager(instanceId); Dbg.Assert(cmdTransportManager.TypeTable != null, "This should be already set in managed C++ code"); } ServerPowerShellDataStructureHandler dsHandler = new ServerPowerShellDataStructureHandler(instanceId, runspacePoolId, remoteStreamOptions, cmdTransportManager, localPowerShell); lock (_associationSyncObject) { _associatedShells.Add(dsHandler.PowerShellId, dsHandler); } dsHandler.RemoveAssociation += new EventHandler(HandleRemoveAssociation); return(dsHandler); }
internal ServerPowerShellDriver( PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId, Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver, ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions, bool addToHistory, Runspace rsToUse) { this.clientPowerShellId = clientPowerShellId; this.clientRunspacePoolId = clientRunspacePoolId; this.remoteStreamOptions = streamOptions; this.apartmentState = apartmentState; this.localPowerShell = powershell; this.extraPowerShell = extraPowerShell; this.localPowerShellOutput = new PSDataCollection <PSObject>(); this.noInput = noInput; this.addToHistory = addToHistory; if (!noInput) { this.input = new PSDataCollection <object>(); this.input.ReleaseOnEnumeration = true; } this.dsHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(this); this.remoteHost = this.dsHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost); this.localPowerShellOutput.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleOutputDataAdded); if (this.localPowerShell != null) { this.localPowerShell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(this.HandlePowerShellInvocationStateChanged); this.localPowerShell.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleErrorDataAdded); this.localPowerShell.Streams.Debug.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleDebugAdded); this.localPowerShell.Streams.Verbose.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleVerboseAdded); this.localPowerShell.Streams.Warning.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleWarningAdded); this.localPowerShell.Streams.Progress.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleProgressAdded); } if (extraPowerShell != null) { extraPowerShell.InvocationStateChanged += new EventHandler <PSInvocationStateChangedEventArgs>(this.HandlePowerShellInvocationStateChanged); extraPowerShell.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleErrorDataAdded); extraPowerShell.Streams.Debug.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleDebugAdded); extraPowerShell.Streams.Verbose.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleVerboseAdded); extraPowerShell.Streams.Warning.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleWarningAdded); extraPowerShell.Streams.Progress.DataAdded += new EventHandler <DataAddedEventArgs>(this.HandleProgressAdded); } this.dsHandler.InputEndReceived += new EventHandler(this.HandleInputEndReceived); this.dsHandler.InputReceived += new EventHandler <RemoteDataEventArgs <object> >(this.HandleInputReceived); this.dsHandler.StopPowerShellReceived += new EventHandler(this.HandleStopReceived); this.dsHandler.HostResponseReceived += new EventHandler <RemoteDataEventArgs <RemoteHostResponse> >(this.HandleHostResponseReceived); if (rsToUse != null) { this.localPowerShell.Runspace = rsToUse; if (extraPowerShell == null) { return; } extraPowerShell.Runspace = rsToUse; } else { this.localPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; if (extraPowerShell == null) { return; } extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool; } }
private void UnregisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler) { dsHandler.InputEndReceived -= new EventHandler(this.HandleInputEndReceived); dsHandler.InputReceived -= new EventHandler<RemoteDataEventArgs<object>>(this.HandleInputReceived); dsHandler.StopPowerShellReceived -= new EventHandler(this.HandleStopReceived); dsHandler.HostResponseReceived -= new EventHandler<RemoteDataEventArgs<RemoteHostResponse>>(this.HandleHostResponseReceived); dsHandler.OnSessionConnected -= new EventHandler(this.HandleSessionConnected); }
private void UnregisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler) { dsHandler.InputEndReceived -= HandleInputEndReceived; dsHandler.InputReceived -= HandleInputReceived; dsHandler.StopPowerShellReceived -= HandleStopReceived; dsHandler.HostResponseReceived -= HandleHostResponseReceived; dsHandler.OnSessionConnected -= HandleSessionConnected; }
/// <summary> /// Creates a powershell data structure handler from this runspace pool /// </summary> /// <param name="instanceId">powershell instance id</param> /// <param name="runspacePoolId">runspace pool id</param> /// <param name="remoteStreamOptions">remote stream options</param> /// <param name="localPowerShell">local PowerShell object</param> /// <returns>ServerPowerShellDataStructureHandler</returns> internal ServerPowerShellDataStructureHandler CreatePowerShellDataStructureHandler( Guid instanceId, Guid runspacePoolId, RemoteStreamOptions remoteStreamOptions, PowerShell localPowerShell) { // start with pool's transport manager. AbstractServerTransportManager cmdTransportManager = _transportManager; if (instanceId != Guid.Empty) { cmdTransportManager = _transportManager.GetCommandTransportManager(instanceId); Dbg.Assert(cmdTransportManager.TypeTable != null, "This should be already set in managed C++ code"); } ServerPowerShellDataStructureHandler dsHandler = new ServerPowerShellDataStructureHandler(instanceId, runspacePoolId, remoteStreamOptions, cmdTransportManager, localPowerShell); lock (_associationSyncObject) { _associatedShells.Add(dsHandler.PowerShellId, dsHandler); } dsHandler.RemoveAssociation += new EventHandler(HandleRemoveAssociation); return dsHandler; }