Esempio n. 1
0
 public void SetResult(PortableObjectBox value)
 {
     lock (synchronization) {
         if (!isResultSet)
         {
             result            = value;
             isExceptionThrown = false;
             isResultSet       = true;
             Thread.MemoryBarrier();
             countdown.Release(int.MaxValue);
         }
     }
 }
 public object HandleInvocation(string action, PortableObjectBox genericArgumentsDto, PortableObjectBox methodArgumentsDto)
 {
     Type[]   genericArguments;
     object[] methodArguments;
     if (portableObjectBoxConverter.TryConvertFromDataTransferObject(genericArgumentsDto, out genericArguments) &&
         portableObjectBoxConverter.TryConvertFromDataTransferObject(methodArgumentsDto, out methodArguments))
     {
         return(HandleInvocation(action, genericArguments, methodArguments));
     }
     else
     {
         throw new PortableException(new Exception("Could not deserialize data in argument dto."));
     }
 }
Esempio n. 3
0
        public async Task <object> Invoke(Guid serviceGuid, string methodName, PortableObjectBox genericArgumentsDto, PortableObjectBox methodArgumentsDto)
        {
            logger.Trace($"Invoke called for service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments.");
            object result = null;

            try {
                Type[]   genericArguments;
                object[] methodArguments;
                if (portableObjectBoxConverter.TryConvertFromDataTransferObject(genericArgumentsDto, out genericArguments) &&
                    portableObjectBoxConverter.TryConvertFromDataTransferObject(methodArgumentsDto, out methodArguments) &&
                    localServiceContainer.TryInvoke(serviceGuid, methodName, genericArguments, methodArguments, out result))
                {
                    logger.Trace($"Invoke succeeded at local service container for service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments.");
                }
                else
                {
                    logger.Trace($"Trying remote invocation for service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments against {remoteInvokables.Count} remote invokables.");
                    bool invocationSuccessful = false;
                    foreach (var remoteInvokable in remoteInvokables)
                    {
                        var invocation = await remoteInvokable.TryRemoteInvoke(serviceGuid, methodName, genericArgumentsDto, methodArgumentsDto);

                        if (invocation.Success)
                        {
                            logger.Trace($"Successfully remotely invoked service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments.");
                            result = invocation.ReturnValue;
                            invocationSuccessful = true;
                            break;
                        }
                    }

                    if (!invocationSuccessful)
                    {
                        logger.Trace($"Could not remotely invoke service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments.");
                        throw new ServiceUnavailableException(serviceGuid, methodName);
                    }
                }
            } catch (Exception e) {
                logger.Trace($"Remove invocation of service {serviceGuid} method {methodName} with {methodArgumentsDto.Length} bytes of arguments threw", e);
                if (e is IPortableObject)
                {
                    result = e;
                }
                else
                {
                    result = new PortableException(e);
                }
            }
            return(result);
        }
        public bool TryInvoke(Guid serviceGuid, string methodName, PortableObjectBox genericArgumentsDto, PortableObjectBox methodArgumentsDto, out object result)
        {
            InvokableServiceContext invokableServiceContext;

            if (!serviceContextsByGuid.TryGetValue(serviceGuid, out invokableServiceContext))
            {
                result = null;
                return(false);
            }
            else
            {
                result = invokableServiceContext.HandleInvocation(methodName, genericArgumentsDto, methodArgumentsDto);
                return(true);
            }
        }
Esempio n. 5
0
 public Task <RemoteInvocationResult> TryRemoteInvoke(Guid serviceGuid, string methodName, PortableObjectBox genericArguments, PortableObjectBox argumentsDto)
 {
     logger.Trace($"Remote invoking service {serviceGuid} method {methodName} with {genericArguments.Length} generic arguments and {argumentsDto.Length} bytes of arguments.");
     if (!remotelyHostedServices.Contains(serviceGuid))
     {
         logger.Trace($"Remote does not have service {serviceGuid}.");
         return(Task.FromResult(new RemoteInvocationResult(false, null)));
     }
     else
     {
         logger.Trace($"Remote has service {serviceGuid}.");
         return(Task.Factory.StartNew(async(throwaway) => {
             var invocationId = availableInvocationIds.TakeUniqueID();
             logger.Trace($"Took iid {invocationId} for service {serviceGuid} method {methodName}.");
             var asyncValueBox = invocationResponseBoxesById.GetOrAdd(invocationId, id => new AsyncValueBoxImpl());
             await messageSender.SendServiceInvocationAsync(invocationId, serviceGuid, methodName, genericArguments, argumentsDto);
             var returnValue = await asyncValueBox.GetResultAsync();
             logger.Trace($"Got result for iid {invocationId} service {serviceGuid} method {methodName}.");
             var removed = invocationResponseBoxesById.Remove(new KeyValuePair <uint, AsyncValueBox>(invocationId, asyncValueBox));
             Trace.Assert(removed, "Failed to remove AsyncValueBox from dict");
             return new RemoteInvocationResult(true, returnValue);
         }, CancellationToken.None, TaskCreationOptions.LongRunning).Unwrap());
     }
 }