summaryrefslogtreecommitdiff
path: root/Userland/Services
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-04-15 13:33:02 +0100
committerTim Flynn <trflynn89@pm.me>2022-04-16 13:27:51 -0400
commit3b1e063d30d915826949ce13e069761d7a32e8a5 (patch)
treeea04c82daeea29fda7505a5a85e750d208e22457 /Userland/Services
parent6654efcd8207811bf3442368bf3cba04ac4ec5ae (diff)
downloadserenity-3b1e063d30d915826949ce13e069761d7a32e8a5.zip
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
Diffstat (limited to 'Userland/Services')
-rw-r--r--Userland/Services/EchoServer/Client.cpp6
-rw-r--r--Userland/Services/FileOperation/main.cpp8
-rw-r--r--Userland/Services/InspectorServer/InspectableProcess.cpp16
-rw-r--r--Userland/Services/LookupServer/LookupServer.cpp2
-rw-r--r--Userland/Services/TelnetServer/Client.cpp4
5 files changed, 18 insertions, 18 deletions
diff --git a/Userland/Services/EchoServer/Client.cpp b/Userland/Services/EchoServer/Client.cpp
index 0a3ae489ba..2538ffa24f 100644
--- a/Userland/Services/EchoServer/Client.cpp
+++ b/Userland/Services/EchoServer/Client.cpp
@@ -30,16 +30,16 @@ ErrorOr<void> Client::drain_socket()
auto buffer = TRY(ByteBuffer::create_uninitialized(1024));
while (TRY(m_socket->can_read_without_blocking())) {
- auto nread = TRY(m_socket->read(buffer));
+ auto bytes_read = TRY(m_socket->read(buffer));
- dbgln("Read {} bytes.", nread);
+ dbgln("Read {} bytes.", bytes_read.size());
if (m_socket->is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
break;
}
- TRY(m_socket->write({ buffer.data(), nread }));
+ TRY(m_socket->write(bytes_read));
}
return {};
diff --git a/Userland/Services/FileOperation/main.cpp b/Userland/Services/FileOperation/main.cpp
index f27caa229d..05f4432277 100644
--- a/Userland/Services/FileOperation/main.cpp
+++ b/Userland/Services/FileOperation/main.cpp
@@ -238,15 +238,15 @@ ErrorOr<int> execute_work_items(Vector<WorkItem> const& items)
while (true) {
print_progress();
auto bytes_read = TRY(source_file->read(buffer.bytes()));
- if (bytes_read == 0)
+ if (bytes_read.is_empty())
break;
- if (auto result = destination_file->write(buffer); result.is_error()) {
+ if (auto result = destination_file->write(bytes_read); result.is_error()) {
// FIXME: Return the formatted string directly. There is no way to do this right now without the temporary going out of scope and being destroyed.
report_warning(String::formatted("Failed to write to destination file: {}", result.error()));
return result.error();
}
- item_done += bytes_read;
- executed_work_bytes += bytes_read;
+ item_done += bytes_read.size();
+ executed_work_bytes += bytes_read.size();
print_progress();
// FIXME: Remove this once the kernel is smart enough to schedule other threads
// while we're doing heavy I/O. Right now, copying a large file will totally
diff --git a/Userland/Services/InspectorServer/InspectableProcess.cpp b/Userland/Services/InspectorServer/InspectableProcess.cpp
index 1b085db255..a709d180fe 100644
--- a/Userland/Services/InspectorServer/InspectableProcess.cpp
+++ b/Userland/Services/InspectorServer/InspectableProcess.cpp
@@ -43,8 +43,8 @@ String InspectableProcess::wait_for_response()
}
u32 length {};
- auto nread = m_socket->read({ (u8*)&length, sizeof(length) }).release_value_but_fixme_should_propagate_errors();
- if (nread != sizeof(length)) {
+ auto length_bytes_read = m_socket->read({ (u8*)&length, sizeof(length) }).release_value_but_fixme_should_propagate_errors();
+ if (length_bytes_read.size() != sizeof(length)) {
dbgln("InspectableProcess got malformed data: PID {}", m_pid);
m_socket->close();
return {};
@@ -54,17 +54,17 @@ String InspectableProcess::wait_for_response()
auto remaining_data_buffer = data_buffer.bytes();
while (!remaining_data_buffer.is_empty()) {
- auto maybe_nread = m_socket->read(remaining_data_buffer);
- if (maybe_nread.is_error()) {
- dbgln("InspectableProcess::wait_for_response: Failed to read data: {}", maybe_nread.error());
+ auto maybe_bytes_read = m_socket->read(remaining_data_buffer);
+ if (maybe_bytes_read.is_error()) {
+ dbgln("InspectableProcess::wait_for_response: Failed to read data: {}", maybe_bytes_read.error());
break;
}
- auto nread = maybe_nread.release_value();
- if (nread == 0)
+ auto bytes_read = maybe_bytes_read.release_value();
+ if (bytes_read.is_empty())
break;
- remaining_data_buffer = remaining_data_buffer.slice(nread);
+ remaining_data_buffer = remaining_data_buffer.slice(bytes_read.size());
}
VERIFY(data_buffer.size() == length);
diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp
index c69749e401..5cea3bf57b 100644
--- a/Userland/Services/LookupServer/LookupServer.cpp
+++ b/Userland/Services/LookupServer/LookupServer.cpp
@@ -239,7 +239,7 @@ ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, String const& nam
TRY(udp_socket->write(buffer));
u8 response_buffer[4096];
- int nrecv = TRY(udp_socket->read({ response_buffer, sizeof(response_buffer) }));
+ int nrecv = TRY(udp_socket->read({ response_buffer, sizeof(response_buffer) })).size();
if (udp_socket->is_eof())
return Vector<Answer> {};
diff --git a/Userland/Services/TelnetServer/Client.cpp b/Userland/Services/TelnetServer/Client.cpp
index d45800975c..68dda4074d 100644
--- a/Userland/Services/TelnetServer/Client.cpp
+++ b/Userland/Services/TelnetServer/Client.cpp
@@ -76,9 +76,9 @@ ErrorOr<void> Client::drain_socket()
auto buffer = TRY(ByteBuffer::create_uninitialized(1024));
while (TRY(m_socket->can_read_without_blocking())) {
- auto nread = TRY(m_socket->read(buffer));
+ auto read_bytes = TRY(m_socket->read(buffer));
- m_parser.write({ buffer.data(), nread });
+ m_parser.write(StringView { read_bytes });
if (m_socket->is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });