Gorilla.Arena.Message Unit Reference

Overview

The Gorilla.Arena.Message unit defines the wire format the Arena session and match components speak: one JSON envelope carrying an envelope version, an application version, a type, its sender, a sequence number and a free form payload. It also provides the gate that keeps late and duplicated messages out.

Nothing in the unit knows what an application does with any of it.

Class Hierarchy

TObject
  ├── TGorillaArenaMessage
  └── TGorillaArenaSequenceGate

Constants

Envelope

Constant Value Description
ARENA_MESSAGE_VERSION 1 Version of the envelope itself, owned by the framework
ARENA_FIELD_VERSION v Envelope version
ARENA_FIELD_APPVERSION app Application version
ARENA_FIELD_TYPE type Message type
ARENA_FIELD_FROM from Sender id
ARENA_FIELD_SEQ seq Sequence number
ARENA_FIELD_DATA data Payload object

Message Types of the Match Layer

Constant Value Direction
ARENA_MSG_HELLO hello client → host
ARENA_MSG_LOBBY lobby host → all
ARENA_MSG_START start host → all
ARENA_MSG_TURN turn host → all
ARENA_MSG_STATE state host → all
ARENA_MSG_OVER over host → all
ARENA_MSG_MOVE move client → host

An application is free to use any other type name for messages of its own; they arrive at TGorillaArenaSession.OnAppMessage.

TGorillaArenaMessage

One message on the wire. It owns its payload object: build one, fill in Data through the helpers, hand it to the session — the session frees it once it is on its way.

Constructor

constructor Create(const AType : String; const AFrom : String = '';
  const AAppVersion : Integer = 0);

Prefer TGorillaArenaSession.NewMessage, which fills in the sender and the version for you.

Properties

Property Type Access Description
MsgType String read What the message is for
From String read/write Sender's member id
Seq Int64 read/write Sequence number, set by the session
Version Integer read Envelope version
AppVersion Integer read/write Application version
Data TJSONObject read The payload

Methods

Method Description
ToJSON() : String Serialises the complete envelope
class Parse(const AText : String; const AAppVersion : Integer; out AError : String) : TGorillaArenaMessage Reads a message off the wire. Returns nil and fills AError with something a user can be shown

Parse refuses a foreign envelope version and, unless AAppVersion is 0, a foreign application version. Pass 0 to accept whatever arrives.

Payload Helpers

All work on Data.

Method Description
SetStr(const AName, AValue : String)
GetStr(const AName : String; const ADefault : String = '') : String
SetInt(const AName : String; const AValue : Integer)
GetInt(const AName : String; const ADefault : Integer = 0) : Integer
SetBool(const AName : String; const AValue : Boolean)
GetBool(const AName : String; const ADefault : Boolean = false) : Boolean
SetIntArray(const AName : String; const AValues : TArray<Integer>)
GetIntArray(const AName : String) : TArray<Integer>
SetStrArray(const AName : String; const AValues : TArray<String>)
GetStrArray(const AName : String) : TArray<String>
SetObject(const AName : String; AValue : TJSONObject) Takes ownership of AValue
GetObject(const AName : String) : TJSONObject Borrowed — the message keeps owning it

TGorillaArenaSequenceGate

Keeps late and duplicate messages out. Tethering does not promise ordering across resources, and a resource can be delivered twice. Every sender numbers its own messages upwards, and anything not newer than what was already seen from that sender is dropped.

Method Description
Accept(const AFrom : String; const ASeq : Int64) : Boolean True when the message is new; remembers it in that case
Forget(const AFrom : String) Forgets one sender — call it when somebody leaves, so a returning participant starting over at 1 is not ignored for good
Clear() Forgets everybody

TGorillaArenaSession owns one and applies it to everything that arrives, so an application normally never touches this class.

Portability Note

The unit deliberately avoids TJSONObject.GetValue<T>(AName, ADefault): that overload is younger than some of the IDE versions the package is built for. Private helpers do the same job.

See Also