Пример #1
0
        public void WriteValue(SerializationCtx context, UnsafeWriter writer)
        {
            Assertion.Assert(!myHandler.IsSendWithoutContexts, "!myHandler.IsWritingOwnMessages");
            var value = Context.Value;

            if (value == null)
            {
                InternId.Write(writer, InternId.Invalid);
                writer.Write(false);
            }
            else
            {
                using (myHandler.CreateSendWithoutContextsCookie())
                {
                    if (!myProtocolValueSet.Contains(value))
                    {
                        Assertion.Require(Proto.Scheduler.IsActive, "Attempting to use previously unused context value {0} on a background thread for key {1}", value, Context.Key);
                        myProtocolValueSet.Add(Context.Value);
                    }

                    var internedId = myInternRoot.Intern(value);
                    InternId.Write(writer, internedId);
                    if (!internedId.IsValid)
                    {
                        writer.Write(true);
                        Context.WriteDelegate(context, writer, value);
                    }
                }
            }
        }
Пример #2
0
        public void WriteValue(SerializationCtx context, UnsafeWriter writer)
        {
            Assertion.Assert(!myHandler.IsSendWithoutContexts, "!myHandler.IsWritingOwnMessages");
            var value = Context.Value;

            if (value == null)
            {
                InternId.Write(writer, InternId.Invalid);
                writer.Write(false);
            }
            else
            {
                using (myHandler.CreateSendWithoutContextsCookie())
                {
                    AddValueToProtocolValueSetImpl(value);

                    var internedId = myInternRoot.Intern(value);
                    InternId.Write(writer, internedId);
                    if (!internedId.IsValid)
                    {
                        writer.Write(true);
                        Context.WriteDelegate(context, writer, value);
                    }
                }
            }
        }
Пример #3
0
        public T ReadValue(SerializationCtx context, UnsafeReader reader)
        {
            var id = InternId.Read(reader);

            if (!id.IsValid)
            {
                var hasValue = reader.ReadBool();
                if (hasValue)
                {
                    return(Context.ReadDelegate(context, reader));
                }
                return(default);
Пример #4
0
        public bool TryUnIntern <T>(InternId id, out T result) where T : TBase
        {
            var value = TryGetValue(id);

            if (value != null)
            {
                result = (T)value;
                return(true);
            }

            result = default;
            return(false);
        }
Пример #5
0
        public InternId Intern(TBase value)
        {
            IdPair pair;

            if (myInverseMap.TryGetValue(value, out pair))
            {
                return(pair.Id);
            }

            pair.Id = pair.ExtraId = InternId.Invalid;

            if (myInverseMap.TryAdd(value, pair))
            {
                InternId allocatedId = new InternId(Interlocked.Increment(ref myInternedIdCounter) * 2);

                Assertion.Assert(allocatedId.IsLocal, "Newly allocated ID must be local");

                RdReactiveBase.SendTrace?.Log($"InternRoot `{Location}` ({RdId}):: {allocatedId} = {value}");

                myDirectMap[allocatedId] = value;
                using (Proto.Contexts.CreateSendWithoutContextsCookie())
                    Proto.Wire.Send(RdId, writer =>
                    {
                        myWriteDelegate(SerializationContext, writer, value);
                        InternId.Write(writer, allocatedId);
                    });

                while (true)
                {
                    var oldPair      = myInverseMap[value];
                    var modifiedPair = oldPair;
                    modifiedPair.Id = allocatedId;
                    if (myInverseMap.TryUpdate(value, modifiedPair, oldPair))
                    {
                        break;
                    }
                }
            }

            return(myInverseMap[value].Id);
        }
Пример #6
0
 private object TryGetValue(InternId id)
 {
     myDirectMap.TryGetValue(id, out var value);
     return(value);
 }
Пример #7
0
 public T UnIntern <T>(InternId id) where T : TBase
 {
     return((T)TryGetValue(id));
 }