1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:18:13 +00:00

Chess: Avoid IODevice and DeprecatedFile

This commit is contained in:
Ben Wiederhake 2023-05-07 00:53:19 +02:00 committed by Jelle Raaijmakers
parent 715f4a8d7e
commit 0fe29a48ad
5 changed files with 30 additions and 33 deletions

View file

@ -8,15 +8,14 @@
#include "UCIEndpoint.h"
#include <AK/ByteBuffer.h>
#include <AK/Debug.h>
#include <AK/DeprecatedString.h>
#include <LibCore/EventLoop.h>
namespace Chess::UCI {
Endpoint::Endpoint(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out)
: m_in(in)
, m_out(out)
, m_in_notifier(Core::Notifier::construct(in->fd(), Core::Notifier::Type::Read))
Endpoint::Endpoint(NonnullOwnPtr<Core::File> in, NonnullOwnPtr<Core::File> out)
: m_in_fd(in->fd())
, m_in(Core::BufferedFile::create(move(in)).release_value_but_fixme_should_propagate_errors())
, m_out(move(out))
{
set_in_notifier();
}
@ -25,7 +24,7 @@ void Endpoint::send_command(Command const& command)
{
auto command_string = command.to_string().release_value_but_fixme_should_propagate_errors();
dbgln_if(UCI_DEBUG, "{} Sent UCI Command: {}", class_name(), command_string);
m_out->write(command_string);
m_out->write_until_depleted(command_string.bytes()).release_value_but_fixme_should_propagate_errors();
}
void Endpoint::event(Core::Event& event)
@ -73,16 +72,17 @@ void Endpoint::custom_event(Core::CustomEvent& custom_event)
void Endpoint::set_in_notifier()
{
m_in_notifier = Core::Notifier::construct(m_in->fd(), Core::Notifier::Type::Read);
m_in_notifier = Core::Notifier::construct(m_in_fd.value(), Core::Notifier::Type::Read);
m_in_notifier->on_activation = [this] {
if (!m_in->can_read_line()) {
if (!m_in->can_read_line().release_value_but_fixme_should_propagate_errors()) {
Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(EndpointEventType::UnexpectedEof));
m_in_notifier->set_enabled(false);
return;
}
auto buffer = ByteBuffer::create_zeroed(4096).release_value_but_fixme_should_propagate_errors();
while (m_in->can_read_line()) {
auto line = m_in->read_line(4096).trim_whitespace();
while (m_in->can_read_line().release_value_but_fixme_should_propagate_errors()) {
auto line = m_in->read_line(buffer).release_value_but_fixme_should_propagate_errors().trim_whitespace();
if (line.is_empty())
continue;

View file

@ -8,7 +8,7 @@
#pragma once
#include <LibChess/UCICommand.h>
#include <LibCore/IODevice.h>
#include <LibCore/File.h>
#include <LibCore/Notifier.h>
#include <LibCore/Object.h>
@ -41,19 +41,17 @@ public:
virtual void event(Core::Event&) override;
Core::IODevice& in() { return *m_in; }
Core::IODevice& out() { return *m_out; }
void set_in(RefPtr<Core::IODevice> in)
void set_in(NonnullOwnPtr<Core::File> in)
{
m_in = in;
m_in_fd = in->fd();
m_in = Core::BufferedFile::create(move(in)).release_value_but_fixme_should_propagate_errors();
set_in_notifier();
}
void set_out(RefPtr<Core::IODevice> out) { m_out = out; }
void set_out(NonnullOwnPtr<Core::File> out) { m_out = move(out); }
protected:
Endpoint() = default;
Endpoint(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out);
Endpoint(NonnullOwnPtr<Core::File> in, NonnullOwnPtr<Core::File> out);
virtual void custom_event(Core::CustomEvent&) override;
private:
@ -63,8 +61,9 @@ private:
void set_in_notifier();
ErrorOr<NonnullOwnPtr<Command>> read_command(StringView line) const;
RefPtr<Core::IODevice> m_in;
RefPtr<Core::IODevice> m_out;
Optional<int> m_in_fd {};
OwnPtr<Core::BufferedFile> m_in;
OwnPtr<Core::File> m_out;
RefPtr<Core::Notifier> m_in_notifier;
};