Subject: Re: bug#50849: 28.0.50; Proposal for Emacs daemon to signal when being busy

From: Lars Ingebrigtsen <[email-masked]>
Date: Fri, 02 Sep 2022 14:28:18 +0200

Eli Zaretskii <[email-masked]> writes:

> Looks like it doesn't by default, but we can ask it to do so.  See,
> for example,
> https://stackoverflow.com/questions/30395258/setting-timeout-to-recv-function.

For posterity (in case Stackoverflow goes away before somebody gets
around to fixing this 🫠), the suggested solution is to do a setsockopt
with a RCVTIMEO.  Which seems reasonable.

What would be a reasonable timeout here?  Say...  five seconds?

---
On Windows, the code would look like this:

DWORD timeout = SOCKET_READ_TIMEOUT_SEC * 1000;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));

//...

recv_size = recv(s, rx_tmp, bufSize, 0);
if (recv_size == SOCKET_ERROR)
{
    if (WSAGetLastError() != WSAETIMEDOUT)
        //...
}

On other platforms, the code would look like this instead:

struct timeval timeout;
timeout.tv_sec = SOCKET_READ_TIMEOUT_SEC;
timeout.tv_usec = 0;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

//...

recv_size = recv(s, rx_tmp, bufSize, 0);
if (recv_size == -1)
{
    if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
        //...
}