Skip to content

Commit

Permalink
Async: Simplify reporting AsyncSocketReceive disconnected on posix
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Aug 25, 2024
1 parent 3735432 commit a7c64cc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
2 changes: 0 additions & 2 deletions Libraries/Async/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,6 @@ struct AsyncSocketReceive : public AsyncRequest
Span<char> buffer;
#if SC_PLATFORM_WINDOWS
detail::WinOverlappedOpaque overlapped;
#else
bool disconnectedReceived = true;
#endif
};

Expand Down
22 changes: 4 additions & 18 deletions Libraries/Async/Internal/AsyncPosix.inl
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix

if (numBytesSent < 0)
{
if (errno == EWOULDBLOCK || errno == EAGAIN)
const auto sendError = errno;
if (sendError == EWOULDBLOCK || sendError == EAGAIN)
{
// Partial write case:
// Socket is not writable right now, we should wait for it to be writable again, to finish
Expand Down Expand Up @@ -657,24 +658,9 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
const ssize_t res = ::recv(async.handle, async.buffer.data(), async.buffer.sizeInBytes(), 0);
SC_TRY_MSG(res >= 0, "error in recv");
result.completionData.numBytes = static_cast<size_t>(res);
// It can happen that async.disconnectedReceived == true when reading the last trail of bytes.
// To unify differences between different posix-es, let's set disconnected flag only after
// the final zero read, possibly on next loop run.
if (async.disconnectedReceived)
if (res == 0)
{
if (res == 0)
{
result.completionData.disconnected = true;
}
}
else
{
const auto& event = events[result.getAsync().eventIndex];
#if SC_ASYNC_USE_EPOLL
async.disconnectedReceived = (event.events & EPOLLRDHUP) != 0;
#else
async.disconnectedReceived = (event.flags & EV_EOF) != 0;
#endif
result.completionData.disconnected = true;
}
return Result(true);
}
Expand Down

0 comments on commit a7c64cc

Please sign in to comment.