1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibCore+Everywhere: Make Core::Stream::read() return Bytes

A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
This commit is contained in:
Sam Atkins 2022-04-15 13:33:02 +01:00 committed by Tim Flynn
parent 6654efcd82
commit 3b1e063d30
22 changed files with 103 additions and 103 deletions

View file

@ -162,31 +162,31 @@ private:
#ifdef __serenity__
m_socket->on_ready_to_read = [this] {
u32 length;
auto maybe_nread = m_socket->read({ (u8*)&length, sizeof(length) });
if (maybe_nread.is_error()) {
dbgln("InspectorServerConnection: Failed to read message length from inspector server connection: {}", maybe_nread.error());
auto maybe_bytes_read = m_socket->read({ (u8*)&length, sizeof(length) });
if (maybe_bytes_read.is_error()) {
dbgln("InspectorServerConnection: Failed to read message length from inspector server connection: {}", maybe_bytes_read.error());
shutdown();
return;
}
auto nread = maybe_nread.release_value();
if (nread == 0) {
auto bytes_read = maybe_bytes_read.release_value();
if (bytes_read.is_empty()) {
dbgln_if(EVENTLOOP_DEBUG, "RPC client disconnected");
shutdown();
return;
}
VERIFY(nread == sizeof(length));
VERIFY(bytes_read.size() == sizeof(length));
auto request_buffer = ByteBuffer::create_uninitialized(length).release_value();
maybe_nread = m_socket->read(request_buffer.bytes());
if (maybe_nread.is_error()) {
dbgln("InspectorServerConnection: Failed to read message content from inspector server connection: {}", maybe_nread.error());
maybe_bytes_read = m_socket->read(request_buffer.bytes());
if (maybe_bytes_read.is_error()) {
dbgln("InspectorServerConnection: Failed to read message content from inspector server connection: {}", maybe_bytes_read.error());
shutdown();
return;
}
nread = maybe_nread.release_value();
bytes_read = maybe_bytes_read.release_value();
auto request_json = JsonValue::from_string(request_buffer);
if (request_json.is_error() || !request_json.value().is_object()) {