Skip to content

Latest commit

 

History

History
1087 lines (771 loc) · 66.4 KB

pulse.md

File metadata and controls

1087 lines (771 loc) · 66.4 KB

Pulse

This page covers the Pulse transport — an ENet-based UDP peer transport that runs alongside LiveKit. Pulse is a second, experimental real-time channel for multiplayer traffic (movement, emotes, profile propagation, teleports). It is gated by a feature flag and ships off by default; when enabled, it runs concurrently with LiveKit rather than replacing it.

The transport-neutral pieces (encoding, send system, receive pipeline, interpolation, SDK propagation, remote-player tables) live in multiplayer.md and are shared with LiveKit. This page is specifically about the Pulse transport's internals.


What Pulse is and why

Pulse is a direct peer transport built on ENet (reliable-UDP), distinct from LiveKit's WebRTC-based room model:

  • No rooms — a single peer connection per client, with peer-ids assigned server-side and mapped to Web3 wallets.
  • Protobuf framing — every message is a ClientMessage or ServerMessage envelope wrapping a oneof payload (movement, emote, profile, teleport, disconnect, …).
  • Flat addressing — senders identify themselves by their peer-id; receivers use PeerIdCache to translate peer-ids back to wallets.
  • Pool-backed packet buffers — PacketPool + MessagePacket keep ENet packet allocation out of the hot path.

Pulse was introduced as a parallel pipe rather than a LiveKit replacement so:

  1. Roll-out can be gated behind FeatureId.Pulse and flipped on per-user or per-environment without rebuilding.
  2. Messages sent over both transports are transparently de-duplicated by downstream consumers (see multiplayer.md → Core Interfaces — the proxy implementations union inputs from both transports).
  3. LiveKit remains the source of truth for chat, voice, and scene-room semantics; Pulse handles player-state traffic that benefits from a tighter, more direct path.

See multiplayer.md → Transport Selection & Wiring for how both containers are composed under MultiplayerContainer and how the four proxy implementations fan out calls to each.


Feature flag and program arg

Pulse is toggled by a single logical flag, evaluated by FeaturesRegistry as a remote feature flag with an optional launch-arg override:

Source Symbol Where
Program arg (override) --pulse true / --pulse false AppArgsFlags.PULSE_MULTIPLAYER = "pulse"
Remote flag "pulse" FeatureFlagsStrings.PULSE = "pulse"
Registry key FeatureId.Pulse Final boolean exposed to code

From DCL/FeatureFlags/FeaturesRegistry.cs:

// Local scene development resolves no remote feature flags, so Pulse is deterministically on there; --pulse false opts back into LiveKit-only.
[FeatureId.Pulse] = appArgs.ResolveFeatureFlagArg(AppArgsFlags.PULSE_MULTIPLAYER, localSceneDevelopment || featureFlags.IsEnabled(FeatureFlagsStrings.PULSE), requireDebug: false),

Override semantics: when --pulse is specified its value wins over the remote flag (--pulse true force-enables, --pulse false force-disables — useful for local development, integration testing, and ops kill-switching); when it is not specified, Pulse is driven by the remote feature flag.

Local scene development is the exception: it resolves no remote feature flags (the feature-flag host is the local dev server), so the flag can never be on there and Pulse defaults on instead. --pulse false is the way back to the LiveKit-only behaviour local scene development had before. See Realm — PulseRealm for how concurrent dev processes stay isolated from one another.

Where the flag is consumed

The flag is read once in MultiplayerContainer.CreateAsync, which seeds a single shared PulseActivation (Connections/Pulse/PulseActivation.cs) — the session-wide source of truth for "is Pulse the active transport". Consumers read PulseActivation.IsActive rather than the flag:

  • PulseContainer — receives the PulseActivation. During InitializeInternalAsync, from pulseActivation.IsActive:
    • pulseMultiplayerService is either a real PulseMultiplayerService(transport, messagePipe, urlsSource) or new IPulseMultiplayerService.Dummy().
    • pulseProfilePropagationBus is either a real PulseProfilePropagationBus(...) or new IProfilePropagation.Dummy().
    • The PulseMultiplayerBus, PulseIncomingProfileAnnouncements, PulseRemoveIntentions, and ENetTransport are always constructed; activation only swaps the Service and ProfilePropagation paths. The bus and incoming collectors simply never receive traffic when the service is a dummy (or the real service is disconnected).
  • LiveKitMessagesBroadcaster — holds the PulseActivation and reads IsActive live on every send (movement, emotes, and profiles all route through it). When Pulse is active it sends only to the peers that announced over LiveKit (the rest receive over Pulse); when Pulse is absent it broadcasts to every peer in the rooms.

PulseActivation.IsActive starts equal to FeatureId.Pulse. It flips to false exactly once — when the start-up connection is unreachable, or when the realm cannot be resolved (see Start-up fallback). It never flips at runtime.

Disabling behavior

When Pulse is inactive (FeatureId.Pulse off, --pulse false, or after a start-up fallback):

  • All four proxies in MultiplayerContainer still fan out to Pulse, but the Pulse side is a no-op — PulseMultiplayerBus methods early-return against a Dummy (or disconnected) service.
  • Incoming proxies' Fill(...) / Bunch() calls include empty Pulse lists.
  • IProfilePropagation is a no-op (the profile still propagates over LiveKit via IProfileBroadcast); PropagateSelfProfileSystem also gates on IsActive.
  • LiveKitMessagesBroadcaster broadcasts to all peers (its original serialization mode).

The effective runtime cost of Pulse when disabled is a few dummy virtual calls per frame.

Start-up fallback

StartPulseMultiplayerStartupOperation connects to Pulse during login, passing a bounded attempt count (5) to IPulseMultiplayerService.ConnectAsync(ct, maxAttempts):

  • Before connecting it awaits PulseRealm.EnsureResolvedAsync(ct). If the realm is still empty afterwards — in local scene development, that means the dev server is unreachable — it calls PulseActivation.Deactivate() and returns success without connecting. An empty realm violates the server contract, so connecting anyway would join a session nothing can be filtered into.
  • If the connection keeps failing transiently across all 5 attempts (timing out, or dropped mid-handshake for a retriable reason such as GRACEFUL or SERVER_FULL), or fails terminally on any attempt (rejected handshake, BANNED, or another non-retriable disconnect reason), the operation calls PulseActivation.Deactivate() and returns success — login continues and the client behaves as if Pulse were absent.
  • If Pulse is already inactive (disabled / --pulse false), the operation is a no-op.

Runtime reconnection failures (after a successful initial connect) do not fall back — the StartRouting / HandleDisconnect reconnection loop keeps retrying without flipping PulseActivation. A terminal handshake failure during a runtime reconnect (rejection, ban) stops the loop instead of retrying forever.


Realm — PulseRealm

Pulse has no rooms. The server partitions visibility by exact realm-string match, so the realm a peer announces is the only thing separating two sessions that share a Pulse instance. Every realm read and write in PulseMultiplayerBus — peer lookup (GetWalletInRealm), the stale-peer purge (CollectWalletsNotInRealm / RemoveWhereNotInRealm), the PlayerJoined / TeleportPerformed filters, BroadcastTeleport, and the handshake's PlayerInitialState.Realm — goes through Connections/Pulse/PulseRealm.cs:

public class PulseRealm
{
    public string Value { get; }

    public PulseRealm(IRealmData realmData, ILocalSceneEntityIdSource? localSceneEntityIdSource = null);

    public UniTask EnsureResolvedAsync(CancellationToken ct);
}

There is no interface and no second implementation — the optional constructor argument is the mode:

localSceneEntityIdSource Value EnsureResolvedAsync
null (the normal case) IRealmData.RealmName, read live no-op
supplied (local scene development) the derived local-development key, resolved once fetches the dev server's entity id

The normal case reads live rather than caching because a realm change — teleporting between Genesis and a world — has to be visible to the very next message the bus sends or filters. It is a pure passthrough: outside local scene development this class changes nothing.

Value is empty while a local-development realm is unresolved, and callers must not connect in that state: an empty realm violates the server contract and every incoming message would be filtered out.

The local-development realm key

Local scene development has no realm of its own — every dev process would otherwise land in the same one and see each other's avatars. Each process instead derives a key from the preview entity id its dev server serves:

previewSceneId = "b64-" + base64(absoluteProjectRoot + "-" + machineId)   // minted by sdk-commands
realmKey       = "lsd:" + previewSceneId

and, when that would exceed Pulse's MaxRealmLength of 255, it collapses deterministically:

realmKey = "lsd:sha256:" + SHA256Hex(previewSceneId)

The hash is taken over previewSceneId including its b64- prefix, hex-encoded lowercase. The overflow form is always 75 characters, so it always fits. Hashed rather than truncated on purpose — see below.

Because the hostname is part of previewSceneId, this yields per-folder and per-machine isolation: two developers who check the same project out to the same path still get different realms. All processes of one dev session share the key, and LAN clients pointed at the same dev server read the same served id, so they share it too.

Nothing is exchanged. Every party — this client, sdk-commands, bevy-explorer — derives the key independently. That is what makes isolation work without a paired endpoint or handshake, and it is also the failure mode: two implementations that derive even slightly different strings do not error, their peers simply never see each other. This is the same class of bug as the LiveKit preview-${sceneId} vs LocalPreview:{sceneId} room-name mismatch.

The contract is written down once in js-sdk-toolchain's docs/lsd-identity-and-pulse-realm.md; PulseRealmShould pins the exact strings, including the worked examples published there. Keep both in sync.

Stability. The key derives from the entity id only, never from a content hash. js-sdk-toolchain #1529 versions per-file preview hashes by mtime but deliberately leaves the project directory's own entity id path-only, so the realm survives content edits, hot reloads and dev-server restarts. A content-shaped input would re-partition comms on every file save.

Where it is resolved

StartPulseMultiplayerStartupOperation awaits EnsureResolvedAsync before ConnectAsync, because the realm goes out in the very first message — the handshake's PlayerInitialState.Realm. The fetch is the same two-step the gatekeeper scene room already does (GET scene.json → base parcel → POST content/entities/active → result[0].id), extracted into Connections/GateKeeper/Meta/LocalSceneEntityIdSource.cs so there is one definition of "the local scene's entity id" rather than two that can drift.

It never throws: an unresolvable realm leaves Value empty, the start-up operation deactivates Pulse and log-in continues on LiveKit alone.

Caveats

  • Genesis bounds. Pulse's FieldValidator rejects parcel indices outside Genesis City and disconnects the peer, so a local scene outside those bounds would join a realm and then silently fail to sync. PulseRealm logs a warning naming the parcel and the bounds when it sees one. (sdk-commands already refuses to start such a scene, so this only fires for dev servers it did not launch.)
  • Endpoint. Local scene development connects to the same org Pulse endpoint as any other session; there is no --pulse-url override.
  • Privacy. The non-overflow key is reversible base64 of an absolute path and a hostname. The same information already goes to the gatekeeper today, so this is not a new exposure — but it now travels as a comms realm. The hashed form is the switch if that ever becomes a requirement.

Transport layer

ITransport — the abstraction

Connections/Pulse/ITransport.cs is the interface the rest of Pulse uses. It is intentionally narrow so the ENet implementation can be swapped for tests or alternate backends:

public interface ITransport : IDisposable
{
    TransportState State { get; }

    long BytesSent, BytesReceived;
    long PacketsSent, PacketsReceived;

    UniTask ConnectAsync(string address, int port, CancellationToken ct);
    void   Disconnect(DisconnectReason reason);
    void   Send(IMessage message, PacketMode mode);

    public enum TransportState
    {
        NONE, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED,
    }
}

PacketMode and DisconnectReason come from the external Pulse.Transport UPM package (com.decentraland.pulse.transport) — the same enums the server uses.

ENetTransport — the implementation

Connections/Pulse/ENet/ENetTransport.cs wraps the managed ENet binding and drives the socket on a dedicated worker.

Single-peer topology — client.Create(peerLimit: 1, channelLimit: ENetChannel.COUNT) and serverPeer = client.Connect(...). Pulse is strictly client↔server; there is no peer mesh. The serverPeer nullable field tracks the one remote.

Lifecycle — ConnectAsync(ip, port, ct):

  1. Initialize the ENet native library lazily (guarded by static volatile bool isLibInitialized). Throws InvalidOperationException if Library.Initialize() fails.
  2. Allocate a new Host, resolve the address, client.Connect(...).
  3. Spawn the receive/send loop via UniTask.RunOnThreadPool(...).
  4. UniTask.WaitUntil(State == CONNECTED).Timeout(ConnectTimeoutMs) — surfaces a TimeoutException if the handshake stalls, and cancels the loop on timeout.

TransportState is computed by mapping ENet's PeerState to the five TransportState values: Connecting / ConnectionPending / AcknowledgingConnect → CONNECTING; Connected / ConnectionSucceeded → CONNECTED; the two disconnect states → DISCONNECTING / DISCONNECTED.

The service loop

ENet is not thread-safe — all socket I/O must happen on a single thread. ListenForIncomingDataAsync enforces this:

return UniTask.RunOnThreadPool(async () =>
{
    while (!ct.IsCancellationRequested)
    {
        if (client == null) continue;

        // Service does socket I/O + returns one event. Short timeout so we never block outgoing flushes.
        if (client.Service(options.ServiceTimeoutMs, out Event netEvent) > 0)
            ReceiveIncomingMessage(in netEvent);

        // Service only returns one event per call. If multiple packets arrived in that I/O pass,
        // the rest are queued internally. CheckEvents drains them without redundant socket I/O.
        while (client.CheckEvents(out netEvent) > 0)
            ReceiveIncomingMessage(in netEvent);

        SendOutgoingMessages();
        await Task.Yield();
    }

    client?.Flush();
    client?.Dispose();
    client = null;
}, configureAwait: false, cancellationToken: ct);

Two nuances:

  • Service vs. CheckEvents — Service(timeoutMs) performs socket I/O and returns at most one event. Remaining queued events from that I/O pass are drained in the tight CheckEvents inner loop. This avoids redundant socket syscalls when a burst of packets arrives together.
  • Outgoing on the same thread — SendOutgoingMessages drains the MessagePipe output queue on the service thread, because Send must run on the thread that owns the socket.

ServiceTimeoutMs defaults to 1 ms in ENetTransportOptions so the loop is effectively a tight spin — optimized for low latency over CPU efficiency. BufferSize defaults to 4096 bytes; both send and receive buffers are pre-allocated and reused.

Channels and packet modes

Pulse opens ENetChannel.COUNT = 3 channels on connect — see Pulse.Transport.ENetChannel:

Channel PacketMode ENet flags Used for
0 RELIABLE RELIABLE PacketFlags.Reliable Handshakes, teleports, profile propagation, disconnects — anything that must arrive.
1 UNRELIABLE_SEQUENCED UNRELIABLE_SEQUENCED PacketFlags.Unthrottled High-frequency STATE_DELTA (movement) fan-out.
2 UNRELIABLE_UNSEQUENCED UNRELIABLE_UNSEQUENCED PacketFlags.Unsequenced One-shot events where ordering doesn't matter.

The UNRELIABLE_SEQUENCED channel intentionally uses PacketFlags.Unthrottled (not just PacketFlags.Unreliable). The package's own doc comment explains:

The Unthrottled flag disables ENet's send-side throttle. Without it, ENet silently destroys unreliable packets before they reach the wire when packetThrottle drops below 32 — which happens when measured RTT exceeds lastRTT + 40 ms + 2 × variance. Dropping a STATE_DELTA is strictly worse than sending it: the client detects a sequence gap and issues a RESYNC_REQUEST, which costs a full reliable round-trip.

In plain terms: for movement deltas, losing the packet on the wire (and resyncing on a gap) is cheaper than having ENet silently drop it and potentially hold back subsequent packets on the same channel. Application-level rate control (the adaptive sendRate in PlayerMovementNetSendSystem) already governs send frequency, so the transport-level throttle is redundant and harmful.

Warning: Reliable packets on a channel block sequenced-unreliable packets on the same channel — head-of-line blocking. That's why reliable traffic (channel 0) and STATE_DELTA (channel 1) are kept on separate channels.

Incoming event handling

ReceiveIncomingMessage(in Event) switches on netEvent.Type:

  • Connect — latch serverPeer = netEvent.Peer. This is the moment State transitions to CONNECTED.
  • Disconnect — clear serverPeer, call messagePipe.OnDisconnected((DisconnectReason)netEvent.Data), cancel the lifecycle CTS. netEvent.Data is an uint set by the remote when it called Disconnect(reason); it's cast into the shared DisconnectReason enum (GRACEFUL, AUTH_TIMEOUT, AUTH_FAILED, DUPLICATE_SESSION, KICKED, SERVER_FULL).
  • Timeout — same as Disconnect but reports DisconnectReason.NONE — the remote never got a chance to tell us why, we just stopped hearing from it.
  • Receive — copy the packet payload into the pre-allocated receiveBuffer, bump counters, dispatch through messagePipe.OnDataReceived(new MessagePacket(span, peerId)). The ENet Packet is disposed via the using pattern to release unmanaged memory back to ENet.

Outgoing send path

Send(IMessage message, PacketMode mode) is called from any thread via MessagePipe. The message is enqueued into the pipe's outbox; actual socket writes happen on the service thread inside SendOutgoingMessages:

private void SendOutgoingMessages()
{
    while (messagePipe.TryReadOutgoingMessage(out OutgoingMessage msg))
    {
        ENetChannel channel = ToENetChannel(msg.PacketMode);
        using OutgoingMessage _ = msg;

        if (serverPeer != null)
            SendToPeer(serverPeer.Value, channel, msg.Message);
    }
}

private void SendToPeer(Peer peer, ENetChannel channel, IMessage message)
{
    int size = message.CalculateSize();
    var span = new Span<byte>(sendBuffer, 0, size);
    message.WriteTo(span);
    BytesSent += size;
    PacketsSent++;
    var packet = default(Packet);
    packet.Create(span, channel.PacketMode);
    peer.Send(channel.ChannelId, ref packet);
}

Two details worth calling out:

  • Zero-copy serialize — the Protobuf message is serialized directly into the pre-allocated sendBuffer, then ENet copies from that span into its own packet. No per-send heap allocation for the payload.
  • using OutgoingMessage _ — the message envelope is pooled; the using returns it to PacketPool after send, regardless of success.

Disconnect and dispose

Disconnect(DisconnectReason reason) calls serverPeer?.Disconnect((uint)reason) — the reason round-trips to the server through ENet's disconnect-data field.

Dispose():

  1. Clears serverPeer and cancels the lifecycle CTS.
  2. Spins (Thread.Sleep(10)) until the service loop observes the cancellation and nulls out client itself (from inside the same thread — satisfying ENet's threading requirement).
  3. Calls Library.Deinitialize() and flips isLibInitialized = false.

The spin is ugly but necessary: Host.Dispose() must be called on the thread that created it, and there's no other safe place to wait for that.

ENetTransportOptions

A plain ScriptableObject / serializable config with three fields:

Field Default Purpose
ConnectTimeoutMs 10 000 Upper bound on ConnectAsync before it throws TimeoutException.
ServiceTimeoutMs 1 Per-iteration Host.Service timeout. Low values trade CPU for latency.
BufferSize 4 096 Size of the fixed send/receive byte buffers. Must exceed the largest expected Protobuf payload.

Serialized into PulseContainer.Settings so the options can be tuned per build without code changes.


ENet native libraries — build provenance

The native ENet libraries that back the managed ENet.cs P/Invoke wrapper (shipped by the com.decentraland.pulse.transport package) live in the repo at Explorer/Assets/Plugins/ENet/runtimes/. The bindings expect the native symbol library to resolve to the name enet (Windows) / libenet (macOS, Linux) — see the [DllImport] declarations in that package's Runtime/ENet.cs.

Keep this section in sync whenever any binary under runtimes/ is rebuilt or swapped, so the exact source and flags behind each slice stay auditable.

Upstream source: https://github.com/SoftwareGuy/ENet-CSharp

Building the macOS libraries

All commands run from the upstream repository root.

Note (CMake 4.x): Homebrew's CMake 4.x removed support for the cmake_minimum_required(VERSION 3.1) this project declares. Pass -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to configure anyway (shown below). It does not affect the output.

Universal binary (arm64 + x86_64 in one file) — via CMake:

cmake -S . -B build-mac \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
  -DCMAKE_POLICY_VERSION_MINIMUM=3.5
cmake --build build-mac --config Release

Windows & Linux libraries

The Windows (runtimes/win-x64/native/enet.dll) and Linux (runtimes/linux-x64/native/libenet.so) binaries were not built locally — they were taken directly from the upstream prebuilt release: https://github.com/SoftwareGuy/ENet-CSharp/releases/tag/autobuild-14c4dc5


Service and message model

One abstraction up from ITransport: PulseMultiplayerService owns the connection lifecycle (including the authentication handshake), routes incoming messages to registered handlers, and exposes a Send API. Everything above (PulseMultiplayerBus, PulseProfilePropagationBus) consumes this service through the IPulseMultiplayerService interface.

IPulseMultiplayerService

public interface IPulseMultiplayerService : IDisposable
{
    public bool IsAuthenticated { get; }

    public void Dispose();
    public void RegisterSyncHandler(ServerMessage.MessageOneofCase type, Action<IncomingMessage> handler);
    public void RegisterDisconnectHandler(Func<DisconnectReason, bool> handler);
    public void UnregisterAllHandlers();

    public UniTask ConnectAsync(CancellationToken ct);
    public void    Disconnect();
    public void    Send(OutgoingMessage outgoingMessage);

    public class Dummy : IPulseMultiplayerService { /* no-op everything; disposes OutgoingMessage in Send */ }
}

The Dummy is the feature-flag-off path — it returns immediately from every method and, crucially, disposes any OutgoingMessage passed to Send so the pool doesn't leak.

Protobuf envelopes — ClientMessage and ServerMessage

All Pulse traffic is framed as one of two Protobuf messages with a oneof payload, generated into Decentraland.Pulse:

Client → Server (ClientMessage.MessageOneofCase):

Case Payload Typical channel
Handshake HandshakeRequest (auth chain) RELIABLE
Input PlayerStateInput UNRELIABLE_SEQUENCED
Resync PlayerStateInput RELIABLE
ProfileAnnouncement PlayerStateInput (reused proto) RELIABLE
EmoteStart EmoteStart RELIABLE
EmoteStop EmoteStop RELIABLE
Teleport TeleportRequest RELIABLE

Server → Client (ServerMessage.MessageOneofCase):

Case Payload
Handshake HandshakeResponse (success, optional error)
PlayerJoined PlayerJoined
PlayerStateDelta PlayerStateDeltaTier0
PlayerStateFull PlayerStateFull
PlayerLeft PlayerLeft
PlayerProfileVersionAnnounced PlayerProfileVersionsAnnounced
EmoteStarted EmoteStarted
EmoteStopped EmoteStopped
Teleported TeleportPerformed

Note: Some client-side cases (Resync, ProfileAnnouncement) share the PlayerStateInput payload type — these are different actions framed in the same proto shape. Server-side responses are all distinct message types.

OutgoingMessage and IncomingMessage — pooled envelopes

Both are readonly struct wrappers that implement IDisposable; disposal returns the underlying Protobuf message to the correct per-case pool.

OutgoingMessage:

public readonly struct OutgoingMessage : IDisposable
{
    private static readonly ClientMessagePool POOL = new();
    public readonly ClientMessage Message;
    public readonly PacketMode PacketMode;

    public static OutgoingMessage Create(PacketMode packetMode, ClientMessage.MessageOneofCase kind) =>
        new(POOL.Get(kind), packetMode);

    public void Dispose() => POOL.Release(Message);
}

Usage is always var msg = OutgoingMessage.Create(PacketMode.RELIABLE, ClientMessage.MessageOneofCase.Handshake); msg.Message.Handshake.AuthChain = ...; pulseMultiplayerService.Send(msg); — the service transfers ownership to the transport, which using-disposes after send.

IncomingMessage:

public readonly struct IncomingMessage : IDisposable
{
    private static readonly ServerMessagePool POOL = new();
    public PeerId From { get; }
    public ServerMessage Message { get; }

    public static bool TryCreate(PeerId from, ReadOnlySpan<byte> data, out IncomingMessage message) { ... }
    public void Dispose() => POOL.Release(Message);
}

TryCreate has an interesting optimization: it extracts the MessageOneofCase directly from the first tag byte (data[0] >> 3) without fully parsing the wire bytes, uses that to get a correctly-typed ServerMessage from the pool, then calls MergeFrom(data) to populate it. This lets the pool pre-allocate the right sub-message shape for each case instead of allocating on every receive.

Warning: IncomingMessage.Dispose() returns the underlying proto to the pool. Handlers must not store references to Message, Message.PlayerStateDelta, or any sub-message — those references become invalid as soon as the handler returns. Copy anything that needs to outlive the callback.

MessagePacket — the transport-layer wrapper

public readonly ref struct MessagePacket
{
    public readonly ReadOnlySpan<byte> Data;
    public readonly PeerId FromPeer;
}

A ref struct because it carries a ReadOnlySpan<byte> pointing at ENetTransport.receiveBuffer — it can't escape the stack frame. The service thread constructs it in ReceiveIncomingMessage and hands it to MessagePipe.OnDataReceived; the pipe parses into an IncomingMessage right there (on the service thread) before the span goes out of scope.

ClientMessagePool / ServerMessagePool — per-case pooling

Connections/Pulse/PacketPool.cs defines two pools, one per envelope direction. Each internally holds a Dictionary<OneofCase, IObjectPool<TMessage>>, and Get(kind) / Release(message) routes to the right sub-pool by case:

private static readonly IReadOnlyDictionary<ClientMessage.MessageOneofCase, IObjectPool<ClientMessage>> POOLS = new Dictionary<...>
{
    [ClientMessage.MessageOneofCase.Handshake]   = CreatePool<HandshakeRequest>((packet, mes) => packet.Handshake = mes),
    [ClientMessage.MessageOneofCase.Input]       = CreatePool<PlayerStateInput>((packet, mes) => packet.Input = mes),
    [ClientMessage.MessageOneofCase.EmoteStart]  = CreatePool<EmoteStart>((packet, mes) => packet.EmoteStart = mes),
    // ...
};

The inner ThreadSafeObjectPool pre-allocates a fresh sub-message when a new pool entry is created (so packet.Handshake, packet.Input, etc. are non-null out of the pool) and clears Protobuf fields on release via packet.GetUnderlyingData()?.ClearProtobufComponent().

Release routes by message.MessageCase — so if a handler mutates the case, the message can end up in a different sub-pool than it came from. This is normally fine because the case is set at Create time and not changed.

MessagePipe — thread boundary and event stream

Connections/Pulse/MessagePipe.cs is the producer-consumer queue between the ENet service thread and the Pulse service:

public sealed class MessagePipe
{
    private readonly Channel<MessagePipeEvent> eventChannel = Channel.CreateUnbounded<MessagePipeEvent>();
    private readonly Channel<OutgoingMessage>  outgoingChannel = Channel.CreateUnbounded<OutgoingMessage>();

    public IAsyncEnumerable<MessagePipeEvent> ReadEventsAsync(CancellationToken ct) =>
        eventChannel.Reader.ReadAllAsync(ct);

    public bool TryReadOutgoingMessage(out OutgoingMessage message) =>
        outgoingChannel.Reader.TryRead(out message);

    public void Send(OutgoingMessage message) => outgoingChannel.Writer.TryWrite(message);

    public void OnDisconnected(DisconnectReason reason) =>
        eventChannel.Writer.TryWrite(MessagePipeEvent.FromDisconnectEvent(reason));

    public void OnDataReceived(MessagePacket packet)
    {
        if (IncomingMessage.TryCreate(packet.FromPeer, packet.Data, out IncomingMessage incomingMessage))
            eventChannel.Writer.TryWrite(MessagePipeEvent.FromMessage(incomingMessage));
    }
}

Two channels (System.Threading.Channels), both unbounded:

  • eventChannel — incoming events (IncomingMessage or DisconnectEvent). Written by the ENet thread; read by the Pulse service's routing loop.
  • outgoingChannel — outgoing messages. Written by Pulse callers (any thread); read by the ENet thread in SendOutgoingMessages.

MessagePipeEvent — tagged union

Connections/Pulse/MessagePipeEvent.cs uses the [REnum] source generator (from the REnum package) to emit a tagged-union readonly partial struct:

[REnum]
[REnumField(typeof(IncomingMessage), "Message")]
[REnumField(typeof(DisconnectEvent))]
public readonly partial struct MessagePipeEvent : IDisposable
{
    public readonly struct DisconnectEvent
    {
        public readonly DisconnectReason Reason;
        // implicit conversions to/from DisconnectReason
    }

    public void Dispose() => Match(static m => m.Dispose(), static _ => { });
}

This guarantees disconnects and messages flow through the same channel in wire order. A handler can rely on "every delta before the disconnect event was observed before the socket closed". Routing the disconnect through a side channel would lose that ordering.

Dispose() uses the source-generated Match to dispose the IncomingMessage variant (returning the proto to the pool) and no-op on the disconnect variant.

PulseMultiplayerService — connection lifecycle

Connections/Pulse/PulseMultiplayerService.cs orchestrates:

  • Connection with retries (MAX_CONNECT_ATTEMPTS = 3).
  • Authentication handshake (round-trip of HandshakeRequest/HandshakeResponse).
  • Routing loop (dispatch incoming messages to registered sync handlers).
  • Automatic reconnection after disconnect, if the disconnect handler returns true.

Connect — ConnectWithRetriesAsync:

for (var attempt = 1; attempt <= MAX_CONNECT_ATTEMPTS; attempt++)
{
    try { await ConnectInternalAsync(ct); return; }
    catch (TimeoutException) when (attempt < MAX_CONNECT_ATTEMPTS)
    {
        ReportHub.LogWarning(ReportCategory.MULTIPLAYER,
            $"Pulse connection attempt {attempt}/{MAX_CONNECT_ATTEMPTS} timed out, retrying...");
    }
}

Only TimeoutException and retriable PulseHandshakeDisconnectedExceptions (transport dropped before a HandshakeResponse for a transient reason) are caught and retried, with exponential backoff. A non-retriable handshake failure (rejection, BANNED, eviction, protocol violations — see PulseHandshakeDisconnectedException.IsRetriable) returns false immediately without burning the remaining attempts. Other failures propagate.

Authentication handshake — ConnectInternalAsync:

  1. transport.ConnectAsync(urlsSource.Url(DecentralandUrl.Pulse), PORT = 7777, ct) — ENet handshake.
  2. Register a one-shot sync handler for ServerMessage.MessageOneofCase.Handshake that completes a UniTaskCompletionSource<(bool, string?)>.
  3. Start the routing loop (StartRouting).
  4. Build and send a HandshakeRequest:
    var handshakePacket = OutgoingMessage.Create(PacketMode.RELIABLE, ClientMessage.MessageOneofCase.Handshake);
    handshakePacket.Message.Handshake.AuthChain = ByteString.CopyFromUtf8(BuildAuthChain());
    Send(handshakePacket);
  5. Await the handshake response. On an explicit failure response, Disconnect() and throw PulseHandshakeDisconnectedException(error). If the transport disconnects before any response arrives, the routing loop faults the pending completion with the same exception (instead of starting its own reconnection). ConnectWithRetriesAsync then retries with backoff for transient reasons, or gives up immediately for terminal ones.

BuildAuthChain() signs connect:/:{timestamp}:{} with the cached Web3 identity, builds the canonical x-identity-auth-chain-0/1/... + x-identity-timestamp + x-identity-metadata dictionary, and JSON-serializes it. Same auth-chain shape used across the platform, just delivered inside a Protobuf ByteString instead of HTTP headers.

Routing loop — StartRouting(connectionCt, parentCt):

UniTask.RunOnThreadPool(async () =>
{
    await foreach (MessagePipeEvent evt in pipe.ReadEventsAsync(connectionCt))
    {
        if (evt.IsDisconnectEvent(out DisconnectEvent disconnectEvent))
        {
            bool shouldReconnect = disconnectHandler?.Invoke(disconnectEvent) ?? false;
            if (shouldReconnect && !parentCt.IsCancellationRequested)
            {
                ResetConnectionLifecycle();
                await Task.Delay(RECONNECTION_DELAY_MS, parentCt);
                try { await ConnectAsync(parentCt); } catch { /* logged */ }
            }
            break;
        }

        if (!evt.IsMessage(out IncomingMessage message)) continue;
        try { if (syncHandlers.TryGetValue(message.Message.MessageCase, out var handler)) handler(message); }
        finally { evt.Dispose(); }
    }
}, configureAwait: false, cancellationToken: connectionCt).Forget();

Important details:

  • RunOnThreadPool + configureAwait: false keep continuations on the thread pool — matches the ENet transport pattern (avoids spurious main-thread hops).
  • The comment inside StartRouting warns against UniTask.Delay here — it schedules on the Unity player loop and would resume on the main thread. Task.Delay respects the null SynchronizationContext of thread-pool threads.
  • Two cancellation tokens: connectionCt (per-connection, cancelled on disconnect to stop routing cleanly) and parentCt (outer lifecycle; cancelling it stops reconnect attempts).
  • evt.Dispose() in finally returns the proto to the pool even if the handler throws.

Disconnect handler contract — RegisterDisconnectHandler(Func<DisconnectReason, bool>) — the boolean return decides whether to reconnect. This lets DUPLICATE_SESSION stop the loop (another client took over, don't fight it) while transient disconnects ask for a RECONNECTION_DELAY_MS = 10s cooldown followed by a fresh ConnectAsync. PulseMultiplayerBus wires the handler.

PulseHandshakeDisconnectedException

The single "handshake did not complete" exception, thrown in two places: by the routing loop (faulting the pending handshake completion with the DisconnectReason) when the transport disconnects before a HandshakeResponse arrives, and by PulseMultiplayerBus.HandshakeAsync when the server explicitly rejects the handshake (with the server-supplied error message, or "Handshake failed" if none).

Its IsRetriable flag drives ConnectWithRetriesAsync: transient reasons (NONE, GRACEFUL, AUTH_TIMEOUT, AUTH_FAILED, SERVER_FULL, PRE_AUTH_IP_LIMIT_EXHAUSTED, PRE_AUTH_BUDGET_EXHAUSTED) are retried with backoff; everything else — explicit rejections, BANNED, DUPLICATE_SESSION, replay/validation/corruption reasons, and any future unknown reason — is terminal and fails the connect attempt immediately.


Identity mapping

Pulse frames every message with a PeerId (a small server-assigned uint). The rest of the stack (movement inbox, emote bus, profile announcements, entity-participant table) works in Web3Address / wallet-ID strings. PeerIdCache is the translation layer between the two.

PeerId

Connections/Pulse/PeerId.cs — a readonly struct wrapping a uint:

public readonly struct PeerId : IEquatable<PeerId>
{
    public readonly uint Value;
    public PeerId(uint value) => Value = value;
    public static implicit operator uint(PeerId id) => id.Value;
    public bool Equals(PeerId other) => Value == other.Value;
    public override int GetHashCode() => (int)Value;
}

The struct exists purely to keep the app layer from assuming "peer identifier == uint" — its own doc comment says "Keeps the app domain separated from the transport assumption." If the transport ever moves off ENet, this abstraction stays intact.

PeerIdCache

PeerIdCache retains each announced peer's wallet and realm until that peer leaves. One peer per wallet is selected for movement, profile and emote routing. Reads, membership changes and selection share a lock.

The newest new-peer announcement selects the peer for a wallet. Reannouncing or teleporting an already-known shadow peer preserves selection. Announcement order does not prove session ownership, so either session can temporarily drive the avatar during overlap.

Keep the avatar while any peer for its wallet remains in the current realm. When the selected peer leaves, select the newest eligible survivor and restore its cached pose and emote. Teleports and realm cleanup follow the same membership rule; disconnect removes each wallet once. A join that selects the peer in the current realm cancels any still-queued removal for that wallet.

Shadow state updates refresh the cache without driving the avatar. Promotion also requests the survivor's current pose through the existing resync path to cover missed updates. Changing peers resets the local movement timeline: a stationary survivor's timestamp can precede the departing peer's last movement and would otherwise be discarded as stale.

Note: The field names peersByWallet / walletsByPeerId are inverted relative to the actual key types (peersByWallet is keyed by uint peer-id, not by wallet). Be careful reading call sites — trust the types, not the names.

RemoveAll and disconnect fan-out

On disconnect, PulseMultiplayerBus.RemoveAllPeers calls RemoveAll to enqueue one Pulse removal per wallet, then clears movement baselines, pending resyncs, cached emotes and queued intentions. Clearing retry generations also invalidates batches already collected by the main thread.

Cache callbacks run inside its lock. Keep lock ordering one-way: code holding an emote or removal-queue lock must not acquire the cache lock.

SELF_MIRROR_WALLET_ID — self-mirror sentinel

PulseMultiplayerBus.cs defines:

internal const string SELF_MIRROR_WALLET_ID = "self_mirror";

private Web3Address ResolveSelfMirrorWallet(string userId)
{
    if (userId != SELF_MIRROR_WALLET_ID)
        return new Web3Address(userId);

    return identityCache.EnsuredIdentity().Address;
}

The Pulse server can advertise a player with the literal string "self_mirror" as the user id. When the client sees it, ResolveSelfMirrorWallet rewrites the wallet to the local identity — effectively making the host's own avatar visible as a remote player for this connection.

This is a debug/testing affordance: it lets a developer run Pulse end-to-end on a single client by having the server mirror their own state back at them, and exercises the whole receive pipeline (HandlePlayerStateDelta, interpolation, animation blending) against the host's real wallet.

The only caller is HandlePlayerJoined in PulseMultiplayerBus.PlayerState.cs — once the wallet is resolved at join time, the rest of the bus treats the mirrored peer as any other remote player.

Use the sentinel in isolated development environments. If the same wallet is also announced as a regular peer, the normal overlap-selection rules apply.


PulseMultiplayerBus — the app-layer bus

PulseMultiplayerBus implements both IMovementMessageBus and IEmotesMessageBus over the IPulseMultiplayerService. It is split across six partial class files under Movement/Systems/:

File Responsibility
PulseMultiplayerBus.cs Fields, constructor, Dispose, SELF_MIRROR_WALLET_ID, SubscribeToIncomingMessages (handler registration), RemoveAllPeers shared teardown, and the Inbox helper.
.PlayerState.cs Movement send, handlers for PlayerJoined / PlayerLeft / PlayerStateFull / PlayerStateDelta, the resync state machine, proto ↔ NetworkMovementMessage marshalling.
.Teleports.cs BroadcastTeleport and HandleTeleport.
.Profiles.cs HandleProfileAnnouncement.
.Emotes.cs IEmotesMessageBus surface (Send, SendStop, intention bunches, retry), emote handlers and per-peer emote cache.
.Disconnects.cs HandleDisconnect — reconnect-or-bail decision.

The routing loop updates per-peer movement baselines, pending resyncs and peerEmotes. Emote intention queues and retry generations share emoteSync, since main-thread consumers collect batches and return deferred intentions.

.cs — subscription wiring

SubscribeToIncomingMessages is called by PulseContainer right after the bus is constructed. It registers one handler per ServerMessage.MessageOneofCase and the disconnect callback:

pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.PlayerJoined,                    HandlePlayerJoined);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.PlayerLeft,                      HandlePlayerLeft);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.PlayerStateFull,                 HandlePlayerStateFull);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.PlayerStateDelta,                HandlePlayerStateDelta);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.PlayerProfileVersionAnnounced,  HandleProfileAnnouncement);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.Teleported,                      HandleTeleport);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.EmoteStarted,                    HandleEmoteStarted);
pulseService.RegisterSyncHandler(ServerMessage.MessageOneofCase.EmoteStopped,                    HandleEmoteStopped);
pulseService.RegisterDisconnectHandler(HandleDisconnect);

HandleDisconnect calls RemoveAllPeers for the shared teardown described above.

.PlayerState.cs — movement send and receive

Send — Send(NetworkMovementMessage) on the IMovementMessageBus surface:

public void Send(NetworkMovementMessage message)
{
    if (isDisposed || !pulseService.IsAuthenticated) return;

    var clientMessage = OutgoingMessage.Create(PacketMode.UNRELIABLE_SEQUENCED, ClientMessage.MessageOneofCase.Input);
    WritePlayerStateInput(message, clientMessage.Message.Input);
    pulseService.Send(clientMessage);
}

WritePlayerStateInput serializes the NetworkMovementMessage into a PlayerStateInput proto: parcel index via parcelEncoder.Encode, parcel-relative position, velocity, quantized rotation, MovementBlend (clamped 0..3), SlideBlend, head yaw/pitch (only when enabled), GlideState enum cross-mapping, and a packed StateFlags uint (Grounded | LongJump | Falling | LongFall | Stunned | HeadYaw | HeadPitch).

Note: There's a // TODO Override the last movement message in the pipe as it doesn't make sense to send more than 1 — the intent is to collapse queued duplicate movement messages at the pipe level so the worker thread only ships the newest one per flush. Not implemented today.

Receive: HandlePlayerJoined — registers the peer and caches its initial pose. Selected peers enqueue the profile and pose; known shadows keep their cached state without taking over routing.

Receive: HandlePlayerLeft — removes the registration and its cached state. It restores a selected survivor or enqueues avatar removal when no registration remains in the current realm.

Receive: HandlePlayerStateFull — updates the cached sequence and pose, completing eligible recovery. Only the selected peer forwards movement to the inbox.

Receive: HandlePlayerStateDelta — the heart of the sequence-based resync logic:

             peer unknown ──► drop  (log warning)
             lastMessage missing ──► request resync on RELIABLE channel
             delta.NewSeq ≤ lastMovement.sequence ──► drop (stale)
             delta.BaselineSeq > lastMovement.sequence ──► request resync, drop delta
             delta.BaselineSeq == lastMovement.sequence ──► accept, clear pending resync
             delta.BaselineSeq <  lastMovement.sequence ──► drop (old resync delta)

Sequence numbers are per-peer; each delta references the baseline it was computed against. If the server's baseline is ahead of the client's known sequence, the client has missed packets and issues a ResyncRequest(subjectId, knownSequence) on the reliable channel. pendingResyncs acts as the dedup set — TryAdd returns false if a resync is already in flight.

Interleaving is deliberate: STATE_DELTA travels on UNRELIABLE_SEQUENCED (channel 1, unthrottled) while ResyncRequest/PlayerStateFull ride RELIABLE (channel 0). That's why a "normal" delta can sometimes arrive after a resync has been requested — if its BaselineSeq == lastSequence, it's the consecutive delta and the pending resync is cleared.

MergeIntoNetworkMovementMessage(last, delta) applies the Has… flags from the delta: every quantized field is optional in the proto, so the bus copies just the fields that changed, keeping the rest from last. Movement blend and velocity get their derived bookkeeping (velocitySqrMagnitude, movementKind inferred from blend value) recomputed.

TryUpdateLastMovementAndCompleteResync updates the per-peer movement baseline and completes packet-loss recovery. Promotion recovery remains pending until a full state or a consecutive delta can reset the selected peer's timeline. Shadow peers cache consecutive updates but wait until promotion to request missing state.

.Teleports.cs — teleport broadcast and reception

Send — BroadcastTeleport(realmName, worldPosition) builds a reliable TeleportRequest with parcel index + relative position + realm name:

var outgoing = OutgoingMessage.Create(PacketMode.RELIABLE, ClientMessage.MessageOneofCase.Teleport);
Vector2Int parcelIndex = worldPosition.ToParcel();
TeleportRequest teleport = outgoing.Message.Teleport;
teleport.ParcelIndex = parcelEncoder.Encode(parcelIndex);
teleport.Position = relativePosition.ToProtoVector();
teleport.Realm = realmName;
pulseService.Send(outgoing);

Receive — HandleTeleport updates the per-peer baseline with isInstant: true. Only the selected peer's pose reaches the inbox, where RemotePlayersMovementSystem snaps rather than interpolates.

.Profiles.cs — profile version announcements

A one-method partial. When the server says "peer X now has profile version V", the bus looks up the wallet and pushes a (wallet, version) pair into PulseIncomingProfileAnnouncements — that collector implements IRemoteAnnouncements, so MultiplayerProfilesSystem picks it up next frame through the RemoteAnnouncementsProxy (see multiplayer.md → Remote Players).

.Emotes.cs — emote send/receive and IEmotesMessageBus surface

State:

  • peerEmotes — each peer's latest emote sequence, URN, mask and active flag, retained until its registration is purged. The payload is copied out of the pooled incoming message.
  • emoteIntentions: HashSet<RemoteEmoteIntention> + emoteStopIntentions: HashSet<RemoteEmoteStopIntention> — per-frame outbound batches consumed by the emote proxy; both guarded by a shared MutexSync (see multiplayer.md → Deduplication & Concurrency Primitives for OwnedBunch / MutexSync).

IEmotesMessageBus.Send(urn, loopCyclePassed, mask, durationMs, playerState) — skips when loopCyclePassed is true (the local emote already finished a loop cycle, the server doesn't need to know again), builds a reliable EmoteStart with the emote urn, optional duration, and optional embedded PlayerStateInput so the server can fold a movement state update into the emote.

SendStop — emits a reliable EmoteStop. No subject id needed; the server knows who's sending.

OnPlayerRemoved(walletId) — no-op on Pulse (LiveKit clears a scheduler here; Pulse doesn't have one). The method exists to satisfy the interface.

SaveForRetry returns an intention to its originating bus. Pulse accepts it under emoteSync only if its local generation still matches the wallet's current emote. Selection changes, later emote events and cleanup invalidate older retries, including intentions already collected by the main thread. Source and generation are local metadata; payload equality still deduplicates equivalent transport messages.

HandleEmoteStarted / HandleEmoteStopped update the peer's cached emote and movement state. Pulse advances the snapshot sequence for each accepted emote start and stop, including stationary peers. A start can share a movement snapshot's sequence, but a new start cannot reuse its preceding stop's sequence; a stop therefore wins over a replayed start at the same sequence. A reliable start can override an equal-sequence movement snapshot; emoteStateMismatchCount records correction of a racing delta's inactive flag. A stop clears that flag even when its embedded pose is older than the cached pose.

Only selected peers emit avatar intentions. On selection changes, RestorePeerEmote replaces queued intentions for the wallet with the survivor's current emote or an explicit stop. Retaining the URN and mask matters: movement resync does not replay an emote start already delivered by Pulse.

IsPeerEmoting(Web3Address wallet) reports the selected peer's cached active flag to the debug system.

.Disconnects.cs — reconnect-or-bail

private bool HandleDisconnect(DisconnectReason reason)
{
    ReportHub.LogWarning(ReportCategory.MULTIPLAYER, $"Pulse transport disconnected: {reason}");
    RemoveAllPeers();
    return reason is DisconnectReason.NONE or DisconnectReason.GRACEFUL;
}

Every disconnect triggers RemoveAllPeers — every remote we knew about is evicted from the caches and surfaced as a RemoveIntention, so the entity/profile layer tears down remote avatars cleanly.

The return value is the reconnect flag consumed by PulseMultiplayerService.StartRouting. Only NONE (timeout) and GRACEFUL (clean server shutdown) ask the service to retry. AUTH_FAILED, DUPLICATE_SESSION, KICKED, SERVER_FULL, and AUTH_TIMEOUT return false — retrying those would just fail again or (for DUPLICATE_SESSION) fight another session.


Profile propagation over Pulse

Pulse implements DCL.Profiles.Self.IProfilePropagation with PulseProfilePropagationBus. The interface is two lines:

public interface IProfilePropagation
{
    void Propagate(Profile profile);

    public class Dummy : IProfilePropagation
    {
        public void Propagate(Profile profile) { }
    }
}

The Dummy is the feature-flag-off fallback — when Pulse is disabled, PulseContainer substitutes it so the rest of the code calls Propagate unconditionally and it becomes a no-op.

PulseProfilePropagationBus

Connections/Pulse/PulseProfilePropagationBus.cs:

public void Propagate(Profile profile)
{
    var message = OutgoingMessage.Create(PacketMode.RELIABLE, ClientMessage.MessageOneofCase.ProfileAnnouncement);

    message.Message.ProfileAnnouncement = new ProfileVersionAnnouncement
    {
        Version = profile.Version,
    };

    service.Send(message);
}

Key facts:

  • The wire message is ProfileVersionAnnouncement { Version } — only the version number. The full profile is fetched via HTTP by receivers through the normal IProfileRepository path, same as on LiveKit. Remote clients who already hold that version skip the fetch.
  • Sent on PacketMode.RELIABLE (channel 0) — profile announcements must arrive.

Where it's called from

DCL/UserInAppInitializationFlow/StartupOperations/StartPulseMultiplayerStartupOperation.cs announces the first version at connect:

protected override async UniTask InternalExecuteAsync(IStartupOperation.Params args, CancellationToken ct)
{
    if (!pulseActivation.IsActive) return;                        // disabled / --pulse false

    if (!await service.ConnectAsync(ct, maxAttempts: 5))          // unreachable after 5 attempts
    {
        pulseActivation.Deactivate();                            // full fallback to LiveKit-only
        return;
    }

    ProfileReadResult read = await selfProfile.ProfileAsync(ct);

    if (read.IsCancelled)
        throw new OperationCanceledException(ct);

    if (!read.IsOk(out _))
        throw new InvalidOperationException($"Own profile could not be resolved ({read}), nothing to propagate to Pulse");

    // Only the version the catalyst confirmed is propagated, never a pending edit's.
    Option<Profile> confirmed = selfProfile.CurrentProfileSnapshot.ConfirmedProfile;

    if (!confirmed.Has)
        return;

    await UniTask.SwitchToMainThread();
    profilePropagation.PropagateIfNewVersion(confirmed.Value);
}

On startup, connect to Pulse, read the host's profile, and announce the version the catalyst confirmed; while the first deploy is still in flight nothing is confirmed yet and the announcement is left to PropagateSelfProfileSystem. The connect is bounded to 5 attempts; if the server is unreachable the operation deactivates Pulse (full fallback to LiveKit) and lets login continue — see Start-up fallback. SelfProfile.ProfileAsync never throws: the read resolves to Ok(profile) or to a ProfileReadError, and by this point the avatar start-up operation has already resolved the profile, so a non-Ok read only means the identity was lost mid-flow.

Later versions reach Pulse through PropagateSelfProfileSystem (Movement/Systems/PropagateSelfProfileSystem.cs): every frame while PulseActivation.IsActive it reads SelfProfile.CurrentProfileSnapshot.ConfirmedProfile and calls PropagateIfNewVersion. The confirmed profile is the one the catalyst holds: while an edit is deploying it stays at the previous version, so peers are never told about a version they cannot fetch yet, and a failed deploy needs no retraction. PulseProfilePropagationBus sends only when the user or the version differs from the last announcement (and records nothing while unauthenticated), so the per-frame poll costs one comparison and no throttle is needed.


Incoming collectors

Both of these are the Pulse half of transport-neutral interfaces (IRemoteAnnouncements, IRemoveIntentions). The LiveKit halves are in livekit-networking.md; the proxies in multiplayer.md union the two.

PulseIncomingProfileAnnouncements

Profiles/Announcements/PulseIncomingProfileAnnouncements.cs:

public class PulseIncomingProfileAnnouncements : IRemoteAnnouncements
{
    private readonly ConcurrentQueue<RemoteAnnouncement> queue = new();

    public void Enqueue(string userId, int version) =>
        queue.Enqueue(new RemoteAnnouncement(version, userId, RoomSource.PULSE));

    public void Fill(List<RemoteAnnouncement> announcements)
    {
        while (queue.TryDequeue(out RemoteAnnouncement item))
            announcements.Add(item);
    }

    public void Remove(IReadOnlyCollection<RemoveIntention> removeIntentions) { }
}

Called by PulseMultiplayerBus:

  • HandlePlayerJoined — enqueues (userId, profileVersion) on first sight of a peer.
  • HandleProfileAnnouncement — enqueues on subsequent profile-version announcements from the server.

Both tag the announcement with RoomSource.PULSE so RemoteProfiles' pending-request tracking knows which transport sourced it (important for mergeable pending requests — see multiplayer.md → RemoteProfiles).

Uses a ConcurrentQueue because Enqueue may run on the routing thread while Fill runs on the main thread. Remove is a no-op — Pulse doesn't need to drop pending announcements when a player leaves; the removal flows through PulseRemoveIntentions instead.

PulseRemoveIntentions

Profiles/RemoveIntentions/PulseRemoveIntentions.cs:

public class PulseRemoveIntentions : IRemoveIntentions
{
    private readonly MutexSync mutexSync = new();
    private readonly HashSet<RemoveIntention> set = new();

    public void Enqueue(string walletId)
    {
        using (mutexSync.GetScope())
            set.Add(new RemoveIntention(walletId, RoomSource.PULSE));
    }

    public OwnedBunch<RemoveIntention> Bunch() => new(mutexSync, set);
}

Called by PulseMultiplayerBus:

  • HandlePlayerLeft — enqueues on explicit peer departure.
  • RemoveAllPeers (called from HandleDisconnect) — enqueues every known peer when the transport drops.

Guarded by MutexSync because enqueues arrive from the routing thread but Bunch() is consumed from the main thread — the OwnedBunch<RemoveIntention> pattern ensures the consumer holds the mutex while reading and clears the set on dispose. See multiplayer.md → Deduplication & Concurrency Primitives for the primitive.


PulseContainer wiring

Movement/Systems/PulseContainer.cs is an internal DCLWorldContainer<Settings> — it manages the Pulse stack's lifecycle and settings.

Construction

public static async UniTask<PulseContainer> CreateAsync(IPluginSettingsContainer pluginSettingsContainer,
    IWeb3IdentityCache web3IdentityCache, MovementInbox movementInbox, LandscapeData landscapeData,
    IDecentralandUrlsSource urlsSource, CancellationToken ct)
{
    var container = new PulseContainer(web3IdentityCache, movementInbox,
        new ParcelEncoder(landscapeData.terrainData), urlsSource);
    await container.InitializeContainerAsync<PulseContainer, Settings>(pluginSettingsContainer, ct);
    return container;
}

Two pieces allocated eagerly:

  • peerIdCache — a shared PeerIdCache used by the bus and internal consumers.
  • parcelEncoder — built from LandscapeData.terrainData (same Genesis-City-aware encoder used by multiplayer.md → Encoding). Re-exposed on MultiplayerContainer.ParcelEncoder so other systems can share it.

InitializeInternalAsync

Ordered construction of the internal stack:

transport = new ENetTransport(settings.ENetTransportOptions, messagePipe);
pulseMultiplayerService = pulseActivation.IsActive
    ? new PulseMultiplayerService(transport, messagePipe, urlsSource)
    : new IPulseMultiplayerService.Dummy();

pulseMultiplayerBus = new PulseMultiplayerBus(pulseMultiplayerService, peerIdCache, movementInbox,
    parcelEncoder, IncomingProfiles, RemoveIntentions, identityCache, settings.ReconnectionSettings, selfProfile, realmData);
pulseMultiplayerBus.SubscribeToIncomingMessages();

pulseProfilePropagationBus = pulseActivation.IsActive
    ? new PulseProfilePropagationBus(pulseMultiplayerService)
    : new IProfilePropagation.Dummy();

Observations:

  1. ENetTransport is always constructed, even when Pulse is off — it's just never connected. The service (not the transport) is what gets swapped for the Dummy.
  2. PulseMultiplayerBus is always constructed and wired to the service. When the service is a Dummy, SubscribeToIncomingMessages registers handlers that will never be called — harmless.
  3. Subscribe before connect — SubscribeToIncomingMessages runs in InitializeInternalAsync, but the first ConnectAsync happens later in StartPulseMultiplayerStartupOperation. Handlers must be ready before the routing loop starts producing events.

Settings

PulseContainer.Settings : IDCLPluginSettings has two serialized fields:

[field: SerializeField] public ENetTransportOptions ENetTransportOptions { get; private set; }
[field: SerializeField] public LandscapeDataRef LandscapeData { get; private set; }

Loaded via the standard IPluginSettingsContainer — the same mechanism other plugins use (see feature-flags.md for the pattern).

Dispose

lifeCycleCts?.SafeCancelAndDispose();
pulseMultiplayerBus?.Dispose();
pulseMultiplayerService?.Dispose();

Note: pulseMultiplayerService.Dispose() disposes the underlying ENetTransport too (transport.Dispose() is called from PulseMultiplayerService.Dispose), so the container doesn't need to dispose the transport separately.


Differences from LiveKit at a glance

Concern LiveKit Pulse
Topology Room-based (Island + Scene + Chat + VoiceChat, each a LiveKit room) Single client↔server peer connection
Underlying protocol WebRTC via the LiveKit client SDK Reliable-UDP via ENet, through the Pulse.Transport UPM package
Transport channels LiveKit data tracks with internal reliability choices Three explicit ENet channels (RELIABLE / UNRELIABLE_SEQUENCED-unthrottled / UNRELIABLE_UNSEQUENCED)
Authentication Per-room sign flow (Archipelago-served token, GateKeeper-served token, connection strings for voice) One-time handshake with a signed auth chain over channel 0
Framing Protobuf messages (MovementCompressed or Decentraland.Kernel.Comms.Rfc4.Movement) over LiveKit data pipes Protobuf ClientMessage/ServerMessage envelopes (oneof) over ENet packets
Movement encoding NetworkMessageEncoder bit-packing (160 bits per frame) when UseCompression is on PlayerStateInput proto with quantized fields + delta encoding (PlayerStateDeltaTier0)
Identity Per-room participants keyed by wallet (LKParticipant.Identity) Server-assigned PeerId uint mapped to wallets via PeerIdCache
Movement loss handling Periodic full-state via send system's adaptive rate Sequence-baselined deltas with on-demand ResyncRequest when a gap is detected
Profile announcement trigger MultiplayerProfilesSystem calls IProfileBroadcast.NotifyRemotes() each update, throttled by DebounceLiveKitProfileBroadcast; the broadcast reads SelfProfile.ProfileAsync StartPulseMultiplayerStartupOperation at connect, then PropagateSelfProfileSystem polling SelfProfile.CurrentProfileSnapshot.ConfirmedProfile with version dedupe in the bus
Profile wire payload Version only (AnnounceProfileVersion) Version only (ProfileVersionAnnouncement)
Disconnect semantics IConnectiveRoom state machine with per-room reconnection; DuplicateIdentity stops the loop Single-connection; reconnect only on NONE (timeout) or GRACEFUL disconnect reasons
Voice chat VoiceChatActivatableConnectiveRoom (on-demand room) ❌ Not supported — no voice track plumbing on Pulse
Text chat ChatConnectiveRoom ❌ Not supported — chat flows over LiveKit's chat room
Scene CRDT sync GateKeeper Scene room (host-only scope) ❌ Not supported — scene CRDT stays on LiveKit
Remove-intention delivery LiveKitRemoveIntentions subscribes to per-room participant Disconnected events from the LiveKit thread PulseRemoveIntentions populated by HandlePlayerLeft and RemoveAllPeers from the routing thread
Thread handoff primitive MutexSync + OwnedBunch<T> (identical) MutexSync + OwnedBunch<T> (identical)

In short: Pulse focuses on low-latency player-state traffic (movement, emotes, teleports, profile version announcements) and leaves chat, voice, and scene CRDT sync entirely to LiveKit. That's why both transports must be active for full functionality — Pulse is additive, not a replacement.


See Also

  • Multiplayer — transport-neutral hub: shared interfaces, movement pipeline, entity/profile tables, SDK propagation.
  • Network Synchronization — LiveKit transport, rooms, voice chat, message pipes.
  • Feature Flags — how FeaturesRegistry and FeatureId.Pulse are resolved.
  • App Arguments — --pulse and other launch flags.