Esempio n. 1
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;
        }
Esempio n. 2
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);
 }
Esempio n. 3
0
 private void SetReconnectAsCompleted()
 {
     if ((this.reconnectAsyncResult != null) && !this.reconnectAsyncResult.IsCompleted)
     {
         this.reconnectAsyncResult.SetAsCompleted(base.stateInfo.Reason);
         this.reconnectAsyncResult = null;
     }
 }
 private void SetOpenAsCompleted()
 {
     using (RemoteRunspacePoolInternal.tracer.TraceMethod())
     {
         if (this.openAsyncResult == null || this.openAsyncResult.IsCompleted)
         {
             return;
         }
         this.openAsyncResult.SetAsCompleted(this.stateInfo.Reason);
         this.openAsyncResult = (RunspacePoolAsyncResult)null;
     }
 }
Esempio n. 5
0
 private void SetCloseAsCompleted()
 {
     this.DispatchTable.AbortAllCalls();
     if (this.closeAsyncResult != null)
     {
         this.closeAsyncResult.SetAsCompleted(base.stateInfo.Reason);
         this.closeAsyncResult = null;
     }
     else
     {
         this.SetOpenAsCompleted();
     }
 }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public void EndOpen(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw PSTraceSource.NewArgumentNullException("asyncResult");
            }
            RunspacePoolAsyncResult result = asyncResult as RunspacePoolAsyncResult;

            if (((result == null) || (result.OwnerId != this.instanceId)) || !result.IsAssociatedWithAsyncOpen)
            {
                throw PSTraceSource.NewArgumentException("asyncResult", resBaseName, "AsyncResultNotOwned", new object[] { "IAsyncResult", "BeginOpen" });
            }
            result.EndInvoke();
        }
 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);
     }
 }
Esempio n. 9
0
        public override void EndDisconnect(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw PSTraceSource.NewArgumentNullException("asyncResult");
            }
            RunspacePoolAsyncResult result = asyncResult as RunspacePoolAsyncResult;

            if (((result == null) || (result.OwnerId != base.instanceId)) || result.IsAssociatedWithAsyncOpen)
            {
                throw PSTraceSource.NewArgumentException("asyncResult", RunspacePoolInternal.resBaseName, "AsyncResultNotOwned", new object[] { "IAsyncResult", "BeginOpen" });
            }
            result.EndInvoke();
        }
Esempio n. 10
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;
        }
 private void SetCloseAsCompleted()
 {
     using (RemoteRunspacePoolInternal.tracer.TraceMethod())
     {
         this.DispatchTable.AbortAllCalls();
         if (this.closeAsyncResult != null)
         {
             this.closeAsyncResult.SetAsCompleted(this.stateInfo.Reason);
             this.closeAsyncResult = (RunspacePoolAsyncResult)null;
         }
         else
         {
             this.SetOpenAsCompleted();
         }
     }
 }
Esempio n. 12
0
        protected override IAsyncResult CoreOpen(bool isAsync, AsyncCallback callback, object asyncState)
        {
            PSEtwLog.SetActivityIdForCurrentThread(base.InstanceId);
            PSEtwLog.LogOperationalVerbose(PSEventId.RunspacePoolOpen, PSOpcode.Open, PSTask.CreateRunspace, PSKeyword.UseAlwaysOperational, new object[0]);
            lock (base.syncObject)
            {
                base.AssertIfStateIsBeforeOpen();
                base.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opening, null);
            }
            base.RaiseStateChangeEvent(base.stateInfo);
            RunspacePoolAsyncResult result = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, true);

            this.openAsyncResult = result;
            this.dataStructureHandler.CreateRunspacePoolAndOpenAsync();
            return(result);
        }
Esempio n. 13
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);
 }
 protected override IAsyncResult CoreOpen(
     bool isAsync,
     AsyncCallback callback,
     object asyncState)
 {
     using (RemoteRunspacePoolInternal.tracer.TraceMethod())
     {
         this.etwTracer.OperationalChannel.WriteVerbose(PSEventId.RunspacePoolOpen, PSOpcode.Open, PSTask.CreateRunspace);
         lock (this.syncObject)
         {
             this.AssertIfStateIsBeforeOpen();
             this.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Opening, (Exception)null);
         }
         this.RaiseStateChangeEvent(this.stateInfo);
         RunspacePoolAsyncResult runspacePoolAsyncResult = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, true);
         this.openAsyncResult = runspacePoolAsyncResult;
         this.dataStructureHandler.CreateRunspacePoolAndOpenAsync();
         return((IAsyncResult)runspacePoolAsyncResult);
     }
 }
Esempio n. 15
0
        /// <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;
        }
Esempio n. 16
0
        /// <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;
            }
        }
Esempio n. 17
0
        /// <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;
        }
Esempio n. 18
0
        /// <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;
        }
Esempio n. 19
0
        /// <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;
        }
Esempio n. 20
0
        /// <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;
            }
        }