Ejemplo n.º 1
0
 internal void EnqueueAndStartProcessingThread(RemoteDataObject<PSObject> remoteObject, TransportErrorOccuredEventArgs transportErrorArgs, object privateData)
 {
     if (!this.isClosed)
     {
         lock (this.callbackNotificationQueue)
         {
             if (((remoteObject != null) || (transportErrorArgs != null)) || (privateData != null))
             {
                 CallbackNotificationInformation item = new CallbackNotificationInformation {
                     remoteObject = remoteObject,
                     transportError = transportErrorArgs,
                     privateData = privateData
                 };
                 if ((remoteObject != null) && (((remoteObject.DataType == RemotingDataType.PublicKey) || (remoteObject.DataType == RemotingDataType.EncryptedSessionKey)) || (remoteObject.DataType == RemotingDataType.PublicKeyRequest)))
                 {
                     base.CryptoHelper.Session.BaseSessionDataStructureHandler.RaiseKeyExchangeMessageReceived(remoteObject);
                 }
                 else
                 {
                     this.callbackNotificationQueue.Enqueue(item);
                 }
             }
             if ((!this.isServicingCallbacks && !this.suspendQueueServicing) && (this.callbackNotificationQueue.Count > 0))
             {
                 this.isServicingCallbacks = true;
                 ThreadPool.QueueUserWorkItem(new WaitCallback(this.ServicePendingCallbacks));
             }
         }
     }
 }
Ejemplo n.º 2
0
 internal virtual void ProcessRawData(byte[] data, string stream)
 {
     try
     {
         this.ProcessRawData(data, stream, this.onDataAvailableCallback);
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         baseTracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception.Message }), new object[0]);
         PSRemotingTransportException   e         = new PSRemotingTransportException(exception.Message, exception);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveShellOutputEx);
         this.RaiseErrorHandler(eventArgs);
     }
 }
 internal override void RaiseErrorHandler(TransportErrorOccuredEventArgs eventArgs)
 {
     string stackTrace;
     if (!string.IsNullOrEmpty(eventArgs.Exception.StackTrace))
     {
         stackTrace = eventArgs.Exception.StackTrace;
     }
     else if ((eventArgs.Exception.InnerException != null) && !string.IsNullOrEmpty(eventArgs.Exception.InnerException.StackTrace))
     {
         stackTrace = eventArgs.Exception.InnerException.StackTrace;
     }
     else
     {
         stackTrace = string.Empty;
     }
     PSEtwLog.LogOperationalError(PSEventId.TransportError, PSOpcode.Open, PSTask.None, PSKeyword.UseAlwaysOperational, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString(), eventArgs.Exception.ErrorCode.ToString(CultureInfo.InvariantCulture), eventArgs.Exception.Message, stackTrace });
     PSEtwLog.LogAnalyticError(PSEventId.TransportError_Analytic, PSOpcode.Open, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString(), eventArgs.Exception.ErrorCode.ToString(CultureInfo.InvariantCulture), eventArgs.Exception.Message, stackTrace });
     base.RaiseErrorHandler(eventArgs);
 }
 internal void ProcessWSManTransportError(TransportErrorOccuredEventArgs eventArgs)
 {
     base.EnqueueAndStartProcessingThread(null, eventArgs, null);
 }
 private static void OnRemoteCmdSignalCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data)
 {
     BaseClientTransportManager.tracer.WriteLine("Signal Completed callback received.", new object[0]);
     long cmdTMId = 0L;
     WSManClientCommandTransportManager cmdTransportManager = null;
     if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId))
     {
         BaseClientTransportManager.tracer.WriteLine("Unable to find a transport manager for the given command context {0}.", new object[] { cmdTMId });
     }
     else
     {
         PSEtwLog.LogAnalyticInformational(PSEventId.WSManSignalCallbackReceived, PSOpcode.Disconnect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { cmdTransportManager.RunspacePoolInstanceId.ToString(), cmdTransportManager.powershellInstanceId.ToString() });
         if (!shellOperationHandle.Equals(cmdTransportManager.wsManShellOperationHandle) || !commandOperationHandle.Equals(cmdTransportManager.wsManCmdOperationHandle))
         {
             BaseClientTransportManager.tracer.WriteLine("Cmd Signal callback: ShellOperationHandles are not the same as the signal is initiated with", new object[0]);
             PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.CommandSendExFailed);
             TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.CommandInputEx);
             cmdTransportManager.ProcessWSManTransportError(eventArgs);
         }
         else
         {
             if (IntPtr.Zero != cmdTransportManager.cmdSignalOperationHandle)
             {
                 WSManNativeApi.WSManCloseOperation(cmdTransportManager.cmdSignalOperationHandle, 0);
                 cmdTransportManager.cmdSignalOperationHandle = IntPtr.Zero;
             }
             if (cmdTransportManager.signalCmdCompleted != null)
             {
                 cmdTransportManager.signalCmdCompleted.Dispose();
                 cmdTransportManager.signalCmdCompleted = null;
             }
             if (cmdTransportManager.isClosed)
             {
                 BaseClientTransportManager.tracer.WriteLine("Client Command TM: Transport manager is closed. So returning", new object[0]);
             }
             else
             {
                 if (IntPtr.Zero != error)
                 {
                     WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error);
                     if (errorStruct.errorCode != 0)
                     {
                         BaseClientTransportManager.tracer.WriteLine("Cmd Signal callback: WSMan reported an error: {0}", new object[] { errorStruct.errorDetail });
                         TransportErrorOccuredEventArgs args2 = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, null, errorStruct, TransportMethodEnum.CommandInputEx, RemotingErrorIdStrings.CommandSendExCallBackError, new object[] { WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail) });
                         cmdTransportManager.ProcessWSManTransportError(args2);
                         return;
                     }
                 }
                 cmdTransportManager.EnqueueAndStartProcessingThread(null, null, true);
             }
         }
     }
 }
Ejemplo n.º 6
0
 internal virtual void ProcessRawData(byte[] data, string stream)
 {
     try
     {
         this.ProcessRawData(data, stream, this.onDataAvailableCallback);
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         baseTracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception.Message }), new object[0]);
         PSRemotingTransportException e = new PSRemotingTransportException(exception.Message, exception);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveShellOutputEx);
         this.RaiseErrorHandler(eventArgs);
     }
 }
 private void HandleTransportErrorForRedirection(object sender, TransportErrorOccuredEventArgs e)
 {
     this._transportManager.CloseCompleted -= new EventHandler<EventArgs>(this.HandleTransportCloseCompleteForRedirection);
     this._transportManager.WSManTransportErrorOccured -= new EventHandler<TransportErrorOccuredEventArgs>(this.HandleTransportErrorForRedirection);
     this._transportManager.CloseCompleted += new EventHandler<EventArgs>(this.HandleCloseComplete);
     this._transportManager.WSManTransportErrorOccured += new EventHandler<TransportErrorOccuredEventArgs>(this.HandleTransportError);
     this.HandleTransportError(sender, e);
 }
Ejemplo n.º 8
0
        internal void HandleTransportError(object sender, TransportErrorOccuredEventArgs e)
        {
            PSRemotingTransportRedirectException exception = e.Exception as PSRemotingTransportRedirectException;

            if ((exception != null) && (this.maxUriRedirectionCount > 0))
            {
                Exception exception2 = null;
                try
                {
                    this.maxUriRedirectionCount--;
                    this.PerformURIRedirection(exception.RedirectLocation);
                    return;
                }
                catch (ArgumentNullException exception3)
                {
                    exception2 = exception3;
                }
                catch (UriFormatException exception4)
                {
                    exception2 = exception4;
                }
                if (exception2 != null)
                {
                    PSRemotingTransportException exception5 = new PSRemotingTransportException(PSRemotingErrorId.RedirectedURINotWellFormatted, RemotingErrorIdStrings.RedirectedURINotWellFormatted, new object[] { this._session.Context.RemoteAddress.OriginalString, exception.RedirectLocation })
                    {
                        TransportMessage = e.Exception.TransportMessage
                    };
                    e.Exception = exception5;
                }
            }
            RemoteSessionEvent connectFailed = RemoteSessionEvent.ConnectFailed;

            switch (e.ReportingTransportMethod)
            {
            case TransportMethodEnum.CreateShellEx:
                connectFailed = RemoteSessionEvent.ConnectFailed;
                break;

            case TransportMethodEnum.SendShellInputEx:
            case TransportMethodEnum.CommandInputEx:
                connectFailed = RemoteSessionEvent.SendFailed;
                break;

            case TransportMethodEnum.ReceiveShellOutputEx:
            case TransportMethodEnum.ReceiveCommandOutputEx:
                connectFailed = RemoteSessionEvent.ReceiveFailed;
                break;

            case TransportMethodEnum.CloseShellOperationEx:
                connectFailed = RemoteSessionEvent.CloseFailed;
                break;

            case TransportMethodEnum.DisconnectShellEx:
                connectFailed = RemoteSessionEvent.DisconnectFailed;
                break;

            case TransportMethodEnum.ReconnectShellEx:
                connectFailed = RemoteSessionEvent.ReconnectFailed;
                break;
            }
            RemoteSessionStateMachineEventArgs arg = new RemoteSessionStateMachineEventArgs(connectFailed, e.Exception);

            this._stateMachine.RaiseEvent(arg, false);
        }
 internal void HandleTransportError(object sender, TransportErrorOccuredEventArgs e)
 {
     PSInvocationStateInfo data = new PSInvocationStateInfo(PSInvocationState.Failed, e.Exception);
     this.InvocationStateInfoReceived.SafeInvoke<RemoteDataEventArgs<PSInvocationStateInfo>>(this, new RemoteDataEventArgs<PSInvocationStateInfo>(data));
 }
        /// <summary>
        /// Handler which handles transport errors.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void HandleTransportError(object sender, TransportErrorOccuredEventArgs e)
        {
            Dbg.Assert(e != null, "HandleTransportError expects non-null eventargs");
            // handle uri redirections
            PSRemotingTransportRedirectException redirectException = e.Exception as PSRemotingTransportRedirectException;
            if ((redirectException != null) && (_maxUriRedirectionCount > 0))
            {
                Exception exception = null;

                try
                {
                    // honor max redirection count given by the user.
                    _maxUriRedirectionCount--;
                    PerformURIRedirection(redirectException.RedirectLocation);
                    return;
                }
                catch (ArgumentNullException argumentException)
                {
                    exception = argumentException;
                }
                catch (UriFormatException uriFormatException)
                {
                    exception = uriFormatException;
                }
                // if we are here, there must be an exception constructing a uri
                if (null != exception)
                {
                    PSRemotingTransportException newException =
                        new PSRemotingTransportException(PSRemotingErrorId.RedirectedURINotWellFormatted, RemotingErrorIdStrings.RedirectedURINotWellFormatted,
                            _session.Context.RemoteAddress.OriginalString,
                            redirectException.RedirectLocation);
                    newException.TransportMessage = e.Exception.TransportMessage;
                    e.Exception = newException;
                }
            }

            RemoteSessionEvent sessionEvent = RemoteSessionEvent.ConnectFailed;

            switch (e.ReportingTransportMethod)
            {
                case TransportMethodEnum.CreateShellEx:
                    sessionEvent = RemoteSessionEvent.ConnectFailed;
                    break;
                case TransportMethodEnum.SendShellInputEx:
                case TransportMethodEnum.CommandInputEx:
                    sessionEvent = RemoteSessionEvent.SendFailed;
                    break;
                case TransportMethodEnum.ReceiveShellOutputEx:
                case TransportMethodEnum.ReceiveCommandOutputEx:
                    sessionEvent = RemoteSessionEvent.ReceiveFailed;
                    break;
                case TransportMethodEnum.CloseShellOperationEx:
                    sessionEvent = RemoteSessionEvent.CloseFailed;
                    break;
                case TransportMethodEnum.DisconnectShellEx:
                    sessionEvent = RemoteSessionEvent.DisconnectFailed;
                    break;
                case TransportMethodEnum.ReconnectShellEx:
                    sessionEvent = RemoteSessionEvent.ReconnectFailed;
                    break;
            }

            RemoteSessionStateMachineEventArgs errorArgs =
                new RemoteSessionStateMachineEventArgs(sessionEvent, e.Exception);
            _stateMachine.RaiseEvent(errorArgs);
        }
        private void HandleTransportErrorForRedirection(object sender, TransportErrorOccuredEventArgs e)
        {
            _transportManager.CloseCompleted -= HandleTransportCloseCompleteForRedirection;
            _transportManager.WSManTransportErrorOccured -= HandleTransportErrorForRedirection;

            // reattach the close complete and error handlers
            _transportManager.CloseCompleted += HandleCloseComplete;
            _transportManager.WSManTransportErrorOccured += HandleTransportError;

            HandleTransportError(sender, e);
        }
 private static void OnRemoteSessionSendCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data)
 {
     BaseClientTransportManager.tracer.WriteLine("Client Session TM: SendComplete callback received", new object[0]);
     long sessnTMId = 0L;
     WSManClientSessionTransportManager sessnTransportManager = null;
     if (!TryGetSessionTransportManager(operationContext, out sessnTransportManager, out sessnTMId))
     {
         BaseClientTransportManager.tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unable to find a transport manager for context {0}.", new object[] { sessnTMId }), new object[0]);
     }
     else
     {
         PSEtwLog.LogAnalyticInformational(PSEventId.WSManSendShellInputExCallbackReceived, PSOpcode.Connect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { sessnTransportManager.RunspacePoolInstanceId.ToString(), Guid.Empty.ToString() });
         if (!shellOperationHandle.Equals(sessnTransportManager.wsManShellOperationHandle))
         {
             PSRemotingTransportException e = new PSRemotingTransportException(PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.SendExFailed, new object[] { sessnTransportManager.ConnectionInfo.ComputerName }));
             TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.SendShellInputEx);
             sessnTransportManager.ProcessWSManTransportError(eventArgs);
         }
         else
         {
             sessnTransportManager.ClearReceiveOrSendResources(flags, true);
             if (sessnTransportManager.isClosed)
             {
                 BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]);
             }
             else
             {
                 if (IntPtr.Zero != error)
                 {
                     WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error);
                     if ((errorStruct.errorCode != 0) && (errorStruct.errorCode != 0x3e3))
                     {
                         BaseClientTransportManager.tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Got error with error code {0}. Message {1}", new object[] { errorStruct.errorCode, errorStruct.errorDetail }), new object[0]);
                         TransportErrorOccuredEventArgs args2 = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(wsManApiStaticData.WSManAPIHandle, sessnTransportManager, errorStruct, TransportMethodEnum.SendShellInputEx, RemotingErrorIdStrings.SendExCallBackError, new object[] { sessnTransportManager.ConnectionInfo.ComputerName, WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail) });
                         sessnTransportManager.ProcessWSManTransportError(args2);
                         return;
                     }
                 }
                 sessnTransportManager.SendOneItem();
             }
         }
     }
 }
Ejemplo n.º 13
0
 internal virtual void RaiseErrorHandler(TransportErrorOccuredEventArgs eventArgs)
 {
     this.WSManTransportErrorOccured.SafeInvoke <TransportErrorOccuredEventArgs>(this, eventArgs);
 }
 internal override void CreateAsync()
 {
     if (this.connectionInfo != null)
     {
         this._processInstance = this.connectionInfo.Process ?? new PowerShellProcessInstance(this.connectionInfo.PSVersion, this.connectionInfo.Credential, this.connectionInfo.InitializationScript, this.connectionInfo.RunAs32);
         if (this.connectionInfo.Process != null)
         {
             this._processCreated = false;
         }
     }
     PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateShell, PSOpcode.Connect, PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString() });
     try
     {
         lock (base.syncObject)
         {
             if (base.isClosed)
             {
                 return;
             }
             this.serverProcess = this._processInstance.Process;
             if (this._processInstance.RunspacePool != null)
             {
                 this._processInstance.RunspacePool.Close();
                 this._processInstance.RunspacePool.Dispose();
             }
             this.stdInWriter = this._processInstance.StdInWriter;
             this.serverProcess.OutputDataReceived += new DataReceivedEventHandler(this.OnOutputDataReceived);
             this.serverProcess.ErrorDataReceived += new DataReceivedEventHandler(this.OnErrorDataReceived);
             this.serverProcess.Exited += new EventHandler(this.OnExited);
             this._processInstance.Start();
             if (this.stdInWriter != null)
             {
                 this.serverProcess.CancelErrorRead();
                 this.serverProcess.CancelOutputRead();
             }
             this.serverProcess.BeginOutputReadLine();
             this.serverProcess.BeginErrorReadLine();
             this.stdInWriter = new OutOfProcessTextWriter(this.serverProcess.StandardInput);
             this._processInstance.StdInWriter = this.stdInWriter;
         }
     }
     catch (Win32Exception exception)
     {
         PSRemotingTransportException e = new PSRemotingTransportException(exception, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, new object[] { exception.Message }) {
             ErrorCode = exception.ErrorCode
         };
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.CreateShellEx);
         this.RaiseErrorHandler(eventArgs);
         return;
     }
     catch (Exception exception3)
     {
         CommandProcessorBase.CheckForSevereException(exception3);
         PSRemotingTransportException exception4 = new PSRemotingTransportException(PSRemotingErrorId.IPCExceptionLaunchingProcess, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, new object[] { exception3.Message });
         TransportErrorOccuredEventArgs args2 = new TransportErrorOccuredEventArgs(exception4, TransportMethodEnum.CreateShellEx);
         this.RaiseErrorHandler(args2);
         return;
     }
     this.SendOneItem();
 }
Ejemplo n.º 15
0
 internal override void ProcessRawData(byte[] data, string stream)
 {
     if (!this.isClosed)
     {
         try
         {
             base.ProcessRawData(data, stream, this.onDataAvailableCallback);
         }
         catch (PSRemotingTransportException exception)
         {
             tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception.Message }), new object[0]);
             TransportErrorOccuredEventArgs transportErrorArgs = new TransportErrorOccuredEventArgs(exception, TransportMethodEnum.ReceiveShellOutputEx);
             this.EnqueueAndStartProcessingThread(null, transportErrorArgs, null);
         }
         catch (Exception exception2)
         {
             CommandProcessorBase.CheckForSevereException(exception2);
             tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception2.Message }), new object[0]);
             PSRemotingTransportException e = new PSRemotingTransportException(exception2.Message);
             TransportErrorOccuredEventArgs args2 = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveShellOutputEx);
             this.EnqueueAndStartProcessingThread(null, args2, null);
         }
     }
 }
 internal override void ConnectAsync()
 {
     base.ReceivedDataCollection.PrepareForStreamConnect();
     base.serializedPipeline.Read();
     this.cmdContextId = GetNextCmdTMHandleId();
     AddCmdTransportManager(this.cmdContextId, this);
     this.connectCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdConnectCallback);
     this.reconnectCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdReconnectCallback);
     lock (base.syncObject)
     {
         if (base.isClosed)
         {
             return;
         }
         WSManNativeApi.WSManConnectShellCommandEx(this.wsManShellOperationHandle, 0, base.PowershellInstanceId.ToString().ToUpper(CultureInfo.InvariantCulture), IntPtr.Zero, IntPtr.Zero, (IntPtr) this.connectCmdCompleted, ref this.wsManCmdOperationHandle);
     }
     if (this.wsManCmdOperationHandle == IntPtr.Zero)
     {
         PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.RunShellCommandExFailed);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ConnectShellCommandEx);
         this.ProcessWSManTransportError(eventArgs);
     }
 }
Ejemplo n.º 17
0
 internal void ServicePendingCallbacks(object objectToProcess)
 {
     tracer.WriteLine("ServicePendingCallbacks thread is starting", new object[0]);
     PSEtwLog.ReplaceActivityIdForCurrentThread(this.runspacePoolInstanceId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Transport, PSTask.None);
     try
     {
         while (!this.isClosed)
         {
             CallbackNotificationInformation information = null;
             lock (this.callbackNotificationQueue)
             {
                 if ((this.callbackNotificationQueue.Count <= 0) || this.suspendQueueServicing)
                 {
                     return;
                 }
                 information = this.callbackNotificationQueue.Dequeue();
             }
             if (information != null)
             {
                 if (information.transportError != null)
                 {
                     this.RaiseErrorHandler(information.transportError);
                     return;
                 }
                 if (information.privateData != null)
                 {
                     this.ProcessPrivateData(information.privateData);
                 }
                 else
                 {
                     base.OnDataAvailableCallback(information.remoteObject);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception.Message }), new object[0]);
         PSRemotingTransportException e = new PSRemotingTransportException(exception.Message, exception);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveShellOutputEx);
         this.RaiseErrorHandler(eventArgs);
     }
     finally
     {
         lock (this.callbackNotificationQueue)
         {
             tracer.WriteLine("ServicePendingCallbacks thread is exiting", new object[0]);
             this.isServicingCallbacks = false;
             this.EnqueueAndStartProcessingThread(null, null, null);
         }
     }
 }
        internal override void CreateAsync()
        {
            byte[] firstArgument = base.serializedPipeline.ReadOrRegisterCallback(null);
			if (firstArgument == null) firstArgument = new byte[0];
            bool flag = true;
            if (commandCodeSendRedirect != null)
            {
                object[] objArray2 = new object[2];
                objArray2[1] = firstArgument;
                object[] objArray = objArray2;
                flag = (bool) commandCodeSendRedirect.DynamicInvoke(objArray);
                firstArgument = (byte[]) objArray[0];
            }
            if (!flag)
            {
                return;
            }
            WSManNativeApi.WSManCommandArgSet set = new WSManNativeApi.WSManCommandArgSet(firstArgument);
            this.cmdContextId = GetNextCmdTMHandleId();
            AddCmdTransportManager(this.cmdContextId, this);
            PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateCommand, PSOpcode.Connect, PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString() });
            this.createCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdCreateCallback);
            this.createCmdCompletedGCHandle = GCHandle.Alloc(this.createCmdCompleted);
            this.reconnectCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdReconnectCallback);
            using (set)
            {
                lock (base.syncObject)
                {
                    if (!base.isClosed)
                    {
                        WSManNativeApi.WSManRunShellCommandEx(this.wsManShellOperationHandle, 0, base.PowershellInstanceId.ToString().ToUpper(CultureInfo.InvariantCulture), ((this.cmdLine == null) || (this.cmdLine.Length == 0)) ? " " : ((this.cmdLine.Length <= 0x100) ? this.cmdLine : this.cmdLine.Substring(0, 0xff)), (IntPtr) set, IntPtr.Zero, (IntPtr) this.createCmdCompleted, ref this.wsManCmdOperationHandle);
                        BaseClientTransportManager.tracer.WriteLine("Started cmd with command context : {0} Operation context: {1}", new object[] { this.cmdContextId, this.wsManCmdOperationHandle });
                    }
                }
            }
            if (this.wsManCmdOperationHandle == IntPtr.Zero)
            {
                PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.RunShellCommandExFailed);
                TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.RunShellCommandEx);
                this.ProcessWSManTransportError(eventArgs);
            }
        }
Ejemplo n.º 19
0
 // handle transport manager related errors 
 internal void HandleTransportError(Object sender, TransportErrorOccuredEventArgs eventArgs)
 {
     Exception reasonForClose = null;
     if (null != eventArgs)
     {
         reasonForClose = eventArgs.Exception;
     }
     Close(reasonForClose);
 }
 private static void OnReconnectCmdCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data)
 {
     long cmdTMId = 0L;
     WSManClientCommandTransportManager cmdTransportManager = null;
     if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId))
     {
         BaseClientTransportManager.tracer.WriteLine("Unable to find a transport manager for the given command context {0}.", new object[] { cmdTMId });
     }
     else if (!shellOperationHandle.Equals(cmdTransportManager.wsManShellOperationHandle) || !commandOperationHandle.Equals(cmdTransportManager.wsManCmdOperationHandle))
     {
         BaseClientTransportManager.tracer.WriteLine("Cmd Signal callback: ShellOperationHandles are not the same as the signal is initiated with", new object[0]);
         PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.ReconnectShellCommandExCallBackError);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReconnectShellCommandEx);
         cmdTransportManager.ProcessWSManTransportError(eventArgs);
     }
     else
     {
         if (IntPtr.Zero != error)
         {
             WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error);
             if (errorStruct.errorCode != 0)
             {
                 BaseClientTransportManager.tracer.WriteLine("OnReconnectCmdCompleted callback: WSMan reported an error: {0}", new object[] { errorStruct.errorDetail });
                 TransportErrorOccuredEventArgs args2 = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, null, errorStruct, TransportMethodEnum.ReconnectShellCommandEx, RemotingErrorIdStrings.ReconnectShellCommandExCallBackError, new object[] { WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail) });
                 cmdTransportManager.ProcessWSManTransportError(args2);
                 return;
             }
         }
         cmdTransportManager.shouldStartReceivingData = true;
         cmdTransportManager.SendOneItem();
         cmdTransportManager.RaiseReconnectCompleted();
     }
 }
        internal void HandleTransportError(object sender, TransportErrorOccuredEventArgs e)
        {
            PSRemotingTransportRedirectException exception = e.Exception as PSRemotingTransportRedirectException;
            if ((exception != null) && (this.maxUriRedirectionCount > 0))
            {
                Exception exception2 = null;
                try
                {
                    this.maxUriRedirectionCount--;
                    this.PerformURIRedirection(exception.RedirectLocation);
                    return;
                }
                catch (ArgumentNullException exception3)
                {
                    exception2 = exception3;
                }
                catch (UriFormatException exception4)
                {
                    exception2 = exception4;
                }
                if (exception2 != null)
                {
                    PSRemotingTransportException exception5 = new PSRemotingTransportException(PSRemotingErrorId.RedirectedURINotWellFormatted, RemotingErrorIdStrings.RedirectedURINotWellFormatted, new object[] { this._session.Context.RemoteAddress.OriginalString, exception.RedirectLocation }) {
                        TransportMessage = e.Exception.TransportMessage
                    };
                    e.Exception = exception5;
                }
            }
            RemoteSessionEvent connectFailed = RemoteSessionEvent.ConnectFailed;
            switch (e.ReportingTransportMethod)
            {
                case TransportMethodEnum.CreateShellEx:
                    connectFailed = RemoteSessionEvent.ConnectFailed;
                    break;

                case TransportMethodEnum.SendShellInputEx:
                case TransportMethodEnum.CommandInputEx:
                    connectFailed = RemoteSessionEvent.SendFailed;
                    break;

                case TransportMethodEnum.ReceiveShellOutputEx:
                case TransportMethodEnum.ReceiveCommandOutputEx:
                    connectFailed = RemoteSessionEvent.ReceiveFailed;
                    break;

                case TransportMethodEnum.CloseShellOperationEx:
                    connectFailed = RemoteSessionEvent.CloseFailed;
                    break;

                case TransportMethodEnum.DisconnectShellEx:
                    connectFailed = RemoteSessionEvent.DisconnectFailed;
                    break;

                case TransportMethodEnum.ReconnectShellEx:
                    connectFailed = RemoteSessionEvent.ReconnectFailed;
                    break;
            }
            RemoteSessionStateMachineEventArgs arg = new RemoteSessionStateMachineEventArgs(connectFailed, e.Exception);
            this._stateMachine.RaiseEvent(arg, false);
        }
 private static void OnRemoteCmdDataReceived(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data)
 {
     BaseClientTransportManager.tracer.WriteLine("Remote Command DataReceived callback.", new object[0]);
     long cmdTMId = 0L;
     WSManClientCommandTransportManager cmdTransportManager = null;
     if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId))
     {
         BaseClientTransportManager.tracer.WriteLine("Unable to find a transport manager for the given command context {0}.", new object[] { cmdTMId });
     }
     else if (!shellOperationHandle.Equals(cmdTransportManager.wsManShellOperationHandle) || !commandOperationHandle.Equals(cmdTransportManager.wsManCmdOperationHandle))
     {
         BaseClientTransportManager.tracer.WriteLine("CmdReceive callback: ShellOperationHandles are not the same as the Receive is initiated with", new object[0]);
         PSRemotingTransportException e = new PSRemotingTransportException(RemotingErrorIdStrings.CommandReceiveExFailed);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveCommandOutputEx);
         cmdTransportManager.ProcessWSManTransportError(eventArgs);
     }
     else
     {
         cmdTransportManager.ClearReceiveOrSendResources(flags, false);
         if (cmdTransportManager.isClosed)
         {
             BaseClientTransportManager.tracer.WriteLine("Client Command TM: Transport manager is closed. So returning", new object[0]);
         }
         else
         {
             if (IntPtr.Zero != error)
             {
                 WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error);
                 if (errorStruct.errorCode != 0)
                 {
                     BaseClientTransportManager.tracer.WriteLine("CmdReceive callback: WSMan reported an error: {0}", new object[] { errorStruct.errorDetail });
                     TransportErrorOccuredEventArgs args2 = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, null, errorStruct, TransportMethodEnum.ReceiveCommandOutputEx, RemotingErrorIdStrings.CommandReceiveExCallBackError, new object[] { errorStruct.errorDetail });
                     cmdTransportManager.ProcessWSManTransportError(args2);
                     return;
                 }
             }
             if (flags == 0x2000)
             {
                 cmdTransportManager.isDisconnectedOnInvoke = true;
                 cmdTransportManager.RaiseDelayStreamProcessedEvent();
             }
             else
             {
                 WSManNativeApi.WSManReceiveDataResult result = WSManNativeApi.WSManReceiveDataResult.UnMarshal(data);
                 if (result.data != null)
                 {
                     BaseClientTransportManager.tracer.WriteLine("Cmd Received Data : {0}", new object[] { result.data.Length });
                     object[] args = new object[] { cmdTransportManager.RunspacePoolInstanceId.ToString(), cmdTransportManager.powershellInstanceId.ToString(), result.data.Length.ToString(CultureInfo.InvariantCulture) };
                     PSEtwLog.LogAnalyticInformational(PSEventId.WSManReceiveShellOutputExCallbackReceived, PSOpcode.Receive, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, args);
                     cmdTransportManager.ProcessRawData(result.data, result.stream);
                 }
             }
         }
     }
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Handler which handles transport errors.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void HandleTransportError(object sender, TransportErrorOccuredEventArgs e)
        {
            Dbg.Assert(e != null, "HandleTransportError expects non-null eventargs");
            // handle uri redirections
            PSRemotingTransportRedirectException redirectException = e.Exception as PSRemotingTransportRedirectException;

            if ((redirectException != null) && (_maxUriRedirectionCount > 0))
            {
                Exception exception = null;

                try
                {
                    // honor max redirection count given by the user.
                    _maxUriRedirectionCount--;
                    PerformURIRedirection(redirectException.RedirectLocation);
                    return;
                }
                catch (ArgumentNullException argumentException)
                {
                    exception = argumentException;
                }
                catch (UriFormatException uriFormatException)
                {
                    exception = uriFormatException;
                }
                // if we are here, there must be an exception constructing a uri
                if (exception != null)
                {
                    PSRemotingTransportException newException =
                        new PSRemotingTransportException(PSRemotingErrorId.RedirectedURINotWellFormatted, RemotingErrorIdStrings.RedirectedURINotWellFormatted,
                                                         _session.Context.RemoteAddress.OriginalString,
                                                         redirectException.RedirectLocation);
                    newException.TransportMessage = e.Exception.TransportMessage;
                    e.Exception = newException;
                }
            }

            RemoteSessionEvent sessionEvent = RemoteSessionEvent.ConnectFailed;

            switch (e.ReportingTransportMethod)
            {
            case TransportMethodEnum.CreateShellEx:
                sessionEvent = RemoteSessionEvent.ConnectFailed;
                break;

            case TransportMethodEnum.SendShellInputEx:
            case TransportMethodEnum.CommandInputEx:
                sessionEvent = RemoteSessionEvent.SendFailed;
                break;

            case TransportMethodEnum.ReceiveShellOutputEx:
            case TransportMethodEnum.ReceiveCommandOutputEx:
                sessionEvent = RemoteSessionEvent.ReceiveFailed;
                break;

            case TransportMethodEnum.CloseShellOperationEx:
                sessionEvent = RemoteSessionEvent.CloseFailed;
                break;

            case TransportMethodEnum.DisconnectShellEx:
                sessionEvent = RemoteSessionEvent.DisconnectFailed;
                break;

            case TransportMethodEnum.ReconnectShellEx:
                sessionEvent = RemoteSessionEvent.ReconnectFailed;
                break;
            }

            RemoteSessionStateMachineEventArgs errorArgs =
                new RemoteSessionStateMachineEventArgs(sessionEvent, e.Exception);

            _stateMachine.RaiseEvent(errorArgs);
        }
Ejemplo n.º 24
0
 internal virtual void RaiseErrorHandler(TransportErrorOccuredEventArgs eventArgs)
 {
     this.WSManTransportErrorOccured.SafeInvoke<TransportErrorOccuredEventArgs>(this, eventArgs);
 }