mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:27:35 +00:00
SpiceAgent: Implement FileTransferStart
messages
This commit is contained in:
parent
0d98920930
commit
476774d681
3 changed files with 89 additions and 0 deletions
|
@ -186,4 +186,59 @@ ErrorOr<String> ClipboardMessage::debug_description()
|
|||
return builder.to_string();
|
||||
}
|
||||
|
||||
ErrorOr<FileTransferStartMessage> FileTransferStartMessage::read_from_stream(AK::Stream& stream)
|
||||
{
|
||||
auto id = TRY(stream.read_value<u32>());
|
||||
|
||||
auto metadata_bytes = TRY(stream.read_until_eof());
|
||||
auto metadata_content = TRY(String::from_utf8(metadata_bytes));
|
||||
|
||||
// TODO: We need some sort of INIParser, or we need to make Core::ConfigFile not depend on having an actual Core::File
|
||||
// The first line in the file should always be `[vdagent-file-xfer]`
|
||||
auto lines = TRY(metadata_content.split('\n'));
|
||||
if (lines.is_empty() || lines.at(0) != "[vdagent-file-xfer]") {
|
||||
return Error::from_string_literal("Failed to parse file transfer metadata");
|
||||
}
|
||||
|
||||
Optional<String> name = {};
|
||||
Optional<u32> size = {};
|
||||
|
||||
for (auto const& line : lines) {
|
||||
// Ignore the header, we already assume that it is [vdagent-file-xfer]
|
||||
if (line.starts_with('['))
|
||||
continue;
|
||||
|
||||
if (line.starts_with_bytes("name="sv)) {
|
||||
auto parsed_name = TRY(line.replace("name="sv, ""sv, ReplaceMode::FirstOnly));
|
||||
if (parsed_name.is_empty()) {
|
||||
return Error::from_string_literal("Failed to parse file name!");
|
||||
}
|
||||
|
||||
name = parsed_name;
|
||||
}
|
||||
|
||||
if (line.starts_with_bytes("size="sv)) {
|
||||
auto size_string = TRY(line.replace("size="sv, ""sv, ReplaceMode::FirstOnly));
|
||||
size = size_string.to_number<u32>(TrimWhitespace::Yes);
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that we actually parsed any data
|
||||
if (!name.has_value() || !size.has_value()) {
|
||||
return Error::from_string_literal("Invalid transfer start message received!");
|
||||
}
|
||||
|
||||
return FileTransferStartMessage(id, Metadata { name.release_value(), size.release_value() });
|
||||
}
|
||||
|
||||
ErrorOr<String> FileTransferStartMessage::debug_description()
|
||||
{
|
||||
StringBuilder builder;
|
||||
TRY(builder.try_append("FileTransferStart { "sv));
|
||||
TRY(builder.try_appendff("id = {}, ", id()));
|
||||
TRY(builder.try_appendff("metadata = Metadata {{ name = {}, size = {} }}", metadata().name, metadata().size));
|
||||
TRY(builder.try_append(" }"sv));
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -179,6 +179,33 @@ private:
|
|||
ByteBuffer m_contents;
|
||||
};
|
||||
|
||||
// Sent to the agent to indicate that a file transfer has been requested.
|
||||
class FileTransferStartMessage : public Message {
|
||||
public:
|
||||
struct Metadata {
|
||||
String name;
|
||||
u32 size;
|
||||
};
|
||||
|
||||
static ErrorOr<FileTransferStartMessage> read_from_stream(AK::Stream& stream);
|
||||
|
||||
ErrorOr<String> debug_description() override;
|
||||
|
||||
u32 id() const { return m_id; }
|
||||
Metadata const& metadata() { return m_metadata; }
|
||||
|
||||
private:
|
||||
FileTransferStartMessage(u32 id, FileTransferStartMessage::Metadata const& metadata)
|
||||
: Message(Type::FileTransferStart)
|
||||
, m_id(id)
|
||||
, m_metadata(metadata)
|
||||
{
|
||||
}
|
||||
|
||||
u32 m_id { 0 };
|
||||
Metadata m_metadata;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
|
|
@ -154,6 +154,13 @@ ErrorOr<void> SpiceAgent::on_message_received()
|
|||
break;
|
||||
}
|
||||
|
||||
case Message::Type::FileTransferStart: {
|
||||
auto message = TRY(FileTransferStartMessage::read_from_stream(stream));
|
||||
dbgln("File transfer request received: {}", TRY(message.debug_description()));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// We ignore certain messages to prevent it from clogging up the logs.
|
||||
case Message::Type::MonitorsConfig:
|
||||
dbgln_if(SPICE_AGENT_DEBUG, "Ignored message: {}", header);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue