示例#1
0
文件: RdCall.cs 项目: Tryweirder/rd
        public override void OnWireReceived(UnsafeReader reader) //endpoint's side
        {
            var taskId = RdId.Read(reader);

            var wiredTask = new WiredRdTask <TReq, TRes> .Endpoint(myBindLifetime, this, taskId, myCancellationScheduler ?? SynchronousScheduler.Instance);

            //subscribe for lifetime cancellation
            var externalCancellation = wiredTask.Lifetime;

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                RdTask <TRes> rdTask;
                try
                {
                    var value = ReadRequestDelegate(SerializationContext, reader);
                    ReceiveTrace?.Log($"{wiredTask} :: received request: {value.PrintToString()}");
                    var handler = Handler;
                    if (handler == null)
                    {
                        var message = $"Handler is not set for {wiredTask} :: received request: {value.PrintToString()}";
                        ourLogReceived.Error(message);
                        rdTask = RdTask <TRes> .Faulted(new Exception(message));
                    }
                    else
                    {
                        try
                        {
                            rdTask = handler(externalCancellation, value);
                        }
                        catch (Exception ex)
                        {
                            rdTask = RdTask <TRes> .Faulted(ex);
                        }
                    }
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(new Exception($"Unexpected exception in {wiredTask}", e));
                }

                rdTask.Result.Advise(Lifetime.Eternal, result =>
                {
                    try
                    {
                        if (result.Status == RdTaskStatus.Success)
                        {
                            AssertNullability(result.Result);
                        }

                        wiredTask.ResultInternal.SetIfEmpty(result);
                    }
                    catch (Exception ee)
                    {
                        ourLogSend.Error($"Problem when responding to `{wiredTask}`", ee);
                        wiredTask.Set(new RdFault(ee));
                    }
                });
            }
        }
示例#2
0
        public override void OnWireReceived(UnsafeReader reader) //endpoint's side
        {
            var taskId = RdId.Read(reader);

            var wiredTask = new WiredRdTask <TReq, TRes>(this, taskId, myEndpointSchedulerForHandlerAndCancellation ?? WireScheduler, true);
            //subscribe for lifetime cancellation
            var externalCancellation = wiredTask.Subscribe(myBindLifetime);

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                RdTask <TRes> rdTask;
                try
                {
                    var value = ReadRequestDelegate(SerializationContext, reader);
                    ReceiveTrace?.Log($"{wiredTask} :: received request: {value.PrintToString()}");
                    rdTask = Handler(externalCancellation, value);
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(e);
                }

                rdTask.Result.Advise(Lifetime.Eternal, result =>
                {
                    try
                    {
                        if (result.Status == RdTaskStatus.Success)
                        {
                            AssertNullability(result.Result);
                        }

                        wiredTask.ResultInternal.SetIfEmpty(result);
                    }
                    catch (Exception ee)
                    {
                        ourLogSend.Error($"Problem when responding to `{wiredTask}`", ee);
                        wiredTask.Set(new RdFault(ee));
                    }
                });
            }
        }