Arena Manager

TGorillaArenaManager is the bottom layer of the Arena: it wraps RTL App Tethering, pairs the devices, keeps the list of who is connected, and mirrors entities between them.

Most applications never touch it directly — Session and Match descend from it and do the work. Use it on its own when you want the mirrored-object model and your own protocol on top.

Table of Contents

What It Does

The manager owns a TTetheringManager and a TTetheringAppProfile, both created for you and both disabled until Join(). Around them it adds:

  • Pairing and profile connection. Pairing two tethering managers only discovers them; until their profiles are connected nothing can be sent. ConnectRemoteProfiles closes that gap.
  • A user list. Every participant appears as an IGorillaArenaUser, kept in step through the UserList resource.
  • Entities. Objects that are serialised to JSON and mirrored to everybody.
  • A bind address. See Choosing a Network Card.

Identity

Two names, and they are not the same thing:

Property
Id Stable identity inside the group. Defaults to a fresh GUID and is meant to stay put for the manager's lifetime. Set it before Join() to carry an identity over from somewhere else.
DisplayName What other participants read. Free text, need not be unique.

Target names the group to join. Set it before Join(); changing it while joined disconnects and reconnects.

Joining and Leaving

FManager := TGorillaArenaManager.Create(Self);
FManager.DisplayName := 'Anna';
FManager.Join();
try
  // Myself exists from here on
  ShowMessage(FManager.Myself.GUIDStr);
finally
  FManager.Leave();
end;

Join() raises OnStatusChanged with Pending, brings tethering up, creates the local user, and reports Joined. Leave() reports None.

Property
Joined / Active True once Join() brought the manager up — with or without anybody else
HasPeers True when at least one remote manager is actually paired

Opening a table and waiting is a normal state, so Joined deliberately does not depend on a partner being there. Ask HasPeers for that.

Users

Users is a TGorillaArenaUserList, keyed by GUID. Myself is your own entry and exists only between Join() and Leave().

LEnum := FManager.Users.GetEnumerator();
try
  while LEnum.MoveNext() do
    Memo1.Lines.Add(LEnum.Current.Value.Id);
finally
  FreeAndNil(LEnum);
end;
  • UpdateUsers() asks every remote profile for its user list and raises OnUserEvent with Update for each.
  • CleanupUsers() drops users whose profile is gone and raises OnUserEvent with Remove.

To use a user class of your own, call SetUserClass() before joining:

FManager.SetUserClass(TMyArenaUser);

Entities

An entity is an object that exists on every device. Descend from TGorillaArenaEntity and give each property a getter and setter that go through GetJSONProperty / SetJSONProperty — no field declarations needed, the JSON document is the storage.

type
  TCard = class(TGorillaArenaEntity)
    protected
      function  GetSuit() : String;
      procedure SetSuit(const AValue : String);
    published
      property Suit : String read GetSuit write SetSuit;
  end;

function TCard.GetSuit() : String;
begin
  Result := GetJSONProperty('Suit').AsString().DeQuotedString('"');
end;

procedure TCard.SetSuit(const AValue : String);
begin
  SetJSONProperty('Suit', AValue);
end;

Changing a property updates the local resource, which the other side mirrors. When you change several at once, wrap them — otherwise every single assignment goes on the wire:

LCard.BeginUpdate();
try
  LCard.Suit  := 'hearts';
  LCard.Value := 12;
  LCard.Group := 'hand';
finally
  LCard.EndUpdate();
  LCard.Submit();
end;
Method
BeginUpdate / EndUpdate Hold back updates while several properties change
Submit Update the local resource and push the whole entity to everybody
Changed Update the local resource only

Events

Event
OnStatusChanged None, Pending or Joined
OnUserEvent Somebody was Added, Removed or Updated
OnMessage Log output — see Log() and DoOutputMessage
OnError Problems in plain text. Everything here also goes to OnMessage

Broadcast(AResource, AData) sends a string to a named resource on every connected profile — not to profiles discovery has merely seen. Greeting(AMessage) is a broadcast to the built-in Greeting resource.

Choosing a Network Card

On a machine with more than one IPv4 subnet — a VPN client, a virtual switch, a second card — the RTL multicast adapter writes the literal placeholder MULTIHOMED into its connection string instead of an address. Everybody who answers a discovery then opens a TCP connection to the host "MULTIHOMED", which resolves nowhere. Nobody finds anybody, and no error is visible.

EnsureBindAddress works around it by setting TTetheringManager.BindToAddress when the machine has two or more subnets. Which card it picks is a heuristic: the ordinary home and office ranges first, then anything else routable, and the carrier grade NAT range 100.64/10 last, because that is where VPN clients such as Tailscale live.

Set BindToAddress yourself to take the guess out, or override PickBindAddress. Note that TGorillaArenaSession already overrides it to return an empty string — see Session for why that is the better answer for a broadcast based session.

Extending It

SetupDefaultProfile is the extension point for declaring resources of your own:

procedure TMyManager.SetupDefaultProfile();
var LRes : TLocalResource;
begin
  inherited;

  LRes := FAppProfile.Resources.Add();
  LRes.Name := 'MyChannel';
  LRes.OnResourceReceived := MyChannelReceived;
end;

It is deliberately not called from the constructor: a virtual method called out of a constructor runs before the constructor of the descendant that overrides it, so the override would see its own fields still empty. The manager runs it on the first Join() instead.

Threading

The manager is not synchronized. It relies on the tethering manager delivering its events on the main thread, which is what TTetheringManager.SynchronizeEvents does and what it defaults to. Switch that off and every handler runs on a worker thread, and guarding the user and item lists becomes your job.

The only work the manager does off the main thread by itself is RequestRemoteResourcesAsync, and that one hands its result back through TThread.Queue.

See Also