- Title: Gudp Engine - Send Guaranteed UDP
- Description: Allows the user to send guaranteed UDP packets instead of TCP packets, allows your online game players to host their games without having to forward ports.
- GM Version:

- Registered: Yes
- File Type: .zip (contains a .gmk and a .dll)
- File Size: 82KB
- File Link: Click me
- Required DLLs: 39DLL V2.5
Last post update: 25/8/2011
Latest Gudp version: V508 (25/8/2011)
Introduction
One may wonder what advantage a Guaranteed UDP system has over the standard TCP. It is a well known fact that TCP is slow yet guaranteed, UDP is fast yet unreliable, Yet many overlook one more advantage UDP has: UDP Hole punching. UDP Hole punching is a nat traversal technique, in other words, when the server is protected by a router firewall, UDP hole punching allows the guest(client) to connect to the host(server) without the server having to forward the router ports. this is very convenient for the people who want to play your online game, because must people are not willing to play around with their router settings.
There is still one problem though, UDP does not send guaranteed packets
Gudp
And that is why I invented Gudp, which combines the Hole Punching power of UDP with the reliability of TCP.
Features:
- Simulates the features of TCP, simulates a connection oriented Protocol over UDP.
- Guranteed and ordered arrival of messages, just like TCP.
- UDP hole punching, players don't have to forward their ports. (assuming they know each others' IP addresses)
- Built-in timeout and keepalive system.
- Very modular, therefore can be easily implemented in an existing game.
- allows connections to multiple peers.
- does not interfere with regular UDP packets.
Scripts explained
Gudp contains two sets of scripts: "Gscripts", and "Gsys_DONT_EXECUTE". obviously, the latter is not meant to be executed by the user. this short guide will explain "Gscripts", It will not explain the behind-the-scenes of Gudp, it will only explain how to use Gudp. If you are curious though, see the commenting in Gsys_DONT_EXECUTE, and feel free to PM me regarding any questions
Gscripts
- G Init
- Ginit(Source UDP socket, Game Str);
- Gfree();
- Gconnect(IP, Port, Game Str); Return: GSockId
- Gdisconnect(GSockId);
- accept();
- Gaddsock(IP, Port); Return: GsockID
- Gpunch(IP, Port);
- Greceivemessage();
- Gsend(GSockID,[optional]BufferID);
- Gstep();
G Init
Ginit(Source UDP socket, Game Str); - This script will initlize Gudp, it must be executed once. The script will create an instance of the object Gudp, this object is only used as a variable storage for Gudp. argument0 is the udp socket Gudp will use (the one which is returned when executing udpconnect). argument1 is the GameStr, it is the string that the clients need to send in order to connect, this is done to make sure that only the proper clients are connecting.
Gfree(); - Frees Gudp by destroying all data structures then destroying the Gudp instance.
G Connect
Gconnect(IP, Port, Game Str,BufferID); Return: GSockId - Connects to a Gudp server, the game str must be equal to the game str in Ginit. returns a GsockID that will be used in later scripts.
Buffer ID is the buffer to send along the connection request (in other words, the buffer that the accept(); script will read), this usually contains username and password. leave empty or set to zero in order to send the default buffer, use -1 if you don't want to send a buffer.
usage example
clearbuffer(); //sending the default buffer writestring(username); writestring(password); Gconnect(serverip,serverport,"MyOnlinePlatformer",0); //the string(Game str) should be equal to the string used at Ginit in the server side. //example 2: clearbuffer(newbuff); //sending a non-default buffer writestring(username,newbuff); writestring(password,newbuff); Gconnect(serverip,serverport,"MYonlinePlatformer",newbuff);
Gdisconnect(GSockId); - No need to explain.
G Accept Connections
accept(); , and Gaddsock(IP, Port); Return: GsockID:
- accept is a script that SHOULD BE EDITED and customized to fit your game.
- accept is automatically triggered when a player is trying to connect to you (triggered by Gstep).
- to accept the connection, you must execute Gaddsock(argument0, argument1);
- to reject, simply don't execute Gaddsock();
- Gaddsock should only be executed inside accept();
- Gaddsock will return a GsockID, this should be stored for later use.
- in script accept() , argument0 represents the ip of the person trying to connect, argument1 represents the port.
- in script accept() , one must read from the buffer Gbuff, and not the default buffer.
exit;
ini_open("usernames.ini");
user=readshort(Gbuff);
pass=readshort(Gbuff);
if accept_player(user,pass)
{
var playerID,MyGsock,MyIP,MyPort;
MyGsock=Gaddsock(argument0,argument1);
playerID=instance_create(x,y,player);
PlayerID.MyIP=argument0;
PlayerID.MyPort=argument1;
playerID.name=user;
var i;
//-loop and get the i stuff-
global.players[i]=playerID;
clearbuffer(); //Gsend usage example
writebyte(129);
writestring("Welcome "+user);
Gsend(PlayerID.MyGsock);
}
Gpunch(IP, Port); - if we are behind a firewall and someone is trying to connect to us, this will make us punch the firewall and let them use Gconnect(); , note that we must know their IP and port, which is the drawback of hole punching.
G others
Greceivemessage([optional]bufferid); Return: message size or -1 - receives the next message and returns its' size, or returns -1 if there are no messages. note that Gudp adds a short at the beginning of any received message, this short represents the GsockID that received the message. also note that when a connection is lost UDP generates a mesid of 0.
Gstep(); - just put this in a step event, will cause an error if executed before Ginit.
step event example:
Gstep();
while (Greceivemessage()>=0)
{
var SOckid,player;
SOckid=readshort(); //this is NOT sent to us by anyone, it is generated by the local Gudp.
//the receiving GsockID.
mesid=readbyte();
with (Player)
{
if (MyGsock==SOckid)
{
player=id;
break;
}
}
switch(mesid)
{
case 20:
player.x=readshort();
player.y=readshort();
break;
case 0: //this is NOT sent to us by anyone, it is generated by the local Gudp.
with (player){show_message("connection lost :( ");}
break;
}
}
clearbuffer();
Gsend(GSockID,[optional]BufferID); - sends a message, works exactly like sendmessage();usage example - see the accept(); usage example above.
Change log:
Re uploaded V508
Exactly the same version, but removed a left-over unused script folder (Binaryconversion)
V508
Fixed two critical bugs:
- Removed simulated lag (which was used for testing and was never removed)
- Removed buggy example code.
V507 - First GMC version
To Do list:
- Fix the Keepalive bug: the lower the ping, the more frequent the keepalives are.
- Implement a smarter delay system: currently, when Gudp sends a message, it waits for an ack, if no ack is received after two seconds, the message is resent. this could slow down the communication whenever the ping is lower than 2000ms(two seconds), therefore, a smarter delay system will be implemented. (which will take the average ping into account).
- Include a simple online game example.
Edited by safwat1995, 25 August 2011 - 03:57 AM.













