NET_WaitUntilConnected(3)

πŸ“¦
version 3.2.0 Β· 2 versions
, in fedora 43, fedora 44

πŸ’– NET_WaitUntilConnected β€” the "bestie just WAIT" page πŸ’–

okay so this is NET_WaitUntilConnected(3), from the SDL_net3 FUNCTIONS crew. it's a manual page, it's giving "let me pause your whole program for you," and honestly? iconic. let's get into it 🫢


✨ NAME ✨

NET_WaitUntilConnected β€” block until a stream socket has connected to a server. like literally just sit there and wait, no thoughts, head empty, until the socket is done doing its thing πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3_net/SDL_net.h>

NET_Status NET_WaitUntilConnected(NET_StreamSocket *sock, Sint32 timeout);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the NET_StreamSocket objects you get back from NET_CreateClient? they take time to do their work. that's why they do it asynchronously instead of making your program wait an indefinite amount of time. main character behavior honestly β€” they're off doing their own thing while you live your life ✨

but. BUT. if you want your program to sleep until the connection is complete? that's what this function is for, bestie. call it and go night-night 😴

this function takes a timeout value, and it's in milliseconds β€” that's how long it waits for resolution to complete. and here's the vibe check on the special numbers:

  • a timeout of -1 tells the library to wait indefinitely. forever. no cap, until the end of time πŸ’€
  • a timeout of 0 just checks the current status and returns immediately β€” and it's functionally equivalent to calling NET_GetConnectionStatus.
⚠️ important!! connections can fail after some time (server took awhile to respond at all, and then refused the connection outright), so be sure to check the result of this function instead of assuming it worked! do not assume. assuming is not the move. πŸ™ˆ

once a connection is successfully made, the socket may read data from, or write data to, the connected server. slay, that's the whole point πŸŽ€

and if you don't want your program to block? you can call NET_GetConnectionStatus() from time to time until you get a non-zero result. casual. lowkey polling. we love her 🧚


πŸ‘‘ FUNCTION PARAMETERS πŸ‘‘

  • sock β€” the NET_StreamSocket object to wait on. this is the one you're waiting for, your little guy 🌈
  • timeout β€” number of milliseconds to wait for resolution to complete. -1 to wait indefinitely, 0 to check once without waiting. that's it, that's the rule ⭐

πŸ”₯ RETURN VALUE πŸ”₯

  • returns NET_SUCCESS if successfully connected πŸ’–
  • returns NET_FAILURE if connection failed 😭
  • returns NET_WAITING if still connecting (this function timed out without resolution) β€” she's still cooking πŸ“

and if you got NET_FAILURE, call SDL_GetError() for details. go get the tea, it'll tell you what went wrong.


πŸ’œ THREAD SAFETY πŸ’œ

you should not operate on the same socket from multiple threads at the same time without supplying a serialization mechanism. like, don't. it's not that kind of party.

however, different threads may access different sockets at the same time without problems. so everyone can have their own. sharing is not caring here πŸ’…


🌈 AVAILABILITY 🌈

this function is available since SDL_net 3.0.0. she's been here. she's a legend. periodt.


🌟 SEE ALSO 🌟


and that's the page!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«ΆπŸ˜­πŸ”₯πŸ‘‘πŸŒˆβ­πŸ’€πŸ™ˆπŸ“πŸ§šπŸ’œ

okay so like, this whole thing is from the SDL_net manual, version SDL_net 3.2.0 β€” shoutout to the source, couldn't have said it better myself (well, except for all the parts I did say better, obviously). anyway bestie, go forth and wait on your sockets responsibly πŸ’–

πŸ“¦ where this page comes from