Переглянути джерело

Chess: Avoid IODevice and DeprecatedFile

Ben Wiederhake 2 роки тому
батько
коміт
0fe29a48ad

+ 7 - 9
Userland/Games/Chess/Engine.cpp

@@ -6,7 +6,7 @@
  */
  */
 
 
 #include "Engine.h"
 #include "Engine.h"
-#include <LibCore/DeprecatedFile.h>
+#include <LibCore/File.h>
 #include <fcntl.h>
 #include <fcntl.h>
 #include <spawn.h>
 #include <spawn.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -27,12 +27,12 @@ void Engine::connect_to_engine_service()
 {
 {
     int wpipefds[2];
     int wpipefds[2];
     int rpipefds[2];
     int rpipefds[2];
-    if (pipe2(wpipefds, O_CLOEXEC) < 0) {
+    if (pipe2(wpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
         perror("pipe2");
         perror("pipe2");
         VERIFY_NOT_REACHED();
         VERIFY_NOT_REACHED();
     }
     }
 
 
-    if (pipe2(rpipefds, O_CLOEXEC) < 0) {
+    if (pipe2(rpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
         perror("pipe2");
         perror("pipe2");
         VERIFY_NOT_REACHED();
         VERIFY_NOT_REACHED();
     }
     }
@@ -54,13 +54,11 @@ void Engine::connect_to_engine_service()
     close(wpipefds[0]);
     close(wpipefds[0]);
     close(rpipefds[1]);
     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());
     send_command(Chess::UCI::UCICommand());
     m_connected = true;
     m_connected = true;

+ 10 - 10
Userland/Libraries/LibChess/UCIEndpoint.cpp

@@ -8,15 +8,14 @@
 #include "UCIEndpoint.h"
 #include "UCIEndpoint.h"
 #include <AK/ByteBuffer.h>
 #include <AK/ByteBuffer.h>
 #include <AK/Debug.h>
 #include <AK/Debug.h>
-#include <AK/DeprecatedString.h>
 #include <LibCore/EventLoop.h>
 #include <LibCore/EventLoop.h>
 
 
 namespace Chess::UCI {
 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();
     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();
     auto command_string = command.to_string().release_value_but_fixme_should_propagate_errors();
     dbgln_if(UCI_DEBUG, "{} Sent UCI Command: {}", class_name(), command_string);
     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)
 void Endpoint::event(Core::Event& event)
@@ -73,16 +72,17 @@ void Endpoint::custom_event(Core::CustomEvent& custom_event)
 
 
 void Endpoint::set_in_notifier()
 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] {
     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));
             Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(EndpointEventType::UnexpectedEof));
             m_in_notifier->set_enabled(false);
             m_in_notifier->set_enabled(false);
             return;
             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())
             if (line.is_empty())
                 continue;
                 continue;
 
 

+ 9 - 10
Userland/Libraries/LibChess/UCIEndpoint.h

@@ -8,7 +8,7 @@
 #pragma once
 #pragma once
 
 
 #include <LibChess/UCICommand.h>
 #include <LibChess/UCICommand.h>
-#include <LibCore/IODevice.h>
+#include <LibCore/File.h>
 #include <LibCore/Notifier.h>
 #include <LibCore/Notifier.h>
 #include <LibCore/Object.h>
 #include <LibCore/Object.h>
 
 
@@ -41,19 +41,17 @@ public:
 
 
     virtual void event(Core::Event&) override;
     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();
         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:
 protected:
     Endpoint() = default;
     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;
     virtual void custom_event(Core::CustomEvent&) override;
 
 
 private:
 private:
@@ -63,8 +61,9 @@ private:
     void set_in_notifier();
     void set_in_notifier();
     ErrorOr<NonnullOwnPtr<Command>> read_command(StringView line) const;
     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;
     RefPtr<Core::Notifier> m_in_notifier;
 };
 };
 
 

+ 2 - 2
Userland/Services/ChessEngine/ChessEngine.h

@@ -26,8 +26,8 @@ public:
     Function<void(int)> on_quit;
     Function<void(int)> on_quit;
 
 
 private:
 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) {
         on_command_read_error = [](auto command, auto error) {
             outln("{}: '{}'", error, command);
             outln("{}: '{}'", error, command);

+ 2 - 2
Userland/Services/ChessEngine/main.cpp

@@ -5,8 +5,8 @@
  */
  */
 
 
 #include "ChessEngine.h"
 #include "ChessEngine.h"
-#include <LibCore/DeprecatedFile.h>
 #include <LibCore/EventLoop.h>
 #include <LibCore/EventLoop.h>
+#include <LibCore/File.h>
 #include <LibCore/System.h>
 #include <LibCore/System.h>
 #include <LibMain/Main.h>
 #include <LibMain/Main.h>
 
 
@@ -16,7 +16,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
     Core::EventLoop loop;
     Core::EventLoop loop;
     TRY(Core::System::unveil(nullptr, nullptr));
     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) {
     engine->on_quit = [&](auto status_code) {
         loop.quit(status_code);
         loop.quit(status_code);
     };
     };