Skip to content

Commit 9df5eea

Browse files
committed
api: Introduce NET_Status.
Fixed up several other return values as well. Fixes #137.
1 parent fbae75e commit 9df5eea

File tree

6 files changed

+99
-71
lines changed

6 files changed

+99
-71
lines changed

examples/echo-server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
3939
NET_Address *server_addr = NULL;
4040
if (interface) {
4141
server_addr = NET_ResolveHostname(interface);
42-
if (!server_addr || (NET_WaitUntilResolved(server_addr, -1) != 1)) {
42+
if (!server_addr || (NET_WaitUntilResolved(server_addr, -1) != NET_SUCCESS)) {
4343
SDL_Log("Failed to resolve interface for '%s': %s", interface, SDL_GetError());
4444
if (server_addr) {
4545
NET_UnrefAddress(server_addr);
@@ -61,7 +61,7 @@ int main(int argc, char **argv)
6161
void *vsockets[128];
6262
SDL_zeroa(vsockets);
6363
vsockets[0] = server;
64-
while (NET_WaitUntilInputAvailable(vsockets, num_vsockets, -1) == 1) {
64+
while (NET_WaitUntilInputAvailable(vsockets, num_vsockets, -1) >= 0) {
6565
NET_StreamSocket *streamsocket = NULL;
6666
if (!NET_AcceptClient(server, &streamsocket)) {
6767
SDL_Log("NET_AcceptClient failed: %s", SDL_GetError());

examples/resolve-hostnames.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main(int argc, char **argv)
2727
for (int i = 1; i < argc; i++) {
2828
NET_WaitUntilResolved(addrs[i], -1);
2929

30-
if (NET_GetAddressStatus(addrs[i]) == -1) {
30+
if (NET_GetAddressStatus(addrs[i]) == NET_FAILURE) {
3131
SDL_Log("%s: [FAILED TO RESOLVE: %s]", argv[i], SDL_GetError());
3232
} else {
3333
SDL_Log("%s: %s", argv[i], NET_GetAddressString(addrs[i]));

examples/simple-http-get.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(int argc, char **argv)
2121
for (int i = 1; i < argc; i++) {
2222
SDL_Log("Looking up %s ...", argv[i]);
2323
NET_Address *addr = NET_ResolveHostname(argv[i]);
24-
if (NET_WaitUntilResolved(addr, -1) == -1) {
24+
if (NET_WaitUntilResolved(addr, -1) == NET_FAILURE) {
2525
SDL_Log("Failed to lookup %s: %s", argv[i], SDL_GetError());
2626
} else {
2727
SDL_Log("%s is %s", argv[i], NET_GetAddressString(addr));
@@ -32,7 +32,7 @@ int main(int argc, char **argv)
3232
SDL_Log("Out of memory!");
3333
} else if (!sock) {
3434
SDL_Log("Failed to create stream socket to %s: %s\n", argv[i], SDL_GetError());
35-
} else if (NET_WaitUntilConnected(sock, -1) < 0) {
35+
} else if (NET_WaitUntilConnected(sock, -1) == NET_FAILURE) {
3636
SDL_Log("Failed to connect to %s: %s", argv[i], SDL_GetError());
3737
} else if (!NET_WriteToStreamSocket(sock, req, (int) SDL_strlen(req))) {
3838
SDL_Log("Failed to write to %s: %s", argv[i], SDL_GetError());

examples/voipchat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static void run_voipchat(int argc, char **argv)
290290
SDL_Log("SERVER: Resolving binding hostname '%s' ...", hostname);
291291
socket_address = NET_ResolveHostname(hostname);
292292
if (socket_address) {
293-
if (NET_WaitUntilResolved(socket_address, -1) < 0) {
293+
if (NET_WaitUntilResolved(socket_address, -1) == NET_FAILURE) {
294294
NET_UnrefAddress(socket_address);
295295
socket_address = NULL;
296296
}
@@ -315,7 +315,7 @@ static void run_voipchat(int argc, char **argv)
315315
SDL_Log("CLIENT: Resolving server hostname '%s' ...", hostname);
316316
server_addr = NET_ResolveHostname(hostname);
317317
if (server_addr) {
318-
if (NET_WaitUntilResolved(server_addr, -1) < 0) {
318+
if (NET_WaitUntilResolved(server_addr, -1) == NET_FAILURE) {
319319
NET_UnrefAddress(server_addr);
320320
server_addr = NULL;
321321
}

include/SDL3_net/SDL_net.h

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,39 @@ extern "C" {
173173
*
174174
* \returns SDL_net version.
175175
*
176+
* \threadsafety It is safe to call this function from any thread.
177+
*
176178
* \since This function is available since SDL_net 3.0.0.
177179
*/
178180
extern SDL_DECLSPEC int SDLCALL NET_Version(void);
179181

180182

183+
/**
184+
* A tri-state for asynchronous operations.
185+
*
186+
* Lots of tasks in SDL_net are asynchronous, as they can't complete until
187+
* data passes over a network at some murky future point in time.
188+
*
189+
* This includes sending data over a stream socket, resolving a hostname,
190+
* connecting to a remote system, and other tasks.
191+
*
192+
* The library never blocks on tasks that take time to complete, with the
193+
* exception of functions named "Wait", which are intended to do nothing but
194+
* block until a task completes. Functions that are attempting to do something
195+
* that might block, or are querying the status of a task in-progress, will
196+
* return a NET_Status, so an app can see if a task completed, and its final
197+
* outcome.
198+
*
199+
* \since This enum is available since SDL_net 3.0.0.
200+
*/
201+
typedef enum NET_Status
202+
{
203+
NET_FAILURE = -1, /**< Async operation complete, result was failure. */
204+
NET_WAITING = 0, /**< Async operation is still in progress, check again later. */
205+
NET_SUCCESS = 1 /**< Async operation complete, result was success. */
206+
} NET_Status;
207+
208+
181209
/* init/quit functions... */
182210

183211
/**
@@ -313,9 +341,10 @@ extern SDL_DECLSPEC NET_Address * SDLCALL NET_ResolveHostname(const char *host);
313341
* \param address The NET_Address object to wait on.
314342
* \param timeout Number of milliseconds to wait for resolution to complete.
315343
* -1 to wait indefinitely, 0 to check once without waiting.
316-
* \returns 1 if successfully resolved, -1 if resolution failed, 0 if still
317-
* resolving (this function timed out without resolution); if -1,
318-
* call SDL_GetError() for details.
344+
* \returns NET_SUCCESS if successfully resolved, NET_FAILURE if resolution
345+
* failed, NET_WAITING if still resolving (this function timed out
346+
* without resolution); if NET_FAILURE, call SDL_GetError() for
347+
* details.
319348
*
320349
* \threadsafety It is safe to call this function from any thread, and several
321350
* threads can block on the same address simultaneously.
@@ -324,7 +353,7 @@ extern SDL_DECLSPEC NET_Address * SDLCALL NET_ResolveHostname(const char *host);
324353
*
325354
* \sa NET_GetAddressStatus
326355
*/
327-
extern SDL_DECLSPEC int SDLCALL NET_WaitUntilResolved(NET_Address *address, Sint32 timeout);
356+
extern SDL_DECLSPEC NET_Status SDLCALL NET_WaitUntilResolved(NET_Address *address, Sint32 timeout);
328357

329358
/**
330359
* Check if an address is resolved, without blocking.
@@ -344,16 +373,18 @@ extern SDL_DECLSPEC int SDLCALL NET_WaitUntilResolved(NET_Address *address, Sint
344373
* host represented by the address.
345374
*
346375
* \param address The NET_Address to query.
347-
* \returns 1 if successfully resolved, -1 if resolution failed, 0 if still
348-
* resolving; if -1, call SDL_GetError() for details.
376+
* \returns NET_SUCCESS if successfully resolved, NET_FAILURE if resolution
377+
* failed, NET_WAITING if still resolving (this function timed out
378+
* without resolution); if NET_FAILURE, call SDL_GetError() for
379+
* details.
349380
*
350381
* \threadsafety It is safe to call this function from any thread.
351382
*
352383
* \since This function is available since SDL_net 3.0.0.
353384
*
354385
* \sa NET_WaitUntilResolved
355386
*/
356-
extern SDL_DECLSPEC int SDLCALL NET_GetAddressStatus(NET_Address *address);
387+
extern SDL_DECLSPEC NET_Status SDLCALL NET_GetAddressStatus(NET_Address *address);
357388

358389
/**
359390
* Get a human-readable string from a resolved address.
@@ -486,7 +517,8 @@ extern SDL_DECLSPEC void SDLCALL NET_SimulateAddressResolutionLoss(int percent_l
486517
*
487518
* \param a first address to compare.
488519
* \param b second address to compare.
489-
* \returns -1 if `a` is "less than" `b`, 1 if "greater than", 0 if equal.
520+
* \returns a value less than zero if `a` is "less than" `b`, a value greater than zero
521+
* if "greater than", zero if equal.
490522
*
491523
* \threadsafety It is safe to call this function from any thread.
492524
*
@@ -653,9 +685,10 @@ extern SDL_DECLSPEC NET_StreamSocket * SDLCALL NET_CreateClient(NET_Address *add
653685
* \param sock The NET_StreamSocket object to wait on.
654686
* \param timeout Number of milliseconds to wait for resolution to complete.
655687
* -1 to wait indefinitely, 0 to check once without waiting.
656-
* \returns 1 if successfully connected, -1 if connection failed, 0 if still
657-
* connecting (this function timed out without resolution); if -1,
658-
* call SDL_GetError() for details.
688+
* \returns NET_SUCCESS if successfully connected, NET_FAILURE if connection
689+
* failed, NET_WAITING if still connecting (this function timed out
690+
* without resolution); if NET_FAILURE, call SDL_GetError() for
691+
* details.
659692
*
660693
* \threadsafety You should not operate on the same socket from multiple
661694
* threads at the same time without supplying a serialization
@@ -666,7 +699,7 @@ extern SDL_DECLSPEC NET_StreamSocket * SDLCALL NET_CreateClient(NET_Address *add
666699
*
667700
* \sa NET_GetConnectionStatus
668701
*/
669-
extern SDL_DECLSPEC int SDLCALL NET_WaitUntilConnected(NET_StreamSocket *sock, Sint32 timeout);
702+
extern SDL_DECLSPEC NET_Status SDLCALL NET_WaitUntilConnected(NET_StreamSocket *sock, Sint32 timeout);
670703

671704
/**
672705
* The receiving end of a stream connection.
@@ -838,8 +871,9 @@ extern SDL_DECLSPEC NET_Address * SDLCALL NET_GetStreamSocketAddress(NET_StreamS
838871
* connection dropped later when your reads and writes report failures.
839872
*
840873
* \param sock the stream socket to query.
841-
* \returns 1 if successfully connected, -1 if connection failed, 0 if still
842-
* connecting; if -1, call SDL_GetError() for details.
874+
* \returns NET_SUCCESS if successfully connected, NET_FAILURE if connection
875+
* failed, NET_WAITING if still connecting; if NET_FAILURE, call
876+
* SDL_GetError() for details.
843877
*
844878
* \threadsafety You should not operate on the same socket from multiple
845879
* threads at the same time without supplying a serialization
@@ -850,7 +884,7 @@ extern SDL_DECLSPEC NET_Address * SDLCALL NET_GetStreamSocketAddress(NET_StreamS
850884
*
851885
* \sa NET_WaitUntilConnected
852886
*/
853-
extern SDL_DECLSPEC int SDLCALL NET_GetConnectionStatus(NET_StreamSocket *sock);
887+
extern SDL_DECLSPEC NET_Status SDLCALL NET_GetConnectionStatus(NET_StreamSocket *sock);
854888

855889
/**
856890
* Send bytes over a stream socket to a remote system.

0 commit comments

Comments
 (0)