public object HandleAsync(string methodName, byte[] methodHash, object[] args)
        {
            var methodContext = context.GetMethodContext(methodHash.ToByteArray());

            if (methodContext == null)
            {
                throw new ArgumentNullException(nameof(methodContext));
            }

            var    methodInfo = typeof(TActAs).GetRuntimeMethod(methodName, args.Select(a => a.GetType()).ToArray());
            string result     = "";

            result += methodInfo.Name;
            foreach (var a in args)
            {
                result += a.ToString();
            }

            result += handlerArgument.Arg;

            var tcs = new TaskCompletionSource <string>();

            tcs.SetResult(result);

            var task = tcs.GetType().GetRuntimeMethod("get_Task", new Type[0]).Invoke(tcs, null);

            return(task);
        }
Beispiel #2
0
        public object HandleAsync(string methodName, byte[] actionHashBytes, object[] args)
        {
            var watch = Stopwatch.StartNew();

            var stop = ProtocolWatch.Start("handle-async");

            var actionHash    = new ByteArray(actionHashBytes);
            var methodContext = context.GetMethodContext(actionHash);

            CancellationToken cancellationToken;

            if (methodContext.CancellationIndex >= 0)
            {
                cancellationToken = (CancellationToken)args[methodContext.CancellationIndex];
            }
            else
            {
                var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
                cancellationToken = cts.Token;
            }

            stop();

            stop = ProtocolWatch.Start("handle-send-request");

            var actionHashDataContext = new DataContext()
            {
                DataFlag = DataFlag.ActionHash,
                Data     = actionHash,
            };

            var additionalCount = 1;

            var argumentContexts = new DataContext[methodContext.ArgumentIndexes.Length + additionalCount]; // { actionDataContext, dataDataContext };

            argumentContexts[0] = actionHashDataContext;

            for (int i = 0; i < methodContext.ArgumentIndexes.Length; i++)
            {
                argumentContexts[i + additionalCount] = new DataContext()
                {
                    DataFlag = DataFlag.Data,
                    Data     = args[methodContext.ArgumentIndexes[i]],
                };
            }

            var dataContent = new DataContent(argumentContexts);

            var result = protocolHandler.HandleDataAsync(resolverSession, dataContent, cancellationToken); // todo change to Add session

            stop();
            stop = ProtocolWatch.Start("handle-async");

            var tcsType = methodContext.TaskCompletionSourceType;
            var task    = WrapTask(result, tcsType, methodContext.ContextInfo.MethodInfo.ReturnType.GenericTypeArguments.Length > 0);

            stop();
            watch.Stop();
            ts += (int)watch.Elapsed.Ticks;

            return(task);
        }