mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +00:00
LibIPC+Everywhere: Change IPC::encode's return type to ErrorOr
In doing so, this removes all uses of the Encoder's stream operator, except for where it is currently still used in the generated IPC code. So the stream operator currently discards any errors, which is the existing behavior. A subsequent commit will propagate the errors.
This commit is contained in:
parent
d0f3f3d5ff
commit
ab99ed5fba
29 changed files with 224 additions and 238 deletions
|
@ -14,6 +14,7 @@
|
|||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibCore/Proxy.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/Dictionary.h>
|
||||
#include <LibIPC/Encoder.h>
|
||||
#include <LibIPC/File.h>
|
||||
|
@ -21,132 +22,113 @@
|
|||
namespace IPC {
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, float const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, float const& value)
|
||||
{
|
||||
return encoder.encode(bit_cast<u32>(value));
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, double const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, double const& value)
|
||||
{
|
||||
return encoder.encode(bit_cast<u64>(value));
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, StringView const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, StringView const& value)
|
||||
{
|
||||
auto result = encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length());
|
||||
return !result.is_error();
|
||||
TRY(encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length()));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, DeprecatedString const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, DeprecatedString const& value)
|
||||
{
|
||||
if (value.is_null())
|
||||
return encoder.encode(-1);
|
||||
|
||||
if (!encoder.encode(static_cast<i32>(value.length())))
|
||||
return false;
|
||||
return encoder.encode(value.view());
|
||||
TRY(encoder.encode(static_cast<i32>(value.length())));
|
||||
TRY(encoder.encode(value.view()));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, ByteBuffer const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, ByteBuffer const& value)
|
||||
{
|
||||
if (!encoder.encode(static_cast<i32>(value.size())))
|
||||
return false;
|
||||
|
||||
auto result = encoder.append(value.data(), value.size());
|
||||
return !result.is_error();
|
||||
TRY(encoder.encode(static_cast<i32>(value.size())));
|
||||
TRY(encoder.append(value.data(), value.size()));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, JsonValue const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, JsonValue const& value)
|
||||
{
|
||||
return encoder.encode(value.serialized<StringBuilder>());
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, URL const& value)
|
||||
ErrorOr<void> encode(Encoder& encoder, URL const& value)
|
||||
{
|
||||
return encoder.encode(value.to_deprecated_string());
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, Dictionary const& dictionary)
|
||||
ErrorOr<void> encode(Encoder& encoder, Dictionary const& dictionary)
|
||||
{
|
||||
if (!encoder.encode(static_cast<u64>(dictionary.size())))
|
||||
return false;
|
||||
TRY(encoder.encode(static_cast<u64>(dictionary.size())));
|
||||
|
||||
bool had_error = false;
|
||||
TRY(dictionary.try_for_each_entry([&](auto const& key, auto const& value) -> ErrorOr<void> {
|
||||
TRY(encoder.encode(key));
|
||||
TRY(encoder.encode(value));
|
||||
return {};
|
||||
}));
|
||||
|
||||
dictionary.for_each_entry([&](auto const& key, auto const& value) {
|
||||
if (had_error)
|
||||
return;
|
||||
if (!encoder.encode(key) || !encoder.encode(value))
|
||||
had_error = true;
|
||||
});
|
||||
|
||||
return !had_error;
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, File const& file)
|
||||
ErrorOr<void> encode(Encoder& encoder, File const& file)
|
||||
{
|
||||
int fd = file.fd();
|
||||
|
||||
if (fd != -1) {
|
||||
auto result = dup(fd);
|
||||
if (result < 0) {
|
||||
perror("dup");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
fd = result;
|
||||
}
|
||||
if (fd != -1)
|
||||
fd = TRY(Core::System::dup(fd));
|
||||
|
||||
if (encoder.append_file_descriptor(fd).is_error())
|
||||
return false;
|
||||
return true;
|
||||
TRY(encoder.append_file_descriptor(fd));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder&, Empty const&)
|
||||
ErrorOr<void> encode(Encoder&, Empty const&)
|
||||
{
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
||||
ErrorOr<void> encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
||||
{
|
||||
if (!encoder.encode(buffer.is_valid()))
|
||||
return false;
|
||||
TRY(encoder.encode(buffer.is_valid()));
|
||||
|
||||
if (buffer.is_valid()) {
|
||||
if (!encoder.encode(static_cast<u32>(buffer.size())))
|
||||
return false;
|
||||
if (!encoder.encode(IPC::File { buffer.fd() }))
|
||||
return false;
|
||||
TRY(encoder.encode(static_cast<u32>(buffer.size())));
|
||||
TRY(encoder.encode(IPC::File { buffer.fd() }));
|
||||
}
|
||||
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, Core::DateTime const& datetime)
|
||||
ErrorOr<void> encode(Encoder& encoder, Core::DateTime const& datetime)
|
||||
{
|
||||
return encoder.encode(static_cast<i64>(datetime.timestamp()));
|
||||
}
|
||||
|
||||
template<>
|
||||
bool encode(Encoder& encoder, Core::ProxyData const& proxy)
|
||||
ErrorOr<void> encode(Encoder& encoder, Core::ProxyData const& proxy)
|
||||
{
|
||||
if (!encoder.encode(proxy.type))
|
||||
return false;
|
||||
if (!encoder.encode(proxy.host_ipv4))
|
||||
return false;
|
||||
if (!encoder.encode(proxy.port))
|
||||
return false;
|
||||
return true;
|
||||
TRY(encoder.encode(proxy.type));
|
||||
TRY(encoder.encode(proxy.host_ipv4));
|
||||
TRY(encoder.encode(proxy.port));
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue