1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 07:07:44 +00:00

SpiceAgent: Add support for reading chunks larger than 2048 bytes

This commit is contained in:
Caoimhe 2023-05-13 15:09:51 +01:00 committed by Andreas Kling
parent 3b6d63f723
commit 50a8db3922
3 changed files with 69 additions and 24 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include "ChunkHeader.h"
#include "Message.h"
#include "MessageHeader.h"
#include <AK/MemoryStream.h>
@ -16,16 +17,12 @@
namespace SpiceAgent {
// The maximum amount of data that can be contained within a message's buffer.
// If the buffer's length is equal to this, then the next data recieved will be more data from the same buffer.
constexpr u32 message_buffer_threshold = 2048;
class SpiceAgent {
public:
// Indicates where the message has come from.
enum class Port : u32 {
Client = 1,
// There are currently no messages which are meant for the server, so all messages sent by the agent (us) with this port are discarded.
Server
};
static ErrorOr<NonnullOwnPtr<SpiceAgent>> create(StringView device_path);
SpiceAgent(NonnullOwnPtr<Core::File> spice_device, Vector<Capability> const& capabilities);
@ -39,20 +36,20 @@ public:
TRY(message.write_to_stream(message_stream));
// Create a header to be sent.
auto header_stream = AK::AllocatingMemoryStream();
auto header = MessageHeader(message.type(), message_stream.used_buffer_size());
TRY(header_stream.write_value(header));
auto message_header_stream = AK::AllocatingMemoryStream();
auto message_header = MessageHeader(message.type(), message_stream.used_buffer_size());
TRY(message_header_stream.write_value(message_header));
// The length given in the chunk header is the length of the message header, and the message combined
auto length = message_header_stream.used_buffer_size() + message_stream.used_buffer_size();
// Currently, there are no messages from the agent which are meant for the server.
// So, all messages sent by the agent with a port of Port::Server get dropped silently.
TRY(m_spice_device->write_value(Port::Client));
// The length of the subsequent data.
auto length = header_stream.used_buffer_size() + message_stream.used_buffer_size();
TRY(m_spice_device->write_value<u32>(length));
auto chunk_header = ChunkHeader(ChunkHeader::Port::Client, length);
TRY(m_spice_device->write_value(chunk_header));
// The message's header.
TRY(m_spice_device->write_until_depleted(TRY(header_stream.read_until_eof())));
TRY(m_spice_device->write_until_depleted(TRY(message_header_stream.read_until_eof())));
// The message content.
TRY(m_spice_device->write_until_depleted(TRY(message_stream.read_until_eof())));