File size: 5,864 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Management.Automation.Remoting.Server;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation.Remoting
{
/// <summary>
/// Responsible for routing messages from the server, blocking the callers and
/// then waking them up when there is a response to their message.
/// </summary>
internal class ServerMethodExecutor
{
/// <summary>
/// Default client pipeline id.
/// </summary>
private const long DefaultClientPipelineId = -1;
/// <summary>
/// Client runspace pool id.
/// </summary>
private readonly Guid _clientRunspacePoolId;
/// <summary>
/// Client power shell id.
/// </summary>
private readonly Guid _clientPowerShellId;
/// <summary>
/// Server dispatch table.
/// </summary>
private readonly ServerDispatchTable _serverDispatchTable;
/// <summary>
/// Remote host call data type.
/// </summary>
private readonly RemotingDataType _remoteHostCallDataType;
/// <summary>
/// Transport manager.
/// </summary>
private readonly AbstractServerTransportManager _transportManager;
/// <summary>
/// Constructor for ServerMethodExecutor.
/// </summary>
internal ServerMethodExecutor(
Guid clientRunspacePoolId, Guid clientPowerShellId,
AbstractServerTransportManager transportManager)
{
_clientRunspacePoolId = clientRunspacePoolId;
_clientPowerShellId = clientPowerShellId;
Dbg.Assert(transportManager != null, "Expected transportManager != null");
_transportManager = transportManager;
_remoteHostCallDataType =
clientPowerShellId == Guid.Empty ? RemotingDataType.RemoteHostCallUsingRunspaceHost : RemotingDataType.RemoteHostCallUsingPowerShellHost;
_serverDispatchTable = new ServerDispatchTable();
}
/// <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);
}
/// <summary>
/// Abort all calls.
/// </summary>
internal void AbortAllCalls()
{
_serverDispatchTable.AbortAllCalls();
}
/// <summary>
/// Execute void method.
/// </summary>
internal void ExecuteVoidMethod(RemoteHostMethodId methodId)
{
ExecuteVoidMethod(methodId, Array.Empty<object>());
}
/// <summary>
/// Execute void method.
/// </summary>
internal void ExecuteVoidMethod(RemoteHostMethodId methodId, object[] parameters)
{
Dbg.Assert(parameters != null, "Expected parameters != null");
// Use void call ID so that the call is known to not have a return value.
const long callId = ServerDispatchTable.VoidCallId;
RemoteHostCall remoteHostCall = new RemoteHostCall(callId, methodId, parameters);
// Dispatch the call but don't wait for response since the return value is void.
// TODO: remove redundant data from the RemoteHostCallPacket.
RemoteDataObject<PSObject> dataToBeSent = RemoteDataObject<PSObject>.CreateFrom(RemotingDestination.Client,
_remoteHostCallDataType, _clientRunspacePoolId, _clientPowerShellId,
remoteHostCall.Encode());
// flush is not used here..since this is a void method and server host
// does not expect anything from client..so let the transport manager buffer
// and send as much data as possible.
_transportManager.SendDataToClient(dataToBeSent, false);
}
/// <summary>
/// Execute method.
/// </summary>
internal T ExecuteMethod<T>(RemoteHostMethodId methodId)
{
return ExecuteMethod<T>(methodId, Array.Empty<object>());
}
/// <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();
}
}
}
|