Skip to content

Commit

Permalink
More EINTR checks
Browse files Browse the repository at this point in the history
  • Loading branch information
trikko committed May 29, 2024
1 parent b8b7197 commit f5f6c69
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
32 changes: 28 additions & 4 deletions source/serverino/communicator.d
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,16 @@ package class Communicator
}
else if (sendBuffer.length > 0)
{
immutable sent = clientSkt.send(sendBuffer.array);
buffer_again: immutable sent = clientSkt.send(sendBuffer.array);

if (sent == Socket.ERROR)
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto buffer_again;
}

if(!wouldHaveBlocked)
{
log("Socket Error");
Expand Down Expand Up @@ -366,7 +372,7 @@ package class Communicator
if (maxToSend == 0)
return;

immutable sent = clientSkt.send(sendBuffer.array[bufferSent..maxToSend]);
again: immutable sent = clientSkt.send(sendBuffer.array[bufferSent..maxToSend]);

if (sent >= 0)
{
Expand Down Expand Up @@ -404,6 +410,12 @@ package class Communicator
}
else
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto again;
}

if(!wouldHaveBlocked)
{
log("Socket Error");
Expand Down Expand Up @@ -470,7 +482,7 @@ package class Communicator

if (sendBuffer.length == 0)
{
auto sent = clientSkt.send(data);
again: auto sent = clientSkt.send(data);

if (sent >= 0)
{
Expand Down Expand Up @@ -505,6 +517,12 @@ package class Communicator
}
else
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto again;
}

if(!wouldHaveBlocked)
{
log("Socket error on write. ", lastSocketError);
Expand Down Expand Up @@ -551,7 +569,7 @@ package class Communicator

// Read the data from the client socket if it's not buffered
// Set the requestDatareceived flag to true if the first data is read to check for timeouts
bytesRead = clientSkt.receive(buffer);
again: bytesRead = clientSkt.receive(buffer);
lastRecv = now;

if (bytesRead > 0)
Expand Down Expand Up @@ -860,6 +878,12 @@ package class Communicator
}
else
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto again;
}

if(!wouldHaveBlocked)
{
status = State.READY;
Expand Down
25 changes: 22 additions & 3 deletions source/serverino/interfaces.d
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ class WebSocket
return msg;

ubyte[4096] buffer = void;
auto received = _socket.receive(buffer);
again: auto received = _socket.receive(buffer);

if (received == 0)
{
Expand All @@ -1495,6 +1495,12 @@ class WebSocket

if (received < 0)
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto again;
}

if (!_socket.isAlive || !wouldHaveBlocked) kill("error receiving data");
return WebSocketMessage.init;
}
Expand Down Expand Up @@ -1529,10 +1535,17 @@ class WebSocket

if (_leftover.length > 0)
{
auto ret = _socket.send(_leftover);
leftover_again: auto ret = _socket.send(_leftover);

// Not sent
if (ret < 0)
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto leftover_again;
}

if (wouldHaveBlocked())
{
_leftover ~= data;
Expand All @@ -1559,11 +1572,17 @@ class WebSocket

if (data.length > 0)
{
auto ret = _socket.send(data);
again: auto ret = _socket.send(data);

// Not sent
if (ret < 0)
{
version(Posix)
{
import core.stdc.errno : errno, EINTR;
if (errno == EINTR) goto again;
}

if (wouldHaveBlocked())
{
_leftover ~= data;
Expand Down

0 comments on commit f5f6c69

Please sign in to comment.