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 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); }