From cd797bc64a475b99b40fa3f67e0ae74fb52bfc7f Mon Sep 17 00:00:00 2001 From: nitroxis Date: Sat, 18 Jan 2014 02:58:47 +0100 Subject: [PATCH] Workaround for non-blocking TCP sockets. --- irc.c | 10 +++++++++- lhnet.c | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/irc.c b/irc.c index 3d003e11..48ae7e31 100644 --- a/irc.c +++ b/irc.c @@ -64,14 +64,22 @@ static int IRC_Connect(const char *addr) return 0; } +#ifdef SUPPORTIPV6 + if (!LHNETADDRESS_Resolve(&peeraddress, addr, 6667)) + { + Con_Printf("[IRC] Bad server address: %s.\n", addr); + return 0; + } +#else if (!LHNETADDRESS_FromString(&peeraddress, addr, 6667)) { Con_Printf("[IRC] Bad server address: %s.\n", addr); return 0; } +#endif // this should really be non-blocking, but it does not work. - if(!(irc_socket = LHNET_OpenSocket(&address, &peeraddress, 1, 1, 0))) + if(!(irc_socket = LHNET_OpenSocket(&address, &peeraddress, 1, 0, 0))) { Con_Printf("[IRC] Failed to open a socket.\n"); return 0; diff --git a/lhnet.c b/lhnet.c index 9eae3d1d..6781f2ec 100644 --- a/lhnet.c +++ b/lhnet.c @@ -1022,7 +1022,8 @@ lhnetsocket_t *LHNET_OpenSocket(lhnetaddress_t *address, lhnetaddress_t *peeradd #else char _true = 1; #endif - if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, use_blocking ? &_false : &_true) != -1) + if (1) + // if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, use_blocking ? &_false : &_true) != -1) #endif { #ifdef IPV6_V6ONLY @@ -1059,25 +1060,34 @@ lhnetsocket_t *LHNET_OpenSocket(lhnetaddress_t *address, lhnetaddress_t *peeradd { if (LHNETSOCKET_TryConnect(lhnetsocket, peeraddress) != -1) { - int i = 1; - // enable broadcast on this socket - setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)); -#ifdef IP_TOS - { - // enable DSCP for ToS support - int tos = lhnet_default_dscp << 2; - setsockopt(lhnetsocket->inetsocket, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(tos)); - } +#ifdef MSG_DONTWAIT + if (1) +#else + if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, use_blocking ? &_false : &_true) != -1) #endif - lhnetsocket->next = &lhnet_socketlist; - lhnetsocket->prev = lhnetsocket->next->prev; - lhnetsocket->next->prev = lhnetsocket; - lhnetsocket->prev->next = lhnetsocket; + { + int i = 1; + // enable broadcast on this socket + setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)); +#ifdef IP_TOS + { + // enable DSCP for ToS support + int tos = lhnet_default_dscp << 2; + setsockopt(lhnetsocket->inetsocket, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(tos)); + } +#endif + lhnetsocket->next = &lhnet_socketlist; + lhnetsocket->prev = lhnetsocket->next->prev; + lhnetsocket->next->prev = lhnetsocket; + lhnetsocket->prev->next = lhnetsocket; #ifdef WIN32 - if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1) - Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError()); + if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1) + Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError()); #endif - return lhnetsocket; + return lhnetsocket; + } + else + Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError()); } else { -- 2.39.2