Lobby Directory

Asking a player to read an IP address out to somebody over the phone is a poor thing to ask. The lobby directory replaces it: the host publishes the session it just opened, everybody else reads a list of what is open and picks one.

Two halves — a small PHP endpoint you host, and the TGorillaArenaDirectory component that talks to it. Both are optional; a session works without either.

Table of Contents

How It Works

   host                        directory                    client
   ----                        ---------                    ------
   OpenSession()
   Publish()      --register-->  lobby file
                  <--id, token--
   (every minute) --update---->  refreshed
                                    :
                                 list      <--list------     Refresh()
                                 (no address)  --------->    OnLobbies
                                    :
                                 address   <--join------     JoinLobby()
                                           ----------->      JoinSession(addr)
   ...    <--------------- tethering connects ----------->   ...
   Withdraw()     --unregister-> deleted

The listing carries no address. Only join hands one out, and the component feeds it straight into the session without letting it near the application.

The Delphi Side

uses
  Gorilla.Arena.Session, Gorilla.Arena.Directory;

FDirectory := TGorillaArenaDirectory.Create(Self);
FDirectory.Endpoint := 'https://example.com/arena/';
FDirectory.Session  := FSession;      // or FMatch

FDirectory.OnLobbies   := LobbiesArrived;
FDirectory.OnPublished := TablePublished;
FDirectory.OnJoined    := TableJoined;
FDirectory.OnError     := DirectoryError;

Every call goes out on a background thread and answers on the main thread, so nothing here blocks the user interface. Busy is true while a request is in flight.

Hosting a Table

Open the session first, then publish it:

FSession.OpenSession(PlayerName());
FDirectory.Publish('Anna''s table', 'beat', 'evenings');

From then on the component keeps the registration alive by itself — the directory drops anything it has not heard from for a few minutes. BeatInterval sets how often, and the default of a minute is well inside that window.

procedure TFormMain.TablePublished(const ASender : TObject;
  const AId, APublicAddress : String);
begin
  FLblStatus.Text := 'Your table is listed';
  // Worth showing: it is what somebody types in when the directory is
  // unreachable for them.
  FLblAddress.Text := APublicAddress;
end;

A fourth argument makes the table need a password. It is listed either way, marked Locked:

FDirectory.Publish('Private round', 'beat', '', 'thePassword');

Call Withdraw() when the session closes. Without it the table lingers in the list until it goes stale and people try to join something that is not there:

procedure TFormMain.CloseTable();
begin
  FDirectory.Withdraw();
  FSession.CloseSession();
end;

Update() pushes a change straight away rather than waiting for the next heartbeat — worth it right after the player count or the state visibly changed. The heartbeat reads both off the linked session by itself.

Joining One

FDirectory.JoinLobby(LSelectedId, PlayerName());
FDirectory.JoinLobby(LSelectedId, PlayerName(), LPassword);   // if Locked

That asks the directory where the table is and hands the answer to Session.JoinSession. OnJoined confirms it — with the table's name, and deliberately not with its address.

JoinLobby needs Session to be assigned; without one it reports the error no_session rather than doing nothing.

Showing the List

FDirectory.Refresh();
procedure TFormMain.LobbiesArrived(const ASender : TObject;
  const ALobbies : TGorillaArenaLobbies);
var LLobby : TGorillaArenaLobby;
begin
  FList.Clear();
  for LLobby in ALobbies do
    FList.Add(Format('%s — %s (%d/%d)%s',
      [LLobby.Name, LLobby.Host, LLobby.Players, LLobby.Max,
       IfThen(LLobby.Locked, ' 🔒', '')]));
end;
Field
Id Opaque handle — the only thing JoinLobby needs
Name, Host, Game, Note What to show
Players, Max How many are in, how many fit (0 = unsaid)
Locked Needs a password
Started The match is already running
Age, Seen Seconds since it opened, and since it was last heard from

RefreshInterval refreshes on a timer. Switch it on while the list is on screen and off again when it is not — it is a network request per tick, on somebody's mobile data:

FDirectory.RefreshInterval := 10;   // seconds; 0 turns it off

Errors

Everything reports through OnError with a code you can branch on and a message you can show.

Code
no_endpoint Endpoint was never set
unreachable No network, no DNS, or the server is down
bad_answer Something answered, but not this API
not_found The table is no longer open
locked Wrong or missing password
too_many This address already has as many tables as it may
rate_limited Too many requests — wait a moment
no_session JoinLobby without a Session

Without an OnError handler the message goes to OnNotice instead, so a failure is never silent.

The Server

The endpoint lives in <gorilla>\source\lib\arena\server. PHP 7.4 or newer, no database, no dependencies — one JSON file per lobby.

  1. Upload index.php, config.php, .htaccess and the data/ folder.
  2. Set secret in config.php to something random and private: php -r "echo bin2hex(random_bytes(32));"
  3. Make data/ writable.

Endpoint is then the URL of index.php.

Move the storage out of the document root if you can — set storage_dir to something like /var/lib/arena-lobbies. The shipped data/.htaccess refuses HTTP access, but that only works on Apache with AllowOverride on.

The full API, its error codes and every setting are documented in server/README.md.

What Is Hidden, and What Is Not

The listing never carries an address, and TGorillaArenaLobby has no field that could hold one. Only join hands one out — one table at a time, rate limited, and only while that table is open. So an address is never on public display and cannot be swept out of a listing.

It does reach the program that is joining, and it has to: a direct connection cannot be made to an address you do not have. Somebody who joins and watches their own network traffic can therefore see where they connected. Hiding it from them as well would need a relay carrying the game traffic, which is a different piece of software.

Three things reduce what is exposed in the meantime:

  • The lobby id is not the file name. Files are named after hash_hmac(id, secret), so knowing an id — they are all public — is not enough to guess the file that holds its address.
  • Secrets are stored hashed. The owner token as SHA-256, the password through password_hash. A leaked file does not let anybody take over a table.
  • Nothing is kept. No logs of who asked for what, no history.

Lifetime

Two clocks, and the shorter one wins:

lobby_ttl (default 300 s) How long a table stays listed without a heartbeat
lobby_max_lifetime (2 days) The hard cap: past this it is gone however faithfully it was refreshed

Expired tables are never served, and their files are deleted by a sweep that runs on roughly one request in ten.

Players Behind the Same Router

The directory knows a host by the address its request came from — on a home network that is the router, not the host. Somebody on the same network who connects to it reaches the outside of their own router, which refuses unless it forwards the port and loops it back inside ("NAT loopback"); few home routers do. The symptom is Connection refused for a table that is plainly listed.

So Publish — and every refresh after it — also reports the host's LAN addresses (Session.LocalAddresses, private IPv4 only, best first), and the server hands those out instead, but only to a joiner whose public address is the host's own: somebody already on that network. JoinLobby passes all of them to the session as a:port;b:port, and the session tries them in turn, so a virtual adapter (WSL, Hyper-V, VirtualBox, a VPN) that happens to be listed first costs a few seconds, not the join.

Needs the updated index.php on the server; an older server simply keeps handing out the public address.

Reaching the Host

The directory only passes an address along; it does not make the host reachable. Two devices on the same network are fine, and so is a host with a public address and the game port forwarded (TCP DirectPort, 47810 by default; for App Tethering the tethering ports). Behind a plain consumer NAT with nothing forwarded the connection will not come up — the directory will have done its job and the join will still fail.

Say so in the user interface rather than leaving people guessing: a join that succeeds at the directory and then goes quiet is otherwise indistinguishable from a slow one.

Trying It Out

php -S 127.0.0.1:8777 -t <gorilla>\source\lib\arena\server

tests\directorycheck.dpr in the Tumble & Roll example exercises the whole client against a running endpoint — publishing, listing, updating, locked tables, withdrawal and the error paths:

directorycheck http://127.0.0.1:8777/

See Also