mirror of
https://github.com/zap-proto/cs.git
synced 2026-10-30 15:05:32 +00:00
Finishes the in-flight capnp→ZAP rebrand on this branch. The prior commits
renamed identifiers/namespaces/files/projects (Capnp.Net.Runtime→Zap.Net.Runtime,
CapnpC→ZapC, .capnp→.zap) but left the human-readable brand name "Cap'n Proto"
throughout doc-comments, .csproj/.nuspec descriptions, README, MSBuild .xaml
rule display names, and the generated XML doc. This completes brand neutrality:
- Replace 154 "Cap'n Proto" / 4 "Cap'n-Proto" / 1 "Cap'n'p" prose occurrences
with "ZAP" across 32 first-party files (runtime, generator, zapc-csharp,
MSBuild integration, chocolatey nuspecs, README, docs, XML doc).
- Revert the LICENSE provenance URL to the real upstream fork
(c80k/capnproto-dotnetcore) — upstream-fork license headers must stay.
- Canonicalize project/repo URLs to github.com/zap-proto/cs (were fabricated
c80k/zap-dotnetcore) in csproj/nuspec/VERIFICATION/README badges.
- Canonicalize docs URLs zap-protocol(.github.io|/zap-cs|/zap-cpp-core)
→ zap-proto.dev / github.com/zap-proto/{cs,cpp-core}.
- Collapse redundant "zap \"ZAP\"" PackageTags to "zap".
Verified consistent (no toolchain on host): every .sln/.csproj ProjectReference
resolves; all namespaces are Zap*/ZapC*; MSBuild task chain
(GenerateZapFileCodeBehindTask, ZapFiles ItemGroup, _ZapcCsharp_TaskAssembly)
is uniformly renamed; props/targets/xaml/coverage/chocolatey carry no capnp.
Deliberately preserved as upstream-derived test data (not first-party source):
upstream Cap'n Proto schema fixtures (rpc.zap, rpc-twoparty.zap, json.zap,
rpc-csharp.zap), golden compiler-error outputs (errors.txt), and the
machine-generated DO-NOT-EDIT test.zap.{c++,h}. Their "Cap'n Proto" strings are
the literal output of the external schema compiler the compat test matches.
137 lines
4.7 KiB
C#
137 lines
4.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Zap.Rpc
|
|
{
|
|
abstract class RefCountingCapability: ConsumedCapability
|
|
{
|
|
readonly object _reentrancyBlocker = new object();
|
|
Vine? _vine;
|
|
|
|
// Note on reference counting: Works in analogy to COM. AddRef() adds a reference,
|
|
// Release() removes it. When the reference count reaches zero, the capability must be
|
|
// released remotely, i.e. we need to tell the remote peer that it can remove this
|
|
// capability from its export table. To be on the safe side,
|
|
// this class also implements a finalizer which will auto-release this capability
|
|
// remotely. This might happen if one forgets to Dispose() a Proxy. It might also happen
|
|
// if no Proxy is ever created. The latter situation occurs if the using client never
|
|
// deserializes the capability Proxy from its RPC state. This situation is nearly
|
|
// impossible to handle without relying on GC, since we never know when deserialization
|
|
// happens, and there is no RAII like in C++. Since this situation is expected to happen rarely,
|
|
// it seems acceptable to rely on the finalizer. There are three possible states.
|
|
// A: Initial state after construction: No reference, capability is *not* released.
|
|
// B: Some positive reference count.
|
|
// C: Released state: No reference anymore, capability *is* released.
|
|
// In order to distinguish state A from C, the member _refCount stores the reference count *plus one*.
|
|
// Value 0 has the special meaning of being in state C.
|
|
long _refCount = 1;
|
|
|
|
#if DebugFinalizers
|
|
ILogger Logger { get; } = Logging.CreateLogger<RefCountingCapability>();
|
|
string CreatorStackTrace { get; set; }
|
|
#endif
|
|
|
|
public RefCountingCapability()
|
|
{
|
|
#if DebugFinalizers
|
|
CreatorStackTrace = Environment.StackTrace;
|
|
#endif
|
|
}
|
|
|
|
~RefCountingCapability()
|
|
{
|
|
#if DebugFinalizers
|
|
Logger?.LogWarning($"Caught orphaned capability, created from here: {CreatorStackTrace}.");
|
|
#endif
|
|
|
|
Dispose(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Part of the Dispose pattern implementation.
|
|
/// </summary>
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
try { ReleaseRemotely(); }
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
if (_refCount > 0)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
try { ReleaseRemotely(); }
|
|
catch { }
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
internal override void AddRef()
|
|
{
|
|
lock (_reentrancyBlocker)
|
|
{
|
|
if (++_refCount <= 1)
|
|
{
|
|
--_refCount;
|
|
|
|
#if DebugCapabilityLifecycle
|
|
Logger.LogError($"Attempted to add reference to capability which was already released. " +
|
|
$"Releasing entity: {_releasingFilePath}, line {_releasingLineNumber}, method {_releasingMethodName}" +
|
|
$"Current stack trace: {Environment.StackTrace}");
|
|
#endif
|
|
throw new ObjectDisposedException(ToString(), "Attempted to add reference to capability which was already released");
|
|
}
|
|
}
|
|
}
|
|
|
|
internal override void Release()
|
|
{
|
|
lock (_reentrancyBlocker)
|
|
{
|
|
switch (_refCount)
|
|
{
|
|
case 1: // initial state, actually ref. count 0
|
|
case 2: // actually ref. count 1
|
|
_refCount = 0;
|
|
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
break;
|
|
|
|
case var _ when _refCount > 2:
|
|
--_refCount;
|
|
break;
|
|
|
|
default:
|
|
throw new InvalidOperationException("Capability is already disposed");
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void Validate()
|
|
{
|
|
lock (_reentrancyBlocker)
|
|
{
|
|
if (_refCount <= 0)
|
|
{
|
|
throw new ObjectDisposedException(ToString(), "Validation failed, capability is already disposed");
|
|
}
|
|
}
|
|
}
|
|
|
|
internal override Skeleton AsSkeleton()
|
|
{
|
|
lock (_reentrancyBlocker)
|
|
{
|
|
if (_vine == null)
|
|
_vine = new Vine(this);
|
|
return _vine;
|
|
}
|
|
}
|
|
}
|
|
} |