Skip to content

Commit 2aaf596

Browse files
committed
(socket server) in the loop accepting connections, call select without a timeout on unix to avoid busy looping, and only wake up when a new connection happens
1 parent cd4e51e commit 2aaf596

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All changes to this project will be documented in this file.
44

5+
## [10.2.4] - 2020-08-15
6+
7+
(socket server) in the loop accepting connections, call select without a timeout on unix to avoid busy looping, and only wake up when a new connection happens
8+
59
## [10.2.3] - 2020-08-15
610

711
(socket server) instead of busy looping with a sleep, only wake up the GC thread when a new thread will have to be joined, (we know that thanks to the ConnectionState OnSetTerminated callback

ixwebsocket/IXSocketServer.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ namespace ix
6060

6161
std::pair<bool, std::string> SocketServer::listen()
6262
{
63+
std::string acceptSelectInterruptInitErrorMsg;
64+
if (!_acceptSelectInterrupt->init(acceptSelectInterruptInitErrorMsg))
65+
{
66+
std::stringstream ss;
67+
ss << "SocketServer::listen() error in SelectInterrupt::init: "
68+
<< acceptSelectInterruptInitErrorMsg;
69+
70+
return std::make_pair(false, ss.str());
71+
}
72+
6373
if (_addressFamily != AF_INET && _addressFamily != AF_INET6)
6474
{
6575
std::string errMsg("SocketServer::listen() AF_INET and AF_INET6 are currently "
@@ -195,7 +205,12 @@ namespace ix
195205
if (_thread.joinable())
196206
{
197207
_stop = true;
198-
_acceptSelectInterrupt->notify(SelectInterrupt::kCloseRequest); // Wake up select
208+
// Wake up select
209+
if (!_acceptSelectInterrupt->notify(SelectInterrupt::kCloseRequest))
210+
{
211+
logError("SocketServer::stop: Cannot wake up from select");
212+
}
213+
199214
_thread.join();
200215
_stop = false;
201216
}
@@ -260,7 +275,12 @@ namespace ix
260275
if (_stop) return;
261276

262277
// Use poll to check whether a new connection is in progress
263-
int timeoutMs = 10;
278+
int timeoutMs = -1;
279+
#ifdef _WIN32
280+
// select cannot be interrupted on Windows so we need to pass a small timeout
281+
timeoutMs = 10;
282+
#endif
283+
264284
bool readyToRead = true;
265285
PollResultType pollResult =
266286
Socket::poll(readyToRead, timeoutMs, _serverFd, _acceptSelectInterrupt);

ixwebsocket/IXWebSocketVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
#pragma once
88

9-
#define IX_WEBSOCKET_VERSION "10.2.3"
9+
#define IX_WEBSOCKET_VERSION "10.2.4"

0 commit comments

Comments
 (0)