private static void AddCmdTransportManager(long cmdTMId, WSManClientCommandTransportManager cmdTransportManager) { lock (CmdTMHandles) { CmdTMHandles.Add(cmdTMId, cmdTransportManager); } }
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(); } }
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 = 0; WSManClientCommandTransportManager cmdTransportManager = (WSManClientCommandTransportManager)null; if (!WSManClientCommandTransportManager.TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("Unable to find a transport manager for the given command context {0}.", (object)cmdTMId); } else { BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManSignalCallbackReceived, PSOpcode.Disconnect, PSTask.None, (object)cmdTransportManager.RunspacePoolInstanceId, (object)cmdTransportManager.powershellInstanceId); if (!shellOperationHandle.Equals((object)cmdTransportManager.wsManShellOperationHandle) || !commandOperationHandle.Equals((object)cmdTransportManager.wsManCmdOperationHandle)) { BaseClientTransportManager.tracer.WriteLine("Cmd Signal callback: ShellOperationHandles are not the same as the signal is initiated with", new object[0]); TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(new PSRemotingTransportException(PSRemotingErrorId.CommandSendExFailed, new object[0]), TransportMethodEnum.CommandInputEx); cmdTransportManager.RaiseErrorHandler(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 = (WSManNativeApi.WSManShellAsync)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}", (object)errorStruct.errorDetail); TransportErrorOccuredEventArgs eventArgs = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, IntPtr.Zero, errorStruct, TransportMethodEnum.CommandInputEx, PSRemotingErrorId.CommandSendExCallBackError, (object)WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail)); cmdTransportManager.RaiseErrorHandler(eventArgs); return; } } cmdTransportManager.EnqueueAndStartProcessingThread((RemoteDataObject <PSObject>)null, (PSRemotingTransportException)null, (object)true); } } } }
private static bool TryGetCmdTransportManager(IntPtr operationContext, out WSManClientCommandTransportManager cmdTransportManager, out long cmdTMId) { cmdTMId = operationContext.ToInt64(); cmdTransportManager = null; lock (CmdTMHandles) { return(CmdTMHandles.TryGetValue(cmdTMId, out cmdTransportManager)); } }
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); } } } } }
private static void OnConnectCmdCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data) { BaseClientTransportManager.tracer.WriteLine("OnConnectCmdCompleted callback received", new object[0]); long cmdTMId = 0L; WSManClientCommandTransportManager cmdTransportManager = null; if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("OnConnectCmdCompleted: Unable to find a transport manager for the command context {0}.", new object[] { cmdTMId }); } else { if (cmdTransportManager.connectCmdCompleted != null) { cmdTransportManager.connectCmdCompleted.Dispose(); cmdTransportManager.connectCmdCompleted = null; } cmdTransportManager.wsManCmdOperationHandle = commandOperationHandle; if (IntPtr.Zero != error) { WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error); if (errorStruct.errorCode != 0) { BaseClientTransportManager.tracer.WriteLine("OnConnectCmdCompleted callback: WSMan reported an error: {0}", new object[] { errorStruct.errorDetail }); TransportErrorOccuredEventArgs eventArgs = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, null, errorStruct, TransportMethodEnum.ReconnectShellCommandEx, RemotingErrorIdStrings.ReconnectShellCommandExCallBackError, new object[] { WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail) }); cmdTransportManager.ProcessWSManTransportError(eventArgs); return; } } lock (cmdTransportManager.syncObject) { if (cmdTransportManager.isClosed) { BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]); if (cmdTransportManager.isDisconnectPending) { cmdTransportManager.RaiseReadyForDisconnect(); } return; } if (cmdTransportManager.isDisconnectPending) { cmdTransportManager.RaiseReadyForDisconnect(); return; } cmdTransportManager.isCreateCallbackReceived = true; if (cmdTransportManager.isStopSignalPending) { cmdTransportManager.SendStopSignal(); } } cmdTransportManager.SendOneItem(); cmdTransportManager.RaiseConnectCompleted(); cmdTransportManager.StartReceivingData(); } }
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); } } } }
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 = 0; WSManClientCommandTransportManager cmdTransportManager = (WSManClientCommandTransportManager)null; if (!WSManClientCommandTransportManager.TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("Unable to find a transport manager for the given command context {0}.", (object)cmdTMId); } else if (!shellOperationHandle.Equals((object)cmdTransportManager.wsManShellOperationHandle) || !commandOperationHandle.Equals((object)cmdTransportManager.wsManCmdOperationHandle)) { BaseClientTransportManager.tracer.WriteLine("CmdReceive callback: ShellOperationHandles are not the same as the Receive is initiated with", new object[0]); TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(new PSRemotingTransportException(PSRemotingErrorId.CommandReceiveExFailed, new object[0]), TransportMethodEnum.ReceiveCommandOutputEx); cmdTransportManager.RaiseErrorHandler(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}", (object)errorStruct.errorDetail); TransportErrorOccuredEventArgs eventArgs = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, IntPtr.Zero, errorStruct, TransportMethodEnum.ReceiveCommandOutputEx, PSRemotingErrorId.CommandReceiveExCallBackError, (object)errorStruct.errorDetail); cmdTransportManager.RaiseErrorHandler(eventArgs); return; } } WSManNativeApi.WSManReceiveDataResult receiveDataResult = WSManNativeApi.WSManReceiveDataResult.UnMarshal(data); if (receiveDataResult.data == null) { return; } BaseClientTransportManager.tracer.WriteLine("Cmd Received Data : {0}", (object)receiveDataResult.data.Length); BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManReceiveShellOutputExCallbackReceived, PSOpcode.Receive, PSTask.None, (object)cmdTransportManager.RunspacePoolInstanceId, (object)cmdTransportManager.powershellInstanceId, (object)receiveDataResult.data.Length.ToString((IFormatProvider)CultureInfo.InvariantCulture)); cmdTransportManager.ProcessRawData(receiveDataResult.data, receiveDataResult.stream); } } }
internal override void Dispose(bool isDisposing) { BaseClientTransportManager.tracer.WriteLine("Disposing command with command context: {0} Operation Context: {1}", (object)this.cmdContextId, (object)this.wsManCmdOperationHandle); base.Dispose(isDisposing); WSManClientCommandTransportManager.RemoveCmdTransportManager(this.cmdContextId); if (this.closeCmdCompleted != null) { this.closeCmdCompleted.Dispose(); this.closeCmdCompleted = (WSManNativeApi.WSManShellAsync)null; } this.wsManCmdOperationHandle = IntPtr.Zero; }
internal override void ConnectAsync() { byte[] firstArgument = this.serializedPipeline.ReadOrRegisterCallback((SerializedDataStream.OnDataAvailableCallback)null); if (firstArgument != null) { bool flag = true; if ((object)WSManClientCommandTransportManager.commandCodeSendRedirect != null) { object[] objArray = new object[2] { null, (object)firstArgument }; flag = (bool)WSManClientCommandTransportManager.commandCodeSendRedirect.DynamicInvoke(objArray); firstArgument = (byte[])objArray[0]; } if (!flag) { return; } WSManNativeApi.WSManCommandArgSet manCommandArgSet = new WSManNativeApi.WSManCommandArgSet(firstArgument); this.cmdContextId = WSManClientCommandTransportManager.GetNextCmdTMHandleId(); WSManClientCommandTransportManager.AddCmdTransportManager(this.cmdContextId, this); BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManCreateCommand, PSOpcode.Connect, PSTask.CreateRunspace, (object)this.RunspacePoolInstanceId, (object)this.powershellInstanceId); this.createCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), WSManClientCommandTransportManager.cmdCreateCallback); this.createCmdCompletedGCHandle = GCHandle.Alloc((object)this.createCmdCompleted); using (manCommandArgSet) { lock (this.syncObject) { if (!this.isClosed) { WSManNativeApi.WSManRunShellCommandEx(this.wsManShellOperationHandle, 0, " ", (IntPtr)manCommandArgSet, IntPtr.Zero, (IntPtr)this.createCmdCompleted, ref this.wsManCmdOperationHandle); BaseClientTransportManager.tracer.WriteLine("Started cmd with command context : {0} Operation context: {1}", (object)this.cmdContextId, (object)this.wsManCmdOperationHandle); } } } } if (!(this.wsManCmdOperationHandle == IntPtr.Zero)) { return; } this.RaiseErrorHandler(new TransportErrorOccuredEventArgs(new PSRemotingTransportException(PSRemotingErrorId.RunShellCommandExFailed, new object[0]), TransportMethodEnum.RunShellCommandEx)); }
private static void OnCloseCmdCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data) { BaseClientTransportManager.tracer.WriteLine("OnCloseCmdCompleted callback received for operation context {0}", new object[] { commandOperationHandle }); long cmdTMId = 0L; WSManClientCommandTransportManager cmdTransportManager = null; if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("OnCloseCmdCompleted: Unable to find a transport manager for the command context {0}.", new object[] { cmdTMId }); } else { BaseClientTransportManager.tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Close completed callback received for command: {0}", new object[] { cmdTransportManager.cmdContextId }), new object[0]); PSEtwLog.LogAnalyticInformational(PSEventId.WSManCloseCommandCallbackReceived, PSOpcode.Disconnect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { cmdTransportManager.RunspacePoolInstanceId.ToString(), cmdTransportManager.powershellInstanceId.ToString() }); if (cmdTransportManager.isDisconnectPending) { cmdTransportManager.RaiseReadyForDisconnect(); } cmdTransportManager.RaiseCloseCompleted(); } }
private static void OnCloseCmdCompleted( IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data) { BaseClientTransportManager.tracer.WriteLine("OnCloseCmdCompleted callback received for operation context {0}", (object)commandOperationHandle); long cmdTMId = 0; WSManClientCommandTransportManager cmdTransportManager = (WSManClientCommandTransportManager)null; if (!WSManClientCommandTransportManager.TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("OnCloseCmdCompleted: Unable to find a transport manager for the command context {0}.", (object)cmdTMId); } else { BaseClientTransportManager.tracer.WriteLine(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Close completed callback received for command: {0}", (object)cmdTransportManager.cmdContextId), new object[0]); BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManCloseCommandCallbackReceived, PSOpcode.Disconnect, PSTask.None, (object)cmdTransportManager.RunspacePoolInstanceId, (object)cmdTransportManager.powershellInstanceId); cmdTransportManager.RaiseCloseCompleted(); } }
internal override void CloseAsync() { BaseClientTransportManager.tracer.WriteLine("Closing command with command context: {0} Operation Context {1}", (object)this.cmdContextId, (object)this.wsManCmdOperationHandle); bool flag = false; lock (this.syncObject) { if (this.isClosed) { return; } this.isClosed = true; if (IntPtr.Zero == this.wsManCmdOperationHandle) { flag = true; } } base.CloseAsync(); if (flag) { try { this.RaiseCloseCompleted(); } finally { WSManClientCommandTransportManager.RemoveCmdTransportManager(this.cmdContextId); } } else { BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManCloseCommand, PSOpcode.Disconnect, PSTask.None, (object)this.RunspacePoolInstanceId, (object)this.powershellInstanceId); this.closeCmdCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), WSManClientCommandTransportManager.cmdCloseCallback); WSManNativeApi.WSManCloseCommand(this.wsManCmdOperationHandle, 0, (IntPtr)this.closeCmdCompleted); } }
/// <summary> /// Creates a command transport manager. This will create a new PrioritySendDataCollection which should be used to /// send data to the server. /// </summary> /// <param name="connectionInfo"> /// Connection info to be used for creating the command. /// </param> /// <param name="cmd"> /// Command for which transport manager is created. /// </param> /// <param name="noInput"> /// true if the command has input. /// </param> /// <returns></returns> internal override BaseClientCommandTransportManager CreateClientCommandTransportManager(RunspaceConnectionInfo connectionInfo, ClientRemotePowerShell cmd, bool noInput) { Dbg.Assert(null != cmd, "Cmd cannot be null"); WSManConnectionInfo wsmanConnectionInfo = connectionInfo as WSManConnectionInfo; Dbg.Assert(null != wsmanConnectionInfo, "ConnectionInfo must be WSManConnectionInfo"); WSManClientCommandTransportManager result = new WSManClientCommandTransportManager(wsmanConnectionInfo, _wsManShellOperationHandle, cmd, noInput, this); return result; }
// we need a synchronized add and remove so that multiple threads // update the data store concurrently private static bool TryGetCmdTransportManager(IntPtr operationContext, out WSManClientCommandTransportManager cmdTransportManager, out long cmdTMId) { cmdTMId = operationContext.ToInt64(); cmdTransportManager = null; lock (s_cmdTMHandles) { return s_cmdTMHandles.TryGetValue(cmdTMId, out cmdTransportManager); } }
private static void OnCreateCmdCompleted(IntPtr operationContext, int flags, IntPtr error, IntPtr shellOperationHandle, IntPtr commandOperationHandle, IntPtr operationHandle, IntPtr data) { BaseClientTransportManager.tracer.WriteLine("OnCreateCmdCompleted callback received", new object[0]); long cmdTMId = 0L; WSManClientCommandTransportManager cmdTransportManager = null; if (!TryGetCmdTransportManager(operationContext, out cmdTransportManager, out cmdTMId)) { BaseClientTransportManager.tracer.WriteLine("OnCreateCmdCompleted: Unable to find a transport manager for the command context {0}.", new object[] { cmdTMId }); } else { PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateCommandCallbackReceived, PSOpcode.Connect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { cmdTransportManager.RunspacePoolInstanceId.ToString(), cmdTransportManager.powershellInstanceId.ToString() }); if (cmdTransportManager.createCmdCompleted != null) { cmdTransportManager.createCmdCompletedGCHandle.Free(); cmdTransportManager.createCmdCompleted.Dispose(); cmdTransportManager.createCmdCompleted = null; } cmdTransportManager.wsManCmdOperationHandle = commandOperationHandle; if (IntPtr.Zero != error) { WSManNativeApi.WSManError errorStruct = WSManNativeApi.WSManError.UnMarshal(error); if (errorStruct.errorCode != 0) { BaseClientTransportManager.tracer.WriteLine("OnCreateCmdCompleted callback: WSMan reported an error: {0}", new object[] { errorStruct.errorDetail }); TransportErrorOccuredEventArgs eventArgs = WSManTransportManagerUtils.ConstructTransportErrorEventArgs(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, null, errorStruct, TransportMethodEnum.RunShellCommandEx, RemotingErrorIdStrings.RunShellCommandExCallBackError, new object[] { WSManTransportManagerUtils.ParseEscapeWSManErrorMessage(errorStruct.errorDetail) }); cmdTransportManager.ProcessWSManTransportError(eventArgs); return; } } lock (cmdTransportManager.syncObject) { cmdTransportManager.isCreateCallbackReceived = true; if (cmdTransportManager.isClosed) { BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]); if (cmdTransportManager.isDisconnectPending) { cmdTransportManager.RaiseReadyForDisconnect(); } } else if (cmdTransportManager.isDisconnectPending) { cmdTransportManager.RaiseReadyForDisconnect(); } else { if (cmdTransportManager.serializedPipeline.Length == 0L) { cmdTransportManager.shouldStartReceivingData = true; } cmdTransportManager.SendOneItem(); if (cmdTransportManager.isStopSignalPending) { cmdTransportManager.SendStopSignal(); } } } } }