From 1db412e9701c636fa4bf34fe84d6b41795d481a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Clastres?= Date: Sat, 20 Jun 2020 16:45:23 +0200 Subject: [PATCH] Fix high CPU usage when running the SDK server It was coming from `recv_select()` and `accept_select()` The timeval struct members for select() is reset after each call to select() for whatever reason so like FD_ZERO and FD_SET it needs to be placed inside the loop. Source: https://stackoverflow.com/questions/3324078/why-select-always-return-0-after-the-first-timeout --- NetworkClient.cpp | 5 +++-- NetworkServer.cpp | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 79826896..560884c1 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -230,11 +230,12 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags) { fd_set set; struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; while(1) { + timeout.tv_sec = 5; + timeout.tv_usec = 0; + FD_ZERO(&set); /* clear the set */ FD_SET(s, &set); /* add our file descriptor to the set */ diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 68a205d6..68f5268f 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -267,13 +267,14 @@ int NetworkServer::accept_select(int sockfd, struct sockaddr *addr, socklen_t *a { fd_set set; struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; while(1) { - FD_ZERO(&set); /* clear the set */ - FD_SET(sockfd, &set); /* add our file descriptor to the set */ + timeout.tv_sec = 5; + timeout.tv_usec = 0; + + FD_ZERO(&set); /* clear the set */ + FD_SET(sockfd, &set); /* add our file descriptor to the set */ int rv = select(sockfd + 1, &set, NULL, NULL, &timeout); @@ -298,11 +299,12 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags) { fd_set set; struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; while(1) { + timeout.tv_sec = 5; + timeout.tv_usec = 0; + FD_ZERO(&set); /* clear the set */ FD_SET(s, &set); /* add our file descriptor to the set */