Está en la página 1de 8

NetGMS Tutorial

NetGMS is a base set of scripts that handles GameMaker:Studio's Networking


system in a simple to setup fashion. This tutorial will go through and give an
explanation of each script and it's use in the framework.
Net_Setup( tcp , udp );
Net_Setup() is the initiation script for the NetGMS framework, calling
this script defines whether TCP and UDP will be used or not, as well as the
variables used by both the TCP and UDP sides of the NetGMS framework.
Framework Global Variables Initiate By Script:
If using TCP: NetTcp_Sck, NetTcp_Bfr, NetTcp_Con,
NetTcp_Dis, NetTcp_Ipa,
NetTcp_Prt, NetTcp_Que,
NetTcp_Lst.
If using UDP: NetUdp_Sck, NetUdp_Hst, NetUdp_Bfr,
NetUdp_Ipa,
NetUdp_Prt, NetUdp_Map.
Script Arguments:
tcp : Set to true to use TCP or false to not use TCP.
udp : Set to true to use UDP or false to not use UDP.
Script Note:
The NetGMS framework will throw errors if you do not call the
Net_Setup
script. Calling this script should be done before
attempting to use any other
scripts within the framework.
Script Return Value:
The script returns a default value of 0.

Net_TcpServer( port , maxclients , buffer size );


Net_TcpServer() creates a TCP based server socket. The socket ID for
the TCP server socket is stored in the NetTcp_Sck globalvar.
Extra Assets Created By Script:
buffer : fixed size buffer of a user defined size. This buffer is
stored in the
NetTcp_Bfr globalvar.
ds_list : empty ds_list for storing connected client socket IDs.
This ds_list is
stored in the NetTcp_Lst globalvar.
ds_list : empty ds_list for storing client socket IDs for a client to
request. This
ds_list is stored in the NetTcp_Que
globalvar.

Script Arguments:
port : Port to host the server socket on. Stored in the
NetTcp_Prt globalvar.
maxclients : Total number of clients that can connect to the
server.
buffer size : Size to use when creating the fixed sized buffer.
Script Note:
This script uses dynamic memory, make sure to delete the
memory via
Net_TcpDestory() when the sockets are no longer
needed.
The Net_TcpServer(), Net_TcpClient() and Net_TcpClientRaw()
scripts all
store their socket IDs and buffer IDs inside of the
NetTcp_Sck and NetTcp_Bfr
globalvars. So attempting to call
these scripts one after the other in one
project will result
in each socket and buffer being over written by the next
call of
the scripts. This will result in losing socket IDs and buffer IDs and will
result in a memory leak. To avoid this, call Net_TcpDestroy()
before calling
the next script.
Script Return Value:
1 if the server socket was successfully created, -1 if the server
socket failed to
be created.

Net_TcpClientRaw( port , url / ip , buffer size );


Net_TcpClientRaw() creates a raw based TCP client connection to a
server application not created with GameMaker: Studio. This script can be
used for connecting to a server made in C#, C++ or any other programming
language you're hosting a server on. The socket ID for the raw client
connection is stored in the NetTcp_Sck globalvar.
Extra Assets Created By Script:
buffer : fixed size buffer of a user defined size. This buffer is
stored in the
NetTcp_Bfr globalvar.
Script Arguments:
port : Port to create the raw client connection on(server port?).
Stored in the
NetTcp_Prt globalvar.
url / ip : The URL or IPv4 address the server is hosted on. Stored
in the
NetTcp_Ipa globalvar.
buffer size : Size to use when creating the fixed sized buffer.
Script Notes:

See the Net_TcpServer() script for notes for this script.


Script Return Value:
1 if the raw client socket was successfully created, -1 if the raw
client socket
failed to be created.

Net_TcpClient( port , url / ip , buffer size );


Net_TcpClient() creates a TCP based client connection to a server
created in GameMaker: Studio. This script cannot be used to create a raw
client socket, use the Net_TcpClientRaw() for raw client functionality. The
socket ID for the raw client connection is stored in the NetTcp_Sck globalvar.
Extra Assets Created By Script:
buffer : fixed size buffer of a user defined size. This buffer is
stored in the
NetTcp_Bfr globalvar.
Script Arguments:
port : Port to create the client connection on(server port?).
Stored in the
NetTcp_Prt globalvar.
url / ip : The URL or IPv4 address the server is hosted on. Stored
in the
NetTcp_Ipa globalvar.
buffer size : Size to use when creating the fixed sized buffer.
Script Notes:
See the Net_TcpServer() script for notes for this script.
Script Return Value:
1 if the client socket was successfully created, -1 if the client
socket failed to
be created.

Net_UdpServer( port , maxclients , url / ip , buffer size );


Net_UdpServer() creates a UDP server socket as well as a UDP client
socket. The server socket receives data while the client socket sends data.
Note: The client socket is no longer needed as of GMS update v1.3.1344. The
UDP scripts will be changed in the future for the v1.3.1344 update. The UDP
client socket is stored in NetUdp_Sck globalvar and the UDP server socket is
stored in NetUdp_Hst globalvar.
Extra Assets Created By Script:
buffer : fixed size buffer of a user defined size. This buffer is
stored in the
NetUdp_Bfr globalvar.
ds_map : ds_map for holding the IP address and server name of
servers found
using broadcasting. Broadcasting is done via

NetUdp_Cast(). This
NetUdp_Map globalvar.

ds_map is stored in the

Script Arguments:
port : Port to host the UDP sever on. Stored in the NetUdp_Prt
globalvar.
maxclients : UDP servers do not support client connections, set
to 0.
url / ip : The URL or IPv4 address to host the server on. Stored
in the
NetUdp_Ipa globalvar.
buffer size : Size to use when creating the fixed sized buffer.
Script Notes:
This script uses dynamic memory, make sure to delete the
memory via
Net_UdpDestory() when the sockets are no longer
needed.
Script Return Value:
1 if the client and server sockets were successfully created, -1 if
the client and
server sockets failed to be created.

Net_TcpDestroy(); and Net_UdpDestroy();


Net_TcpDestroy() and Net_UdpDestroy() will delete and reset all the
dynamic memory and variables used by the server and client creation scripts.
Script Return Value:
The script returns a default value of 0.
Net_Handle( script id );
Net_Handle() handles receiving all data from TCP and UDP sockets.
However, the script also handles receiving client connections and removing
clients from the server as they disconnect. When data is received, the data is
passed to a custom user inputted script via "script id". The data passed in
order: buffer ID, buffer size and packet ID.
Script Arguments:
script id : User defined script that will be used to pass to and
process the
received data. The user defined script here
should be used to identify
packets and process those
packets accordingly.
Script Notes:
When a client connects, the client's socket ID is stored in two
ds_lists,
NetTcp_Lst and NetTcp_Que. The ds_List,
NetTcp_Lst, stores the socket ID of
each client until each

client disconnects. The ds_list, NetTcp_Que, stores the


ID of each client until the client requests it's socket ID via Socket
Requesting.

socket

Script Return Value:


The script returns a default value of 0.

Net_UdpCast( packet id , server name );


Net_UdpCast() creates a basic packet with a packet ID(message
id/msgid) with the name of the server attached to the packet and
sends(broadcasts) the packet to a range of IP addresses under the same
subnet. For example, an IP address of "192.145.2.122'' has a subnet of
"192.145.2.x" meaning that all IP addresses with "192.145.2." in them will
receive this packet. The subnet/ip address is that of the UDP socket created
by Net_UdpServer().
Script Arguments:
packetid : The ID of the packet being broadcast.
server name : The name(as string) of the UDP server sending
the broadcast.
Script Return Value:
The script returns a default value of 0.

Net_Bytes( buffer id );
Net_Bytes() returns the total number of bytes written to the buffer from
the current seek position of the buffer. This script is a simple rename and
pass through for GameMaker: Studio's buffer_tell( buffer id ) function.
Script Return Value:
The script returns a default value of 0.
Net_Analyze( packet id );
Net_Analyze() checks the current connection of a client to a server to
see if the connection is still valid.
Script Return Value:
1 if the connection is still valid and -1 if the conneciton has been
lost.

Net_Inquire( buffer id );
Net_Inquire() this script is a followup script to Net_UdpCast(). If a

broadcats is received from another UDP server, Net_Inquire() can be used to


process the packet received from the broadcast. Net_Inquire() will read the
server name from the buffer and get the IP address of the server and store it
in the NetUdp_Map ds_map with server name as the key and IP address as
the value.
Script Return Value:
The script returns a default value of 0.

-- Socket Requesting Scripts -Socket requesting is a system where the server sends each client a packet,
telling the client to request it's socket ID from the server as a form of
identification.
Net_Enqueue( packet id ); (1. Used on Server)
Loops through all of the non-requested sockets in the NetTcp_Que
ds_list on the server and sends a verification packet to each socket. This
packet when received by the client with Net_Requeue() tells the client to
finalize requesting it's socket.
Script Return Value:
The script returns a default value of 0.

Net_Requeue( buffer id , packet id ); (2. Used on Client)


When a socket request packet sent by the server with Net_Enqueue() is
received, this script will send a finalization packet back to the server, telling
the server the client has successfully received it's requested socket ID on the
server.
Script Return Value:
The script returns a default value of 0.

Net_Dequeue( buffer id ); (3. Used on Server)


When the finalization packet sent from a client with Net_Requeue() is
received on the server, this script will remove the client's socket ID from the
socket request ds_lsit, NetTcp_Que.
Script Return Value:
The script returns a default value of 0.
-------------------------------------

Net_TcpAssets( asset id );
Net_TcpAssets() returns the value of the specified asset via asset id.
List of Assets: (asset IDs -> defined constants)
tcp_asset_sck(NetTcp_Sck) : Server / Client socket ID.
tcp_asset_bfr(NetTcp_Bfr) : Server / Client buffer ID.
tcp_asset_con(NetTcp_Con) : Last client socket ID to connect to the
server.
tcp_asset_dis(NetTcp_Dis) : Last client socket ID to disconnect from
the server.
tcp_asset_ipa(NetTcp_Ipa) : IP address the client(raw client) socket is
connected to.
tcp_asset_prt(NetTcp_Prt) : Port the client(raw client) socket is
connected on.
tcp_asset_lst(NetTcp_Lst) : (Server) List ID of client sockets connected
to the server.
tcp_asset_que(NetTcp_Que) : (Server) List ID of unrequested client
socket IDs.
tcp_asset_lsz(size of NetTcp_Lst) : (Server) Size of list in asset
tcp_asset_lst.
tcp_asset_qsz(size of NetTcp_Que) : (Server) Size of list in asset
tcp_asset_que.

Net_UdpAssets( asset id );
Net_UdpAssets() returns the value of the specified asset via asset id.
List of Assets: (asset IDs -> defined constants)
udp_asset_sck(NetUdp_Sck) : UDP "client" socket.
udp_asset_hst(NetUdp_Hst) : UDP "server" socket.
udp_asset_bfr(NetUdp_Bfr) : Server buffer ID.
udp_asset_ipa(NetUdp_Ipa) : URL / IP the server is hosted on.
udp_asset_prt(NetUdp_Prt) : Port the server is hosted on.
udp_asset_map(NetUdp_Map) : ds_map of servers foudn with
broadcasting.
udp_asset_msx(size of NetUdp_Map) : Size of list in asset
udp_asset_map.

Extra Constants:
network_error(-1) : used for checking if a socket has an error.
network_noerror(1) : used for checking if a socket has no error.

msg_queue(253) : preset packet id used for socket requesting.


msg_broadcast(254) : preset packet id used for broadcasting.
msg_analyze(255) : preset packet ID for validing the connection to the
server.
triggered_tcp(0) : used for checking if TCP received a packet.
triggered_udp(1) : used for checking if UDP received a packet.

También podría gustarte