Skip to content

Commit

Permalink
Merge pull request hrydgard#19831 from hrydgard/centralize-socket-inc…
Browse files Browse the repository at this point in the history
…ludes

Centralize socket-related includes, introduce socket_errno
  • Loading branch information
hrydgard authored Jan 8, 2025
2 parents 3b81cb9 + 3ae1739 commit 4218b39
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 331 deletions.
1 change: 0 additions & 1 deletion Common/File/DirListing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <strings.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#endif

#include <cstring>
Expand Down
14 changes: 1 addition & 13 deletions Common/File/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@
#include <errno.h>
#include <cmath>
#include <cstdio>
#ifndef _WIN32
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#else
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <fcntl.h>

#include "Common/CommonTypes.h"
#include "Common/Net/SocketCompat.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/File/FileDescriptor.h"
#include "Common/Log.h"
Expand Down
36 changes: 6 additions & 30 deletions Common/Net/HTTPClient.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
#include <cmath>
#include <cstdio>
#include <cstdlib>

#include "Common/Net/HTTPClient.h"

#include "Common/TimeUtil.h"
#include "Common/StringUtils.h"
#include "Common/System/OSD.h"

#ifndef _WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#define closesocket close
#else
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>
#endif

#include <cmath>
#include <cstdio>
#include <cstdlib>

#include "Common/Net/SocketCompat.h"
#include "Common/Net/Resolve.h"
#include "Common/Net/URL.h"

Expand Down Expand Up @@ -137,17 +120,10 @@ bool Connection::Connect(int maxTries, double timeout, bool *cancelConnect) {
// Start trying to connect (async with timeout.)
errno = 0;
if (connect(sock, possible->ai_addr, (int)possible->ai_addrlen) < 0) {
#if PPSSPP_PLATFORM(WINDOWS)
int errorCode = WSAGetLastError();
int errorCode = socket_errno;
std::string errorString = GetStringErrorMsg(errorCode);
bool unreachable = errorCode == WSAENETUNREACH;
bool inProgress = errorCode == WSAEINPROGRESS || errorCode == WSAEWOULDBLOCK;
#else
int errorCode = errno;
std::string errorString = strerror(errno);
bool unreachable = errorCode == ENETUNREACH;
bool inProgress = errorCode == EINPROGRESS || errorCode == EWOULDBLOCK;
#endif
if (!inProgress) {
char addrStr[128]{};
FormatAddr(addrStr, sizeof(addrStr), possible);
Expand Down
21 changes: 3 additions & 18 deletions Common/Net/NetBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
#include "ppsspp_config.h"
#ifdef _WIN32
#include <winsock2.h>
#undef min
#undef max
#else
#include <sys/socket.h>
#include <unistd.h>
#endif

#include <algorithm>
#include <cstring>

#ifndef MSG_NOSIGNAL
// Default value to 0x00 (do nothing) in systems where it's not supported.
#define MSG_NOSIGNAL 0x00
#endif
#include "Common/Net/SocketCompat.h"

#if _MSC_VER
#pragma warning(disable:4267)
Expand Down Expand Up @@ -98,11 +87,7 @@ bool Buffer::ReadAllWithProgress(int fd, int knownSize, RequestProgress *progres
if (retval == 0) {
return true;
} else if (retval < 0) {
#if PPSSPP_PLATFORM(WINDOWS)
if (WSAGetLastError() != WSAEWOULDBLOCK) {
#else
if (errno != EWOULDBLOCK) {
#endif
if (socket_errno != EWOULDBLOCK) {
ERROR_LOG(Log::IO, "Error reading from buffer: %i", retval);
return false;
}
Expand Down
33 changes: 7 additions & 26 deletions Common/Net/Resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,10 @@
#include <string>
#include <vector>


#ifdef _WIN32
#include <WinSock2.h>
#include <Ws2tcpip.h>
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0x0400
#endif
#undef min
#undef max
#else
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
#endif

#include "Common/Log.h"
#include "Common/TimeUtil.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Net/SocketCompat.h"

#ifndef HTTPS_NOT_AVAILABLE
#include "ext/naett/naett.h"
Expand Down Expand Up @@ -157,7 +138,7 @@ bool GetIPList(std::vector<std::string> &IP4s) {
}
}*/
}

freeifaddrs(ifAddrStruct);
return true;
}
Expand All @@ -170,13 +151,13 @@ bool GetIPList(std::vector<std::string> &IP4s) {

int sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0) {
ERROR_LOG(Log::sceNet, "GetIPList failed to create socket (result = %i, errno = %i)", sd, errno);
ERROR_LOG(Log::sceNet, "GetIPList failed to create socket (result = %i, errno = %i)", sd, socket_errno);
return false;
}

int r = ioctl(sd, SIOCGIFCONF, (char*)&ifc);
if (r != 0) {
ERROR_LOG(Log::sceNet, "GetIPList failed ioctl/SIOCGIFCONF (result = %i, errno = %i)", r, errno);
ERROR_LOG(Log::sceNet, "GetIPList failed ioctl/SIOCGIFCONF (result = %i, errno = %i)", r, socket_errno);
return false;
}

Expand All @@ -192,7 +173,7 @@ bool GetIPList(std::vector<std::string> &IP4s) {
r = ioctl(sd, SIOCGIFADDR, item);
if (r != 0)
{
ERROR_LOG(Log::sceNet, "GetIPList failed ioctl/SIOCGIFADDR (i = %i, result = %i, errno = %i)", i, r, errno);
ERROR_LOG(Log::sceNet, "GetIPList failed ioctl/SIOCGIFADDR (i = %i, result = %i, errno = %i)", i, r, socket_errno);
}

if (ifreqs[i].ifr_addr.sa_family == AF_INET) {
Expand Down Expand Up @@ -221,7 +202,7 @@ bool GetIPList(std::vector<std::string> &IP4s) {
// Get local host name
char szHostName[256] = "";
if (::gethostname(szHostName, sizeof(szHostName))) {
// Error handling
// Error handling
}

int status;
Expand Down Expand Up @@ -322,4 +303,4 @@ int inet_pton(int af, const char* src, void* dst)
return 1;
}

}
} // namespace net
44 changes: 8 additions & 36 deletions Common/Net/Sinks.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
#include "ppsspp_config.h"

#if PPSSPP_PLATFORM(WINDOWS)

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>

#else

#include <sys/socket.h> /* socket definitions */
#include <sys/types.h> /* socket types */
#include <sys/wait.h> /* for waitpid() */
#include <netinet/in.h> /* struct sockaddr_in */
#include <arpa/inet.h> /* inet (3) funtions */
#include <unistd.h> /* misc. UNIX functions */

#endif

#include <algorithm>
#include <cerrno>
#include <cstdarg>

#include "Common/Net/SocketCompat.h"
#include "Common/Net/Sinks.h"

#include "Common/Log.h"
Expand Down Expand Up @@ -192,14 +174,9 @@ bool InputSink::Block() {

void InputSink::AccountFill(int bytes) {
if (bytes < 0) {
#if PPSSPP_PLATFORM(WINDOWS)
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK)
return;
#else
if (errno == EWOULDBLOCK || errno == EAGAIN)
int err = socket_errno;
if (err == EWOULDBLOCK || err == EAGAIN)
return;
#endif
ERROR_LOG(Log::IO, "Error reading from socket");
return;
}
Expand Down Expand Up @@ -349,7 +326,7 @@ bool OutputSink::Flush(bool allowBlock) {

int bytes = send(fd_, buf_ + read_, avail, MSG_NOSIGNAL);
#if !PPSSPP_PLATFORM(WINDOWS)
if (bytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
if (bytes == -1 && (socket_errno == EAGAIN || socket_errno == EWOULDBLOCK))
bytes = 0;
#endif
AccountDrain(bytes);
Expand Down Expand Up @@ -381,7 +358,7 @@ void OutputSink::Drain() {

int bytes = send(fd_, buf_ + read_, avail, MSG_NOSIGNAL);
#if !PPSSPP_PLATFORM(WINDOWS)
if (bytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
if (bytes == -1 && (socket_errno == EAGAIN || socket_errno == EWOULDBLOCK))
bytes = 0;
#endif
AccountDrain(bytes);
Expand All @@ -398,15 +375,10 @@ void OutputSink::AccountPush(size_t bytes) {

void OutputSink::AccountDrain(int bytes) {
if (bytes < 0) {
#if PPSSPP_PLATFORM(WINDOWS)
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK)
int err = socket_errno;
if (err == EWOULDBLOCK || err == EAGAIN)
return;
#else
if (errno == EWOULDBLOCK || errno == EAGAIN)
return;
#endif
ERROR_LOG(Log::IO, "Error writing to socket");
ERROR_LOG(Log::IO, "Error writing to socket: %d", err);
return;
}

Expand Down
37 changes: 31 additions & 6 deletions Common/Net/SocketCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,41 @@

#if PPSSPP_PLATFORM(WINDOWS)
#include "Common/CommonWindows.h"
#include <io.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#if !PPSSPP_PLATFORM(SWITCH)
#include <ifaddrs.h>
#endif
#include <fcntl.h>
#include <errno.h>
#endif
#include <fcntl.h>
#include <errno.h>

#if defined(HAVE_LIBNX) || PPSSPP_PLATFORM(SWITCH)
#undef __BSD_VISIBLE
#define __BSD_VISIBLE 1
#define TCP_MAXSEG 2
#include <netdb.h>
#include <switch.h>
// Missing include, *shrugs*
extern "C" struct hostent *gethostbyname(const char *name);
#endif // defined(HAVE_LIBNX) || PPSSPP_PLATFORM(SWITCH)

// TODO: move this to some common set
#ifdef _WIN32
#undef errno
#if PPSSPP_PLATFORM(WINDOWS)
#undef ESHUTDOWN
#undef ECONNABORTED
#undef ECONNRESET
Expand Down Expand Up @@ -49,7 +68,7 @@
#undef ENETUNREACH
#undef EHOSTUNREACH
#undef ENETDOWN
#define errno WSAGetLastError()
#define socket_errno WSAGetLastError()
#define ESHUTDOWN WSAESHUTDOWN
#define ECONNABORTED WSAECONNABORTED
#define ECONNRESET WSAECONNRESET
Expand Down Expand Up @@ -79,9 +98,10 @@
#define ENETUNREACH WSAENETUNREACH
#define EHOSTUNREACH WSAEHOSTUNREACH
#define ENETDOWN WSAENETDOWN
inline bool connectInProgress(int errcode) { return (errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS || errcode == WSAEALREADY || errcode == WSAEINVAL); } // WSAEINVAL should be treated as WSAEALREADY during connect for backward-compatibility with Winsock 1.1
inline bool connectInProgress(int errcode) { return (errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS || errcode == WSAEALREADY || errcode == WSAEINVAL); } // WSAEINVAL should be treated as WSAEALREADY during connect for backward-compatibility with Winsock 1.1
inline bool isDisconnected(int errcode) { return (errcode == WSAECONNRESET || errcode == WSAECONNABORTED || errcode == WSAESHUTDOWN); }
#else
#define socket_errno errno
#define SOCKET int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
Expand Down Expand Up @@ -114,3 +134,8 @@ inline bool isDisconnected(int errcode) { return (errcode == EPIPE || errcode ==
#ifndef SD_BOTH
#define SD_BOTH SHUT_RDWR //0x02
#endif

#ifndef MSG_NOSIGNAL
// Default value to 0x00 (do nothing) in systems where it's not supported.
#define MSG_NOSIGNAL 0x00
#endif
13 changes: 1 addition & 12 deletions Common/Net/WebsocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@
#include <cctype>
#include <cmath>
#include <cstring>
#ifndef _WIN32
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#else
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include "Common/Net/SocketCompat.h"
#include "Common/Data/Encoding/Base64.h"
#include "Common/Net/HTTPServer.h"
#include "Common/Net/Sinks.h"
Expand Down
Loading

0 comments on commit 4218b39

Please sign in to comment.