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

LibIPC: Replace Result<T, E> use with ErrorOr<T>

This commit is contained in:
Andreas Kling 2021-11-07 11:59:43 +01:00
parent 0b0c4e82b9
commit 2c70c479ab
3 changed files with 18 additions and 19 deletions

View file

@ -30,7 +30,10 @@ public:
, m_client_id(client_id) , m_client_id(client_id)
{ {
VERIFY(this->socket().is_connected()); VERIFY(this->socket().is_connected());
this->socket().on_ready_to_read = [this] { this->drain_messages_from_peer(); }; this->socket().on_ready_to_read = [this] {
// FIXME: Do something about errors.
(void)this->drain_messages_from_peer();
};
} }
virtual ~ClientConnection() override virtual ~ClientConnection() override

View file

@ -113,7 +113,7 @@ void ConnectionBase::wait_for_socket_to_become_readable()
} }
} }
Result<Vector<u8>, bool> ConnectionBase::read_as_much_as_possible_from_socket_without_blocking() ErrorOr<Vector<u8>> ConnectionBase::read_as_much_as_possible_from_socket_without_blocking()
{ {
Vector<u8> bytes; Vector<u8> bytes;
@ -130,12 +130,11 @@ Result<Vector<u8>, bool> ConnectionBase::read_as_much_as_possible_from_socket_wi
break; break;
perror("recv"); perror("recv");
exit(1); exit(1);
return false;
} }
if (nread == 0) { if (nread == 0) {
if (bytes.is_empty()) { if (bytes.is_empty()) {
deferred_invoke([this] { shutdown(); }); deferred_invoke([this] { shutdown(); });
return false; return Error::from_string_literal("IPC connection EOF"sv);
} }
break; break;
} }
@ -150,7 +149,7 @@ Result<Vector<u8>, bool> ConnectionBase::read_as_much_as_possible_from_socket_wi
return bytes; return bytes;
} }
bool ConnectionBase::drain_messages_from_peer() ErrorOr<void> ConnectionBase::drain_messages_from_peer()
{ {
auto bytes = TRY(read_as_much_as_possible_from_socket_without_blocking()); auto bytes = TRY(read_as_much_as_possible_from_socket_without_blocking());
@ -161,17 +160,14 @@ bool ConnectionBase::drain_messages_from_peer()
// Sometimes we might receive a partial message. That's okay, just stash away // Sometimes we might receive a partial message. That's okay, just stash away
// the unprocessed bytes and we'll prepend them to the next incoming message // the unprocessed bytes and we'll prepend them to the next incoming message
// in the next run of this function. // in the next run of this function.
auto remaining_bytes_result = ByteBuffer::copy(bytes.span().slice(index)); auto maybe_remaining_bytes = ByteBuffer::copy(bytes.span().slice(index));
if (!remaining_bytes_result.has_value()) { if (!maybe_remaining_bytes.has_value())
dbgln("{}::drain_messages_from_peer: Failed to allocate buffer", static_cast<Core::Object const&>(*this)); return Error::from_string_literal("drain_messages_from_peer: Failed to allocate buffer"sv);
return false;
}
if (!m_unprocessed_bytes.is_empty()) { if (!m_unprocessed_bytes.is_empty()) {
dbgln("{}::drain_messages_from_peer: Already have unprocessed bytes", static_cast<Core::Object const&>(*this));
shutdown(); shutdown();
return false; return Error::from_string_literal("drain_messages_from_peer: Already have unprocessed bytes"sv);
} }
m_unprocessed_bytes = remaining_bytes_result.release_value(); m_unprocessed_bytes = maybe_remaining_bytes.release_value();
} }
if (!m_unprocessed_messages.is_empty()) { if (!m_unprocessed_messages.is_empty()) {
@ -179,7 +175,7 @@ bool ConnectionBase::drain_messages_from_peer()
handle_messages(); handle_messages();
}); });
} }
return true; return {};
} }
OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id) OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id)
@ -199,7 +195,7 @@ OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32
break; break;
wait_for_socket_to_become_readable(); wait_for_socket_to_become_readable();
if (!drain_messages_from_peer()) if (drain_messages_from_peer().is_error())
break; break;
} }
return {}; return {};

View file

@ -8,7 +8,6 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/NonnullOwnPtrVector.h> #include <AK/NonnullOwnPtrVector.h>
#include <AK/Result.h>
#include <AK/Try.h> #include <AK/Try.h>
#include <LibCore/Event.h> #include <LibCore/Event.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
@ -50,8 +49,8 @@ protected:
OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id); OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id);
void wait_for_socket_to_become_readable(); void wait_for_socket_to_become_readable();
Result<Vector<u8>, bool> read_as_much_as_possible_from_socket_without_blocking(); ErrorOr<Vector<u8>> read_as_much_as_possible_from_socket_without_blocking();
bool drain_messages_from_peer(); ErrorOr<void> drain_messages_from_peer();
void post_message(MessageBuffer); void post_message(MessageBuffer);
void handle_messages(); void handle_messages();
@ -76,7 +75,8 @@ public:
{ {
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
NonnullRefPtr protect = *this; NonnullRefPtr protect = *this;
drain_messages_from_peer(); // FIXME: Do something about errors.
(void)drain_messages_from_peer();
handle_messages(); handle_messages();
}; };
} }