Session 07 scope (5 features, 17 tests, ~1165 Go LOC): - Protocol/ParserTypes.cs: ParserState enum (79 states), PublishArgument, ParseContext - Protocol/IProtocolHandler.cs: handler interface decoupling parser from client - Protocol/ProtocolParser.cs: Parse(), ProtoSnippet(), OverMaxControlLineLimit(), ProcessPub/HeaderPub/RoutedMsgArgs/RoutedHeaderMsgArgs, ClonePubArg(), GetHeader() - tests/Protocol/ProtocolParserTests.cs: 17 tests via TestProtocolHandler stub Auth extras from session 06 (committed separately): - Auth/TpmKeyProvider.cs, Auth/CertificateIdentityProvider/, Auth/CertificateStore/ Internal utilities & data structures (session 06 overflow): - Internal/AccessTimeService.cs, ElasticPointer.cs, SystemMemory.cs, ProcessStatsProvider.cs - Internal/DataStructures/GenericSublist.cs, HashWheel.cs - Internal/DataStructures/SubjectTree.cs, SubjectTreeNode.cs, SubjectTreeParts.cs All 461 tests pass (460 unit + 1 integration). DB updated for features 2588-2592 and tests 2598-2614.
65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
namespace ZB.MOM.NatsNet.Server.Internal;
|
|
|
|
/// <summary>
|
|
/// A pointer that can be toggled between weak and strong references, allowing
|
|
/// the garbage collector to reclaim the target when weakened.
|
|
/// Mirrors the Go <c>elastic.Pointer[T]</c> type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the referenced object. Must be a reference type.</typeparam>
|
|
public sealed class ElasticPointer<T> where T : class
|
|
{
|
|
private WeakReference<T>? _weak;
|
|
private T? _strong;
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="ElasticPointer{T}"/> holding a weak reference to <paramref name="value"/>.
|
|
/// </summary>
|
|
public static ElasticPointer<T> Make(T value)
|
|
{
|
|
return new ElasticPointer<T> { _weak = new WeakReference<T>(value) };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the target. If the pointer is currently strengthened, the strong reference is updated too.
|
|
/// </summary>
|
|
public void Set(T value)
|
|
{
|
|
_weak = new WeakReference<T>(value);
|
|
if (_strong != null)
|
|
_strong = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Promotes to a strong reference, preventing the GC from collecting the target.
|
|
/// No-op if already strengthened or if the weak target has been collected.
|
|
/// </summary>
|
|
public void Strengthen()
|
|
{
|
|
if (_strong != null)
|
|
return;
|
|
if (_weak != null && _weak.TryGetTarget(out var target))
|
|
_strong = target;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reverts to a weak reference, allowing the GC to reclaim the target.
|
|
/// No-op if already weakened.
|
|
/// </summary>
|
|
public void Weaken()
|
|
{
|
|
_strong = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the target value, or <see langword="null"/> if the weak reference has been collected.
|
|
/// </summary>
|
|
public T? Value()
|
|
{
|
|
if (_strong != null)
|
|
return _strong;
|
|
if (_weak != null && _weak.TryGetTarget(out var target))
|
|
return target;
|
|
return null;
|
|
}
|
|
}
|