Пример #1
0
        /// <summary>
        /// Handles changes in data structure handler state
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg">
        /// Event argument which contains the new state
        /// </param>
        private void HandleConnectionStateChanged(object sender, RemoteSessionStateEventArgs arg)
        {
            using (s_trace.TraceEventHandlers())
            {
                if (arg == null)
                {
                    throw PSTraceSource.NewArgumentNullException("arg");
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.EstablishedAndKeyReceived) //TODO - Client session would never get into this state... to be removed
                {
                    // send the public key
                    StartKeyExchange();
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
                {
                    // when the connection is being closed we need to
                    // complete the key exchange process to release
                    // the lock under which the key exchange is happening
                    // if we fail to release the lock, then when
                    // transport manager is closing it will try to
                    // acquire the lock again leading to a deadlock
                    CompleteKeyExchange();
                }

                StateChanged.SafeInvoke(this, arg);
            }
        }
Пример #2
0
 private void HandleStateChanged(object sender, RemoteSessionStateEventArgs arg)
 {
     using (ClientRemoteSessionDSHandlerImpl._trace.TraceMethod())
     {
         if (arg == null)
         {
             throw ClientRemoteSessionDSHandlerImpl._trace.NewArgumentNullException(nameof(arg));
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending)
         {
             this.HandleNegotiationSendingStateChange();
         }
         if (this.ConnectionStateChanged != null)
         {
             this.ConnectionStateChanged((object)this, arg);
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending)
         {
             this.SendNegotiationAsync();
         }
         if (arg.SessionStateInfo.State != RemoteSessionState.ClosingConnection)
         {
             return;
         }
         this.CloseConnectionAsync();
     }
 }
Пример #3
0
        private void RaiseStateMachineEvents()
        {
            RemoteSessionStateEventArgs eventArgs = null;

            while (this._clientRemoteSessionStateChangeQueue.Count > 0)
            {
                eventArgs = this._clientRemoteSessionStateChangeQueue.Dequeue();
                this.StateChanged.SafeInvoke <RemoteSessionStateEventArgs>(this, eventArgs);
            }
        }
Пример #4
0
        /// <summary>
        /// Raises the StateChanged events which are queued
        /// All StateChanged events will be raised once the
        /// processing of the State Machine events are
        /// complete.
        /// </summary>
        private void RaiseStateMachineEvents()
        {
            RemoteSessionStateEventArgs queuedEventArg = null;

            while (_clientRemoteSessionStateChangeQueue.Count > 0)
            {
                queuedEventArg = _clientRemoteSessionStateChangeQueue.Dequeue();

                StateChanged.SafeInvoke(this, queuedEventArg);
            }
        }
Пример #5
0
        private void HandleStateChanged(object sender, RemoteSessionStateEventArgs arg)
        {
            if (arg == null)
            {
                throw PSTraceSource.NewArgumentNullException("arg");
            }

            // Enqueue session related negotiation packets first
            if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
            {
                HandleNegotiationSendingStateChange();
            }

            // this will enable top-layers to enqueue any packets during NegotiationSending and
            // during other states.
            ConnectionStateChanged.SafeInvoke(this, arg);

            if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
            {
                SendNegotiationAsync(arg.SessionStateInfo.State);
            }

            //once session is established.. start receiving data (if not already done and only apples to wsmanclientsessionTM)
            if (arg.SessionStateInfo.State == RemoteSessionState.Established)
            {
                WSManClientSessionTransportManager tm = _transportManager as WSManClientSessionTransportManager;
                if (tm != null)
                {
                    tm.AdjustForProtocolVariations(_session.ServerProtocolVersion);
                    tm.StartReceivingData();
                }
            }

            // Close the transport manager only after powershell's close their transports
            // Powershell's close their transport using the ConnectionStateChanged event notification.
            if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
            {
                CloseConnectionAsync();
            }

            //process disconnect
            if (arg.SessionStateInfo.State == RemoteSessionState.Disconnecting)
            {
                DisconnectAsync();
            }

            //process reconnect
            if (arg.SessionStateInfo.State == RemoteSessionState.Reconnecting)
            {
                ReconnectAsync();
            }
        }
Пример #6
0
        private void SetState(RemoteSessionState newState, Exception reason)
        {
            RemoteSessionState state = this._state;

            if (newState != state)
            {
                this._state = newState;
                _trace.WriteLine("state machine state transition: from state {0} to state {1}", new object[] { state, this._state });
                RemoteSessionStateInfo      remoteSessionStateInfo = new RemoteSessionStateInfo(this._state, reason);
                RemoteSessionStateEventArgs item = new RemoteSessionStateEventArgs(remoteSessionStateInfo);
                this._clientRemoteSessionStateChangeQueue.Enqueue(item);
            }
        }
Пример #7
0
 private void RaiseStateMachineEvents()
 {
     using (ClientRemoteSessionDSHandlerStateMachine._trace.TraceMethod())
     {
         while (this._clientRemoteSessionStateChangeQueue.Count > 0)
         {
             RemoteSessionStateEventArgs e = this._clientRemoteSessionStateChangeQueue.Dequeue();
             if (this.StateChanged != null)
             {
                 this.StateChanged((object)this, e);
             }
         }
     }
 }
Пример #8
0
        /// <summary>
        /// Sets the state of the state machine. Since only
        /// one thread can be manipulating the state at a time
        /// the state is not synchronized.
        /// </summary>
        /// <param name="newState">New state of the state machine.</param>
        /// <param name="reason">reason why the state machine is set
        /// to the new state</param>
        private void SetState(RemoteSessionState newState, Exception reason)
        {
            RemoteSessionState oldState = _state;

            if (newState != oldState)
            {
                _state = newState;
                s_trace.WriteLine("state machine state transition: from state {0} to state {1}", oldState, _state);

                RemoteSessionStateInfo      stateInfo            = new RemoteSessionStateInfo(_state, reason);
                RemoteSessionStateEventArgs sessionStateEventArg = new RemoteSessionStateEventArgs(stateInfo);

                _clientRemoteSessionStateChangeQueue.Enqueue(sessionStateEventArg);
            }
        }
Пример #9
0
 private void HandleConnectionStateChanged(object sender, RemoteSessionStateEventArgs arg)
 {
     using (_trace.TraceEventHandlers())
     {
         if (arg == null)
         {
             throw PSTraceSource.NewArgumentNullException("arg");
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.EstablishedAndKeyReceived)
         {
             this.StartKeyExchange();
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
         {
             this.CompleteKeyExchange();
         }
         this.StateChanged.SafeInvoke <RemoteSessionStateEventArgs>(this, arg);
     }
 }
Пример #10
0
 private void HandleCloseCompleted(object sender, EventArgs args)
 {
     this.dataStructureHandler.RaiseRemoveAssociationEvent();
     if (this.stateInfoQueue.Count != 0)
     {
         while (this.stateInfoQueue.Count > 0)
         {
             PSInvocationStateInfo stateInfo = this.stateInfoQueue.Dequeue();
             this.SetStateInfo(stateInfo);
         }
     }
     else if (!this.IsFinished(this.shell.InvocationStateInfo.State))
     {
         RemoteSessionStateEventArgs args2 = args as RemoteSessionStateEventArgs;
         Exception         reason          = (args2 != null) ? args2.SessionStateInfo.Reason : null;
         PSInvocationState state           = (this.shell.InvocationStateInfo.State == PSInvocationState.Disconnected) ? PSInvocationState.Failed : PSInvocationState.Stopped;
         this.SetStateInfo(new PSInvocationStateInfo(state, reason));
     }
 }
Пример #11
0
        /// <summary>
        /// This is need for the state change events that resulted in closing the underlying
        /// datastructure handler. We cannot send the state back to the upper layers until
        /// close is completed from the datastructure/transport layer. We have to send
        /// the terminal state only when we know that underlying datastructure/transport
        /// is closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void HandleCloseCompleted(object sender, EventArgs args)
        {
            // if state is completed or failed or stopped,
            // then the collections need to be closed as
            // well, else the enumerator will block
            UnblockCollections();

            // close the transport manager when CreateCloseAckPacket is received
            // otherwise may have race conditions in Server.OutOfProcessMediator
            dataStructureHandler.RaiseRemoveAssociationEvent();

            if (_stateInfoQueue.Count == 0)
            {
                // If shell state is not finished on client side and queue is empty
                // then set state to stopped unless the current state is Disconnected
                // in which case transition state to failed.
                if (!IsFinished(shell.InvocationStateInfo.State))
                {
                    // If RemoteSessionStateEventArgs are provided then use them to set the
                    // session close reason when setting finished state.
                    RemoteSessionStateEventArgs sessionEventArgs = args as RemoteSessionStateEventArgs;
                    Exception         closeReason   = (sessionEventArgs != null) ? sessionEventArgs.SessionStateInfo.Reason : null;
                    PSInvocationState finishedState = (shell.InvocationStateInfo.State == PSInvocationState.Disconnected) ?
                                                      PSInvocationState.Failed : PSInvocationState.Stopped;

                    SetStateInfo(new PSInvocationStateInfo(finishedState, closeReason));
                }
            }
            else
            {
                // Apply queued state changes.
                while (_stateInfoQueue.Count > 0)
                {
                    PSInvocationStateInfo stateInfo = _stateInfoQueue.Dequeue();
                    SetStateInfo(stateInfo);
                }
            }
        }
 private void HandleConnectionStateChanged(object sender, RemoteSessionStateEventArgs arg)
 {
     using (ClientRemoteSessionImpl._trace.TraceEventHandlers())
     {
         if (arg == null)
         {
             throw ClientRemoteSessionImpl._trace.NewArgumentNullException(nameof(arg));
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.EstablishedAndKeyReceived)
         {
             this.StartKeyExchange();
         }
         if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
         {
             this.CompleteKeyExchange();
         }
         if (this.StateChanged == null)
         {
             return;
         }
         this.StateChanged((object)this, arg);
     }
 }
Пример #13
0
 private void HandleStateChanged(object sender, RemoteSessionStateEventArgs arg)
 {
     if (arg == null)
     {
         throw PSTraceSource.NewArgumentNullException("arg");
     }
     if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
     {
         this.HandleNegotiationSendingStateChange();
     }
     this.ConnectionStateChanged.SafeInvoke <RemoteSessionStateEventArgs>(this, arg);
     if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
     {
         this.SendNegotiationAsync(arg.SessionStateInfo.State);
     }
     if (arg.SessionStateInfo.State == RemoteSessionState.Established)
     {
         WSManClientSessionTransportManager manager = this._transportManager as WSManClientSessionTransportManager;
         if (manager != null)
         {
             manager.AdjustForProtocolVariations(this._session.ServerProtocolVersion);
             manager.StartReceivingData();
         }
     }
     if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
     {
         this.CloseConnectionAsync();
     }
     if (arg.SessionStateInfo.State == RemoteSessionState.Disconnecting)
     {
         this.DisconnectAsync();
     }
     if (arg.SessionStateInfo.State == RemoteSessionState.Reconnecting)
     {
         this.ReconnectAsync();
     }
 }
        private void HandleStateChanged(object sender, RemoteSessionStateEventArgs arg)
        {
            if (arg == null)
            {
                throw PSTraceSource.NewArgumentNullException("arg");
            }

            // Enqueue session related negotiation packets first
            if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
            {
                HandleNegotiationSendingStateChange();
            }

            // this will enable top-layers to enqueue any packets during NegotiationSending and
            // during other states.
            ConnectionStateChanged.SafeInvoke(this, arg);

            if ((arg.SessionStateInfo.State == RemoteSessionState.NegotiationSending) || (arg.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect))
            {
                SendNegotiationAsync(arg.SessionStateInfo.State);
            }

            //once session is established.. start receiving data (if not already done and only apples to wsmanclientsessionTM)
            if (arg.SessionStateInfo.State == RemoteSessionState.Established)
            {
                WSManClientSessionTransportManager tm = _transportManager as WSManClientSessionTransportManager;
                if (tm != null)
                {
                    tm.AdjustForProtocolVariations(_session.ServerProtocolVersion);
                    tm.StartReceivingData();
                }
            }

            // Close the transport manager only after powershell's close their transports
            // Powershell's close their transport using the ConnectionStateChanged event notification.
            if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
            {
                CloseConnectionAsync();
            }

            //process disconnect
            if (arg.SessionStateInfo.State == RemoteSessionState.Disconnecting)
            {
                DisconnectAsync();
            }

            //process reconnect
            if (arg.SessionStateInfo.State == RemoteSessionState.Reconnecting)
            {
                ReconnectAsync();
            }
        }
        /// <summary>
        /// Sets the state of the state machine. Since only 
        /// one thread can be manipulating the state at a time
        /// the state is not synchronized
        /// </summary>
        /// <param name="newState">new state of the state machine</param>
        /// <param name="reason">reason why the state machine is set
        /// to the new state</param>
        private void SetState(RemoteSessionState newState, Exception reason)
        {
            RemoteSessionState oldState = _state;

            if (newState != oldState)
            {
                _state = newState;
                s_trace.WriteLine("state machine state transition: from state {0} to state {1}", oldState, _state);

                RemoteSessionStateInfo stateInfo = new RemoteSessionStateInfo(_state, reason);
                RemoteSessionStateEventArgs sessionStateEventArg = new RemoteSessionStateEventArgs(stateInfo);

                _clientRemoteSessionStateChangeQueue.Enqueue(sessionStateEventArg);
            }
        }
 private void HandleClientRemoteSessionStateChanged(object sender, RemoteSessionStateEventArgs e)
 {
     if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSending)
     {
         if (this.createRunspaceCalled)
         {
             return;
         }
         lock (this.syncObject)
         {
             if (this.createRunspaceCalled)
             {
                 return;
             }
             this.createRunspaceCalled = true;
         }
         PSPrimitiveDictionary applicationArguments = PSPrimitiveDictionary.CloneAndAddPSVersionTable(this.applicationArguments);
         var createPoolData = RemotingEncoder.GenerateCreateRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces, this.remoteSession.RemoteRunspacePoolInternal, this.host, applicationArguments);
         this.SendDataAsync(createPoolData);
     }
     if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect)
     {
         this.SendDataAsync(RemotingEncoder.GenerateConnectRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
     {
         List <ClientPowerShellDataStructureHandler> list;
         Exception closingReason = this.closingReason;
         if (closingReason == null)
         {
             closingReason      = e.SessionStateInfo.Reason;
             this.closingReason = closingReason;
         }
         lock (this.associationSyncObject)
         {
             list = new List <ClientPowerShellDataStructureHandler>(this.associatedPowerShellDSHandlers.Values);
         }
         foreach (ClientPowerShellDataStructureHandler handler in list)
         {
             handler.CloseConnectionAsync(this.closingReason);
         }
         this.SessionClosing.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(closingReason));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Closed)
     {
         Exception reason = this.closingReason;
         if (reason == null)
         {
             reason             = e.SessionStateInfo.Reason;
             this.closingReason = reason;
         }
         if (reason != null)
         {
             this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Broken, reason));
         }
         else
         {
             this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Closed, reason));
         }
         this.SessionClosed.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(reason));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Connected)
     {
         PSEtwLog.ReplaceActivityIdForCurrentThread(this.clientRunspacePoolId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Runspace, PSTask.CreateRunspace);
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Disconnected)
     {
         this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Disconnected, e.SessionStateInfo.Reason));
         this.SessionDisconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(e.SessionStateInfo.Reason));
     }
     else if (this._reconnecting && (e.SessionStateInfo.State == RemoteSessionState.Established))
     {
         this.SessionReconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null));
         this._reconnecting = false;
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.RCDisconnecting)
     {
         this.SessionRCDisconnecting.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null));
     }
     else if (e.SessionStateInfo.Reason != null)
     {
         this.closingReason = e.SessionStateInfo.Reason;
     }
 }
Пример #17
0
        /// <summary>
        /// Handles changes in data structure handler state
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg">
        /// Event argument which contains the new state
        /// </param>
        private void HandleConnectionStateChanged(object sender, RemoteSessionStateEventArgs arg)
        {
            using (s_trace.TraceEventHandlers())
            {
                if (arg == null)
                {
                    throw PSTraceSource.NewArgumentNullException("arg");
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.EstablishedAndKeyReceived) //TODO - Client session would never get into this state... to be removed
                {
                    // send the public key
                    StartKeyExchange();
                }

                if (arg.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
                {
                    // when the connection is being closed we need to 
                    // complete the key exchange process to release
                    // the lock under which the key exchange is happening
                    // if we fail to release the lock, then when
                    // transport manager is closing it will try to
                    // acquire the lock again leading to a deadlock
                    CompleteKeyExchange();
                }

                StateChanged.SafeInvoke(this, arg);
            }
        }
Пример #18
0
 private void HandleClientRemoteSessionStateChanged(object sender, RemoteSessionStateEventArgs e)
 {
     using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod())
     {
         if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSending)
         {
             if (this.createRunspaceCalled)
             {
                 return;
             }
             lock (this.syncObject)
             {
                 if (this.createRunspaceCalled)
                 {
                     return;
                 }
                 this.createRunspaceCalled = true;
             }
             this.SendDataAsync(RemotingEncoder.GenerateCreateRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces, this.remoteSession.RemoteRunspacePoolInternal, this.host, PSPrimitiveDictionary.CloneAndAddPSVersionTable(this.applicationArguments)));
         }
         else if (e.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
         {
             Exception exception = this.closingReason;
             if (exception == null)
             {
                 exception          = e.SessionStateInfo.Reason;
                 this.closingReason = exception;
             }
             lock (this.associationSyncObject)
             {
                 foreach (ClientPowerShellDataStructureHandler structureHandler in this.associatedPowerShellDSHandlers.Values)
                 {
                     structureHandler.CloseConnection();
                 }
             }
             if (this.SessionClosing == null)
             {
                 return;
             }
             this.SessionClosing((object)this, new RemoteDataEventArgs <Exception>((object)exception));
         }
         else if (e.SessionStateInfo.State == RemoteSessionState.Closed)
         {
             Exception reason = this.closingReason;
             if (reason == null)
             {
                 reason             = e.SessionStateInfo.Reason;
                 this.closingReason = reason;
             }
             if (reason != null)
             {
                 this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Broken, reason));
             }
             else
             {
                 this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Closed, reason));
             }
             if (this.SessionClosed == null)
             {
                 return;
             }
             this.SessionClosed((object)this, new RemoteDataEventArgs <Exception>((object)reason));
         }
         else if (e.SessionStateInfo.State == RemoteSessionState.Connected)
         {
             using (IETWTracer etwTracer = PSETWTracerFactory.GetETWTracer(PSKeyword.Runspace))
                 etwTracer.ReplaceActivityIdForCurrentThread(this.clientRunspacePoolId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Runspace, PSTask.CreateRunspace);
         }
         else
         {
             if (e.SessionStateInfo.Reason == null)
             {
                 return;
             }
             this.closingReason = e.SessionStateInfo.Reason;
         }
     }
 }