Useful Functions Index for 39dll GML Scripts (Compiled by Saber Mage) --------------------------------------------------------------------- dllinit(dllname, bln usewinsock, bln usefilefunctions) Purpose: Initializes the DLL. Use it most often at the beginning of the game, and be sure to free the DLL upon game end. --------- --------- Arguments --------- > dllname - If this is a number, defaults to "39dll.dll." If it is a string, that string should define the DLL's name. > bln usewinsock - Whether or not to use winsock. You probably want to. > bln usefilefunctions - Whether or not you're going to use fileopen(), fileclose(), etc. Usually false. tcplisten(int portnum, int maxconnections, bln frzuntilgetmsg) Purpose: Creates a connection-listening socket on port portnum. Returns a socket ID if the socket init. was successful, else a number <= 0. --------- --------- Arguments --------- > int portnum - The port on which to listen for connections. > int maxconnections - The queue limit for attempted connections at once. This does not pertain to already-connected clients. > bln frzuntilgetmsg - Whether or not the game will freeze until it gets a message or connection. Usually false. tcpaccept(int socketid, bln nonblocking) Purpose: Checks the given socket's "waiting list" to see if someone attempted to connect to it. If someone did connect, it creates a new socket to be used for sending data to and receiving data from them, and returns that socket's ID. Else returns a number <= 0. You'll want to store the returned value, and use it to represent the just-connected player's player ID. --------- --------- Arguments --------- > int socketid - The socket whose waiting list should be checked for attempted connections. Often you'll want to use the socket returned by the tcplisten() function. > bln nonblocking - Whether or not the game will freeze until it gets a message. Usually false. udpconnect(int portnum, bln nonblocking) Purpose: Creates a UDP socket to be used to send messages through the UDP protocol on port portnum, and returns the socket's ID. This does not pertain to any certain player, though (I think) you will want to use a different port than you are using for the TCP messages (at least client-side). --------- --------- Arguments --------- > int portnum - The port through which the UDP data will be sent. > bln nonblocking - Whether or not the game will freeze until it gets a message. Usually false. (I'm not actually sure that this is what this argument represents, but it IS a boolean.) lastip() Purpose: Gets and returns the IP address of the last player accepted via tcpaccept(). This will be needed to send UDP messages to that player. --------- --------- Arguments --------- > NONEBOOYEAH!!1! tcpconnect(string ipaddress, int portnum, bln nonblocking) Purpose: Attempts to connect to a server computer at ipaddress on port portnum. If it is successful, it returns the socket ID through which data will be sent and received, else a number <= 0. --------- --------- Arguments --------- > string ipaddress - The IP address of the server computer. 127.0.0.1 for single-computer testing. > int portnum - The port through which to connect via tcp. > bln nonblocking - Whether or not the game will freeze until it gets a message. Usually false. tcpip(int socketid) Purpose: Returns the IP address of the user/player using the socket socketid. Used especially to get the server's IP using the socket ID returned by tcpconnect(). This IP would be used for e.g. sending UDP messages. --------- --------- Arguments --------- > int socketid - The ID of the socket "occupied" by the user whose IP address should be returned. clearbuffer() Purpose: Clears the internal buffer of any data. Should always be called before formatting then sending any message. --------- --------- Arguments --------- > NONEBOOYEAH!!1! writebyte(byte num) Purpose: Writes a byte to the internal buffer. The value num when this is used right after clearbuffer() usually indicates a/the message ID of the message to be stored in the internal buffer, then sent. --------- --------- Arguments --------- > byte num - A number from 0 to 255. sendmessage(int socketid) //TCP method [OR] sendmessage(int udpsocketid, string ipaddress, int portnum) //UDP method Purpose: Sends all of the data from the internal buffer as a message to either the user who "occupies" socket socketid, or to the user of IP address ipaddress on port portnum through socket udpsocketid. You would most likely use the UDP method for sending movement and x,y location info, while using the TCP method for more important things that are more okay to lag a bit such as chat and values such as health, etc. --------- --------- Arguments --------- > (They're fairly obvious and well-explained in above function descriptions. I think you can figure it out.) receivemessage(int socketid) Purpose: Checks for a message on socket socketid. The socketid can be either TCP or UDP. On both, a message < 0 means simply no message. However, a TCP result of 0 means that they player has disconnected, and a UDP result of 0 also means that there is no message. Anything greater than 0 represents the size of the message in bytes. This result is really only used to test whether or not a message was sent (or if the player d/ced). --------- --------- Arguments --------- > (Obvious) read_() //_ = byte, string, short, etc. Purpose: Reads from the last message received via receivemessage() after the part of the message that was last read via read_(). readbyte() in the beginning is usually used to determine the message ID. --------- --------- Arguments --------- > NONEBOOYEAH!!1! closesocket(int socketid) Purpose: Closes socket socketid. Wise to execute this upon game end on all opened sockets. --------- --------- Arguments --------- > (Obvious) dllfree() Purpose: Frees the memory used by loading the dll into the game. Wise to execute this upon game end. --------- --------- Arguments --------- > NONEBOOYEAH!!1! --------------------------------------------------- --------------------------------------------------- |An Example of A Constant Message-Receiving Script| --------------------------------------------------- [For Client and Server] - Step Event while(1) { size = receivemessage(global.udpsock); //try to receive a message on the udp socket if(size <= 0) size = receivemessage(global.otherplayer); //if no udp message try to receive a tcp message if(size < 0) break; if(size == 0) { show_message("The other player left the game"); game_end(); } messageid = readbyte(); switch(messageid) { case 0: y = readshort(); break; case 1: objBall.x = readshort(); objBall.y = readshort();: break; case 2: chatmessage = readstring(); show_message(chatmessage); break; } } --------------------------------------------------- --------------------------------------------------- |Server Examples | --------------------------------------------------- [Initial Connection] - Single Run joinsocket = tcplisten(750, 2, true); if(joinsocket <= 0) { show_message("Failed to listen on port 750"); } else { show_message("Listening on port 750"); global.online = 1; } [Join Attempt Checks] - Step Event joincheck = tcpaccept(joinsocket, true); if(joincheck > 0) { global.players += 1; global.p_id[joincheck] = global.players; //Use this to find the player number of a tcp socket. global.p_tcpsocket[global.players] = joincheck; //Use this to find the tcp socket of a player number. global.p_ip[global.players] = lastinIP(); global.p_udpport[global.players] = 753; global.udpsocket = udpconnect(755, true); show_message("Another player has connected."); } --------------------------------------------------- --------------------------------------------------- |Client Examples | --------------------------------------------------- [Initial Connection] - Single Run global.s_tcpsocket = tcpconnect(get_string("Enter the server IP address:","127.0.0.1"), 750, true); if(global.s_tcpsocket <= 0) { show_message("Unable to connect to server") } else { show_message("Connected to server") global.s_ip = tcpip(global.s_tcpsocket); global.s_udpport = 755; global.udpsocket = udpconnect(753, true); global.online = 1 }