From b58f4a6e908217afffedae5dd3a0fb47fb0d7537 Mon Sep 17 00:00:00 2001 From: ANR2ME Date: Sat, 4 Jan 2020 07:41:54 +0700 Subject: [PATCH] Enabling KeepAlive on sockets --- Core/HLE/proAdhoc.cpp | 21 +++++++++++++++++++++ Core/HLE/proAdhoc.h | 5 +++++ Core/HLE/proAdhocServer.cpp | 16 ++++++++++++++++ Core/HLE/sceNetAdhoc.cpp | 10 ++++++++++ 4 files changed, 52 insertions(+) diff --git a/Core/HLE/proAdhoc.cpp b/Core/HLE/proAdhoc.cpp index 808e6bab96..68efeb0b21 100644 --- a/Core/HLE/proAdhoc.cpp +++ b/Core/HLE/proAdhoc.cpp @@ -23,6 +23,7 @@ #if !defined(_WIN32) #include +#include #endif #include @@ -1562,6 +1563,24 @@ int setSockBufferSize(int sock, int opt, int size) { // opt = SO_RCVBUF/SO_SNDBU return setsockopt(sock, SOL_SOCKET, opt, (char *)&n, sizeof(n)); } +int setSockKeepAlive(int sock, bool keepalive, const int keepcnt, const int keepidle, const int keepinvl) { + int optval = keepalive ? 1 : 0; + int optlen = sizeof(optval); + int result = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval, optlen); + if (result == 0 && keepalive) { + if (getsockopt(sock, SOL_SOCKET, SO_TYPE, (char*)&optval, (socklen_t*)&optlen) == 0 && optval == SOCK_STREAM) { + optlen = sizeof(optval); + optval = keepcnt; //20 + setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, (char*)&optval, optlen); + optval = keepidle; //180 + setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, (char*)&optval, optlen); + optval = keepinvl; //60 + setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, (char*)&optval, optlen); + } + } + return result; +} + /** * Return the Number of Players with the chosen Nickname in the Local Users current Network * @param nickname To-be-searched Nickname @@ -1625,6 +1644,8 @@ int initNetwork(SceNetAdhocctlAdhocId *adhoc_id){ ERROR_LOG(SCENET, "Invalid socket"); return SOCKET_ERROR; } + setSockKeepAlive(metasocket, true); + struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); //27312 // Maybe read this from config too diff --git a/Core/HLE/proAdhoc.h b/Core/HLE/proAdhoc.h index 7dc32fd8d7..33b4ed466c 100644 --- a/Core/HLE/proAdhoc.h +++ b/Core/HLE/proAdhoc.h @@ -1163,6 +1163,11 @@ int getSockBufferSize(int sock, int opt); */ int setSockBufferSize(int sock, int opt, int size); +/* +* Set Socket KeepAlive (opt = SO_KEEPALIVE) +*/ +int setSockKeepAlive(int sock, bool keepalive, const int keepcnt = 20, const int keepidle = 180, const int keepinvl = 60); + /** * Return the Number of Players with the chosen Nickname in the Local Users current Network * @param nickname To-be-searched Nickname diff --git a/Core/HLE/proAdhocServer.cpp b/Core/HLE/proAdhocServer.cpp index f073865270..f559c73419 100644 --- a/Core/HLE/proAdhocServer.cpp +++ b/Core/HLE/proAdhocServer.cpp @@ -1723,6 +1723,19 @@ void enable_address_reuse(int fd) setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)); } +/** + * Enable KeepAlive on Socket + * @param fd Socket + */ +void enable_keepalive(int fd) +{ + // Enable Value + int on = 1; + + // Enable Port Reuse + setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const char*)&on, sizeof(on)); +} + /** * Change Socket Blocking Mode * @param fd Socket @@ -1770,6 +1783,9 @@ int create_listen_socket(uint16_t port) // Created Socket if(fd != -1) { + // Enable KeepAlive + enable_keepalive(fd); + // Enable Address Reuse enable_address_reuse(fd); // Shouldn't Reuse the port for built-in AdhocServer to prevent conflict with Dedicated AdhocServer diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index a8b0eea9bd..89d2d286d8 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -325,9 +325,13 @@ static int sceNetAdhocPdpCreate(const char *mac, int port, int bufferSize, u32 u if (getSockBufferSize(usocket, SO_SNDBUF) < bufferSize) setSockBufferSize(usocket, SO_SNDBUF, bufferSize); if (getSockBufferSize(usocket, SO_RCVBUF) < bufferSize) setSockBufferSize(usocket, SO_RCVBUF, bufferSize); + // Enable KeepAlive + setSockKeepAlive(usocket, true); + // Enable Port Re-use, this will allow binding to an already used port, but only one of them can read the data (shared receive buffer?) int one = 1; setsockopt(usocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one)); // NO idea if we need this + // Binding Information for local Port sockaddr_in addr; addr.sin_family = AF_INET; @@ -1830,6 +1834,9 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac, if (getSockBufferSize(tcpsocket, SO_SNDBUF) < bufsize) setSockBufferSize(tcpsocket, SO_SNDBUF, bufsize); if (getSockBufferSize(tcpsocket, SO_RCVBUF) < bufsize) setSockBufferSize(tcpsocket, SO_RCVBUF, bufsize); + // Enable KeepAlive + setSockKeepAlive(tcpsocket, true); + // Enable Port Re-use setsockopt(tcpsocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one)); @@ -2330,6 +2337,9 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int if (getSockBufferSize(tcpsocket, SO_SNDBUF) < bufsize) setSockBufferSize(tcpsocket, SO_SNDBUF, bufsize); if (getSockBufferSize(tcpsocket, SO_RCVBUF) < bufsize) setSockBufferSize(tcpsocket, SO_RCVBUF, bufsize); + // Enable KeepAlive + setSockKeepAlive(tcpsocket, true); + // Enable Port Re-use setsockopt(tcpsocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one));