LibCore: Handle partial writes in Socket::send()

Right now Socket::send() assumes that it can send everything in one
go. However, send() is allowed to do partial writes and while that
can't happen at the moment there's nothing that says this can't
happen in the future (like in the next commit).
This commit is contained in:
Gunnar Beutner 2021-05-25 21:28:31 +02:00 committed by Andreas Kling
parent a428812ba2
commit dce97678ae
Notes: sideshowbarker 2024-07-18 17:23:48 +09:00

View file

@ -158,12 +158,15 @@ ByteBuffer Socket::receive(int max_size)
bool Socket::send(ReadonlyBytes data)
{
ssize_t nsent = ::send(fd(), data.data(), data.size(), 0);
if (nsent < 0) {
set_error(errno);
return false;
auto remaining_bytes = data.size();
while (remaining_bytes > 0) {
ssize_t nsent = ::send(fd(), data.data() + (data.size() - remaining_bytes), remaining_bytes, 0);
if (nsent < 0) {
set_error(errno);
return false;
}
remaining_bytes -= nsent;
}
VERIFY(static_cast<size_t>(nsent) == data.size());
return true;
}