• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Networking: Attempt Connection with Non-Blocking Connect Multiple Times?

protosteel

Member
So: I am trying to set up a simple network, and it's going well, but a small issue is bothering me: upon switching to non-blocking connections so that my game doesn't freeze upon attempting a connection, I now can no longer attempt to connect the client to the server multiple times. Create event:
GML:
socket=network_create_socket(network_socket_tcp)
server=-1
network_set_config(network_config_connect_timeout, 5000)
network_set_config(network_config_use_non_blocking_socket,1)
Draw event:
GML:
if keyboard_check_pressed(vk_f2) and server==-1 {
    server=network_connect(socket,ipaddress,9999)
    if server>=0 {
        show_debug_message("ATTEMPTING TO CONNECT...")
    }
}
Async networking event:
GML:
if async_load[?"type"]==network_type_non_blocking_connect {
    if async_load[?"id"]==socket {
        if async_load[?"succeeded"]==1 {
            show_debug_message("CLIENT CONNECTED TO "+async_load[?"ip"])
        } else {
            server=-1
            show_debug_message("CLIENT FAILED TO CONNECT")
        }
    }
}
When it connects, everything works great, but when it doesn't connect (e.g. if the server is down), it assigns an ID for the server socket upon keypress (as per how non-blocking connects work), the connection attempt eventually times out and I reset this value to -1 in the async event, and then subsequent keypresses have no affect - it does not seem to run the "network_connect" function again, ostensibly because this server socket ID still exists (?). I have tried using network_destroy to remove it, but that crashes the game, possibly because it's not an actual socket I'm trying to destroy, just a weird placeholder. Help would be appreciated, thanks.
 
This sounds more like a technical issue than a programming question, so why not submit a bug report?

But let's work with what we have for the moment. How are you determining that network_connect no longer runs after a timeout? What happens if you destroy the socket before the timeout triggers?

In order for this infinitely waiting connection to be blocking future connection requests, that would mean all outgoing connections are handled by a single thread. Have you checked this is the case (e.g. connecting to two servers but making the first server be busy for a moment (trap it in an infinite loop?), and seeing if server2 still receives a connection before server 1 allows the connection?).

Also there appears to be code missing, for instance where is ipadress defined? I know that's just one variable but is there anything else missing?
 
Last edited:

protosteel

Member
This sounds more like a technical issue than a programming question, so why not submit a bug report?

But let's work with what we have for the moment. How are you determining that network_connect no longer runs after a timeout? What happens if you destroy the socket before the timeout triggers?

In order for this infinitely waiting connection to be blocking future connection requests, that would mean all outgoing connections are handled by a single thread. Have you checked this is the case (e.g. connecting to two servers but making the first server be busy for a moment (trap it in an infinite loop?), and seeing if server2 still receives a connection before server 1 allows the connection?).

Also there appears to be code missing, for instance where is ipadress defined? I know that's just one variable but is there anything else missing?
Thanks for the response! It inspired me to do a little trial and error, and for future reference, it seems like the winning formula was to destroy both the client socket and the server reference, and then recreate the client socket immediately, like so:
GML:
network_destroy(server)
network_destroy(socket)
socket=network_create_socket(network_socket_tcp)
server=-1
in the async networking event. I cannot be sure why this is necessary, and I do wonder if there's some sort of technical issue at the heart of this like you said. But as far as I can tell, this works.
 
So destroying a socket with a pending server connection causes a crash, but if you destroy the server first, the destroying the socket won't? Have you tried only destroying the server and not the socket?
 
Top