Skip to content

Commit 040e2ca

Browse files
committed
Use enums for tri-state return values
Resolves #112
1 parent 32f4bb3 commit 040e2ca

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

include/SDL3_net/SDL_net.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ extern "C" {
6161
(SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION >= Y) && \
6262
(SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION > Y || SDL_NET_MICRO_VERSION >= Z))
6363

64-
6564
/**
6665
* This function gets the version of the dynamically linked SDL_net library.
6766
*

src/SDL_net.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ typedef socklen_t SockLen;
7878
typedef struct sockaddr_storage AddressStorage;
7979
#endif
8080

81+
enum TriState {
82+
NET_FAILURE = -1,
83+
NET_WOULDBLOCK,
84+
NET_SUCCESS,
85+
};
86+
8187
typedef enum NET_SocketType
8288
{
8389
SOCKETTYPE_STREAM,
@@ -214,7 +220,7 @@ static bool SetGetAddrInfoErrorBool(const char *msg, int err)
214220
}
215221

216222
// this blocks!
217-
static int ResolveAddress(NET_Address *addr)
223+
static enum TriState ResolveAddress(NET_Address *addr)
218224
{
219225
SDL_assert(addr != NULL); // we control all this, so this shouldn't happen.
220226
struct addrinfo *ainfo = NULL;
@@ -225,23 +231,23 @@ static int ResolveAddress(NET_Address *addr)
225231
//SDL_Log("rc=%d", rc);
226232
if (rc != 0) {
227233
addr->errstr = CreateGetAddrInfoErrorString(rc);
228-
return -1; // error
234+
return NET_FAILURE; // error
229235
} else if (ainfo == NULL) {
230236
addr->errstr = SDL_strdup("Unknown error (query succeeded but result was NULL!)");
231-
return -1;
237+
return NET_FAILURE;
232238
}
233239

234240
char buf[128];
235241
rc = getnameinfo(ainfo->ai_addr, ainfo->ai_addrlen, buf, sizeof (buf), NULL, 0, NI_NUMERICHOST);
236242
if (rc != 0) {
237243
addr->errstr = CreateGetAddrInfoErrorString(rc);
238244
freeaddrinfo(ainfo);
239-
return -1; // error
245+
return NET_FAILURE; // error
240246
}
241247

242248
addr->human_readable = SDL_strdup(buf);
243249
addr->ainfo = ainfo;
244-
return 1; // success (zero means "still in progress").
250+
return NET_SUCCESS; // success (zero means "still in progress").
245251
}
246252

247253
static int SDLCALL ResolverThread(void *data)
@@ -1335,11 +1341,11 @@ NET_DatagramSocket *NET_CreateDatagramSocket(NET_Address *addr, Uint16 port)
13351341
return sock;
13361342
}
13371343

1338-
static int SendOneDatagram(NET_DatagramSocket *sock, NET_Address *addr, Uint16 port, const void *buf, int buflen)
1344+
static enum TriState SendOneDatagram(NET_DatagramSocket *sock, NET_Address *addr, Uint16 port, const void *buf, int buflen)
13391345
{
13401346
struct addrinfo *addrwithport = MakeAddrInfoWithPort(addr, SOCK_DGRAM, port);
13411347
if (!addrwithport) {
1342-
return -1;
1348+
return NET_FAILURE;
13431349
}
13441350
const int rc = sendto(sock->handle, buf, (size_t) buflen, 0, addrwithport->ai_addr, addrwithport->ai_addrlen);
13451351
freeaddrinfo(addrwithport);
@@ -1350,15 +1356,15 @@ static int SendOneDatagram(NET_DatagramSocket *sock, NET_Address *addr, Uint16 p
13501356
}
13511357

13521358
SDL_assert(rc == buflen);
1353-
return 1;
1359+
return NET_SUCCESS;
13541360
}
13551361

13561362
// see if any pending data can finally be sent, etc
13571363
static int PumpDatagramSocket(NET_DatagramSocket *sock)
13581364
{
13591365
if (!sock) {
13601366
SDL_InvalidParamError("sock");
1361-
return -1;
1367+
return NET_FAILURE;
13621368
}
13631369

13641370
while (sock->pending_output_len > 0) {
@@ -1367,7 +1373,7 @@ static int PumpDatagramSocket(NET_DatagramSocket *sock)
13671373
const int rc = SendOneDatagram(sock, dgram->addr, dgram->port, dgram->buf, dgram->buflen);
13681374
if (rc < 0) { // failure!
13691375
return -1;
1370-
} else if (rc == 0) { // wouldblock
1376+
} else if (rc == NET_WOULDBLOCK) {
13711377
break; // stop trying to send packets for now.
13721378
}
13731379

0 commit comments

Comments
 (0)