Enabling KeepAlive on sockets

This commit is contained in:
ANR2ME 2020-01-04 07:41:54 +07:00 committed by Henrik Rydgård
parent c783aaf40d
commit b58f4a6e90
4 changed files with 52 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#if !defined(_WIN32)
#include <unistd.h>
#include <netinet/tcp.h>
#endif
#include <cstring>
@ -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

View File

@ -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

View File

@ -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

View File

@ -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));