Gorilla.Arena.Directory Unit Reference

Overview

The Gorilla.Arena.Directory unit provides TGorillaArenaDirectory, the client side of the Arena lobby directory. A host publishes the session it just opened; everybody else reads a list and picks one, instead of typing an IP address.

It talks to the PHP endpoint in <gorilla>\source\lib\arena\server. Every call is made on a background thread and answers on the main thread, so nothing here blocks the user interface.

The address never surfaces. A listing does not carry one, TGorillaArenaLobby has no field that could hold one, and the address JoinLobby receives goes straight into the session's Target without passing through any published property or event parameter.

Class Hierarchy

TComponent
  └── TGorillaArenaDirectory

TThread
  └── TArenaDirectoryTicker         (internal)

Records

TGorillaArenaLobby

Field Type Description
Id String Opaque handle; the only thing JoinLobby needs
Name String What the list shows
Host String Name of whoever opened it
Game String Which game, as the application names it
Note String One short line
Players Integer How many are in
Max Integer How many fit, or 0 when unsaid
Locked Boolean Needs a password to join
Started Boolean The match is already running
Age Integer Seconds since it was opened
Seen Integer Seconds since it was last heard from

TGorillaArenaLobbies = TArray<TGorillaArenaLobby>

Event Types

Type Signature
TGorillaArenaLobbiesEvent procedure(const ASender : TObject; const ALobbies : TGorillaArenaLobbies) of object
TGorillaArenaPublishedEvent procedure(const ASender : TObject; const AId : String; const APublicAddress : String) of object
TGorillaArenaJoinedEvent procedure(const ASender : TObject; const ALobbyId : String; const ALobbyName : String) of object
TGorillaArenaDirectoryError procedure(const ASender : TObject; const ACode : String; const AMessage : String) of object

APublicAddress in TGorillaArenaPublishedEvent is our own address, which is the one thing worth showing — it is what somebody types in when the directory cannot be reached. TGorillaArenaJoinedEvent deliberately has no address parameter.

IArenaDirectoryLifetime

GUID {4B7A1E62-2D93-4F58-9C10-3A6E5D84B7F1}

Tells a background request whether the component that started it is still alive. A request hands its result back through TThread.Queue, which can run after the component has been destroyed; the thread holds a reference to this token, so it outlives the component.

Member Description
Alive : Boolean The owner is still there
Terminate() Marks it gone. Called from the destructor

TGorillaArenaDirectory

Published Properties

Property Type Default Description
Endpoint String URL of the directory endpoint (index.php)
Session TGorillaArenaSession The session to publish and to join with
Timeout Integer 8000 Milliseconds to wait for the endpoint
BeatInterval Integer 60 Seconds between heartbeats. Clamped to a minimum of 5
RefreshInterval Integer 0 Seconds between automatic refreshes; 0 for none
OnLobbies TGorillaArenaLobbiesEvent A list arrived
OnPublished TGorillaArenaPublishedEvent Our lobby is in the directory
OnJoined TGorillaArenaJoinedEvent We are on our way into somebody's lobby
OnError TGorillaArenaDirectoryError Something went wrong
OnNotice TGorillaArenaNoticeEvent Something worth telling the user

Refresh works without a Session. Publish reads the member count off it, and JoinLobby cannot work at all without one.

Public Properties

Property Type Description
IsPublished Boolean Our own lobby is in the directory
Busy Boolean A request is in flight
LobbyId String Our own lobby's id, or ''

The owner token that proves the registration is ours is held privately and is never exposed.

Public Methods

Method Description
Refresh() Fetches the open lobbies. Answers through OnLobbies
Publish(const AName : String; const AGame : String = ''; const ANote : String = ''; const APassword : String = '') Puts our session into the directory and starts the heartbeat. Calling it while already published does an Update instead
Update() Pushes the current player count and state. The heartbeat does this by itself
Withdraw() Takes our lobby out. IsPublished is false immediately, whatever the endpoint answers
JoinLobby(const ALobbyId : String; const ADisplayName : String; const APassword : String = '') Asks where the lobby is and hands that to Session.JoinSession
Lobbies() : TGorillaArenaLobbies A snapshot of the last list fetched

Protected Methods

Method Description
Request(const AAction : String; APayload : TJSONObject; out AResponse : TJSONObject; out ACode, AError : String) : Boolean One synchronous round trip. virtual — a test seam for the network. The caller owns AResponse
RunAsync(const AWork : TProc) Runs work on a background thread with the lifetime token held
Sync(const AWork : TProc) Queues work to the main thread, unless the component is gone
EnsureTicker() Starts the ticker when there is something to tick, stops it otherwise
Tick() One tick on the main thread: does whatever came due
ParseLobbies(AResponse : TJSONObject) : TGorillaArenaLobbies Reads the lobby array of a list answer
DoError, DoNotice, DoLobbies Raise the matching events

Error Codes

Reported through OnError as ACode.

Code Description
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 lobby is no longer open
forbidden Wrong owner token
locked Wrong or missing password
too_many This address already has as many lobbies as it may
rate_limited Too many requests
full The directory is at its limit
no_session JoinLobby without a Session

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

Behaviour Notes

The heartbeat is a thread, not a timer. A TTimer needs a platform service and a message loop that a console or service application does not have, and it would make this component depend on FireMonkey. The ticker wakes four times a second, and the work itself is still queued to the main thread.

The clock is monotonic. Due times are measured on a TStopwatch, not on the wall clock — a clock that jumps backwards would otherwise stop the heartbeat for as long as the jump was.

A lobby the directory has forgotten is dropped locally. An update that comes back not_found clears LobbyId and the token and stops the heartbeat, so a later Publish makes a fresh registration rather than refreshing a ghost. Other update failures are treated as passing network trouble and left to the next heartbeat.

The destructor does not withdraw. That is a network round trip, and a destructor is no place to wait for one; the lobby goes stale by itself within a few minutes. Call Withdraw() when the session closes.

See Also