Networking problem GMS2 - client Async event doesn't happen

G

Gojken

Guest
I'm working on a pvp protoype and is exploring networking for that project, but I've run into problems: Server seems to be sending (I get a bytes sent count), but the client doesn't even throw the Async Networking Event.

- Server application *is* up and running.
- Client *can* connect with server.
- Client *can* send to server.
- Server *detects* client's disconnect.

Only thing not working: Server sending to client does throw off the necessary Async Networking Event.


Any suggestions on how to track down possible fault areas?
 

FrostyCat

Member
You're probably sending back to the wrong socket, e.g. the server sending the reply to the server socket instead of the client-bound socket, or misusing TCP-based code with a UDP-based connection. The only real way to track down fault areas is to trace out where socket IDs are stored/passed and where they're used.
 
G

Gojken

Guest
You're probably sending back to the wrong socket, e.g. the server sending the reply to the server socket instead of the client-bound socket, or misusing TCP-based code with a UDP-based connection. The only real way to track down fault areas is to trace out where socket IDs are stored/passed and where they're used.

Thanks for this, and I understand/agree.
Although TCP/UDP should probably be correct since I can receive *from* server, right?

Here goes my tracking (pulling out the essentials):

** Serverobject - Create:
server = network_create_server(network_socket_tcp, 6510,32);
clients = ds_map_create();

** Serverobject - Async Event
var eventid = async_load[? "id"];
var eventtype = async_load[? "type"];

if(server == eventid) {
// Client connect detected
if(eventtype == network_type_connect) {
var socket_id = async_load[? "socket"];
var ip = async_load[? "ip"];
var inst = instance_create_layer((64 + (floor(random_range(-32, 32)))), 192, "Instances", obj_player);

ds_map_add(clients, socket_id, inst);

} else {

var socket_id = async_load[? "id"]; // Get socket for the client that is sending stuff
var inst = clients[? socket_id]; // Look up instance of player object based in socket_id
var buff = async_load[? "buffer"]; //load the buffer
var cmd = buffer_read(buff, buffer_s16); // Pull out the command id from the buffer

switch (cmd) {
case LETTER_C2S_CMD:

var letter_from_client = buffer_read(buff, buffer_u8);
show_debug_message("Letter C2S = " + chr(letter_from_client)); // WORKS - so far, so good.


** Serverobject - Step Event

if (k <= 0) {
// send a new random letter to all clients in the DS list and reset k to kooldown
var letter = 70; // Fixed for now

var buff_send = buffer_create(256, buffer_grow, 1);

buffer_seek(buff_send, buffer_seek_start, 0); // Set buffer write position of buffer to pos 0
buffer_write(buff_send, buffer_s16, LETTER_S2C_CMD); // Set packet code/type
buffer_write(buff_send, buffer_u8, letter); // packet's payload the random letter code


var l = ds_map_find_first(clients); // Find the first client socket_id

// Loop through clients Map
while(!is_undefined(l)) { // keep looking for socket_ids
show_debug_message(" buff_send: " + json_encode(buff_send)); // Output is empty. Can you actually output the buffer like this?
// Can you actually output a buffer like this?

var aa = network_send_packet(real(l), buff_send, buffer_tell(buff_send)); // Send the buffer packet. 15 bytes are sent. Is that too little?

show_debug_message(" Buffer size sent to client: " + string(aa));
var l = ds_map_find_next(clients, l); // Find the next socket id after current one
}

k = KOOLDOWN;

} else {
k--; // decrease k



** Clientobject - Create

global.client_socket = network_create_socket(network_socket_tcp);
var a = network_connect(global.client_socket, global.serverip_to_join, 6510); // "127.0.0.1" is inputed by keyboard

/ Create a buffer for sending data to server
var buff_send = buffer_create(256, buffer_grow, 1);

// Set write position of the buffer to position 0
buffer_seek(buff_send, buffer_seek_start, 0);
buffer_write(buff_send, buffer_s16, NAME_CMD);
buffer_write(buff_send, buffer_string, player_name);

var aa = network_send_packet(global.client_socket, buff_send, buffer_tell(buff_send)); // WORKS since player name appears in server object. Confirmed.

** Clientobject - Async Event
// Nothing happens in here

show_message("Client's Async_load: " + json_encode(async_load)); // Can you actually do this?

var eventtype = ds_map_find_value( async_load , "type" );

switch(eventtype) {
case network_type_data:
var buffer = ds_map_find_value( async_load , "buffer" );
show_debug_message("Received buffer payload: " + json_encode(buffer));

show_message("Fire in the right hole!"); // NOT WORKING - no fire shot
break;
}

--------

When going through this I feel there is possibly something wrong with the buffer.
Any opinions and ideas are very welcome!

Adding to this, a debug message of the DS-map of send-buff says:
buff_send: { "ip": "127.0.0.1", "port": 6510.000000, "buffer": 0.000000, "size": 9.000000, "type": 3.000000, "id": 1.000000 }

This feels wrong. Is my buffer empty, and if so would this cause the client to disregard the packet and not fire the Async event?
 

FrostyCat

Member
You can use json_encode() on async_load (which holds a map ID), but you cannot use json_encode() on a buffer ID. That'll just grab the map with the same ID and encode that instead. If you want to output a buffer, use something like buffer_base64_encode() instead.
 
G

Gojken

Guest
You can use json_encode() on async_load (which holds a map ID), but you cannot use json_encode() on a buffer ID. That'll just grab the map with the same ID and encode that instead. If you want to output a buffer, use something like buffer_base64_encode() instead.

Thank you! I’ll modify and see what comes out from that.

I don’t know if had the stamina to look through my cut-out-code pieces. Do you agree with my assessment that the error should reside in the buffer creation?

Or does it matter somehow that I’m running the server app and client app on the same computer?
 
Top