internal ReceiveDataCollection(Fragmentor defragmentor, bool createdByClientTM) { this.pendingDataStream = new MemoryStream(); this.syncObject = new object(); this.defragmentor = defragmentor; this.isCreateByClientTM = createdByClientTM; }
/// <summary> /// Creates a RemoteDataObject by deserializing <paramref name="data"/>. /// </summary> /// <param name="serializedDataStream"></param> /// <param name="defragmentor"> /// Defragmentor used to deserialize an object. /// </param> /// <returns></returns> internal static RemoteDataObject <T> CreateFrom(Stream serializedDataStream, Fragmentor defragmentor) { Dbg.Assert(serializedDataStream != null, "cannot construct a RemoteDataObject from null data"); Dbg.Assert(defragmentor != null, "defragmentor cannot be null."); if ((serializedDataStream.Length - serializedDataStream.Position) < headerLength) { PSRemotingTransportException e = new PSRemotingTransportException(PSRemotingErrorId.NotEnoughHeaderForRemoteDataObject, RemotingErrorIdStrings.NotEnoughHeaderForRemoteDataObject, headerLength + FragmentedRemoteObject.HeaderLength); throw e; } RemotingDestination destination = (RemotingDestination)DeserializeUInt(serializedDataStream); RemotingDataType dataType = (RemotingDataType)DeserializeUInt(serializedDataStream); Guid runspacePoolId = DeserializeGuid(serializedDataStream); Guid powerShellId = DeserializeGuid(serializedDataStream); object actualData = null; if ((serializedDataStream.Length - headerLength) > 0) { actualData = defragmentor.DeserializeToPSObject(serializedDataStream); } T deserializedObject = (T)LanguagePrimitives.ConvertTo(actualData, typeof(T), System.Globalization.CultureInfo.CurrentCulture); return(new RemoteDataObject <T>(destination, dataType, runspacePoolId, powerShellId, deserializedObject)); }
internal static RemoteDataObject <T> CreateFrom( Stream serializedDataStream, Fragmentor defragmentor) { if (serializedDataStream.Length - serializedDataStream.Position < 40L) { throw new PSRemotingTransportException(PSRemotingErrorId.NotEnoughHeaderForRemoteDataObject, new object[1] { (object)61 }); } RemotingDestination destination = (RemotingDestination)RemoteDataObject <T> .DeserializeUInt(serializedDataStream); RemotingDataType dataType = (RemotingDataType)RemoteDataObject <T> .DeserializeUInt(serializedDataStream); Guid runspacePoolId = RemoteDataObject <T> .DeserializeGuid(serializedDataStream); Guid powerShellId = RemoteDataObject <T> .DeserializeGuid(serializedDataStream); object valueToConvert = (object)null; if (serializedDataStream.Length - 40L > 0L) { valueToConvert = (object)defragmentor.DeserializeToPSObject(serializedDataStream); } T data = (T)LanguagePrimitives.ConvertTo(valueToConvert, typeof(T), (IFormatProvider)CultureInfo.CurrentCulture); return(new RemoteDataObject <T>(destination, dataType, runspacePoolId, powerShellId, data)); }
protected BaseTransportManager(PSRemotingCryptoHelper cryptoHelper) { this.cryptoHelper = cryptoHelper; this.fragmentor = new Fragmentor(32768, cryptoHelper); this.recvdData = new PriorityReceiveDataCollection(this.fragmentor, this is BaseClientTransportManager); this.onDataAvailableCallback = new ReceiveDataCollection.OnDataAvailableCallback(this.OnDataAvailableCallback); }
internal virtual void Serialize(Stream streamToWriteTo, Fragmentor fragmentor) { this.SerializeHeader(streamToWriteTo); if (this.data != null) { fragmentor.SerializeToBytes(this.data, streamToWriteTo); } }
internal virtual void Serialize(Stream streamToWriteTo, Fragmentor fragmentor) { this.SerializeHeader(streamToWriteTo); if ((object)this.data == null) { return; } fragmentor.SerializeToBytes((object)this.data, streamToWriteTo); }
/// <summary> /// Construct a priority receive data collection /// </summary> /// <param name="defragmentor">Defragmentor used to deserialize an object.</param> /// <param name="createdByClientTM"> /// True if a client transport manager created this collection. /// This is used to generate custom messages for server and client. /// </param> internal PriorityReceiveDataCollection(Fragmentor defragmentor, bool createdByClientTM) { _defragmentor = defragmentor; string[] names = Enum.GetNames(typeof(DataPriorityType)); _recvdData = new ReceiveDataCollection[names.Length]; for (int index = 0; index < names.Length; index++) { _recvdData[index] = new ReceiveDataCollection(defragmentor, createdByClientTM); } _isCreateByClientTM = createdByClientTM; }
internal PriorityReceiveDataCollection(Fragmentor defragmentor, bool createdByClientTM) { this.defragmentor = defragmentor; string[] names = Enum.GetNames(typeof(DataPriorityType)); this.recvdData = new ReceiveDataCollection[names.Length]; for (int i = 0; i < names.Length; i++) { this.recvdData[i] = new ReceiveDataCollection(defragmentor, createdByClientTM); } this.isCreateByClientTM = createdByClientTM; }
/// <summary> /// Serializes the object into the stream specified. The serialization mechanism uses /// UTF8 encoding to encode data. /// </summary> /// <param name="streamToWriteTo"></param> /// <param name="fragmentor"> /// fragmentor used to serialize and fragment the object. /// </param> internal virtual void Serialize(Stream streamToWriteTo, Fragmentor fragmentor) { Dbg.Assert(streamToWriteTo != null, "Stream to write to cannot be null."); Dbg.Assert(fragmentor != null, "Fragmentor cannot be null."); SerializeHeader(streamToWriteTo); if (Data != null) { fragmentor.SerializeToBytes(Data, streamToWriteTo); } return; }
/// <summary> /// </summary> /// <param name="defragmentor"> /// Defragmentor used to deserialize an object. /// </param> /// <param name="createdByClientTM"> /// True if a client transport manager created this collection. /// This is used to generate custom messages for server and client. /// </param> internal ReceiveDataCollection(Fragmentor defragmentor, bool createdByClientTM) { Dbg.Assert(defragmentor != null, "ReceiveDataCollection needs a defragmentor to work with"); // Memory streams created with an unsigned byte array provide a non-resizable stream view // of the data, and can only be written to. When using a byte array, you can neither append // to nor shrink the stream, although you might be able to modify the existing contents // depending on the parameters passed into the constructor. Empty memory streams are // resizable, and can be written to and read from. _pendingDataStream = new MemoryStream(); _syncObject = new object(); _defragmentor = defragmentor; _isCreateByClientTM = createdByClientTM; }
internal static RemoteDataObject <T> CreateFrom(Stream serializedDataStream, Fragmentor defragmentor) { if ((serializedDataStream.Length - serializedDataStream.Position) < 40L) { PSRemotingTransportException exception = new PSRemotingTransportException(PSRemotingErrorId.NotEnoughHeaderForRemoteDataObject, RemotingErrorIdStrings.NotEnoughHeaderForRemoteDataObject, new object[] { 0x3d }); throw exception; } RemotingDestination destination = (RemotingDestination)RemoteDataObject <T> .DeserializeUInt(serializedDataStream); RemotingDataType dataType = (RemotingDataType)RemoteDataObject <T> .DeserializeUInt(serializedDataStream); Guid runspacePoolId = RemoteDataObject <T> .DeserializeGuid(serializedDataStream); Guid powerShellId = RemoteDataObject <T> .DeserializeGuid(serializedDataStream); object valueToConvert = null; if ((serializedDataStream.Length - 40L) > 0L) { valueToConvert = defragmentor.DeserializeToPSObject(serializedDataStream); } return(new RemoteDataObject <T>(destination, dataType, runspacePoolId, powerShellId, (T)LanguagePrimitives.ConvertTo(valueToConvert, typeof(T), CultureInfo.CurrentCulture))); }
internal ReceiveDataCollection(Fragmentor defragmentor, bool createdByClientTM) { this.defragmentor = defragmentor; this.isCreateByClientTM = createdByClientTM; }
internal void ExecuteConnect(byte[] connectData, out byte[] connectResponseData) { RemoteSessionCapability sessionCapability; connectResponseData = null; Fragmentor fragmentor = new Fragmentor(0x7fffffff, null); Fragmentor defragmentor = fragmentor; int length = connectData.Length; if (length < 0x15) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } FragmentedRemoteObject.GetFragmentId(connectData, 0); bool isStartFragment = FragmentedRemoteObject.GetIsStartFragment(connectData, 0); bool isEndFragment = FragmentedRemoteObject.GetIsEndFragment(connectData, 0); int blobLength = FragmentedRemoteObject.GetBlobLength(connectData, 0); if (blobLength > (length - 0x15)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (!isStartFragment || !isEndFragment) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } RemoteSessionState state = this.SessionDataStructureHandler.StateMachine.State; if ((state != RemoteSessionState.Established) && (state != RemoteSessionState.EstablishedAndKeyExchanged)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } MemoryStream serializedDataStream = new MemoryStream(); serializedDataStream.Write(connectData, 0x15, blobLength); serializedDataStream.Seek(0L, SeekOrigin.Begin); RemoteDataObject <PSObject> obj2 = RemoteDataObject <PSObject> .CreateFrom(serializedDataStream, defragmentor); if (obj2 == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if ((obj2.Destination != (RemotingDestination.InvalidDestination | RemotingDestination.Server)) || (obj2.DataType != RemotingDataType.SessionCapability)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } int num3 = (length - 0x15) - blobLength; if (num3 < 0x15) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } byte[] destinationArray = new byte[num3]; Array.Copy(connectData, 0x15 + blobLength, destinationArray, 0, num3); FragmentedRemoteObject.GetFragmentId(destinationArray, 0); isStartFragment = FragmentedRemoteObject.GetIsStartFragment(destinationArray, 0); isEndFragment = FragmentedRemoteObject.GetIsEndFragment(destinationArray, 0); blobLength = FragmentedRemoteObject.GetBlobLength(destinationArray, 0); if (blobLength != (num3 - 0x15)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (!isStartFragment || !isEndFragment) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } serializedDataStream = new MemoryStream(); serializedDataStream.Write(destinationArray, 0x15, blobLength); serializedDataStream.Seek(0L, SeekOrigin.Begin); RemoteDataObject <PSObject> obj3 = RemoteDataObject <PSObject> .CreateFrom(serializedDataStream, defragmentor); if (obj3 == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } if ((obj3.Destination != (RemotingDestination.InvalidDestination | RemotingDestination.Server)) || (obj3.DataType != RemotingDataType.ConnectRunspacePool)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } try { sessionCapability = RemotingDecoder.GetSessionCapability(obj2.Data); } catch (PSRemotingDataStructureException) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } try { this.RunServerNegotiationAlgorithm(sessionCapability, true); } catch (PSRemotingDataStructureException exception) { throw exception; } int minRunspaces = -1; int maxRunspaces = -1; bool flag3 = false; if ((obj3.Data.Properties["MinRunspaces"] != null) && (obj3.Data.Properties["MinRunspaces"] != null)) { try { minRunspaces = RemotingDecoder.GetMinRunspaces(obj3.Data); maxRunspaces = RemotingDecoder.GetMaxRunspaces(obj3.Data); flag3 = true; } catch (PSRemotingDataStructureException) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } } if (flag3 && (((minRunspaces == -1) || (maxRunspaces == -1)) || (minRunspaces > maxRunspaces))) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (this._runspacePoolDriver == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } if (obj3.RunspacePoolId != this._runspacePoolDriver.InstanceId) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if ((flag3 && (this._runspacePoolDriver.RunspacePool.GetMaxRunspaces() != maxRunspaces)) && (this._runspacePoolDriver.RunspacePool.GetMinRunspaces() != minRunspaces)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnMismatchedRunspacePoolProperties); } RemoteDataObject obj4 = RemotingEncoder.GenerateServerSessionCapability(this._context.ServerCapability, this._runspacePoolDriver.InstanceId); RemoteDataObject obj5 = RemotingEncoder.GenerateRunspacePoolInitData(this._runspacePoolDriver.InstanceId, this._runspacePoolDriver.RunspacePool.GetMaxRunspaces(), this._runspacePoolDriver.RunspacePool.GetMinRunspaces()); SerializedDataStream streamToWriteTo = new SerializedDataStream(0x1000); streamToWriteTo.Enter(); obj4.Serialize(streamToWriteTo, fragmentor); streamToWriteTo.Exit(); streamToWriteTo.Enter(); obj5.Serialize(streamToWriteTo, fragmentor); streamToWriteTo.Exit(); byte[] buffer2 = streamToWriteTo.Read(); streamToWriteTo.Dispose(); connectResponseData = buffer2; ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object s) { RemoteSessionStateMachineEventArgs fsmEventArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.ConnectSession); this._sessionDSHandler.StateMachine.RaiseEvent(fsmEventArg); })); this._runspacePoolDriver.DataStructureHandler.ProcessConnect(); }