private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo, bool raiseEvents) { base.stateInfo = newStateInfo; this.availableForConnection = (base.stateInfo.State == RunspacePoolState.Disconnected) || (base.stateInfo.State == RunspacePoolState.Opened); if (raiseEvents) { base.RaiseStateChangeEvent(newStateInfo); } }
internal void ProcessReceivedData(RemoteDataObject <PSObject> receivedData) { if (receivedData.RunspacePoolId != this.clientRunspacePoolId) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.RunspaceIdsDoNotMatch, new object[] { receivedData.RunspacePoolId, this.clientRunspacePoolId }); } switch (receivedData.DataType) { case RemotingDataType.RunspacePoolOperationResponse: this.SetMaxMinRunspacesResponseRecieved.SafeInvoke <RemoteDataEventArgs <PSObject> >(this, new RemoteDataEventArgs <PSObject>(receivedData.Data)); return; case RemotingDataType.RunspacePoolStateInfo: { RunspacePoolStateInfo runspacePoolStateInfo = RemotingDecoder.GetRunspacePoolStateInfo(receivedData.Data); this.StateInfoReceived.SafeInvoke <RemoteDataEventArgs <RunspacePoolStateInfo> >(this, new RemoteDataEventArgs <RunspacePoolStateInfo>(runspacePoolStateInfo)); this.NotifyAssociatedPowerShells(runspacePoolStateInfo); return; } case RemotingDataType.CreatePowerShell: case RemotingDataType.AvailableRunspaces: case RemotingDataType.GetCommandMetadata: break; case RemotingDataType.PSEventArgs: { PSEventArgs pSEventArgs = RemotingDecoder.GetPSEventArgs(receivedData.Data); this.PSEventArgsReceived.SafeInvoke <RemoteDataEventArgs <PSEventArgs> >(this, new RemoteDataEventArgs <PSEventArgs>(pSEventArgs)); break; } case RemotingDataType.ApplicationPrivateData: { PSPrimitiveDictionary applicationPrivateData = RemotingDecoder.GetApplicationPrivateData(receivedData.Data); this.ApplicationPrivateDataReceived.SafeInvoke <RemoteDataEventArgs <PSPrimitiveDictionary> >(this, new RemoteDataEventArgs <PSPrimitiveDictionary>(applicationPrivateData)); return; } case RemotingDataType.RunspacePoolInitData: { RunspacePoolInitInfo runspacePoolInitInfo = RemotingDecoder.GetRunspacePoolInitInfo(receivedData.Data); this.RSPoolInitInfoReceived.SafeInvoke <RemoteDataEventArgs <RunspacePoolInitInfo> >(this, new RemoteDataEventArgs <RunspacePoolInitInfo>(runspacePoolInitInfo)); return; } case RemotingDataType.RemoteHostCallUsingRunspaceHost: { RemoteHostCall data = RemoteHostCall.Decode(receivedData.Data); this.RemoteHostCallReceived.SafeInvoke <RemoteDataEventArgs <RemoteHostCall> >(this, new RemoteDataEventArgs <RemoteHostCall>(data)); return; } default: return; } }
private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo, bool raiseEvents) { using (RemoteRunspacePoolInternal.tracer.TraceMethod()) { this.stateInfo = newStateInfo; if (!raiseEvents) { return; } this.RaiseStateChangeEvent(newStateInfo); } }
private void HandleSessionClosed(object sender, RemoteDataEventArgs <Exception> eventArgs) { RunspacePoolState state; RunspacePoolStateInfo info; if (eventArgs.Data != null) { this.closingReason = eventArgs.Data; } lock (base.syncObject) { state = base.stateInfo.State; switch (state) { case RunspacePoolState.Opening: case RunspacePoolState.Opened: case RunspacePoolState.Disconnecting: case RunspacePoolState.Disconnected: case RunspacePoolState.Connecting: this.SetRunspacePoolState(new RunspacePoolStateInfo(RunspacePoolState.Broken, this.closingReason)); break; case RunspacePoolState.Closing: this.SetRunspacePoolState(new RunspacePoolStateInfo(RunspacePoolState.Closed, this.closingReason)); break; } info = new RunspacePoolStateInfo(base.stateInfo.State, base.stateInfo.Reason); } try { base.RaiseStateChangeEvent(info); } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); } switch (state) { case RunspacePoolState.Disconnecting: case RunspacePoolState.Disconnected: this.SetDisconnectAsCompleted(); break; default: if (state == RunspacePoolState.Connecting) { this.SetReconnectAsCompleted(); } break; } this.SetCloseAsCompleted(); }
public override IAsyncResult BeginClose(AsyncCallback callback, object asyncState) { bool flag = false; bool flag2 = false; RunspacePoolStateInfo stateInfo = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null); RunspacePoolAsyncResult closeAsyncResult = null; lock (base.syncObject) { if ((base.stateInfo.State == RunspacePoolState.Closed) || (base.stateInfo.State == RunspacePoolState.Broken)) { flag2 = true; closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false); } else if (base.stateInfo.State == RunspacePoolState.BeforeOpen) { stateInfo = base.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closed, null); flag = true; flag2 = true; this.closeAsyncResult = null; closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false); } else if ((base.stateInfo.State == RunspacePoolState.Opened) || (base.stateInfo.State == RunspacePoolState.Opening)) { stateInfo = base.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, null); this.closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false); closeAsyncResult = this.closeAsyncResult; flag = true; } else if (((base.stateInfo.State == RunspacePoolState.Disconnected) || (base.stateInfo.State == RunspacePoolState.Disconnecting)) || (base.stateInfo.State == RunspacePoolState.Connecting)) { this.closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false); closeAsyncResult = this.closeAsyncResult; } else if (base.stateInfo.State == RunspacePoolState.Closing) { return(this.closeAsyncResult); } } if (flag) { base.RaiseStateChangeEvent(stateInfo); } if (!flag2) { this.dataStructureHandler.CloseRunspacePoolAsync(); return(closeAsyncResult); } closeAsyncResult.SetAsCompleted(null); return(closeAsyncResult); }
public override IAsyncResult BeginConnect(AsyncCallback callback, object state) { RunspacePoolState state2; if (!this.AvailableForConnection) { throw PSTraceSource.NewInvalidOperationException("RunspacePoolStrings", "CannotConnect", new object[0]); } bool flag = false; lock (base.syncObject) { state2 = base.stateInfo.State; if (state2 == RunspacePoolState.Disconnected) { RunspacePoolStateInfo newStateInfo = new RunspacePoolStateInfo(RunspacePoolState.Connecting, null); this.SetRunspacePoolState(newStateInfo); flag = true; } } if (flag) { base.RaiseStateChangeEvent(base.stateInfo); } flag = false; if (state2 == RunspacePoolState.Disconnected) { RunspacePoolAsyncResult reconnectAsyncResult; if (this.canReconnect) { this.dataStructureHandler.ReconnectPoolAsync(); this.reconnectAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, state, false); reconnectAsyncResult = this.reconnectAsyncResult; } else { this.dataStructureHandler.ConnectPoolAsync(); this.openAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, state, false); reconnectAsyncResult = this.openAsyncResult; } if (flag) { base.RaiseStateChangeEvent(base.stateInfo); } return(reconnectAsyncResult); } InvalidRunspacePoolStateException exception = new InvalidRunspacePoolStateException(StringUtil.Format(RunspacePoolStrings.InvalidRunspacePoolState, RunspacePoolState.Disconnected, base.stateInfo.State), base.stateInfo.State, RunspacePoolState.Disconnected); throw exception; }
public override IAsyncResult BeginClose(AsyncCallback callback, object asyncState) { using (RemoteRunspacePoolInternal.tracer.TraceMethod()) { bool flag1 = false; bool flag2 = false; RunspacePoolStateInfo stateInfo = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, (Exception)null); RunspacePoolAsyncResult runspacePoolAsyncResult = (RunspacePoolAsyncResult)null; lock (this.syncObject) { if (this.stateInfo.State == RunspacePoolState.Closed || this.stateInfo.State == RunspacePoolState.Broken) { flag2 = true; runspacePoolAsyncResult = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false); } else if (this.stateInfo.State == RunspacePoolState.BeforeOpen) { stateInfo = this.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closed, (Exception)null); flag1 = true; flag2 = true; this.closeAsyncResult = (RunspacePoolAsyncResult)null; runspacePoolAsyncResult = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false); } else if (this.stateInfo.State == RunspacePoolState.Opened || this.stateInfo.State == RunspacePoolState.Opening) { stateInfo = this.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, (Exception)null); this.closeAsyncResult = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false); runspacePoolAsyncResult = this.closeAsyncResult; flag1 = true; } else if (this.stateInfo.State == RunspacePoolState.Closing) { return((IAsyncResult)this.closeAsyncResult); } } if (flag1) { this.RaiseStateChangeEvent(stateInfo); } if (!flag2) { this.dataStructureHandler.CloseRunspacePoolAsync(); } else { runspacePoolAsyncResult.SetAsCompleted((Exception)null); } return((IAsyncResult)runspacePoolAsyncResult); } }
protected RunspacePoolInternal(int minRunspaces, int maxRunspaces) { using (RunspacePoolInternal.tracer.TraceConstructor((object)this)) { if (maxRunspaces < 1) { throw RunspacePoolInternal.tracer.NewArgumentException(nameof(maxRunspaces), RunspacePoolInternal.resBaseName, "MaxPoolLessThan1"); } if (minRunspaces < 1) { throw RunspacePoolInternal.tracer.NewArgumentException(nameof(minRunspaces), RunspacePoolInternal.resBaseName, "MinPoolLessThan1"); } this.maxPoolSz = minRunspaces <= maxRunspaces ? maxRunspaces : throw RunspacePoolInternal.tracer.NewArgumentException(nameof(minRunspaces), RunspacePoolInternal.resBaseName, "MinPoolGreaterThanMaxPool"); this.minPoolSz = minRunspaces; this.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, (Exception)null); this.instanceId = Guid.NewGuid(); } }
internal void HandleStateInfoReceived(object sender, RemoteDataEventArgs <RunspacePoolStateInfo> eventArgs) { RunspacePoolStateInfo data = eventArgs.Data; bool flag = false; if (data.State == RunspacePoolState.Opened) { lock (base.syncObject) { if (base.stateInfo.State == RunspacePoolState.Opening) { this.SetRunspacePoolState(data); flag = true; } } if (flag) { base.RaiseStateChangeEvent(base.stateInfo); this.SetOpenAsCompleted(); } } else if ((data.State == RunspacePoolState.Closed) || (data.State == RunspacePoolState.Broken)) { bool flag3 = false; lock (base.syncObject) { if ((base.stateInfo.State == RunspacePoolState.Closed) || (base.stateInfo.State == RunspacePoolState.Broken)) { return; } if (((base.stateInfo.State == RunspacePoolState.Opening) || (base.stateInfo.State == RunspacePoolState.Opened)) || (base.stateInfo.State == RunspacePoolState.Closing)) { flag3 = true; this.SetRunspacePoolState(data); } } if (flag3 && (this.closeAsyncResult == null)) { this.dataStructureHandler.CloseRunspacePoolAsync(); } } }
internal void HandleInitInfoReceived(object sender, RemoteDataEventArgs <RunspacePoolInitInfo> eventArgs) { RunspacePoolStateInfo newStateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opened, null); bool flag = false; lock (base.syncObject) { base.minPoolSz = eventArgs.Data.MinRunspaces; base.maxPoolSz = eventArgs.Data.MaxRunspaces; if (base.stateInfo.State == RunspacePoolState.Connecting) { flag = true; this.SetRunspacePoolState(newStateInfo); } } if (flag) { base.RaiseStateChangeEvent(newStateInfo); this.SetOpenAsCompleted(); } }
internal void ProcessReceivedData(RemoteDataObject <PSObject> receivedData) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod()) { if (receivedData.RunspacePoolId != this.clientRunspacePoolId) { throw new PSRemotingDataStructureException(PSRemotingErrorId.RunspaceIdsDoNotMatch, new object[2] { (object)receivedData.RunspacePoolId, (object)this.clientRunspacePoolId }); } switch (receivedData.DataType) { case RemotingDataType.RunspacePoolOperationResponse: this.SetMaxMinRunspacesResponseRecieved((object)this, new RemoteDataEventArgs <PSObject>((object)receivedData.Data)); break; case RemotingDataType.RunspacePoolStateInfo: RunspacePoolStateInfo runspacePoolStateInfo = RemotingDecoder.GetRunspacePoolStateInfo(receivedData.Data); this.StateInfoReceived((object)this, new RemoteDataEventArgs <RunspacePoolStateInfo>((object)runspacePoolStateInfo)); this.NotifyAssociatedPowerShells(runspacePoolStateInfo); break; case RemotingDataType.PSEventArgs: this.PSEventArgsReceived((object)this, new RemoteDataEventArgs <PSEventArgs>((object)RemotingDecoder.GetPSEventArgs(receivedData.Data))); break; case RemotingDataType.ApplicationPrivateData: this.ApplicationPrivateDataReceived((object)this, new RemoteDataEventArgs <PSPrimitiveDictionary>((object)RemotingDecoder.GetApplicationPrivateData(receivedData.Data))); break; case RemotingDataType.RemoteHostCallUsingRunspaceHost: this.RemoteHostCallReceived((object)this, new RemoteDataEventArgs <RemoteHostCall>((object)RemoteHostCall.Decode(receivedData.Data))); break; } } }
private void NotifyAssociatedPowerShells(RunspacePoolStateInfo stateInfo) { List <ClientPowerShellDataStructureHandler> list; if (stateInfo.State == RunspacePoolState.Disconnected) { lock (this.associationSyncObject) { list = new List <ClientPowerShellDataStructureHandler>(this.associatedPowerShellDSHandlers.Values); } foreach (ClientPowerShellDataStructureHandler handler in list) { handler.ProcessDisconnect(stateInfo); } } else if ((stateInfo.State == RunspacePoolState.Broken) || (stateInfo.State == RunspacePoolState.Closed)) { lock (this.associationSyncObject) { list = new List <ClientPowerShellDataStructureHandler>(this.associatedPowerShellDSHandlers.Values); this.associatedPowerShellDSHandlers.Clear(); } if (stateInfo.State == RunspacePoolState.Broken) { foreach (ClientPowerShellDataStructureHandler handler2 in list) { handler2.SetStateToFailed(stateInfo.Reason); } } else if (stateInfo.State == RunspacePoolState.Closed) { foreach (ClientPowerShellDataStructureHandler handler3 in list) { handler3.SetStateToStopped(stateInfo.Reason); } } } }
private void NotifyAssociatedPowerShells(RunspacePoolStateInfo stateInfo) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod()) { Collection <ClientPowerShellDataStructureHandler> collection = new Collection <ClientPowerShellDataStructureHandler>(); if (stateInfo.State == RunspacePoolState.Broken || stateInfo.State == RunspacePoolState.Closed) { lock (this.associationSyncObject) { foreach (ClientPowerShellDataStructureHandler structureHandler in this.associatedPowerShellDSHandlers.Values) { collection.Add(structureHandler); } this.associatedPowerShellDSHandlers.Clear(); } } if (stateInfo.State == RunspacePoolState.Broken) { foreach (ClientPowerShellDataStructureHandler structureHandler in collection) { structureHandler.SetStateToFailed(stateInfo.Reason); } } else { if (stateInfo.State != RunspacePoolState.Closed) { return; } foreach (ClientPowerShellDataStructureHandler structureHandler in collection) { structureHandler.SetStateToStopped(stateInfo.Reason); } } } }
internal RunspacePoolStateChangedEventArgs(RunspacePoolState state) { using (RunspacePoolStateChangedEventArgs.tracer.TraceConstructor((object)this)) this.stateInfo = new RunspacePoolStateInfo(state, (Exception)null); }
/// <summary> /// Asynchronous disconnect. /// </summary> /// <param name="callback">AsyncCallback object.</param> /// <param name="state">state object.</param> /// <returns>IAsyncResult</returns> public override IAsyncResult BeginDisconnect(AsyncCallback callback, object state) { if (!CanDisconnect) { throw PSTraceSource.NewInvalidOperationException(RunspacePoolStrings.DisconnectNotSupportedOnServer); } RunspacePoolState currentState; bool raiseEvents = false; lock (syncObject) { currentState = stateInfo.State; if (currentState == RunspacePoolState.Opened) { RunspacePoolStateInfo newStateInfo = new RunspacePoolStateInfo(RunspacePoolState.Disconnecting, null); SetRunspacePoolState(newStateInfo); raiseEvents = true; } } // Raise events outside of lock. if (raiseEvents) { RaiseStateChangeEvent(this.stateInfo); } if (currentState == RunspacePoolState.Opened) { RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult( instanceId, callback, state, false); _disconnectAsyncResult = asyncResult; DataStructureHandler.DisconnectPoolAsync(); // Return local reference to async object since the class member can // be asynchronously nulled if the session closes suddenly. return asyncResult; } else { string message = StringUtil.Format(RunspacePoolStrings.InvalidRunspacePoolState, RunspacePoolState.Opened, stateInfo.State); InvalidRunspacePoolStateException invalidStateException = new InvalidRunspacePoolStateException(message, stateInfo.State, RunspacePoolState.Opened); throw invalidStateException; } }
/// <summary> /// Asynchronous connect. /// </summary> /// <param name="callback">ASyncCallback object.</param> /// <param name="state">state Object.</param> /// <returns>IAsyncResult</returns> public override IAsyncResult BeginConnect(AsyncCallback callback, object state) { if (!AvailableForConnection) { throw PSTraceSource.NewInvalidOperationException(RunspacePoolStrings.CannotConnect); } RunspacePoolState currentState; bool raiseEvents = false; lock (syncObject) { currentState = stateInfo.State; if (currentState == RunspacePoolState.Disconnected) { RunspacePoolStateInfo newStateInfo = new RunspacePoolStateInfo(RunspacePoolState.Connecting, null); SetRunspacePoolState(newStateInfo); raiseEvents = true; } } // Raise events outside of lock. if (raiseEvents) { RaiseStateChangeEvent(this.stateInfo); } raiseEvents = false; if (currentState == RunspacePoolState.Disconnected) { // Assign to local variable to ensure we always pass a non-null value. // The async class members can be nulled out if the session closes suddenly. RunspacePoolAsyncResult ret = new RunspacePoolAsyncResult( instanceId, callback, state, false); if (_canReconnect) { // This indicates a reconnect scenario where this object instance was previously // disconnected. _reconnectAsyncResult = ret; DataStructureHandler.ReconnectPoolAsync(); } else { // This indicates a reconstruction scenario where this object was created // in the disconnect state and is being connected for the first time. _openAsyncResult = ret; DataStructureHandler.ConnectPoolAsync(); } if (raiseEvents) { RaiseStateChangeEvent(this.stateInfo); } return ret; } else { string message = StringUtil.Format(RunspacePoolStrings.InvalidRunspacePoolState, RunspacePoolState.Disconnected, stateInfo.State); InvalidRunspacePoolStateException invalidStateException = new InvalidRunspacePoolStateException(message, stateInfo.State, RunspacePoolState.Disconnected); throw invalidStateException; } }
/// <summary> /// Set the new runspace pool state based on the state of the /// server RunspacePool and raise events if required /// </summary> /// <param name="newStateInfo">state information object /// describing the state change at the server RunspacePool</param> /// <param name="raiseEvents">raise state changed events if true</param> private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo, bool raiseEvents) { stateInfo = newStateInfo; // Update the availableForConnection variable based on state change. AvailableForConnection = (stateInfo.State == RunspacePoolState.Disconnected || stateInfo.State == RunspacePoolState.Opened); if (raiseEvents) { RaiseStateChangeEvent(newStateInfo); } }
private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo) { this.SetRunspacePoolState(newStateInfo, false); }
internal void HandleStateInfoReceived( object sender, RemoteDataEventArgs <RunspacePoolStateInfo> eventArgs) { using (RemoteRunspacePoolInternal.tracer.TraceMethod()) { RunspacePoolStateInfo data = eventArgs.Data; bool flag1 = false; if (data.State == RunspacePoolState.Opened) { lock (this.syncObject) { if (this.stateInfo.State == RunspacePoolState.Opening) { this.SetRunspacePoolState(data); flag1 = true; } } if (!flag1) { return; } this.RaiseStateChangeEvent(this.stateInfo); this.SetOpenAsCompleted(); } else { if (data.State != RunspacePoolState.Closed && data.State != RunspacePoolState.Broken) { return; } bool flag2 = false; lock (this.syncObject) { if (this.stateInfo.State == RunspacePoolState.Closed || this.stateInfo.State == RunspacePoolState.Broken) { return; } if (this.stateInfo.State != RunspacePoolState.Opening && this.stateInfo.State != RunspacePoolState.Opened) { if (this.stateInfo.State != RunspacePoolState.Closing) { goto label_16; } } flag2 = true; this.SetRunspacePoolState(data); } label_16: if (!flag2) { return; } if (this.closeAsyncResult == null) { this.dataStructureHandler.CloseRunspacePoolAsync(); } this.RaiseStateChangeEvent(data); this.SetCloseAsCompleted(); } } }
private void SetStateToBroken(Exception reason) { bool shouldRaiseEvents = false; lock (syncObject) { if ((stateInfo.State == RunspacePoolState.Opening) || (stateInfo.State == RunspacePoolState.Opened) || (stateInfo.State == RunspacePoolState.Disconnecting) || (stateInfo.State == RunspacePoolState.Disconnected) || (stateInfo.State == RunspacePoolState.Connecting)) { stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Broken, null); shouldRaiseEvents = true; } } if (shouldRaiseEvents) { RunspacePoolStateInfo stateInfo = new RunspacePoolStateInfo(this.stateInfo.State, reason); RaiseStateChangeEvent(stateInfo); } }
/// <summary> /// Closes the RunspacePool asynchronously. To get the exceptions /// that might have occurred, call EndOpen /// </summary> /// <param name="callback"> /// An AsyncCallback to call once the BeginClose completes /// </param> /// <param name="asyncState"> /// A user supplied state to call the <paramref name="callback"/> /// with /// </param> /// <returns> /// An AsyncResult object to monitor the state of the async /// operation /// </returns> public override IAsyncResult BeginClose(AsyncCallback callback, object asyncState) { bool raiseEvents = false; bool skipClosing = false; RunspacePoolStateInfo copyState = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null); RunspacePoolAsyncResult asyncResult = null; lock (syncObject) { if ((stateInfo.State == RunspacePoolState.Closed) || (stateInfo.State == RunspacePoolState.Broken)) { skipClosing = true; asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); } else if (stateInfo.State == RunspacePoolState.BeforeOpen) { copyState = stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closed, null); raiseEvents = true; skipClosing = true; _closeAsyncResult = null; asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); } else if (stateInfo.State == RunspacePoolState.Opened || stateInfo.State == RunspacePoolState.Opening) { copyState = stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, null); _closeAsyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); asyncResult = _closeAsyncResult; raiseEvents = true; } else if (stateInfo.State == RunspacePoolState.Disconnected || stateInfo.State == RunspacePoolState.Disconnecting || stateInfo.State == RunspacePoolState.Connecting) { // Continue with closing so the PSRP layer is aware that the client side session is // being closed. This will result in a broken session on the client. _closeAsyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); asyncResult = _closeAsyncResult; } else if (stateInfo.State == RunspacePoolState.Closing) { return _closeAsyncResult; } } // raise the events outside the lock if (raiseEvents) { RaiseStateChangeEvent(copyState); } if (!skipClosing) { //SetRunspacePoolState(new RunspacePoolStateInfo(RunspacePoolState.Closing, null), true); // send a message using the data structure handler to close the RunspacePool // on the remote server DataStructureHandler.CloseRunspacePoolAsync(); } else { // signal the wait handle asyncResult.SetAsCompleted(null); } return asyncResult; }
/// <summary> /// Constructor. /// </summary> /// <param name="state"> /// state to raise the event with. /// </param> internal RunspacePoolStateChangedEventArgs(RunspacePoolState state) { RunspacePoolStateInfo = new RunspacePoolStateInfo(state, null); }
/// <summary> /// Opens the runspacepool synchronously / asynchronously. /// Runspace pool must be opened before it can be used. /// </summary> /// <param name="isAsync"> /// true to open asynchronously /// </param> /// <param name="callback"> /// A AsyncCallback to call once the BeginOpen completes. /// </param> /// <param name="asyncState"> /// A user supplied state to call the <paramref name="callback"/> /// with. /// </param> /// <returns> /// asyncResult object to monitor status of the async /// open operation. This is returned only if <paramref name="isAsync"/> /// is true. /// </returns> /// <exception cref="InvalidRunspacePoolStateException"> /// Cannot open RunspacePool because RunspacePool is not in /// the BeforeOpen state. /// </exception> /// <exception cref="OutOfMemoryException"> /// There is not enough memory available to start this asynchronously. /// </exception> protected override IAsyncResult CoreOpen(bool isAsync, AsyncCallback callback, object asyncState) { PSEtwLog.SetActivityIdForCurrentThread(this.InstanceId); PSEtwLog.LogOperationalVerbose(PSEventId.RunspacePoolOpen, PSOpcode.Open, PSTask.CreateRunspace, PSKeyword.UseAlwaysOperational); TelemetryAPI.ReportRemoteSessionCreated(_connectionInfo); lock (syncObject) { AssertIfStateIsBeforeOpen(); stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opening, null); } // BUGBUG: the following comment needs to be validated // only one thread will reach here, so no need // to lock RaiseStateChangeEvent(stateInfo); RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult( instanceId, callback, asyncState, true); _openAsyncResult = asyncResult; // send a message using the data structure handler to open the RunspacePool // on the remote server DataStructureHandler.CreateRunspacePoolAndOpenAsync(); return asyncResult; }
internal void HandleInitInfoReceived(object sender, RemoteDataEventArgs<RunspacePoolInitInfo> eventArgs) { RunspacePoolStateInfo info = new RunspacePoolStateInfo(RunspacePoolState.Opened, null); bool raiseEvents = false; lock (syncObject) { minPoolSz = eventArgs.Data.MinRunspaces; maxPoolSz = eventArgs.Data.MaxRunspaces; if (stateInfo.State == RunspacePoolState.Connecting) { ResetDisconnectedOnExpiresOn(); raiseEvents = true; SetRunspacePoolState(info); } } if (raiseEvents) { // Private application data is sent after (post) connect. We need // to wait for application data before raising the state change // Connecting -> Opened event. ThreadPool.QueueUserWorkItem(WaitAndRaiseConnectEventsProc, info); } }
/// <summary> /// The session closed, set the state and reason accordingly /// </summary> /// <param name="sender">sender of this event, unused</param> /// <param name="eventArgs">arguments describing this event</param> private void HandleSessionClosed(object sender, RemoteDataEventArgs<Exception> eventArgs) { if (eventArgs.Data != null) { _closingReason = eventArgs.Data; } // Set state under lock. RunspacePoolState prevState; RunspacePoolStateInfo finishedStateInfo; lock (syncObject) { prevState = stateInfo.State; switch (prevState) { case RunspacePoolState.Opening: case RunspacePoolState.Opened: case RunspacePoolState.Disconnecting: case RunspacePoolState.Disconnected: case RunspacePoolState.Connecting: // Since RunspacePool is not in closing state, this close is // happening because of data structure handler error. Set the state to broken. SetRunspacePoolState(new RunspacePoolStateInfo(RunspacePoolState.Broken, _closingReason)); break; case RunspacePoolState.Closing: SetRunspacePoolState(new RunspacePoolStateInfo(RunspacePoolState.Closed, _closingReason)); break; } finishedStateInfo = new RunspacePoolStateInfo(stateInfo.State, stateInfo.Reason); } // Raise notification event outside of lock. try { RaiseStateChangeEvent(finishedStateInfo); } catch (Exception e) { // Don't throw exception on notification thread. CommandProcessor.CheckForSevereException(e); } // Check if we have either an existing disconnect or connect async object // and if so make sure they are set to completed since this is a // final state for the runspace pool. SetDisconnectAsCompleted(); SetReconnectAsCompleted(); // Ensure an existing Close async object is completed. SetCloseAsCompleted(); }
/// <summary> /// /// </summary> /// <param name="stateInfo"></param> internal RunspacePoolStateChangedEventArgs(RunspacePoolStateInfo stateInfo) { RunspacePoolStateInfo = stateInfo; }
/// <summary> /// Closes the runspacepool synchronously / asynchronously. /// </summary> /// <param name="isAsync"> /// true to close asynchronously /// </param> /// <param name="callback"> /// A AsyncCallback to call once the BeginClose completes. /// </param> /// <param name="asyncState"> /// A user supplied state to call the <paramref name="callback"/> /// with. /// </param> /// <returns> /// asyncResult object to monitor status of the async /// open operation. This is returned only if <paramref name="isAsync"/> /// is true. /// </returns> private IAsyncResult CoreClose(bool isAsync, AsyncCallback callback, object asyncState) { lock (syncObject) { if ((stateInfo.State == RunspacePoolState.Closed) || (stateInfo.State == RunspacePoolState.Broken) || (stateInfo.State == RunspacePoolState.Closing) || (stateInfo.State == RunspacePoolState.Disconnecting) || (stateInfo.State == RunspacePoolState.Disconnected)) { if (isAsync) { RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); asyncResult.SetAsCompleted(null); return asyncResult; } else { return null; } } stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, null); } // only one thread will reach here. RaiseStateChangeEvent(stateInfo); if (isAsync) { RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false); //Open pool in another thread ThreadPool.QueueUserWorkItem(new WaitCallback(CloseThreadProc), asyncResult); return asyncResult; } // open the runspace synchronously CloseHelper(); return null; }
/// <summary> /// Constructor /// </summary> /// <param name="state"> /// state to raise the event with. /// </param> internal RunspacePoolStateChangedEventArgs(RunspacePoolState state) { RunspacePoolStateInfo = new RunspacePoolStateInfo(state, null); }
private void CloseHelper() { try { InternalClearAllResources(); } finally { stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closed, null); RaiseStateChangeEvent(stateInfo); } }
internal RunspacePoolStateChangedEventArgs(RunspacePoolStateInfo stateInfo) { throw new NotImplementedException(); }
/// <summary> /// Raise state changed event based on the StateInfo /// object /// </summary> /// <param name="stateInfo">state information object</param> protected void RaiseStateChangeEvent(RunspacePoolStateInfo stateInfo) { StateChanged.SafeInvoke(this, new RunspacePoolStateChangedEventArgs(stateInfo)); }
private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo) { using (RemoteRunspacePoolInternal.tracer.TraceMethod()) this.SetRunspacePoolState(newStateInfo, false); }
/// <summary> /// Constructor for doing common initialization between /// this class and its derivatives /// </summary> /// <param name="maxRunspaces"> /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// </param> /// <param name="minRunspaces"> /// The minimum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// </param> protected RunspacePoolInternal(int minRunspaces, int maxRunspaces) { if (maxRunspaces < 1) { throw PSTraceSource.NewArgumentException("maxRunspaces", RunspacePoolStrings.MaxPoolLessThan1); } if (minRunspaces < 1) { throw PSTraceSource.NewArgumentException("minRunspaces", RunspacePoolStrings.MinPoolLessThan1); } if (minRunspaces > maxRunspaces) { throw PSTraceSource.NewArgumentException("minRunspaces", RunspacePoolStrings.MinPoolGreaterThanMaxPool); } maxPoolSz = maxRunspaces; minPoolSz = minRunspaces; stateInfo = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null); instanceId = Guid.NewGuid(); PSEtwLog.SetActivityIdForCurrentThread(instanceId); _cleanupInterval = s_defaultCleanupPeriod; _cleanupTimer = new Timer(new TimerCallback(CleanupCallback), null, Timeout.Infinite, Timeout.Infinite); }
/// <summary> /// Opens the runspacepool synchronously / asynchronously. /// Runspace pool must be opened before it can be used. /// </summary> /// <param name="isAsync"> /// true to open asynchronously /// </param> /// <param name="callback"> /// A AsyncCallback to call once the BeginOpen completes. /// </param> /// <param name="asyncState"> /// A user supplied state to call the <paramref name="callback"/> /// with. /// </param> /// <returns> /// asyncResult object to monitor status of the async /// open operation. This is returned only if <paramref name="isAsync"/> /// is true. /// </returns> /// <exception cref="InvalidRunspacePoolStateException"> /// Cannot open RunspacePool because RunspacePool is not in /// the BeforeOpen state. /// </exception> /// <exception cref="OutOfMemoryException"> /// There is not enough memory available to start this asynchronously. /// </exception> protected virtual IAsyncResult CoreOpen(bool isAsync, AsyncCallback callback, object asyncState) { lock (syncObject) { AssertIfStateIsBeforeOpen(); stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opening, null); } // only one thread will reach here, so no // need to lock. RaiseStateChangeEvent(stateInfo); if (isAsync) { AsyncResult asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, true); //Open pool in another thread ThreadPool.QueueUserWorkItem(new WaitCallback(OpenThreadProc), asyncResult); return asyncResult; } // open the runspace synchronously OpenHelper(); return null; }
/// <summary> /// Set the new runspace pool state based on the state of the /// server RunspacePool /// </summary> /// <param name="newStateInfo">state information object /// describing the state change at the server RunspacePool</param> private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo) { SetRunspacePoolState(newStateInfo, false); }
/// <summary> /// </summary> /// <param name="stateInfo"></param> internal RunspacePoolStateChangedEventArgs(RunspacePoolStateInfo stateInfo) { RunspacePoolStateInfo = stateInfo; }
/// <summary> /// Creates a Runspace + opens it synchronously and /// pushes it into the stack. /// </summary> /// <remarks> /// Caller to make sure this is thread safe. /// </remarks> protected void OpenHelper() { try { PSEtwLog.SetActivityIdForCurrentThread(this.InstanceId); // Create a Runspace and store it in the pool // for future use. This will validate whether // a runspace can be created + opened successfully Runspace rs = CreateRunspace(); pool.Push(rs); } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); SetStateToBroken(exception); // rethrow the exception throw; } bool shouldRaiseEvents = false; // RunspacePool might be closed while we are still opening // we should not change state from closed to opened.. lock (syncObject) { if (stateInfo.State == RunspacePoolState.Opening) { // Change state to opened and notify the user. stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opened, null); shouldRaiseEvents = true; } } if (shouldRaiseEvents) { RaiseStateChangeEvent(stateInfo); } }