mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Chess: Avoid IODevice and DeprecatedFile
This commit is contained in:
parent
715f4a8d7e
commit
0fe29a48ad
Notes:
sideshowbarker
2024-07-17 08:43:11 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/0fe29a48ad Pull-request: https://github.com/SerenityOS/serenity/pull/18691 Reviewed-by: https://github.com/gmta ✅
5 changed files with 30 additions and 33 deletions
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
#include "Engine.h"
|
||||
#include <LibCore/DeprecatedFile.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <fcntl.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
|
@ -27,12 +27,12 @@ void Engine::connect_to_engine_service()
|
|||
{
|
||||
int wpipefds[2];
|
||||
int rpipefds[2];
|
||||
if (pipe2(wpipefds, O_CLOEXEC) < 0) {
|
||||
if (pipe2(wpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
|
||||
perror("pipe2");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
if (pipe2(rpipefds, O_CLOEXEC) < 0) {
|
||||
if (pipe2(rpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
|
||||
perror("pipe2");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -54,13 +54,11 @@ void Engine::connect_to_engine_service()
|
|||
close(wpipefds[0]);
|
||||
close(rpipefds[1]);
|
||||
|
||||
auto infile = Core::DeprecatedFile::construct();
|
||||
infile->open(rpipefds[0], Core::OpenMode::ReadOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes);
|
||||
set_in(infile);
|
||||
auto infile = Core::File::adopt_fd(rpipefds[0], Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors();
|
||||
set_in(move(infile));
|
||||
|
||||
auto outfile = Core::DeprecatedFile::construct();
|
||||
outfile->open(wpipefds[1], Core::OpenMode::WriteOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes);
|
||||
set_out(outfile);
|
||||
auto outfile = Core::File::adopt_fd(wpipefds[1], Core::File::OpenMode::Write).release_value_but_fixme_should_propagate_errors();
|
||||
set_out(move(outfile));
|
||||
|
||||
send_command(Chess::UCI::UCICommand());
|
||||
m_connected = true;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -26,8 +26,8 @@ public:
|
|||
Function<void(int)> on_quit;
|
||||
|
||||
private:
|
||||
ChessEngine(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out)
|
||||
: Endpoint(in, out)
|
||||
ChessEngine(NonnullOwnPtr<Core::File> in, NonnullOwnPtr<Core::File> out)
|
||||
: Endpoint(move(in), move(out))
|
||||
{
|
||||
on_command_read_error = [](auto command, auto error) {
|
||||
outln("{}: '{}'", error, command);
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
*/
|
||||
|
||||
#include "ChessEngine.h"
|
||||
#include <LibCore/DeprecatedFile.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
|
@ -16,7 +16,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
Core::EventLoop loop;
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
auto engine = TRY(ChessEngine::try_create(Core::DeprecatedFile::standard_input(), Core::DeprecatedFile::standard_output()));
|
||||
auto engine = TRY(ChessEngine::try_create(TRY(Core::File::standard_input()), TRY(Core::File::standard_output())));
|
||||
engine->on_quit = [&](auto status_code) {
|
||||
loop.quit(status_code);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue