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:
parent
b79ff124e6
commit
b94f701db8
2 changed files with 7 additions and 0 deletions
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue