コード例 #1
0
        public override void OnWireReceived(UnsafeReader reader)
        {
            var version = reader.ReadInt();
            var value   = ReadValueDelegate(SerializationContext, reader);

            var rejected = IsMaster && version < myMasterVersion;

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("property `{0}` ({1}):: oldver = {2}, newver = {3}, value = {4}{5}",
                                  Location, RdId, myMasterVersion, version, value.PrintToString(), rejected ? " REJECTED" : "");
            }


            if (rejected)
            {
                return;
            }

            myMasterVersion = version;

            using (UsingDebugInfo())
            {
                myProperty.Value = value;
            }
        }
コード例 #2
0
        public override void OnWireReceived(UnsafeReader reader)
        {
            var taskId = RdId.Read(reader);
            var value  = ReadRequestDelegate(SerializationContext, reader);

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("endpoint `{0}`::({1}), taskId={2}, request = {3}", Location, RdId, taskId, value.PrintToString());
            }


            RdTask <TRes> rdTask;

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                try
                {
                    rdTask = Handler(myBindLifetime, value);
                }
                catch (OperationCanceledException)
                {
                    rdTask = RdTask <TRes> .Cancelled();
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(e);
                }
            }

            rdTask.Result.Advise(myBindLifetime, result =>
            {
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("endpoint `{0}`::({1}), taskId={2}, response = {3}", Location, RdId, taskId, result.PrintToString());
                }

                RdTaskResult <TRes> validatedResult;
                try
                {
                    if (result.Status == RdTaskStatus.Success)
                    {
                        AssertNullability(result.Result);
                    }
                    validatedResult = result;
                }
                catch (Exception e)
                {
                    LogSend.Error(e);
                    validatedResult = RdTaskResult <TRes> .Faulted(e);
                }

                Wire.Send(RdId, (writer) =>
                {
                    taskId.Write(writer);
                    RdTaskResult <TRes> .Write(WriteResponseDelegate, SerializationContext, writer, validatedResult);
                });
            });
        }
コード例 #3
0
ファイル: RdSignal.cs プロジェクト: epeshk/rd
        public override void OnWireReceived(UnsafeReader reader)
        {
            var value = myReadValue(SerializationContext, reader);

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("signal `{0}` ({1}):: value = {2}", Location, RdId, value.PrintToString());
            }
            using (UsingDebugInfo())
                mySignal.Fire(value);
        }
コード例 #4
0
ファイル: RdMap.cs プロジェクト: epeshk/rd
        public override void OnWireReceived(UnsafeReader stream)
        {
            var header       = stream.ReadInt();
            var msgVersioned = (header >> versionedFlagShift) != 0;
            var opType       = header & ((1 << versionedFlagShift) - 1);

            var version = msgVersioned ? stream.ReadLong() : 0L;

            var key = ReadKeyDelegate(SerializationContext, stream);

            if (opType == Ack)
            {
                string error = null;

                if (!msgVersioned)
                {
                    error = "Received ACK while msg hasn't versioned flag set";
                }
                else if (!IsMaster)
                {
                    error = "Received ACK when not a Master";
                }
                else if (!myPendingForAck.TryGetValue(key, out var pendingVersion))
                {
                    error = "No pending for ACK";
                }
                else if (pendingVersion < version)
                {
                    error = string.Format("Pending version `{0}` < ACK version `{1}`", pendingVersion, version);
                }
                // Good scenario
                else if (pendingVersion == version)
                {
                    myPendingForAck.Remove(key);
                }
                //else do nothing, silently drop

                var isError = !string.IsNullOrEmpty(error);
                if (LogReceived.IsTraceEnabled() || isError)
                {
                    LogReceived.LogFormat(isError ? LoggingLevel.ERROR : LoggingLevel.TRACE,
                                          "map `{0}` ({1})  :: ACK :: key = {2} :: version = {3}{4}", Location, RdId, key.PrintToString(), version,
                                          isError ? " >> " + error : "");
                }
            }
            else
            {
                using (UsingDebugInfo())
                {
                    var kind = (AddUpdateRemove)opType;
                    switch (kind)
                    {
                    case AddUpdateRemove.Add:
                    case AddUpdateRemove.Update:
                        var value = ReadValueDelegate(SerializationContext, stream);
                        if (LogReceived.IsTraceEnabled())
                        {
                            LogReceived.Trace("map `{0}` ({1})  :: {2} :: key = {3}{4} :: value = {5}", Location, RdId, kind,
                                              key.PrintToString(), msgVersioned ? " :: version = " + version : "", value.PrintToString());
                        }

                        if (msgVersioned || !IsMaster || !myPendingForAck.ContainsKey(key))
                        {
                            myMap[key] = value;
                        }
                        else
                        {
                            LogReceived.Trace(">> CHANGE IGNORED");
                        }

                        break;

                    case AddUpdateRemove.Remove:
                        if (LogReceived.IsTraceEnabled())
                        {
                            LogReceived.Trace("map `{0}` ({1})  :: {2} :: key = {3}{4}", Location, RdId, kind,
                                              key.PrintToString(), msgVersioned ? " :: version = " + version : "");
                        }

                        if (msgVersioned || !IsMaster || !myPendingForAck.ContainsKey(key))
                        {
                            myMap.Remove(key);
                        }
                        else
                        {
                            LogReceived.Trace(">> CHANGE IGNORED");
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException(kind.ToString());
                    }
                }

                if (msgVersioned)
                {
                    Wire.Send(RdId, (innerWriter) =>
                    {
                        innerWriter.Write((1 << versionedFlagShift) | Ack);
                        innerWriter.Write(version);
                        WriteKeyDelegate.Invoke(SerializationContext, innerWriter, key);

                        if (LogSend.IsTraceEnabled())
                        {
                            LogSend.Trace("map `{0}` ({1}) :: ACK :: key = {2} :: version = {3}", Location, RdId, key.PrintToString(), version);
                        }
                    });

                    if (IsMaster)
                    {
                        LogReceived.Error("Both ends are masters: {0}", Location);
                    }
                }
            }
        }
コード例 #5
0
ファイル: RdCall.cs プロジェクト: LongJohnCoder/rd
        public override void OnWireReceived(UnsafeReader reader)
        {
            var taskId = RdId.Read(reader);
            var value  = ReadRequestDelegate(SerializationContext, reader);

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("endpoint `{0}`::({1}), taskId={2}, request = {3}", Location, RdId, taskId, value.PrintToString());
            }


            var taskLifetimeDef = myBindLifetime.CreateNested();

            //subscribe for lifetime cancellation
            new WiredLifetime(taskLifetimeDef, taskId, this, Wire);

            RdTask <TRes> rdTask;

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                try
                {
                    rdTask = Handler(taskLifetimeDef.Lifetime, value);
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(e);
                }
            }



            rdTask.Result.Advise(taskLifetimeDef.Lifetime, result =>
            {
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("endpoint `{0}`::({1}), taskId={2}, response = {3}", Location, RdId, taskId, result.PrintToString());
                }

                RdTaskResult <TRes> validatedResult;
                try
                {
                    if (result.Status == RdTaskStatus.Success)
                    {
                        AssertNullability(result.Result);
                    }
                    validatedResult = result;
                }
                catch (Exception e)
                {
                    LogSend.Error(e);
                    validatedResult = RdTaskResult <TRes> .Faulted(e);
                }

                Wire.Send(taskId, writer =>
                {
                    RdTaskResult <TRes> .Write(WriteResponseDelegate, SerializationContext, writer, validatedResult);
                });

                taskLifetimeDef.Terminate(); //need to terminate to unsubscribe lifetime listener - not for bindable entries
            });
        }
コード例 #6
0
ファイル: RdList.cs プロジェクト: epeshk/rd
        public override void OnWireReceived(UnsafeReader stream)
        {
            var header  = stream.ReadLong();
            var opType  = header & ((1 << versionedFlagShift) - 1);
            var version = header >> versionedFlagShift;

            var index = stream.ReadInt();


            var kind  = (AddUpdateRemove)opType;
            V   value = default(V);
            var isPut = kind == AddUpdateRemove.Add || kind == AddUpdateRemove.Update;

            if (isPut)
            {
                value = ReadValueDelegate(SerializationContext, stream);
            }

            LogReceived.Trace("list `{0}` ({1}) :: {2} :: index={3} :: version = {4}{5}", Location, RdId, kind, index
                              , version
                              , isPut  ? " :: value = " + value.PrintToString() : "");

            if (version != myNextVersion)
            {
                Assertion.Fail("Version conflict for {0} Expected version {1} received {2}. Are you modifying a list from two sides?",
                               Location,
                               myNextVersion,
                               version);
            }


            myNextVersion++;

            using (UsingDebugInfo())
            {
                switch (kind)
                {
                case AddUpdateRemove.Add:
                    if (index < 0)
                    {
                        myList.Add(value);
                    }
                    else
                    {
                        myList.Insert(index, value);
                    }
                    break;

                case AddUpdateRemove.Update:
                    // ReSharper disable once AssignNullToNotNullAttribute
                    myList[index] = value;
                    break;

                case AddUpdateRemove.Remove:
                    myList.RemoveAt(index);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(kind.ToString());
                }
            }
        }