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); } } } }
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); } } } }
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);
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); }
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); }
private object TryGetValue(InternId id) { myDirectMap.TryGetValue(id, out var value); return(value); }
public T UnIntern <T>(InternId id) where T : TBase { return((T)TryGetValue(id)); }