Created
August 31, 2021 18:22
-
-
Save BenDol/b3f44417043182c8583f84a3d2aee815 to your computer and use it in GitHub Desktop.
server async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @description Server network control | |
var socket_id = async_load[? "id"]; | |
if(server == socket_id) | |
{ | |
// If the socket ID is the server one, then we have a new | |
/// client connecting, OR an old client disconnecting | |
// get connect or disconnect (1=connect) | |
var t = async_load[? "type"]; | |
// Get the NEW socket ID, or the socket that's disconnecting | |
var sock = async_load[? "socket"]; | |
// Get the IP that the socket comes from | |
var ip = async_load[? "ip"]; | |
// Connecting | |
if (t == network_type_connect) { | |
log(self, "Attempting to connect: " + string(ip) + " , socket: " + string(sock), Logger.info, [LoggerType.server, LoggerType.network]); | |
// add client to our list of connected clients | |
ds_list_add(sockets, sock); | |
ds_list_add(login_sockets, sock); | |
// Create a new player | |
event_user(0); | |
// put this instance into a map, using the socket ID as the lookup | |
instance.__callback_id = undefined; | |
instance.__callback_data = undefined; | |
clients[? sock] = instance; | |
} else { | |
log(self, "Attempting to disconnect: " + string(ip) + " , socket: " + string(sock), Logger.info, [LoggerType.server, LoggerType.network]); | |
// disconnect a client, first find the player instance using the socket ID as a lookup | |
instance = clients[? sock]; | |
instance.connected = false; | |
// Fire the disconnected event | |
event_user(1); | |
// Also delete the socket from our global list of connected clients | |
var index = ds_list_find_index(sockets, sock); | |
ds_list_delete(sockets, index); | |
index = ds_list_find_index(login_sockets, sock); | |
if (index != -1) { | |
ds_list_delete(login_sockets, index); | |
} | |
// Delete the socket from out map, and kill the player instance | |
ds_map_delete(clients, sock); | |
instance_destroy(instance); | |
} | |
} | |
else if (ready && socket_id > 0) { | |
// All other sockets are connected client sockets, and we have recieved commands from them. | |
// get the buffer the data resides in | |
buffer = async_load[? "buffer"]; | |
var callback_id = buffer_read(buffer, buffer_string); | |
// read the command | |
opcode = buffer_read(buffer, buffer_s16); | |
// Get the socket ID - this is the CLIENT socket ID. We can use this as a "key" for this client | |
socket = socket_id; | |
// Look up the client details | |
instance = clients[? socket]; | |
if ((exists(instance)) && (instance.connected || opcode == OPCODE_LOGIN)) { | |
log(self, "Consuming network message ["+string(opcode)+"] for [" + instance_details(instance) + "] from [socket: " + string(socket) + "]", Logger.fine, [LoggerType.server, LoggerType.network]); | |
instance.__callback_data = ds_list_create(); | |
instance.__callback_id = callback_id; | |
ds_map_replace_list(callbacks, instance.__callback_id, instance.__callback_data); | |
// Fire the data revieved event | |
event_user(2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment