/// <summary> /// Gets the fragment or if no fragment is available registers the callback which /// gets called once a fragment is available. These 2 steps are performed in a /// synchronized way. /// /// While getting a fragment the following algorithm is used: /// 1. If this is the first time or if the last fragment read is an EndFragment, /// then a new set of fragments is chosen based on the implicit priority. /// PromptResponse is higher in priority order than default. /// 2. If last fragment read is not an EndFragment, then next fragment is chosen from /// the priority collection as the last fragment. This will ensure fragments /// are sent in order. /// </summary> /// <param name="callback"> /// Callback to call once data is available. (This will be used if no data is currently /// available). /// </param> /// <param name="priorityType"> /// Priority stream to which the returned object belongs to, if any. /// If the call does not return any data, the value of this "out" parameter /// is undefined. /// </param> /// <returns> /// A FragmentRemoteObject if available, otherwise null. /// </returns> internal byte[] ReadOrRegisterCallback(OnDataAvailableCallback callback, out DataPriorityType priorityType) { lock (_readSyncObject) { priorityType = DataPriorityType.Default; // Send data from which ever stream that has data directly. byte[] result = null; SerializedDataStream promptDataToBeSent = _dataToBeSent[(int)DataPriorityType.PromptResponse]; if (promptDataToBeSent is not null) { result = promptDataToBeSent.ReadOrRegisterCallback(_onSendCollectionDataAvailable); priorityType = DataPriorityType.PromptResponse; } if (result == null) { SerializedDataStream defaultDataToBeSent = _dataToBeSent[(int)DataPriorityType.Default]; if (defaultDataToBeSent is not null) { result = defaultDataToBeSent.ReadOrRegisterCallback(_onSendCollectionDataAvailable); priorityType = DataPriorityType.Default; } } // No data to return..so register the callback. if (result == null) { // Register callback. _onDataAvailableCallback = callback; } return(result); } }
private void SendData(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Command sending data of size : {0}", new object[] { data.Length }); byte[] buffer = data; bool flag = true; if (commandSendRedirect != null) { object[] objArray3 = new object[2]; objArray3[1] = buffer; object[] args = objArray3; flag = (bool)commandSendRedirect.DynamicInvoke(args); buffer = (byte[])args[0]; } if (flag) { using (WSManNativeApi.WSManData data2 = new WSManNativeApi.WSManData(buffer)) { PSEtwLog.LogAnalyticInformational(PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString(), data2.BufferLength.ToString(CultureInfo.InvariantCulture) }); lock (base.syncObject) { if (base.isClosed) { BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]); } else { this.sendToRemoteCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdSendCallback); WSManNativeApi.WSManSendShellInputEx(this.wsManShellOperationHandle, this.wsManCmdOperationHandle, 0, (priorityType == DataPriorityType.Default) ? "stdin" : "pr", data2, (IntPtr)this.sendToRemoteCompleted, ref this.wsManSendOperationHandle); } } } } }
private void SendOneItem() { DataPriorityType priorityType = DataPriorityType.Default; byte[] data; if (this.serializedPipeline.Length > 0L) { data = this.serializedPipeline.ReadOrRegisterCallback((SerializedDataStream.OnDataAvailableCallback)null); if (this.serializedPipeline.Length == 0L) { this.shouldStartReceivingData = true; } } else { data = this.dataToBeSent.ReadOrRegisterCallback(this.onDataAvailableToSendCallback, out priorityType); } if (data != null) { this.SendData(data, priorityType); } if (!this.shouldStartReceivingData) { return; } this.StartReceivingData(); }
internal void Add <T>(RemoteDataObject <T> data, DataPriorityType priority) { lock (this.syncObjects[(int)priority]) { this.fragmentor.Fragment <T>(data, this.dataToBeSent[(int)priority]); } }
/// <summary> /// Gets the fragment or if no fragment is available registers the callback which /// gets called once a fragment is available. These 2 steps are performed in a /// synchronized way. /// /// While getting a fragment the following algorithm is used: /// 1. If this is the first time or if the last fragment read is an EndFragment, /// then a new set of fragments is chosen based on the implicit priority. /// PromptResponse is higher in priority order than default. /// 2. If last fragment read is not an EndFragment, then next fragment is chosen from /// the priority collection as the last fragment. This will ensure fragments /// are sent in order. /// </summary> /// <param name="callback"> /// Callback to call once data is available. (This will be used if no data is currently /// available). /// </param> /// <param name="priorityType"> /// Priority stream to which the returned object belongs to, if any. /// If the call does not return any data, the value of this "out" parameter /// is undefined. /// </param> /// <returns> /// A FragmentRemoteObject if available, otherwise null. /// </returns> internal byte[] ReadOrRegisterCallback(OnDataAvailableCallback callback, out DataPriorityType priorityType) { lock (_readSyncObject) { priorityType = DataPriorityType.Default; // send data from which ever stream that has data directly. byte[] result = null; result = _dataToBeSent[(int)DataPriorityType.PromptResponse].ReadOrRegisterCallback(_onSendCollectionDataAvailable); priorityType = DataPriorityType.PromptResponse; if (result is null) { result = _dataToBeSent[(int)DataPriorityType.Default].ReadOrRegisterCallback(_onSendCollectionDataAvailable); priorityType = DataPriorityType.Default; } // no data to return..so register the callback. if (result is null) { // register callback. _onDataAvailableCallback = callback; } return(result); } }
/// <summary> /// Process data coming from the transport. This method analyses the data /// and if an object can be created, it creates one and calls the /// <paramref name="callback"/> with the deserialized object. This method /// does not assume all fragments to be available. So if not enough fragments are /// available it will simply return.. /// </summary> /// <param name="data"> /// Data to process. /// </param> /// <param name="priorityType"> /// Priority stream this data belongs to. /// </param> /// <param name="callback"> /// Callback to call once a complete deserialized object is available. /// </param> /// <returns> /// Defragmented Object if any, otherwise null. /// </returns> /// <exception cref="PSRemotingTransportException"> /// 1. Fragment Ids not in sequence /// 2. Object Ids does not match /// 3. The current deserialized object size of the received data exceeded /// allowed maximum object size. The current deserialized object size is {0}. /// Allowed maximum object size is {1}. /// 4.The total data received from the remote machine exceeded allowed maximum. /// The total data received from remote machine is {0}. Allowed maximum is {1}. /// </exception> /// <remarks> /// Might throw other exceptions as the deserialized object is handled here. /// </remarks> internal void ProcessRawData(byte[] data, DataPriorityType priorityType, ReceiveDataCollection.OnDataAvailableCallback callback) { Dbg.Assert(data != null, "Cannot process null data"); try { _defragmentor.DeserializationContext.LogExtraMemoryUsage(data.Length); } catch (System.Xml.XmlException) { PSRemotingTransportException e = null; if (_isCreateByClientTM) { e = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumClient, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumClient, _defragmentor.DeserializationContext.MaximumAllowedMemory.Value); } else { e = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumServer, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumServer, _defragmentor.DeserializationContext.MaximumAllowedMemory.Value); } throw e; } _recvdData[(int)priorityType].ProcessRawData(data, callback); }
private void SendOneItem() { DataPriorityType priorityType = DataPriorityType.Default; byte[] data = this.serializedPipeline.Length <= 0L ? this.dataToBeSent.ReadOrRegisterCallback(this.onDataAvailableToSendCallback, out priorityType) : this.serializedPipeline.ReadOrRegisterCallback((SerializedDataStream.OnDataAvailableCallback)null); if (data == null) { return; } this.SendData(data, priorityType); }
private void SendData(byte[] data, DataPriorityType priorityType) { BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, (object)this.RunspacePoolInstanceId, (object)Guid.Empty, (object)data.Length.ToString((IFormatProvider)CultureInfo.InvariantCulture)); lock (this.syncObject) { if (this.isClosed) { return; } this.stdInWriter.WriteLine(OutOfProcessUtils.CreateDataPacket(data, priorityType, Guid.Empty)); } }
private void SendData(byte[] data, DataPriorityType priorityType) { object[] args = new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString(), data.Length.ToString(CultureInfo.InvariantCulture) }; PSEtwLog.LogAnalyticInformational(PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, args); lock (base.syncObject) { if (!base.isClosed) { this.stdInWriter.WriteLine(OutOfProcessUtils.CreateDataPacket(data, priorityType, base.powershellInstanceId)); } } }
internal static string CreateDataPacket(byte[] data, DataPriorityType streamType, Guid psGuid) { string result = string.Format(CultureInfo.InvariantCulture, "<{0} {1}='{2}' {3}='{4}'>{5}</{0}>", PS_OUT_OF_PROC_DATA_TAG, PS_OUT_OF_PROC_STREAM_ATTRIBUTE, streamType.ToString(), PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, psGuid.ToString(), Convert.ToBase64String(data)); return result; }
/// <summary> /// Adds data to this collection. The data is fragmented in this method /// before being stored into the collection. So the calling thread /// will get affected, if it tries to add a huge object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"> /// data to be added to the collection. Caller should make sure this is not /// null. /// </param> /// <param name="priority"> /// Priority of the data. /// </param> internal void Add <T>(RemoteDataObject <T> data, DataPriorityType priority) { Dbg.Assert(data != null, "Cannot send null data object"); Dbg.Assert(_fragmentor != null, "Fragmentor cannot be null while adding objects"); Dbg.Assert(_dataToBeSent != null, "Serialized streams are not initialized"); // make sure the only one object is fragmented and added to the collection // at any give time. This way the order of fragment is maintained // in the SendDataCollection(s). lock (_dataSyncObjects[(int)priority]) { _fragmentor.Fragment <T>(data, _dataToBeSent[(int)priority]); } }
private void SendOneItem() { byte[] data = null; DataPriorityType priorityType = DataPriorityType.Default; if (base.serializedPipeline.Length > 0L) { data = base.serializedPipeline.ReadOrRegisterCallback(null); } else { data = base.dataToBeSent.ReadOrRegisterCallback(this.onDataAvailableToSendCallback, out priorityType); } if (data != null) { this.SendData(data, priorityType); } }
internal byte[] ReadOrRegisterCallback(OnDataAvailableCallback callback, out DataPriorityType priorityType) { lock (this.readSyncObject) { priorityType = DataPriorityType.Default; byte[] buffer = null; buffer = this.dataToBeSent[1].ReadOrRegisterCallback(this.onSendCollectionDataAvailable); priorityType = DataPriorityType.PromptResponse; if (buffer == null) { buffer = this.dataToBeSent[0].ReadOrRegisterCallback(this.onSendCollectionDataAvailable); priorityType = DataPriorityType.Default; } if (buffer == null) { this.onDataAvailableCallback = callback; } return(buffer); } }
internal void ProcessRawData(byte[] data, DataPriorityType priorityType, ReceiveDataCollection.OnDataAvailableCallback callback) { try { this.defragmentor.DeserializationContext.LogExtraMemoryUsage(data.Length); } catch (XmlException) { PSRemotingTransportException exception = null; if (this.isCreateByClientTM) { exception = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumClient, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumClient, new object[] { this.defragmentor.DeserializationContext.MaximumAllowedMemory.Value }); } else { exception = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumServer, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumServer, new object[] { this.defragmentor.DeserializationContext.MaximumAllowedMemory.Value }); } throw exception; } this.recvdData[(int)priorityType].ProcessRawData(data, callback); }
internal void ProcessRawData(byte[] data, DataPriorityType priorityType, ReceiveDataCollection.OnDataAvailableCallback callback) { try { this.defragmentor.DeserializationContext.LogExtraMemoryUsage(data.Length); } catch (XmlException) { PSRemotingTransportException exception = null; if (this.isCreateByClientTM) { exception = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumClient, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumClient, new object[] { this.defragmentor.DeserializationContext.MaximumAllowedMemory.Value }); } else { exception = new PSRemotingTransportException(PSRemotingErrorId.ReceivedDataSizeExceededMaximumServer, RemotingErrorIdStrings.ReceivedDataSizeExceededMaximumServer, new object[] { this.defragmentor.DeserializationContext.MaximumAllowedMemory.Value }); } throw exception; } this.recvdData[(int) priorityType].ProcessRawData(data, callback); }
internal void ProcessRawData(byte[] data, string stream, System.Management.Automation.Remoting.ReceiveDataCollection.OnDataAvailableCallback dataAvailableCallback) { baseTracer.WriteLine("Processing incoming data for stream {0}.", new object[] { stream }); bool flag = false; DataPriorityType priorityType = DataPriorityType.Default; if (stream.Equals("stdin", StringComparison.OrdinalIgnoreCase) || stream.Equals("stdout", StringComparison.OrdinalIgnoreCase)) { flag = true; } else if (stream.Equals("pr", StringComparison.OrdinalIgnoreCase)) { priorityType = DataPriorityType.PromptResponse; flag = true; } if (!flag) { baseTracer.WriteLine("{0} is not a valid stream", new object[] { stream }); } this.recvdData.ProcessRawData(data, priorityType, dataAvailableCallback); }
internal void SendOneItem() { if (this.isDisconnectPending) { base.RaiseReadyForDisconnect(); } else { byte[] data = null; DataPriorityType priorityType = DataPriorityType.Default; if (base.serializedPipeline.Length > 0L) { data = base.serializedPipeline.ReadOrRegisterCallback(null); if (base.serializedPipeline.Length == 0L) { this.shouldStartReceivingData = true; } } else if (this.chunkToSend != null) { data = this.chunkToSend.Data; priorityType = this.chunkToSend.Type; this.chunkToSend = null; } else { data = base.dataToBeSent.ReadOrRegisterCallback(this.onDataAvailableToSendCallback, out priorityType); } if (data != null) { this.isSendingInput = true; this.SendData(data, priorityType); } if (this.shouldStartReceivingData) { this.StartReceivingData(); } } }
private void SendData(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Session sending data of size : {0}", (object)data.Length); byte[] data1 = data; bool flag = true; if ((object)WSManClientSessionTransportManager.sessionSendRedirect != null) { object[] objArray = new object[2] { null, (object)data1 }; flag = (bool)WSManClientSessionTransportManager.sessionSendRedirect.DynamicInvoke(objArray); data1 = (byte[])objArray[0]; } if (!flag) { return; } using (WSManNativeApi.WSManData streamData = new WSManNativeApi.WSManData(data1)) { BaseTransportManager.ETWTracer.AnalyticChannel.WriteInformation(PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, (object)this.RunspacePoolInstanceId, (object)Guid.Empty, (object)streamData.BufferLength.ToString((IFormatProvider)CultureInfo.InvariantCulture)); lock (this.syncObject) { if (this.isClosed) { BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]); } else { this.sendToRemoteCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.sessionContextID), WSManClientSessionTransportManager.sessionSendCallback); WSManNativeApi.WSManSendShellInputEx(this.wsManShellOperationHandle, IntPtr.Zero, 0, priorityType == DataPriorityType.Default ? "stdin" : "pr", streamData, (IntPtr)this.sendToRemoteCompleted, ref this.wsManSendOperationHandle); } } } }
internal static string CreateDataPacket(byte[] data, DataPriorityType streamType, Guid psGuid) { return string.Format(CultureInfo.InvariantCulture, "<{0} {1}='{2}' {3}='{4}'>{5}</{0}>", new object[] { "Data", "Stream", streamType.ToString(), "PSGuid", psGuid.ToString(), Convert.ToBase64String(data) }); }
private void SendData(byte[] data, DataPriorityType priorityType) { tracer.WriteLine("Command sending data of size : {0}", data.Length); byte[] package = data; #region SHIM: Redirection code for command data send. bool sendContinue = true; if (s_commandSendRedirect != null) { object[] arguments = new object[2] { null, package }; sendContinue = (bool)s_commandSendRedirect.DynamicInvoke(arguments); package = (byte[])arguments[0]; } if (!sendContinue) return; #endregion using (WSManNativeApi.WSManData_ManToUn serializedContent = new WSManNativeApi.WSManData_ManToUn(package)) { PSEtwLog.LogAnalyticInformational( PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, RunspacePoolInstanceId.ToString(), powershellInstanceId.ToString(), serializedContent.BufferLength.ToString(CultureInfo.InvariantCulture)); lock (syncObject) { // make sure the transport is not closed. if (isClosed) { tracer.WriteLine("Client Session TM: Transport manager is closed. So returning"); return; } // send callback _sendToRemoteCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(_cmdContextId), s_cmdSendCallback); WSManNativeApi.WSManSendShellInputEx(_wsManShellOperationHandle, _wsManCmdOperationHandle, 0, priorityType == DataPriorityType.Default ? WSManNativeApi.WSMAN_STREAM_ID_STDIN : WSManNativeApi.WSMAN_STREAM_ID_PROMPTRESPONSE, serializedContent, _sendToRemoteCompleted, ref _wsManSendOperationHandle); } } }
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType) { Dbg.Assert(null != data, "data cannot be null in the data available callback"); tracer.WriteLine("Received data from dataToBeSent store."); Dbg.Assert(_chunkToSend == null, "data callback received while a chunk is pending to be sent"); _chunkToSend = new SendDataChunk(data, priorityType); SendOneItem(); }
public SendDataChunk(byte[] data, DataPriorityType type) { Data = data; Type = type; }
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType) { Dbg.Assert(null != data, "data cannot be null in the data available callback"); tracer.WriteLine("Received data to be sent from the callback."); SendData(data, priorityType); }
internal static string CreateDataPacket(byte[] data, DataPriorityType streamType, Guid psGuid) { return(string.Format(CultureInfo.InvariantCulture, "<{0} {1}='{2}' {3}='{4}'>{5}</{0}>", new object[] { "Data", "Stream", streamType.ToString(), "PSGuid", psGuid.ToString(), Convert.ToBase64String(data) })); }
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Received data from dataToBeSent store.", new object[0]); this.SendData(data, priorityType); }
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Received data to be sent from the callback.", new object[0]); this.SendData(data, priorityType); }
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Received data from dataToBeSent store.", new object[0]); this.chunkToSend = new SendDataChunk(data, priorityType); this.SendOneItem(); }
private void SendData(byte[] data, DataPriorityType priorityType) { BaseClientTransportManager.tracer.WriteLine("Command sending data of size : {0}", new object[] { data.Length }); byte[] buffer = data; bool flag = true; if (commandSendRedirect != null) { object[] objArray3 = new object[2]; objArray3[1] = buffer; object[] args = objArray3; flag = (bool) commandSendRedirect.DynamicInvoke(args); buffer = (byte[]) args[0]; } if (flag) { using (WSManNativeApi.WSManData data2 = new WSManNativeApi.WSManData(buffer)) { PSEtwLog.LogAnalyticInformational(PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString(), this.powershellInstanceId.ToString(), data2.BufferLength.ToString(CultureInfo.InvariantCulture) }); lock (base.syncObject) { if (base.isClosed) { BaseClientTransportManager.tracer.WriteLine("Client Session TM: Transport manager is closed. So returning", new object[0]); } else { this.sendToRemoteCompleted = new WSManNativeApi.WSManShellAsync(new IntPtr(this.cmdContextId), cmdSendCallback); WSManNativeApi.WSManSendShellInputEx(this.wsManShellOperationHandle, this.wsManCmdOperationHandle, 0, (priorityType == DataPriorityType.Default) ? "stdin" : "pr", data2, (IntPtr) this.sendToRemoteCompleted, ref this.wsManSendOperationHandle); } } } } }
private void SendData(byte[] data, DataPriorityType priorityType) { PSEtwLog.LogAnalyticInformational( PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, RunspacePoolInstanceId.ToString(), Guid.Empty.ToString(), data.Length.ToString(CultureInfo.InvariantCulture)); lock (syncObject) { if (isClosed) { return; } stdInWriter.WriteLine(OutOfProcessUtils.CreateDataPacket(data, priorityType, Guid.Empty)); } }
internal void SendDataAsync(PSObject data, DataPriorityType priority) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod()) this.transportManager.DataToBeSentCollection.Add <PSObject>(RemoteDataObject <PSObject> .CreateFrom(RemotingDestination.Server, RemotingDataType.InvalidDataType, this.clientRunspacePoolId, Guid.Empty, data)); }
internal void SendDataAsync <T>(RemoteDataObject <T> data, DataPriorityType priority) { this.transportManager.DataToBeSentCollection.Add <T>(data, priority); }
public SendDataChunk(byte[] data, DataPriorityType type) { this.data = data; this.type = type; }
internal void SendDataAsync(PSObject data, DataPriorityType priority) { RemoteDataObject <PSObject> obj2 = RemoteDataObject <PSObject> .CreateFrom(RemotingDestination.InvalidDestination | RemotingDestination.Server, RemotingDataType.InvalidDataType, this.clientRunspacePoolId, Guid.Empty, data); this.transportManager.DataToBeSentCollection.Add <PSObject>(obj2); }
internal void SendDataAsync <T>(RemoteDataObject <T> data, DataPriorityType priority) { using (ClientRunspacePoolDataStructureHandler.tracer.TraceMethod()) this.transportManager.DataToBeSentCollection.Add <T>(data, priority); }
internal static string CreateDataPacket(byte[] data, DataPriorityType streamType, Guid psGuid) => string.Format((IFormatProvider)CultureInfo.InvariantCulture, "<{0} {1}='{2}' {3}='{4}'>{5}</{0}>", (object)"Data", (object)"Stream", (object)streamType.ToString(), (object)"PSGuid", (object)psGuid.ToString(), (object)Convert.ToBase64String(data));