Fix high CPU usage when running the SDK server

It was coming from `recv_select()` `nd accept_select()`
The timeout for select() is only when waiting for the file descriptor to
be ready for reading/writing so when the FD is ready AND there is
nothing to read, it will not return and instead keep executing the while loop with no timeout.
Fix this by adding a 100ms timeout when there is nothing to read.
This commit is contained in:
Térence Clastres 2020-06-20 16:45:23 +02:00 committed by Adam Honse
parent b79ff124e6
commit b94f701db8
2 changed files with 7 additions and 0 deletions

View file

@ -246,6 +246,7 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags)
}
else if(rv == 0)
{
std::this_thread::sleep_for(100ms);
continue;
}
else
@ -253,6 +254,7 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags)
// socket has something to read
return(recv(s, buf, len, flags));
}
}
}

View file

@ -30,6 +30,9 @@ const char yes = 1;
#include <unistd.h>
#endif
using namespace std::chrono_literals;
NetworkServer::NetworkServer(std::vector<RGBController *>& control) : controllers(control)
{
port_num = OPENRGB_SDK_PORT;
@ -280,6 +283,7 @@ int NetworkServer::accept_select(int sockfd, struct sockaddr *addr, socklen_t *a
}
else if(rv == 0)
{
std::this_thread::sleep_for(100ms);
continue;
}
else
@ -310,6 +314,7 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags)
}
else if(rv == 0)
{
std::this_thread::sleep_for(100ms);
continue;
}
else