diff --git a/Userland/Games/Chess/Engine.cpp b/Userland/Games/Chess/Engine.cpp index 83825e7d6c..b70b83169d 100644 --- a/Userland/Games/Chess/Engine.cpp +++ b/Userland/Games/Chess/Engine.cpp @@ -6,7 +6,7 @@ */ #include "Engine.h" -#include +#include #include #include #include @@ -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; diff --git a/Userland/Libraries/LibChess/UCIEndpoint.cpp b/Userland/Libraries/LibChess/UCIEndpoint.cpp index a990810ba6..52ac7f74fa 100644 --- a/Userland/Libraries/LibChess/UCIEndpoint.cpp +++ b/Userland/Libraries/LibChess/UCIEndpoint.cpp @@ -8,15 +8,14 @@ #include "UCIEndpoint.h" #include #include -#include #include namespace Chess::UCI { -Endpoint::Endpoint(NonnullRefPtr in, NonnullRefPtr out) - : m_in(in) - , m_out(out) - , m_in_notifier(Core::Notifier::construct(in->fd(), Core::Notifier::Type::Read)) +Endpoint::Endpoint(NonnullOwnPtr in, NonnullOwnPtr 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(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; diff --git a/Userland/Libraries/LibChess/UCIEndpoint.h b/Userland/Libraries/LibChess/UCIEndpoint.h index 87f3e00a16..f23bf89385 100644 --- a/Userland/Libraries/LibChess/UCIEndpoint.h +++ b/Userland/Libraries/LibChess/UCIEndpoint.h @@ -8,7 +8,7 @@ #pragma once #include -#include +#include #include #include @@ -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 in) + void set_in(NonnullOwnPtr 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 out) { m_out = out; } + void set_out(NonnullOwnPtr out) { m_out = move(out); } protected: Endpoint() = default; - Endpoint(NonnullRefPtr in, NonnullRefPtr out); + Endpoint(NonnullOwnPtr in, NonnullOwnPtr out); virtual void custom_event(Core::CustomEvent&) override; private: @@ -63,8 +61,9 @@ private: void set_in_notifier(); ErrorOr> read_command(StringView line) const; - RefPtr m_in; - RefPtr m_out; + Optional m_in_fd {}; + OwnPtr m_in; + OwnPtr m_out; RefPtr m_in_notifier; }; diff --git a/Userland/Services/ChessEngine/ChessEngine.h b/Userland/Services/ChessEngine/ChessEngine.h index 5422878843..4a95d47c26 100644 --- a/Userland/Services/ChessEngine/ChessEngine.h +++ b/Userland/Services/ChessEngine/ChessEngine.h @@ -26,8 +26,8 @@ public: Function on_quit; private: - ChessEngine(NonnullRefPtr in, NonnullRefPtr out) - : Endpoint(in, out) + ChessEngine(NonnullOwnPtr in, NonnullOwnPtr out) + : Endpoint(move(in), move(out)) { on_command_read_error = [](auto command, auto error) { outln("{}: '{}'", error, command); diff --git a/Userland/Services/ChessEngine/main.cpp b/Userland/Services/ChessEngine/main.cpp index ddc7058ed0..81975d2feb 100644 --- a/Userland/Services/ChessEngine/main.cpp +++ b/Userland/Services/ChessEngine/main.cpp @@ -5,8 +5,8 @@ */ #include "ChessEngine.h" -#include #include +#include #include #include @@ -16,7 +16,7 @@ ErrorOr 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); };