// closes all the active command sessions. private void CloseAndClearCommandSessions( Exception reasonForClose) { Collection <WSManPluginCommandSession> copyCmdSessions = new Collection <WSManPluginCommandSession>(); lock (shellSyncObject) { Dictionary <IntPtr, WSManPluginCommandSession> .Enumerator cmdEnumerator = _activeCommandSessions.GetEnumerator(); while (cmdEnumerator.MoveNext()) { copyCmdSessions.Add(cmdEnumerator.Current.Value); } _activeCommandSessions.Clear(); } // close the command sessions outside of the lock IEnumerator <WSManPluginCommandSession> cmdSessionEnumerator = copyCmdSessions.GetEnumerator(); while (cmdSessionEnumerator.MoveNext()) { WSManPluginCommandSession cmdSession = cmdSessionEnumerator.Current; // we are not interested in session closed events anymore as we are initiating the close // anyway/ cmdSession.SessionClosed -= new EventHandler <EventArgs>(this.HandleCommandSessionClosed); cmdSession.Close(reasonForClose); } copyCmdSessions.Clear(); }
// Create a new command in the shell context. internal void CreateCommand( IntPtr pluginContext, WSManNativeApi.WSManPluginRequest requestDetails, int flags, string commandLine, WSManNativeApi.WSManCommandArgSet arguments) { try { // inbound cmd information is already verified.. so no need to verify here. WSManPluginCommandTransportManager serverCmdTransportMgr = new WSManPluginCommandTransportManager(transportMgr); serverCmdTransportMgr.Initialize(); // Apply quota limits on the command transport manager _remoteSession.ApplyQuotaOnCommandTransportManager(serverCmdTransportMgr); WSManPluginCommandSession mgdCmdSession = new WSManPluginCommandSession(requestDetails, serverCmdTransportMgr, _remoteSession); AddToActiveCmdSessions(mgdCmdSession); mgdCmdSession.SessionClosed += new EventHandler <EventArgs>(this.HandleCommandSessionClosed); mgdCmdSession.shutDownContext = new WSManPluginOperationShutdownContext( pluginContext, creationRequestDetails.unmanagedHandle, mgdCmdSession.creationRequestDetails.unmanagedHandle, false); do { if (!mgdCmdSession.ProcessArguments(arguments)) { WSManPluginInstance.ReportOperationComplete( requestDetails, WSManPluginErrorCodes.InvalidArgSet, StringUtil.Format( RemotingErrorIdStrings.WSManPluginInvalidArgSet, "WSManPluginCommand")); break; } // Report plugin context to WSMan mgdCmdSession.ReportContext(); } while (false); } catch (System.Exception e) { CommandProcessorBase.CheckForSevereException(e); // if there is an exception creating remote session send the message to client. WSManPluginInstance.ReportOperationComplete( requestDetails, WSManPluginErrorCodes.ManagedException, StringUtil.Format( RemotingErrorIdStrings.WSManPluginManagedException, e.Message)); } }
// returns the command session instance for a given command id. // null if not found. internal WSManPluginCommandSession GetCommandSession( IntPtr cmdContext) { lock (shellSyncObject) { WSManPluginCommandSession result = null; _activeCommandSessions.TryGetValue(cmdContext, out result); return(result); } }
// Closes the command and clears associated resources internal void CloseCommandOperation( WSManPluginOperationShutdownContext context) { WSManPluginCommandSession mgdCmdSession = GetCommandSession(context.commandContext); if (null == mgdCmdSession) { // this should never be the case. this will protect the service. return; } // update the internal data store only if this is not receive operation. if (!context.isReceiveOperation) { DeleteFromActiveCmdSessions(mgdCmdSession.creationRequestDetails.unmanagedHandle); } mgdCmdSession.CloseOperation(context, null); }
// adds command session to active command Sessions store and returns the id // at which the session is added. private void AddToActiveCmdSessions( WSManPluginCommandSession newCmdSession) { lock (shellSyncObject) { if (isClosed) { return; } IntPtr key = newCmdSession.creationRequestDetails.unmanagedHandle; Dbg.Assert(IntPtr.Zero != key, "NULL handles should not be provided"); if (!_activeCommandSessions.ContainsKey(key)) { _activeCommandSessions.Add(key, newCmdSession); return; } } }
// Create a new command in the shell context. internal void CreateCommand( IntPtr pluginContext, WSManNativeApi.WSManPluginRequest requestDetails, int flags, string commandLine, WSManNativeApi.WSManCommandArgSet arguments) { try { // inbound cmd information is already verified.. so no need to verify here. WSManPluginCommandTransportManager serverCmdTransportMgr = new WSManPluginCommandTransportManager(transportMgr); serverCmdTransportMgr.Initialize(); // Apply quota limits on the command transport manager _remoteSession.ApplyQuotaOnCommandTransportManager(serverCmdTransportMgr); WSManPluginCommandSession mgdCmdSession = new WSManPluginCommandSession(requestDetails, serverCmdTransportMgr, _remoteSession); AddToActiveCmdSessions(mgdCmdSession); mgdCmdSession.SessionClosed += new EventHandler<EventArgs>(this.HandleCommandSessionClosed); mgdCmdSession.shutDownContext = new WSManPluginOperationShutdownContext( pluginContext, creationRequestDetails.unmanagedHandle, mgdCmdSession.creationRequestDetails.unmanagedHandle, false); do { if (!mgdCmdSession.ProcessArguments(arguments)) { WSManPluginInstance.ReportOperationComplete( requestDetails, WSManPluginErrorCodes.InvalidArgSet, StringUtil.Format( RemotingErrorIdStrings.WSManPluginInvalidArgSet, "WSManPluginCommand")); break; } // Report plugin context to WSMan mgdCmdSession.ReportContext(); } while (false); } catch (System.Exception e) { CommandProcessorBase.CheckForSevereException(e); // if there is an exception creating remote session send the message to client. WSManPluginInstance.ReportOperationComplete( requestDetails, WSManPluginErrorCodes.ManagedException, StringUtil.Format( RemotingErrorIdStrings.WSManPluginManagedException, e.Message)); } }