/// <summary>
        /// Execute method.
        /// </summary>
        internal T ExecuteMethod <T>(RemoteHostMethodId methodId, object[] parameters)
        {
            Dbg.Assert(parameters != null, "Expected parameters != null");

            // Create the method call object.
            long           callId         = _serverDispatchTable.CreateNewCallId();
            RemoteHostCall remoteHostCall = new RemoteHostCall(callId, methodId, parameters);

            RemoteDataObject <PSObject> dataToBeSent = RemoteDataObject <PSObject> .CreateFrom(RemotingDestination.Client,
                                                                                               _remoteHostCallDataType, _clientRunspacePoolId, _clientPowerShellId,
                                                                                               remoteHostCall.Encode());

            // report that execution is pending host response
            _transportManager.SendDataToClient(dataToBeSent, false, true);

            // Wait for response.
            RemoteHostResponse remoteHostResponse = _serverDispatchTable.GetResponse(callId, null);

            // Null means that the response PSObject was not received and there was an error.
            if (remoteHostResponse == null)
            {
                throw RemoteHostExceptions.NewRemoteHostCallFailedException(methodId);
            }

            // Process the response.
            object returnValue = remoteHostResponse.SimulateExecution();

            Dbg.Assert(returnValue is T, "Expected returnValue is T");
            return((T)remoteHostResponse.SimulateExecution());
        }
Beispiel #2
0
        /// <summary>
        /// Execute non void method.
        /// </summary>
        internal RemoteHostResponse ExecuteNonVoidMethod(PSHost clientHost)
        {
            // The clientHost can be null if the user creates a runspace object without providing
            // a host parameter.
            if (clientHost == null)
            {
                throw RemoteHostExceptions.NewNullClientHostException();
            }

            object             targetObject       = this.SelectTargetObject(clientHost);
            RemoteHostResponse remoteHostResponse = this.ExecuteNonVoidMethodOnObject(targetObject);

            return(remoteHostResponse);
        }
Beispiel #3
0
        internal T ExecuteMethod <T>(RemoteHostMethodId methodId, object[] parameters)
        {
            long newCallId = this._serverDispatchTable.CreateNewCallId();

            this._transportManager.SendDataToClient <PSObject>(RemoteDataObject <PSObject> .CreateFrom(RemotingDestination.Client, this._remoteHostCallDataType, this._clientRunspacePoolId, this._clientPowerShellId, new RemoteHostCall(newCallId, methodId, parameters).Encode()), false);
            RemoteHostResponse response = this._serverDispatchTable.GetResponse(newCallId, (RemoteHostResponse)null);

            if (response == null)
            {
                throw RemoteHostExceptions.NewRemoteHostCallFailedException(methodId);
            }
            response.SimulateExecution();
            return((T)response.SimulateExecution());
        }
Beispiel #4
0
        /// <summary>
        /// Execute.
        /// </summary>
        internal void Execute(Action <ErrorRecord> writeErrorAction)
        {
            if (_remoteHostCall.IsVoidMethod)
            {
                ExecuteVoid(writeErrorAction);
            }
            else
            {
                RemotingDataType remotingDataType =
                    _clientPowerShellId == Guid.Empty ? RemotingDataType.RemoteRunspaceHostResponseData : RemotingDataType.RemotePowerShellHostResponseData;

                RemoteHostResponse          remoteHostResponse = _remoteHostCall.ExecuteNonVoidMethod(_clientHost);
                RemoteDataObject <PSObject> dataToBeSent       = RemoteDataObject <PSObject> .CreateFrom(
                    RemotingDestination.Server, remotingDataType, _clientRunspacePoolId,
                    _clientPowerShellId, remoteHostResponse.Encode());

                _transportManager.DataToBeSentCollection.Add <PSObject>(dataToBeSent, DataPriorityType.PromptResponse);
            }
        }
        /// <summary>
        /// Decode object.
        /// </summary>
        internal static object DecodeObject(object obj, Type type)
        {
            if (obj == null)
            {
                return(null);
            }

            Dbg.Assert(type != null, "Expected type != null");
            if (type == typeof(PSObject))
            {
                return(DecodePSObject(obj));
            }
            else if (type == typeof(ProgressRecord))
            {
                return(ProgressRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
            }
            else if (IsKnownType(type))
            {
                return(obj);
            }
            else if (obj is SecureString)
            {
                return(obj);
            }
            else if (obj is PSCredential)
            {
                return(obj);
            }
            else if (obj is PSObject && type == typeof(PSCredential))
            {
                // BUGBUG: The following piece of code is a workaround
                // because custom serialization is busted. If rehydration
                // works correctly then PSCredential should be available
                PSObject     objAsPSObject = (PSObject)obj;
                PSCredential cred          = null;
                try
                {
                    cred = new PSCredential((string)objAsPSObject.Properties["UserName"].Value,
                                            (SecureString)objAsPSObject.Properties["Password"].Value);
                }
                catch (GetValueException)
                {
                    cred = null;
                }

                return(cred);
            }
            else if (obj is int && type.IsEnum)
            {
                return(Enum.ToObject(type, (int)obj));
            }
            else if (obj is string && type == typeof(CultureInfo))
            {
                return(new CultureInfo((string)obj));
            }
            else if (obj is PSObject && type == typeof(Exception))
            {
                return(DecodeException((PSObject)obj));
            }
            else if (obj is PSObject && type == typeof(object[]))
            {
                return(DecodeObjectArray((PSObject)obj));
            }
            else if (obj is PSObject && type.IsArray)
            {
                return(DecodeArray((PSObject)obj, type));
            }
            else if (obj is PSObject && IsCollection(type))
            {
                return(DecodeCollection((PSObject)obj, type));
            }
            else if (obj is PSObject && IsDictionary(type))
            {
                return(DecodeDictionary((PSObject)obj, type));
            }
            else if (obj is PSObject && IsEncodingAllowedForClassOrStruct(type))
            {
                return(DecodeClassOrStruct((PSObject)obj, type));
            }
            else if (obj is PSObject && IsGenericIEnumerableOfInt(type))
            {
                // we cannot create an instance of interface type like IEnumerable
                // Since a Collection implements IEnumerable, falling back to use
                // that.
                return(DecodeCollection((PSObject)obj, typeof(Collection <int>)));
            }
            else if (obj is PSObject && type == typeof(RemoteHostCall))
            {
                return(RemoteHostCall.Decode((PSObject)obj));
            }
            else if (obj is PSObject && type == typeof(RemoteHostResponse))
            {
                return(RemoteHostResponse.Decode((PSObject)obj));
            }
            else
            {
                throw RemoteHostExceptions.NewRemoteHostDataDecodingNotSupportedException(type);
            }
        }
 /// <summary>
 /// Handle remote host response from client.
 /// </summary>
 internal void HandleRemoteHostResponseFromClient(RemoteHostResponse remoteHostResponse)
 {
     Dbg.Assert(remoteHostResponse != null, "Expected remoteHostResponse != null");
     _serverDispatchTable.SetResponse(remoteHostResponse.CallId, remoteHostResponse);
 }
 internal void SendHostResponseToServer(RemoteHostResponse hostResponse)
 {
     RemoteDataObject<PSObject> data = RemoteDataObject<PSObject>.CreateFrom(RemotingDestination.InvalidDestination | RemotingDestination.Server, RemotingDataType.RemotePowerShellHostResponseData, this.clientRunspacePoolId, this.clientPowerShellId, hostResponse.Encode());
     this.transportManager.DataToBeSentCollection.Add<PSObject>(data, DataPriorityType.PromptResponse);
 }
Beispiel #8
0
 internal static object DecodeObject(object obj, Type type)
 {
     if (obj == null)
     {
         return(obj);
     }
     if (type == typeof(PSObject))
     {
         return(DecodePSObject(obj));
     }
     if (type == typeof(System.Management.Automation.Runspaces.Runspace))
     {
         return(RemoteRunspace.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
     }
     if (type == typeof(ProgressRecord))
     {
         return(ProgressRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
     }
     if (IsKnownType(type))
     {
         return(obj);
     }
     if (obj is SecureString)
     {
         return(obj);
     }
     if (obj is PSCredential)
     {
         return(obj);
     }
     if ((obj is PSObject) && (type == typeof(PSCredential)))
     {
         PSObject obj2 = (PSObject)obj;
         try
         {
             return(new PSCredential((string)obj2.Properties["UserName"].Value, (SecureString)obj2.Properties["Password"].Value));
         }
         catch (GetValueException)
         {
             return(null);
         }
     }
     if ((obj is int) && type.IsEnum)
     {
         return(Enum.ToObject(type, (int)obj));
     }
     if ((obj is string) && (type == typeof(CultureInfo)))
     {
         return(new CultureInfo((string)obj));
     }
     if ((obj is PSObject) && (type == typeof(Exception)))
     {
         return(DecodeException((PSObject)obj));
     }
     if ((obj is PSObject) && (type == typeof(object[])))
     {
         return(DecodeObjectArray((PSObject)obj));
     }
     if ((obj is PSObject) && type.IsArray)
     {
         return(DecodeArray((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsCollection(type))
     {
         return(DecodeCollection((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsDictionary(type))
     {
         return(DecodeDictionary((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsEncodingAllowedForClassOrStruct(type))
     {
         return(DecodeClassOrStruct((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsGenericIEnumerableOfInt(type))
     {
         return(DecodeCollection((PSObject)obj, typeof(Collection <int>)));
     }
     if ((obj is PSObject) && (type == typeof(RemoteHostCall)))
     {
         return(RemoteHostCall.Decode((PSObject)obj));
     }
     if (!(obj is PSObject) || (type != typeof(RemoteHostResponse)))
     {
         throw RemoteHostExceptions.NewRemoteHostDataDecodingNotSupportedException(type);
     }
     return(RemoteHostResponse.Decode((PSObject)obj));
 }
 internal void HandleRemoteHostResponseFromClient(RemoteHostResponse remoteHostResponse)
 {
     this._serverDispatchTable.SetResponse(remoteHostResponse.CallId, remoteHostResponse);
 }