BlitzMax/Modules/Networking/GameNet

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The GameNet module (GNet for short) provides a set of commands for creating and managing multiplayer network games.

GNet works a little differently than other networking libraries. Instead of being primarily 'message based', GNet works by synchronizing a collection of GNet objects over a network.

Each GNet object contains 32 &slots which are similar in nature to the fields of BlitzMax objects. You can write to these slots using the SetGNetInt, SetGNetFloat and SetGNetString commands, and read from these slots using the GetGNetInt, GetGNetFloat and GetGNetString commands. The actual meaning of the data contained in these slots is completely up to you, but will typically include such information as player position, score, hitpoints and so on.

Note that you can only modify GNet objects that you have yourself created. Such objects are known as local objects, while objects created elsewhere are known as remote objects.

To start using GNet, you must first create a GNet host with the CreateGNetHost command. Once you have created a host, you can either connect to other GNet hosts using GNetConnect, or prepare to accept connections from other hosts using GNetListen.

The GNetSync command brings all GNet objects up to date. This involves notifying other hosts about any modifications you have made to local GNet objects, and processing notifications from other hosts about any modifications to remote GNet objects.

Following a GNetSync, you can check which objects have been modified, created or closed using the GnetObjects command. This returns a linked list of GNet objects in a particular state.

GNet also provides a simple messaging system. A GNet message is actually just a special type of GNet object, so you initialize messages using the standard GNet commands for writing slots. Once created and initialized, a message can be sent to a remote object using the SendGNetMessage command.

Incoming messages can be processed using the GNetMessages command after a GNetSync. This function returns a linked list of messages objects which can be examined using the standard GNet commands for reading slots. In addition, the GNetMessageObject command can be used to determine which local object a message was intended for.

Functions[edit | edit source]

CreateGNetHost[edit | edit source]

Function CreateGNetHost:TGNetHost()

Description: Create GNet host

Returns: A new GNet host

Information: Once you have created a GNet host, you can use it to create objects with CreateGNetObject, connect to other hosts with GNetConnect and listen for connections from other hosts with GNetListen.

CloseGNetHost[edit | edit source]

Function CloseGNetHost( host:TGNetHost )

Description: Close a GNet host

Information: Once closed, a GNet host cannot be reopened.

GNetSync[edit | edit source]

Function GNetSync( host:TGNetHost )

Description: Synchronize GNet host

Information: GNetSync will update the state of all GNet objects. Once you have used this command, use the GNetObjects function to determine which objects have been remotely created, modified or closed.

GNetListen[edit | edit source]

Function GNetListen( host:TGNetHost,port )

Description: Listen for connections

Returns: True if successful, otherwise false

Information: Causes host to start listening for connection attempts on the specified port. Once a host is listening, hosts on other machines can connect using GNetConnect.

GNetListen may fail if port is already in use by another application, or if host is already listening or has already connected to a remote host using GNetConnect.

GNetConnect[edit | edit source]

Function GNetConnect( host:TGNetHost,address$,port,timeout_ms=10000 )

Description: Connect to a remote GNet host

Returns: True if connection successful, otherwise false

Information: Attempts to connect host to the specified remote address and port.

A GNet host must be listening (see GNetListen) at the specified address and port for the connection to succeed.

GNetObjects[edit | edit source]

Function GNetObjects:TList( host:TGNetHost,state=GNET_ALL )

Description: Get a list of GNet objects

Returns: A linked list

Information: GNetObjects returns a list of GNet objects in a certain state.

The state parameter controls which objects are listed, and can be one of &GNET_ALL, &GNET_CREATED, &GNET_MODIFIED or &GNET_CLOSED.

Note that with the exception of &GNET_ALL, the returned lists will only ever contain remote objects.

GNetMessages[edit | edit source]

Function GNetMessages:TList( host:TGNetHost )

Description: Get a list of GNet messages sent to local objects

Returns: A linked list

CreateGNetObject[edit | edit source]

Function CreateGNetObject:TGNetObject( host:TGNetHost )

Description: Create a GNet object

Returns: A new GNet object

CreateGNetMessage[edit | edit source]

Function CreateGNetMessage:TGNetObject( host:TGNetHost )

Description: Create a GNet message object

Returns: A new GNet object

SendGNetMessage[edit | edit source]

Function SendGNetMessage( msg:TGNetObject,toObject:TGNetObject )

Description: Send a GNet message to a remote object

GNetMessageObject[edit | edit source]

Function GNetMessageObject:TGNetObject( msg:TGNetObject )

Description: Get message target object

Returns: The object that msg was sent to

GNetObjectState[edit | edit source]

Function GNetObjectState( obj:TGNetObject )

Description: Get state of a GNet object

Returns: An integer state

Information:The returned value can be one of the following:

Object StateMeaning
GNET_CREATEDObject has been created
GNET_SYNCEDObject is in sync
GNET_MODIFIEDObject has been modified
GNET_CLOSEDObject has been closed
GNET_ZOMBIEObject is a zombie
GNET_MESSAGEObject is a message object

Zombie objects are objects that have been successfully closed and will never again be used by GameNet. Therefore, such objects will never appear in any list returned by the GNetObjects function.

GNetObjectLocal[edit | edit source]

Function GNetObjectLocal( obj:TGNetObject )

Description: Determine whether a GNet object is local

Returns: True if object is a local object

GNetObjectRemote[edit | edit source]

Function GNetObjectRemote( obj:TGNetObject )

Description: Determine whether a GNet object is remote

Returns: True if object is a remote object

SetGNetInt[edit | edit source]

Function SetGNetInt( obj:TGNetObject,index,value )

Description: Set GNet object int data

SetGNetFloat[edit | edit source]

Function SetGNetFloat( obj:TGNetObject,index,value# )

Description: Set GNet object float data

SetGNetString[edit | edit source]

Function SetGNetString( obj:TGNetObject,index,value$ )

Description: Set GNet object string data

GetGNetInt[edit | edit source]

Function GetGNetInt( obj:TGNetObject,index )

Description: Get GNet object int data

GetGNetFloat[edit | edit source]

Function GetGNetFloat#( obj:TGNetObject,index )

Description: Get GNet object float data

GetGNetString[edit | edit source]

Function GetGNetString$( obj:TGNetObject,index )

Description: Get GNet object string data

SetGNetTarget[edit | edit source]

Function SetGNetTarget( obj:TGNetObject,target:Object )

Description: Set a GNet object's target object

Information: This command allows you to bind an arbitrary object to a GNet object.

GetGNetTarget[edit | edit source]

Function GetGNetTarget:Object( obj:TGNetObject )

Description: Get a GNet object's target object

Returns: The currently bound target object

CloseGNetObject[edit | edit source]

Function CloseGNetObject( obj:TGNetObject )

Description: Close a GNet object