Prepare I/O device support

This commit is contained in:
momo5502
2024-11-05 20:54:38 +01:00
parent 67b204b695
commit 4c0c1bf0c6
3 changed files with 85 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ struct handle_types
{
invalid = 0,
file,
device,
event,
section,
symlink,

View File

@@ -0,0 +1,80 @@
#pragma once
#include <memory>
#include <serialization.hpp>
struct io_device
{
io_device() = default;
virtual ~io_device() = default;
// TODO
virtual void read() = 0;
virtual void write() = 0;
virtual void serialize(utils::buffer_serializer& buffer) const = 0;
virtual void deserialize(utils::buffer_deserializer& buffer) = 0;
};
// TODO
inline std::unique_ptr<io_device> create_device(const std::wstring_view device)
{
(void)device;
return {};
}
class io_device_container : public io_device
{
public:
io_device_container() = default;
io_device_container(std::wstring device)
: device_name_(std::move(device))
{
this->setup();
}
void read() override
{
this->assert_validity();
this->device_->read();
}
void write() override
{
this->assert_validity();
this->device_->write();
}
void serialize(utils::buffer_serializer& buffer) const override
{
this->assert_validity();
buffer.write_string(this->device_name_);
this->device_->serialize(buffer);
}
void deserialize(utils::buffer_deserializer& buffer) override
{
buffer.read_string(this->device_name_);
this->setup();
this->device_->deserialize(buffer);
}
private:
std::wstring device_name_{};
std::unique_ptr<io_device> device_{};
void setup()
{
this->device_ = create_device(this->device_name_);
}
void assert_validity() const
{
if (!this->device_)
{
throw std::runtime_error("Device not created!");
}
}
};

View File

@@ -11,6 +11,7 @@
#include <x64_emulator.hpp>
#include <serialization_helper.hpp>
#include "io_device.hpp"
#define PEB_SEGMENT_SIZE (20 << 20) // 20 MB
#define GS_SEGMENT_SIZE (1 << 20) // 1 MB
@@ -392,6 +393,7 @@ struct process_context
handle_store<handle_types::event, event> events{};
handle_store<handle_types::file, file> files{};
handle_store<handle_types::device, io_device_container> devices{};
handle_store<handle_types::semaphore, semaphore> semaphores{};
handle_store<handle_types::port, port> ports{};
handle_store<handle_types::registry, registry_key, 2> registry_keys{};
@@ -428,6 +430,7 @@ struct process_context
buffer.write(this->shared_section_size);
buffer.write(this->events);
buffer.write(this->files);
buffer.write(this->devices);
buffer.write(this->semaphores);
buffer.write(this->ports);
buffer.write(this->registry_keys);
@@ -469,6 +472,7 @@ struct process_context
buffer.read(this->shared_section_size);
buffer.read(this->events);
buffer.read(this->files);
buffer.read(this->devices);
buffer.read(this->semaphores);
buffer.read(this->ports);
buffer.read(this->registry_keys);