DEV9: Eliminate some c-style casts in UDP_Session

This commit is contained in:
TheLastRar 2024-04-23 15:44:03 +01:00 committed by Connor McLaughlin
parent 2fc4d02dd6
commit a0f6036337
2 changed files with 36 additions and 36 deletions

View File

@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team // SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
// SPDX-License-Identifier: LGPL-3.0+ // SPDX-License-Identifier: LGPL-3.0+
#include <bit>
#include "common/Assertions.h" #include "common/Assertions.h"
#include "common/Console.h" #include "common/Console.h"
@ -47,7 +49,7 @@ namespace Sessions
} }
const int reuseAddress = true; //BOOL const int reuseAddress = true; //BOOL
ret = setsockopt(client, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseAddress, sizeof(reuseAddress)); ret = setsockopt(client, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&reuseAddress), sizeof(reuseAddress));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
Console.Error("DEV9: UDP: Failed to set SO_REUSEADDR. Error: %d", Console.Error("DEV9: UDP: Failed to set SO_REUSEADDR. Error: %d",
@ -58,7 +60,7 @@ namespace Sessions
#endif #endif
const int broadcastEnable = true; //BOOL const int broadcastEnable = true; //BOOL
ret = setsockopt(client, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcastEnable, sizeof(broadcastEnable)); ret = setsockopt(client, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char*>(&broadcastEnable), sizeof(broadcastEnable));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
Console.Error("DEV9: UDP: Failed to set SO_BROADCAST. Error: %d", Console.Error("DEV9: UDP: Failed to set SO_BROADCAST. Error: %d",
@ -68,12 +70,12 @@ namespace Sessions
errno); errno);
#endif #endif
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
*(IP_Address*)&endpoint.sin_addr = adapterIP; endpoint.sin_addr = std::bit_cast<in_addr>(adapterIP);
endpoint.sin_port = htons(parPort); endpoint.sin_port = htons(parPort);
ret = bind(client, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = bind(client, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
Console.Error("DEV9: UDP: Failed to bind socket. Error: %d", Console.Error("DEV9: UDP: Failed to bind socket. Error: %d",
@ -93,7 +95,7 @@ namespace Sessions
fd_set sReady; fd_set sReady;
fd_set sExcept; fd_set sExcept;
timeval nowait{0}; timeval nowait{};
FD_ZERO(&sReady); FD_ZERO(&sReady);
FD_ZERO(&sExcept); FD_ZERO(&sExcept);
FD_SET(client, &sReady); FD_SET(client, &sReady);
@ -118,11 +120,11 @@ namespace Sessions
int error = 0; int error = 0;
#ifdef _WIN32 #ifdef _WIN32
int len = sizeof(error); int len = sizeof(error);
if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) < 0)
Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", WSAGetLastError()); Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", WSAGetLastError());
#elif defined(__POSIX__) #elif defined(__POSIX__)
socklen_t len = sizeof(error); socklen_t len = sizeof(error);
if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) < 0)
Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", errno); Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", errno);
#endif #endif
else else
@ -136,7 +138,7 @@ namespace Sessions
unsigned long available = 0; unsigned long available = 0;
PayloadData* recived = nullptr; PayloadData* recived = nullptr;
std::unique_ptr<u8[]> buffer; std::unique_ptr<u8[]> buffer;
sockaddr endpoint{0}; sockaddr_in endpoint{};
//FIONREAD returns total size of all available messages //FIONREAD returns total size of all available messages
//but we will read one message at a time //but we will read one message at a time
@ -154,7 +156,7 @@ namespace Sessions
#elif defined(__POSIX__) #elif defined(__POSIX__)
socklen_t fromlen = sizeof(endpoint); socklen_t fromlen = sizeof(endpoint);
#endif #endif
ret = recvfrom(client, (char*)buffer.get(), available, 0, &endpoint, &fromlen); ret = recvfrom(client, reinterpret_cast<char*>(buffer.get()), available, 0, reinterpret_cast<sockaddr*>(&endpoint), &fromlen);
} }
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
@ -175,9 +177,8 @@ namespace Sessions
UDP_Packet* iRet = new UDP_Packet(recived); UDP_Packet* iRet = new UDP_Packet(recived);
iRet->destinationPort = port; iRet->destinationPort = port;
sockaddr_in* sockaddr = (sockaddr_in*)&endpoint; destIP = std::bit_cast<IP_Address>(endpoint.sin_addr);
destIP = *(IP_Address*)&sockaddr->sin_addr; iRet->sourcePort = ntohs(endpoint.sin_port);
iRet->sourcePort = ntohs(sockaddr->sin_port);
{ {
std::lock_guard numberlock(connectionSentry); std::lock_guard numberlock(connectionSentry);
@ -213,7 +214,6 @@ namespace Sessions
UDP_Session* s = new UDP_Session(parNewKey, adapterIP, parIsBrodcast, parIsMulticast, client); UDP_Session* s = new UDP_Session(parNewKey, adapterIP, parIsBrodcast, parIsMulticast, client);
s->AddConnectionClosedHandler([&](BaseSession* session) { HandleChildConnectionClosed(session); }); s->AddConnectionClosedHandler([&](BaseSession* session) { HandleChildConnectionClosed(session); });
{ {
std::lock_guard numberlock(connectionSentry); std::lock_guard numberlock(connectionSentry);
connections.push_back(s); connections.push_back(s);

View File

@ -80,7 +80,7 @@ namespace Sessions
fd_set sReady; fd_set sReady;
fd_set sExcept; fd_set sExcept;
timeval nowait{0}; timeval nowait{};
FD_ZERO(&sReady); FD_ZERO(&sReady);
FD_ZERO(&sExcept); FD_ZERO(&sExcept);
FD_SET(client, &sReady); FD_SET(client, &sReady);
@ -105,11 +105,11 @@ namespace Sessions
int error = 0; int error = 0;
#ifdef _WIN32 #ifdef _WIN32
int len = sizeof(error); int len = sizeof(error);
if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) < 0)
Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", WSAGetLastError()); Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", WSAGetLastError());
#elif defined(__POSIX__) #elif defined(__POSIX__)
socklen_t len = sizeof(error); socklen_t len = sizeof(error);
if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) < 0)
Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", errno); Console.Error("DEV9: UDP: Unkown UDP Connection Error (getsockopt Error: %d)", errno);
#endif #endif
else else
@ -123,7 +123,7 @@ namespace Sessions
unsigned long available = 0; unsigned long available = 0;
PayloadData* recived = nullptr; PayloadData* recived = nullptr;
std::unique_ptr<u8[]> buffer; std::unique_ptr<u8[]> buffer;
sockaddr endpoint{0}; sockaddr_in endpoint{};
//FIONREAD returns total size of all available messages //FIONREAD returns total size of all available messages
//but we will read one message at a time //but we will read one message at a time
@ -141,7 +141,7 @@ namespace Sessions
#elif defined(__POSIX__) #elif defined(__POSIX__)
socklen_t fromlen = sizeof(endpoint); socklen_t fromlen = sizeof(endpoint);
#endif #endif
ret = recvfrom(client, (char*)buffer.get(), available, 0, &endpoint, &fromlen); ret = recvfrom(client, reinterpret_cast<char*>(buffer.get()), available, 0, reinterpret_cast<sockaddr*>(&endpoint), &fromlen);
} }
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
@ -235,7 +235,7 @@ namespace Sessions
} }
const int reuseAddress = true; //BOOL const int reuseAddress = true; //BOOL
ret = setsockopt(client, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseAddress, sizeof(reuseAddress)); ret = setsockopt(client, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&reuseAddress), sizeof(reuseAddress));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
Console.Error("DEV9: UDP: Failed to set SO_REUSEADDR. Error: %d", Console.Error("DEV9: UDP: Failed to set SO_REUSEADDR. Error: %d",
@ -247,11 +247,11 @@ namespace Sessions
if (adapterIP.integer != 0) if (adapterIP.integer != 0)
{ {
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
*(IP_Address*)&endpoint.sin_addr = adapterIP; endpoint.sin_addr = std::bit_cast<in_addr>(adapterIP);
ret = bind(client, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = bind(client, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
Console.Error("DEV9: UDP: Failed to bind socket. Error: %d", Console.Error("DEV9: UDP: Failed to bind socket. Error: %d",
@ -264,12 +264,12 @@ namespace Sessions
pxAssert(isMulticast == false); pxAssert(isMulticast == false);
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
*(IP_Address*)&endpoint.sin_addr = destIP; endpoint.sin_addr = std::bit_cast<in_addr>(destIP);
endpoint.sin_port = htons(destPort); endpoint.sin_port = htons(destPort);
ret = connect(client, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = connect(client, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
{ {
@ -293,24 +293,24 @@ namespace Sessions
int ret = SOCKET_ERROR; int ret = SOCKET_ERROR;
if (isBroadcast) if (isBroadcast)
{ {
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
endpoint.sin_addr.s_addr = INADDR_BROADCAST; endpoint.sin_addr.s_addr = INADDR_BROADCAST;
endpoint.sin_port = htons(destPort); endpoint.sin_port = htons(destPort);
ret = sendto(client, (const char*)udpPayload->data, udpPayload->GetLength(), 0, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = sendto(client, reinterpret_cast<const char*>(udpPayload->data), udpPayload->GetLength(), 0, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
} }
else if (isMulticast | isFixedPort) else if (isMulticast | isFixedPort)
{ {
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
*(IP_Address*)&endpoint.sin_addr = destIP; endpoint.sin_addr = std::bit_cast<in_addr>(destIP);
endpoint.sin_port = htons(destPort); endpoint.sin_port = htons(destPort);
ret = sendto(client, (const char*)udpPayload->data, udpPayload->GetLength(), 0, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = sendto(client, reinterpret_cast<const char*>(udpPayload->data), udpPayload->GetLength(), 0, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
} }
else else
ret = send(client, (const char*)udpPayload->data, udpPayload->GetLength(), 0); ret = send(client, reinterpret_cast<const char*>(udpPayload->data), udpPayload->GetLength(), 0);
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
{ {
@ -335,16 +335,16 @@ namespace Sessions
pxAssert(isBroadcast == false && isMulticast == false); pxAssert(isBroadcast == false && isMulticast == false);
if (isFixedPort) if (isFixedPort)
{ {
sockaddr_in endpoint{0}; sockaddr_in endpoint{};
endpoint.sin_family = AF_INET; endpoint.sin_family = AF_INET;
*(IP_Address*)&endpoint.sin_addr = destIP; endpoint.sin_addr = std::bit_cast<in_addr>(destIP);
endpoint.sin_port = htons(destPort); endpoint.sin_port = htons(destPort);
ret = sendto(client, (const char*)udpPayload->data, udpPayload->GetLength(), 0, (const sockaddr*)&endpoint, sizeof(endpoint)); ret = sendto(client, reinterpret_cast<const char*>(udpPayload->data), udpPayload->GetLength(), 0, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));
} }
else else
//Do we need to clear the error somehow? //Do we need to clear the error somehow?
ret = send(client, (const char*)udpPayload->data, udpPayload->GetLength(), 0); ret = send(client, reinterpret_cast<const char*>(udpPayload->data), udpPayload->GetLength(), 0);
if (ret == SOCKET_ERROR) if (ret == SOCKET_ERROR)
{ {