Просмотр исходного кода

Chess+ChessEngine: Fix stockfish by setting correct blocking flag

Stockfish apparently cannot handle non-blocking I/O, and it does not
make sense to assume that all chess engines can do so.

Fixes #18946.
Ben Wiederhake 2 лет назад
Родитель
Сommit
bdeccf8844
2 измененных файлов с 10 добавлено и 3 удалено
  1. 5 2
      Userland/Games/Chess/Engine.cpp
  2. 5 1
      Userland/Services/ChessEngine/main.cpp

+ 5 - 2
Userland/Games/Chess/Engine.cpp

@@ -7,6 +7,7 @@
 
 #include "Engine.h"
 #include <LibCore/File.h>
+#include <LibCore/System.h>
 #include <fcntl.h>
 #include <spawn.h>
 #include <stdio.h>
@@ -27,12 +28,12 @@ void Engine::connect_to_engine_service()
 {
     int wpipefds[2];
     int rpipefds[2];
-    if (pipe2(wpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
+    if (pipe2(wpipefds, O_CLOEXEC) < 0) {
         perror("pipe2");
         VERIFY_NOT_REACHED();
     }
 
-    if (pipe2(rpipefds, O_CLOEXEC | O_NONBLOCK) < 0) {
+    if (pipe2(rpipefds, O_CLOEXEC) < 0) {
         perror("pipe2");
         VERIFY_NOT_REACHED();
     }
@@ -55,9 +56,11 @@ void Engine::connect_to_engine_service()
     close(rpipefds[1]);
 
     auto infile = Core::File::adopt_fd(rpipefds[0], Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors();
+    infile->set_blocking(false).release_value_but_fixme_should_propagate_errors();
     set_in(move(infile)).release_value_but_fixme_should_propagate_errors();
 
     auto outfile = Core::File::adopt_fd(wpipefds[1], Core::File::OpenMode::Write).release_value_but_fixme_should_propagate_errors();
+    outfile->set_blocking(false).release_value_but_fixme_should_propagate_errors();
     set_out(move(outfile));
 
     send_command(Chess::UCI::UCICommand());

+ 5 - 1
Userland/Services/ChessEngine/main.cpp

@@ -16,7 +16,11 @@ ErrorOr<int> serenity_main(Main::Arguments)
     Core::EventLoop loop;
     TRY(Core::System::unveil(nullptr, nullptr));
 
-    auto engine = TRY(ChessEngine::try_create(TRY(Core::File::standard_input()), TRY(Core::File::standard_output())));
+    auto infile = TRY(Core::File::standard_input());
+    TRY(infile->set_blocking(false));
+    auto outfile = TRY(Core::File::standard_output());
+    TRY(outfile->set_blocking(false));
+    auto engine = TRY(ChessEngine::try_create(move(infile), move(outfile)));
     engine->on_quit = [&](auto status_code) {
         loop.quit(status_code);
     };