예제 #1
0
        /// <summary>
        /// Send a message with the RunspacePoolStateInfo to the client
        /// </summary>
        /// <param name="stateInfo">state info to send</param>
        internal void SendStateInfoToClient(RunspacePoolStateInfo stateInfo)
        {
            RemoteDataObject data = RemotingEncoder.GenerateRunspacePoolStateInfo(
                    _clientRunspacePoolId, stateInfo);

            SendDataAsync(data);
        }
예제 #2
0
        protected void OpenHelper()
        {
            try
            {
                PSEtwLog.SetActivityIdForCurrentThread(this.InstanceId);
                Runspace item = this.CreateRunspace();
                this.pool.Push(item);
            }
            catch (Exception exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                this.SetStateToBroken(exception);
                throw;
            }
            bool flag = false;

            lock (this.syncObject)
            {
                if (this.stateInfo.State == RunspacePoolState.Opening)
                {
                    this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Opened, null);
                    flag           = true;
                }
            }
            if (flag)
            {
                this.RaiseStateChangeEvent(this.stateInfo);
            }
        }
예제 #3
0
 protected RunspacePoolInternal(int minRunspaces, int maxRunspaces)
 {
     this.runspaceList   = new List <Runspace>();
     this.syncObject     = new object();
     this.apartmentState = System.Threading.ApartmentState.Unknown;
     if (maxRunspaces < 1)
     {
         throw PSTraceSource.NewArgumentException("maxRunspaces", resBaseName, "MaxPoolLessThan1", new object[0]);
     }
     if (minRunspaces < 1)
     {
         throw PSTraceSource.NewArgumentException("minRunspaces", resBaseName, "MinPoolLessThan1", new object[0]);
     }
     if (minRunspaces > maxRunspaces)
     {
         throw PSTraceSource.NewArgumentException("minRunspaces", resBaseName, "MinPoolGreaterThanMaxPool", new object[0]);
     }
     this.maxPoolSz  = maxRunspaces;
     this.minPoolSz  = minRunspaces;
     this.stateInfo  = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null);
     this.instanceId = Guid.NewGuid();
     PSEtwLog.SetActivityIdForCurrentThread(this.instanceId);
     this.cleanupInterval = DefaultCleanupPeriod;
     this.cleanupTimer    = new Timer(new TimerCallback(this.CleanupCallback), null, -1, -1);
 }
예제 #4
0
 private IAsyncResult CoreClose(bool isAsync, AsyncCallback callback, object asyncState)
 {
     lock (this.syncObject)
     {
         if (((this.stateInfo.State == RunspacePoolState.Closed) || (this.stateInfo.State == RunspacePoolState.Broken)) || (((this.stateInfo.State == RunspacePoolState.Closing) || (this.stateInfo.State == RunspacePoolState.Disconnecting)) || (this.stateInfo.State == RunspacePoolState.Disconnected)))
         {
             if (isAsync)
             {
                 RunspacePoolAsyncResult result = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
                 result.SetAsCompleted(null);
                 return(result);
             }
             return(null);
         }
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Closing, null);
     }
     this.RaiseStateChangeEvent(this.stateInfo);
     if (isAsync)
     {
         RunspacePoolAsyncResult state = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.CloseThreadProc), state);
         return(state);
     }
     this.CloseHelper();
     return(null);
 }
예제 #5
0
        /// <summary>
        /// Send a message with the RunspacePoolStateInfo to the client
        /// </summary>
        /// <param name="stateInfo">state info to send</param>
        internal void SendStateInfoToClient(RunspacePoolStateInfo stateInfo)
        {
            RemoteDataObject data = RemotingEncoder.GenerateRunspacePoolStateInfo(
                _clientRunspacePoolId, stateInfo);

            SendDataAsync(data);
        }
예제 #6
0
 protected RunspacePoolInternal(int minRunspaces, int maxRunspaces)
 {
     this.runspaceList = new List<Runspace>();
     this.syncObject = new object();
     this.apartmentState = System.Threading.ApartmentState.Unknown;
     if (maxRunspaces < 1)
     {
         throw PSTraceSource.NewArgumentException("maxRunspaces", resBaseName, "MaxPoolLessThan1", new object[0]);
     }
     if (minRunspaces < 1)
     {
         throw PSTraceSource.NewArgumentException("minRunspaces", resBaseName, "MinPoolLessThan1", new object[0]);
     }
     if (minRunspaces > maxRunspaces)
     {
         throw PSTraceSource.NewArgumentException("minRunspaces", resBaseName, "MinPoolGreaterThanMaxPool", new object[0]);
     }
     this.maxPoolSz = maxRunspaces;
     this.minPoolSz = minRunspaces;
     this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null);
     this.instanceId = Guid.NewGuid();
     PSEtwLog.SetActivityIdForCurrentThread(this.instanceId);
     this.cleanupInterval = DefaultCleanupPeriod;
     this.cleanupTimer = new Timer(new TimerCallback(this.CleanupCallback), null, -1, -1);
 }
예제 #7
0
 private void CloseHelper()
 {
     try
     {
         this.InternalClearAllResources();
     }
     finally
     {
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Closed, null);
         this.RaiseStateChangeEvent(this.stateInfo);
     }
 }
예제 #8
0
 protected virtual IAsyncResult CoreOpen(bool isAsync, AsyncCallback callback, object asyncState)
 {
     lock (this.syncObject)
     {
         this.AssertIfStateIsBeforeOpen();
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Opening, null);
     }
     this.RaiseStateChangeEvent(this.stateInfo);
     if (isAsync)
     {
         AsyncResult state = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, true);
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.OpenThreadProc), state);
         return(state);
     }
     this.OpenHelper();
     return(null);
 }
예제 #9
0
        private void SetStateToBroken(Exception reason)
        {
            bool flag = false;

            lock (this.syncObject)
            {
                if (((this.stateInfo.State == RunspacePoolState.Opening) || (this.stateInfo.State == RunspacePoolState.Opened)) || (((this.stateInfo.State == RunspacePoolState.Disconnecting) || (this.stateInfo.State == RunspacePoolState.Disconnected)) || (this.stateInfo.State == RunspacePoolState.Connecting)))
                {
                    this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Broken, null);
                    flag           = true;
                }
            }
            if (flag)
            {
                System.Management.Automation.RunspacePoolStateInfo stateInfo = new System.Management.Automation.RunspacePoolStateInfo(this.stateInfo.State, reason);
                this.RaiseStateChangeEvent(stateInfo);
            }
        }
예제 #10
0
 internal static RemoteDataObject GenerateRunspacePoolStateInfo(
     Guid clientRunspacePoolId,
     RunspacePoolStateInfo stateInfo)
 {
     using (RemotingEncoder._trace.TraceMethod())
     {
         PSObject       emptyPsObject  = RemotingEncoder.CreateEmptyPSObject();
         PSNoteProperty psNoteProperty = new PSNoteProperty("RunspaceState", (object)(int)stateInfo.State);
         emptyPsObject.Properties.Add((PSPropertyInfo)psNoteProperty);
         if (stateInfo.Reason != null)
         {
             string         errorId           = "RemoteRunspaceStateInfoReason";
             PSNoteProperty exceptionProperty = RemotingEncoder.GetExceptionProperty(stateInfo.Reason, errorId, ErrorCategory.NotSpecified);
             emptyPsObject.Properties.Add((PSPropertyInfo)exceptionProperty);
         }
         return(RemoteDataObject.CreateFrom(RemotingDestination.Client, RemotingDataType.RunspacePoolStateInfo, clientRunspacePoolId, Guid.Empty, (object)emptyPsObject));
     }
 }
예제 #11
0
 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);
     }
 }
예제 #12
0
        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();
        }
예제 #13
0
 private void SetRunspacePoolState(RunspacePoolStateInfo newStateInfo)
 {
     this.SetRunspacePoolState(newStateInfo, false);
 }
예제 #14
0
 public override IAsyncResult BeginDisconnect(AsyncCallback callback, object state)
 {
     RunspacePoolState state2;
     if (!this.CanDisconnect)
     {
         throw PSTraceSource.NewInvalidOperationException("RunspacePoolStrings", "DisconnectNotSupportedOnServer", new object[0]);
     }
     bool flag = false;
     lock (base.syncObject)
     {
         state2 = base.stateInfo.State;
         if (state2 == RunspacePoolState.Opened)
         {
             RunspacePoolStateInfo newStateInfo = new RunspacePoolStateInfo(RunspacePoolState.Disconnecting, null);
             this.SetRunspacePoolState(newStateInfo);
             flag = true;
         }
     }
     if (flag)
     {
         base.RaiseStateChangeEvent(base.stateInfo);
     }
     if (state2 == RunspacePoolState.Opened)
     {
         this.disconnectAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, state, false);
         this.dataStructureHandler.DisconnectPoolAsync();
         return this.disconnectAsyncResult;
     }
     InvalidRunspacePoolStateException exception = new InvalidRunspacePoolStateException(StringUtil.Format(RunspacePoolStrings.InvalidRunspacePoolState, RunspacePoolState.Opened, base.stateInfo.State), base.stateInfo.State, RunspacePoolState.Opened);
     throw exception;
 }
예제 #15
0
 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();
     }
 }
예제 #16
0
 protected virtual IAsyncResult CoreOpen(bool isAsync, AsyncCallback callback, object asyncState)
 {
     lock (this.syncObject)
     {
         this.AssertIfStateIsBeforeOpen();
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Opening, null);
     }
     this.RaiseStateChangeEvent(this.stateInfo);
     if (isAsync)
     {
         AsyncResult state = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, true);
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.OpenThreadProc), state);
         return state;
     }
     this.OpenHelper();
     return null;
 }
예제 #17
0
 protected void RaiseStateChangeEvent(System.Management.Automation.RunspacePoolStateInfo stateInfo)
 {
     this.StateChanged.SafeInvoke <RunspacePoolStateChangedEventArgs>(this, new RunspacePoolStateChangedEventArgs(stateInfo));
 }
예제 #18
0
 protected void OpenHelper()
 {
     try
     {
         PSEtwLog.SetActivityIdForCurrentThread(this.InstanceId);
         Runspace item = this.CreateRunspace();
         this.pool.Push(item);
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         this.SetStateToBroken(exception);
         throw;
     }
     bool flag = false;
     lock (this.syncObject)
     {
         if (this.stateInfo.State == RunspacePoolState.Opening)
         {
             this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Opened, null);
             flag = true;
         }
     }
     if (flag)
     {
         this.RaiseStateChangeEvent(this.stateInfo);
     }
 }
예제 #19
0
 private void SetStateToBroken(Exception reason)
 {
     bool flag = false;
     lock (this.syncObject)
     {
         if (((this.stateInfo.State == RunspacePoolState.Opening) || (this.stateInfo.State == RunspacePoolState.Opened)) || (((this.stateInfo.State == RunspacePoolState.Disconnecting) || (this.stateInfo.State == RunspacePoolState.Disconnected)) || (this.stateInfo.State == RunspacePoolState.Connecting)))
         {
             this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Broken, null);
             flag = true;
         }
     }
     if (flag)
     {
         System.Management.Automation.RunspacePoolStateInfo stateInfo = new System.Management.Automation.RunspacePoolStateInfo(this.stateInfo.State, reason);
         this.RaiseStateChangeEvent(stateInfo);
     }
 }
 internal void ProcessDisconnect(RunspacePoolStateInfo rsStateInfo)
 {
     PSInvocationStateInfo data = new PSInvocationStateInfo(PSInvocationState.Disconnected, (rsStateInfo != null) ? rsStateInfo.Reason : null);
     this.InvocationStateInfoReceived.SafeInvoke<RemoteDataEventArgs<PSInvocationStateInfo>>(this, new RemoteDataEventArgs<PSInvocationStateInfo>(data));
     Interlocked.CompareExchange(ref this.connectionState, 3, 1);
 }
예제 #21
0
        internal static RemoteDataObject GenerateRunspacePoolStateInfo(Guid clientRunspacePoolId, RunspacePoolStateInfo stateInfo)
        {
            PSObject       data   = CreateEmptyPSObject();
            PSNoteProperty member = new PSNoteProperty("RunspaceState", (int)stateInfo.State);

            data.Properties.Add(member);
            if (stateInfo.Reason != null)
            {
                string         errorId   = "RemoteRunspaceStateInfoReason";
                PSNoteProperty property2 = GetExceptionProperty(stateInfo.Reason, errorId, ErrorCategory.NotSpecified);
                data.Properties.Add(property2);
            }
            return(RemoteDataObject.CreateFrom(RemotingDestination.InvalidDestination | RemotingDestination.Client, RemotingDataType.RunspacePoolStateInfo, clientRunspacePoolId, Guid.Empty, data));
        }
예제 #22
0
 internal void SetStateInfo(System.Management.Automation.RunspacePoolStateInfo stateInfo)
 {
     this.stateInfo = stateInfo;
 }
예제 #23
0
 private void CloseHelper()
 {
     try
     {
         this.InternalClearAllResources();
     }
     finally
     {
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Closed, null);
         this.RaiseStateChangeEvent(this.stateInfo);
     }
 }
 internal RunspacePoolStateChangedEventArgs(RunspacePoolState state)
 {
     this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(state, null);
 }
예제 #25
0
 private IAsyncResult CoreClose(bool isAsync, AsyncCallback callback, object asyncState)
 {
     lock (this.syncObject)
     {
         if (((this.stateInfo.State == RunspacePoolState.Closed) || (this.stateInfo.State == RunspacePoolState.Broken)) || (((this.stateInfo.State == RunspacePoolState.Closing) || (this.stateInfo.State == RunspacePoolState.Disconnecting)) || (this.stateInfo.State == RunspacePoolState.Disconnected)))
         {
             if (isAsync)
             {
                 RunspacePoolAsyncResult result = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
                 result.SetAsCompleted(null);
                 return result;
             }
             return null;
         }
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Closing, null);
     }
     this.RaiseStateChangeEvent(this.stateInfo);
     if (isAsync)
     {
         RunspacePoolAsyncResult state = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.CloseThreadProc), state);
         return state;
     }
     this.CloseHelper();
     return null;
 }
 internal RunspacePoolStateChangedEventArgs(System.Management.Automation.RunspacePoolStateInfo stateInfo)
 {
     this.stateInfo = stateInfo;
 }
 internal RunspacePoolStateChangedEventArgs(RunspacePoolState state)
 {
     this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(state, null);
 }
예제 #28
0
 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;
 }
예제 #29
0
		internal void SetStateInfo(System.Management.Automation.RunspacePoolStateInfo stateInfo)
		{
			this.stateInfo = stateInfo;
		}
 internal RunspacePoolStateChangedEventArgs(System.Management.Automation.RunspacePoolStateInfo stateInfo)
 {
     this.stateInfo = stateInfo;
 }
예제 #31
0
 internal void SendStateInfoToClient(RunspacePoolStateInfo stateInfo)
 {
     using (ServerRunspacePoolDataStructureHandler.tracer.TraceMethod())
         this.SendDataAsync(RemotingEncoder.GenerateRunspacePoolStateInfo(this.clientRunspacePoolId, stateInfo));
 }
예제 #32
0
 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;
 }