public object DecodeRpcCallResultMessage(IRpcChannel channel, IRpcObjectRepository localRepository, IRpcObjectRepository remoteRepository, IRpcSerializer serializer, RpcCallResultMessage message, Type resultType) { if (message.Type == RpcMessageType.Exception) { throw new TargetInvocationException((Exception)message.Result.Value); } return(DecodeRpcArgument(channel, localRepository, remoteRepository, serializer, message.Result, resultType)); }
protected RpcServerChannel( IRpcSerializer serializer, IRpcMessageFactory messageFactory, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null, ILoggerFactory loggerFactory = null) : base(serializer, messageFactory, RpcChannelType.Server, localRepository, remoteRepository, loggerFactory) { }
public TcpRpcServerChannel( IRpcSerializer serializer, IRpcMessageFactory messageFactory, IPAddress address, int port, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null, ILoggerFactory loggerFactory = null) : base(serializer, messageFactory, localRepository, remoteRepository, loggerFactory) { _address = address; _port = port; }
public NamedPipeRpcClientChannel( IRpcSerializer serializer, IRpcMessageFactory messageFactory, string pipeName, TokenImpersonationLevel tokenImpersonationLevel = TokenImpersonationLevel.None, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null, ILoggerFactory loggerFactory = null) : base(serializer, messageFactory, localRepository, remoteRepository, loggerFactory) { _pipeName = pipeName; _tokenImpersonationLevel = tokenImpersonationLevel; _logger = loggerFactory?.CreateLogger <NamedPipeRpcClientChannel>(); }
protected RpcChannel(IRpcSerializer serializer, IRpcMessageFactory messageFactory, RpcChannelType channelType, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null, ILoggerFactory loggerFactory = null) { MessageFactory = messageFactory; _channelType = channelType; Serializer = serializer; _logger = loggerFactory?.CreateLogger <RpcChannel <TChannel> >(); _remoteRepository = remoteRepository ?? (() => new RpcObjectRepository(channelType == RpcChannelType.Server)); LocalRepository = localRepository ?? new RpcObjectRepository(channelType == RpcChannelType.Client); }
public NamedPipeRpcServerChannel( IRpcSerializer serializer, IRpcMessageFactory messageFactory, string pipeName, PipeSecurity pipeSecurity = null, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null, ILoggerFactory loggerFactory = null) : base(serializer, messageFactory, localRepository, remoteRepository, loggerFactory) { _pipeName = pipeName; _pipeSecurity = pipeSecurity; _logger = loggerFactory?.CreateLogger <NamedPipeRpcServerChannel>(); }
public object DecodeRpcArgument(IRpcChannel channel, IRpcObjectRepository localRepository, IRpcObjectRepository remoteRepository, IRpcSerializer serializer, RpcArgument argument, Type argumentType) { switch (argument.Type) { case RpcType.Builtin: if (argument.Value == null || argumentType == typeof(void)) { return(null); } if (argumentType.IsAssignableFrom(argument.Value.GetType())) { return(argument.Value); } return(serializer.ChangeType(argument.Value, argumentType)); case RpcType.Proxy: var instanceId = (int)serializer.ChangeType(argument.Value, typeof(int)); var instance = localRepository.GetInstance(instanceId); if (instance == null) { var intfTypes = localRepository.ResolveTypes(argument.TypeId, argumentType); instance = remoteRepository.GetProxyObject(channel, intfTypes, instanceId); } return(instance); case RpcType.Serialized: var type = localRepository.ResolveTypes(argument.TypeId, argumentType)[0]; return(serializer.ChangeType(argument.Value, type)); case RpcType.ObjectArray: var arrayType = localRepository.ResolveTypes(argument.TypeId, argumentType)[0]; var elementType = arrayType?.GetElementType() ?? throw new InvalidOperationException(); var array = Array.CreateInstance(elementType, (int)argument.Value); for (int i = 0; i < array.Length; i++) { array.SetValue(DecodeRpcArgument(channel, localRepository, remoteRepository, serializer, argument.ArrayElements[i], elementType), i); } return(array); default: throw new InvalidDataException(); } }
private async Task <IRpcServerChannel> CreateServer <T>(T instance, ChannelType channelType, IRpcSerializer serializer = null, TokenImpersonationLevel tokenImpersonationLevel = TokenImpersonationLevel.None, IRpcObjectRepository localRepository = null) where T : class { if (serializer == null) { serializer = new BinaryRpcSerializer(); } switch (channelType) { case ChannelType.Tcp: { var server = new TcpRpcServerChannel( serializer, new RpcMessageFactory(), IPAddress.Loopback, 11234, localRepository); if (instance != null) { server.ObjectRepository.RegisterSingleton(instance); } await server.ListenAsync(); return(server); } case ChannelType.NamedPipe: { _pipeName = Guid.NewGuid().ToString(); var server = new NamedPipeRpcServerChannel( serializer, new RpcMessageFactory(), _pipeName, localRepository: localRepository); if (instance != null) { server.ObjectRepository.RegisterSingleton(instance); } await server.ListenAsync(); return(server); } default: throw new NotSupportedException(); } }
private async Task <IRpcClientChannel> CreateClient(ChannelType channelType, IRpcSerializer serializer = null, TokenImpersonationLevel tokenImpersonationLevel = TokenImpersonationLevel.None, IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null) { if (serializer == null) { serializer = new BinaryRpcSerializer(); } switch (channelType) { case ChannelType.Tcp: { var client = new TcpRpcClientChannel( serializer, new RpcMessageFactory(), IPAddress.Loopback, 11234, localRepository, remoteRepository); await client.ConnectAsync(); return(client); } case ChannelType.NamedPipe: { var client = new NamedPipeRpcClientChannel( serializer, new RpcMessageFactory(), _pipeName, tokenImpersonationLevel, localRepository, remoteRepository); await client.ConnectAsync(); return(client); } default: throw new NotSupportedException(); } }
public ChannelInfo(IRpcObjectRepository repository) { Repository = repository; }
public RpcMethodCallMessage CreateMethodCallMessage(ITransportChannel channel, IRpcObjectRepository localRepository, int instanceId, string methodName, Type[] argumentTypes, object[] arguments) { return(new RpcMethodCallMessage { Type = RpcMessageType.CallMethod, CallId = Interlocked.Increment(ref _callId), MethodName = methodName, InstanceId = instanceId, Arguments = arguments .Select((a, idx) => CreateRpcArgument(channel, localRepository, a, argumentTypes[idx])) .ToArray() }); }
public RpcCallResultMessage CreateCallResultMessage(ITransportChannel channel, IRpcObjectRepository localRepository, RpcMethodCallMessage call, MethodInfo calledMethod, object result) { var resultArgument = CreateRpcArgument(channel, localRepository, result, calledMethod.ReturnType); var resultMessage = new RpcCallResultMessage { CallId = call.CallId, Type = RpcMessageType.CallMethodResult, Result = resultArgument }; return(resultMessage); }
private RpcArgument CreateRpcArgument(ITransportChannel channel, IRpcObjectRepository localRepository, object argument, Type argumentType) { string typeid = null; RpcType type = RpcType.Proxy; RpcArgument[] arrayElements = null; if (argument == null || argument is IConvertible) { type = RpcType.Builtin; } else if (argument is Array array) { if ((argument.GetType().GetElementType()?.IsPrimitive ?? false) || argument.GetType().GetElementType() == typeof(string)) { type = RpcType.Builtin; } else { type = RpcType.ObjectArray; // make sure we use the correct interface definition as base type (we might get a specific array here) var arrayTemplate = Array.CreateInstance(argumentType.GetElementType(), 0); typeid = localRepository.CreateTypeId(arrayTemplate); argument = array.Length; var elementType = argumentType.GetElementType(); arrayElements = new RpcArgument[array.Length]; for (int i = 0; i < array.Length; i++) { arrayElements[i] = CreateRpcArgument(channel, localRepository, array.GetValue(i), elementType); } } } else if (argumentType != typeof(object) && !argumentType.IsSubclassOf(typeof(Delegate)) && (argumentType.GetCustomAttribute <SerializableAttribute>() != null || (argument.GetType().GetCustomAttribute <SerializableAttribute>() != null))) { type = RpcType.Serialized; typeid = localRepository.CreateTypeId(argument); } else { typeid = localRepository.CreateTypeId(argument); if (argument is IRpcObjectProxy proxy) { /*var instance = localRepository.GetInstance(proxy.LocalInstanceId); * if (instance != null) // todo this check is not necessary * { * argument = proxy.LocalInstanceId; * }*/ argument = proxy.RemoteInstanceId; } else { argument = localRepository.AddInstance(argumentType, argument, channel).InstanceId; } } return(new RpcArgument { Type = type, Value = argument, TypeId = typeid, ArrayElements = arrayElements }); }