diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-06-23 10:25:18 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-06-23 10:25:18 +0200 |
commit | 9ad1e5d635e2fd4c2486d5f2bf1cb52d8e768558 (patch) | |
tree | 00b379e3f84ff5b428f0fafd5bdeac0489ede0c4 /dep/CascLib/src/common/Sockets.cpp | |
parent | 54596eb8ce5d7933164073eb3768f52124ba4262 (diff) |
Dep/CascLib: Update to ladislav-zezula/CascLib@ebd79e8fd43279343c543a27fce620f6b1b53cb9
Diffstat (limited to 'dep/CascLib/src/common/Sockets.cpp')
-rw-r--r-- | dep/CascLib/src/common/Sockets.cpp | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/dep/CascLib/src/common/Sockets.cpp b/dep/CascLib/src/common/Sockets.cpp index bb490505b15..8eb9680d448 100644 --- a/dep/CascLib/src/common/Sockets.cpp +++ b/dep/CascLib/src/common/Sockets.cpp @@ -13,14 +13,35 @@ #include "../CascLib.h" #include "../CascCommon.h" +#ifdef CASCLIB_PLATFORM_WINDOWS +#include <ws2tcpip.h> +#endif + //----------------------------------------------------------------------------- // Local variables #define BUFFER_INITIAL_SIZE 0x8000 +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (SOCKET)(-1) // Not defined in Linux +#endif + CASC_SOCKET_CACHE SocketCache; //----------------------------------------------------------------------------- +// Conversion functions + +static SOCKET inline HandleToSocket(HANDLE sock) +{ + return (SOCKET)(intptr_t)(sock); +} + +static HANDLE inline SocketToHandle(SOCKET sock) +{ + return (HANDLE)(intptr_t)(sock); +} + +//----------------------------------------------------------------------------- // CASC_SOCKET functions // Guarantees that there is zero terminator after the response @@ -42,10 +63,10 @@ char * CASC_SOCKET::ReadResponse(const char * request, size_t request_length, CA // Send the request to the remote host. On Linux, this call may send signal(SIGPIPE), // we need to prevend that by using the MSG_NOSIGNAL flag. On Windows, it fails normally. - while(send(sock, request, (int)request_length, MSG_NOSIGNAL) == SOCKET_ERROR) + while(send(HandleToSocket(sock), request, (int)request_length, MSG_NOSIGNAL) == SOCKET_ERROR) { // If the connection was closed by the remote host, we try to reconnect - if(ReconnectAfterShutdown(sock, remoteItem) == INVALID_SOCKET) + if(ReconnectAfterShutdown(sock, remoteItem) == SocketToHandle(INVALID_SOCKET)) { SetCascError(ERROR_NETWORK_NOT_AVAILABLE); CascUnlock(Lock); @@ -74,7 +95,7 @@ char * CASC_SOCKET::ReadResponse(const char * request, size_t request_length, CA // Receive the next part of the response, up to buffer size // Return value 0 means "connection closed", -1 means an error - bytes_received = recv(sock, server_response + total_received, (int)(buffer_length - total_received), 0); + bytes_received = recv(HandleToSocket(sock), server_response + total_received, (int)(buffer_length - total_received), 0); if(bytes_received <= 0) { MimeResponse.ParseResponse(server_response, total_received, true); @@ -192,28 +213,28 @@ DWORD CASC_SOCKET::GetAddrInfoWrapper(const char * hostName, unsigned portNum, P } } -SOCKET CASC_SOCKET::CreateAndConnect(addrinfo * remoteItem) +HANDLE CASC_SOCKET::CreateAndConnect(PADDRINFO remoteItem) { SOCKET sock; // Create new socket - // On error, returns returns INVALID_SOCKET (-1) on Windows, -1 on Linux + // On error, returns returns INVALID_SOCKET (0 on Windows, -1 on Linux) if((sock = socket(remoteItem->ai_family, remoteItem->ai_socktype, remoteItem->ai_protocol)) > 0) { // Connect to the remote host // On error, returns SOCKET_ERROR (-1) on Windows, -1 on Linux if(connect(sock, remoteItem->ai_addr, (int)remoteItem->ai_addrlen) == 0) - return sock; + return SocketToHandle(sock); - // Failed. Close the socket and return 0 + // Failed. Close the socket and return INVALID_SOCKET closesocket(sock); sock = INVALID_SOCKET; } - return sock; + return SocketToHandle(sock); } -SOCKET CASC_SOCKET::ReconnectAfterShutdown(SOCKET & sock, addrinfo * remoteItem) +HANDLE CASC_SOCKET::ReconnectAfterShutdown(HANDLE & sock, PADDRINFO remoteItem) { // Retrieve the error code related to previous socket operation switch(GetSockError()) @@ -222,8 +243,8 @@ SOCKET CASC_SOCKET::ReconnectAfterShutdown(SOCKET & sock, addrinfo * remoteItem) case WSAECONNRESET: // Windows { // Close the old socket - if(sock != INVALID_SOCKET) - closesocket(sock); + if(sock != SocketToHandle(INVALID_SOCKET)) + closesocket(HandleToSocket(sock)); // Attempt to reconnect sock = CreateAndConnect(remoteItem); @@ -232,10 +253,10 @@ SOCKET CASC_SOCKET::ReconnectAfterShutdown(SOCKET & sock, addrinfo * remoteItem) } // Another problem - return INVALID_SOCKET; + return SocketToHandle(INVALID_SOCKET); } -PCASC_SOCKET CASC_SOCKET::New(addrinfo * remoteList, addrinfo * remoteItem, const char * hostName, unsigned portNum, SOCKET sock) +PCASC_SOCKET CASC_SOCKET::New(PADDRINFO remoteList, PADDRINFO remoteItem, const char * hostName, unsigned portNum, HANDLE sock) { PCASC_SOCKET pSocket; size_t length = strlen(hostName); @@ -268,7 +289,7 @@ PCASC_SOCKET CASC_SOCKET::Connect(const char * hostName, unsigned portNum) addrinfo * remoteList; addrinfo * remoteItem; addrinfo hints = {0}; - SOCKET sock; + HANDLE sock; int nErrCode; // Retrieve the information about the remote host @@ -293,7 +314,7 @@ PCASC_SOCKET CASC_SOCKET::Connect(const char * hostName, unsigned portNum) } // Close the socket - closesocket(sock); + closesocket(HandleToSocket(sock)); } } @@ -316,7 +337,7 @@ void CASC_SOCKET::Delete() // Close the socket, if any if(sock != 0) - closesocket(sock); + closesocket(HandleToSocket(sock)); sock = 0; // Free the lock |