mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 23:38:12 +00:00
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:
parent
a428812ba2
commit
dce97678ae
1 changed files with 8 additions and 5 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue