Ejemplo n.º 1
0
        async void IRequestHandler.HandleRequest(IRequest req)
        {
            Exception  ex     = null;
            object     result = null;
            MethodCall call   = null;

            try
            {
                call = _serializer.DeserializeCall(new MemoryStream(req.Data), _binder, _selector);
            }
            catch (Exception e)
            {
                ex = e;
            }
            if (call != null)
            {
                object res = null;
                try
                {
                    res = call.Method.Invoke(call.Target, call.Arguments);
                }
                catch (Exception e)
                {
                    ex = e;
                }
                if (res != null)
                {
                    var task = res as Task;
                    if (task == null)
                    {
                        result = res;
                    }
                    else
                    {
                        await task;
                        if (call.Method.ReturnType != typeof(Task))
                        {
                            result = task.GetType().GetProperty("Result").GetValue(task);
                        }
                    }
                }
            }

            byte[] response = null;
            if (ex == null)
            {
                try
                {
                    response = Serialize(s => _serializer.SerializeResult(s, result));
                }
                catch (Exception e)
                {
                    ex = e;
                }
            }
            if (ex != null)
            {
                response = Serialize(s => _serializer.SerializeException(s, ex.ToString()));
            }
            try
            {
                await req.RespondAsync(response);
            }
            catch
            {
                //TODO: redirect it somewhere?
            }
        }
Ejemplo n.º 2
0
        async Task IRequestHandler.HandleRequest(IRequest req)
        {
            Exception  ex     = null;
            object     result = null;
            MethodCall call   = null;

            try
            {
                if (req.Data is RecyclableMemoryStream || req.Data is MemoryStream)
                {
                    call = _serializer.DeserializeCall(req.Data, _binder, _selector, req.Context);
                }
                else
                {
                    using var copy = new RecyclableMemoryStream(StreamPool.Shared);
                    await req.Data.CopyToAsync(copy);

                    req.Data.Dispose();
                    copy.Position = 0;
                    call          = _serializer.DeserializeCall(copy, _binder, _selector, req.Context);
                }
            }
            catch (Exception e)
            {
                ex = e;
            }
            if (call != null)
            {
                object res = null;
                try
                {
                    if (_interceptor != null)
                    {
                        res = _interceptor.Intercept(call, req.Context,
                                                     () => ConvertToTask(call.Method.Invoke(call.Target, call.Arguments)));
                    }
                    else
                    {
                        res = call.Method.Invoke(call.Target, call.Arguments);
                    }
                }
                catch (Exception e)
                {
                    ex = e;
                }
                if (res != null)
                {
                    var task = res as Task;
                    if (task == null)
                    {
                        result = res;
                    }
                    else
                    {
                        try
                        {
                            await task;
                            if (call.Method.ReturnType != typeof(Task))
                            {
                                result = task.GetType().GetProperty("Result")?.GetValue(task);
                            }
                        }
                        catch (Exception e)
                        {
                            ex = e;
                        }
                    }
                }
            }

            using var response = new RecyclableMemoryStream(StreamPool.Shared);
            if (ex == null)
            {
                try
                {
                    _serializer.SerializeResult(response, result);
                    response.Position = 0;
                }
                catch (Exception e)
                {
                    ex = e;
                }
            }

            if (ex != null)
            {
                response.Position = 0;
                response.SetLength(0);
                _serializer.SerializeException(response, ex.ToString());
                response.Position = 0;
            }

            try
            {
                await req.RespondAsync(response);
            }
            catch
            {
                //TODO: redirect it somewhere?
            }
        }