1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

Chess: Gracefully handle ChessEngine disconnections

The GUI now tracks when it becomes disconnected from ChessEngine.
If not currently waiting for a move from ChessEngine, it will
automatically reconnect on the next engine move. If a disconnection
occurs while waiting for a move, the player is asked whether they
want to try again or not.
This commit is contained in:
Tim Ledbetter 2023-04-02 21:14:16 +01:00 committed by Sam Atkins
parent 55347ed6a5
commit 8b6c538f2a
4 changed files with 63 additions and 20 deletions

View file

@ -17,6 +17,11 @@ Engine::~Engine()
}
Engine::Engine(StringView command)
: m_command(command)
{
}
void Engine::connect_to_engine_service()
{
int wpipefds[2];
int rpipefds[2];
@ -35,9 +40,9 @@ Engine::Engine(StringView command)
posix_spawn_file_actions_adddup2(&file_actions, wpipefds[0], STDIN_FILENO);
posix_spawn_file_actions_adddup2(&file_actions, rpipefds[1], STDOUT_FILENO);
DeprecatedString cstr(command);
char const* argv[] = { cstr.characters(), nullptr };
if (posix_spawnp(&m_pid, cstr.characters(), &file_actions, nullptr, const_cast<char**>(argv), environ) < 0) {
char const* argv[] = { m_command.characters(), nullptr };
pid_t pid = -1;
if (posix_spawnp(&pid, m_command.characters(), &file_actions, nullptr, const_cast<char**>(argv), environ) < 0) {
perror("posix_spawnp");
VERIFY_NOT_REACHED();
}
@ -56,6 +61,7 @@ Engine::Engine(StringView command)
set_out(outfile);
send_command(Chess::UCI::UCICommand());
m_connected = true;
}
void Engine::handle_bestmove(Chess::UCI::BestMoveCommand const& command)
@ -68,5 +74,21 @@ void Engine::handle_bestmove(Chess::UCI::BestMoveCommand const& command)
void Engine::quit()
{
if (!m_connected)
return;
send_command(Chess::UCI::QuitCommand());
m_connected = false;
}
void Engine::handle_unexpected_eof()
{
m_connected = false;
if (m_bestmove_callback)
m_bestmove_callback(Error::from_errno(EPIPE));
m_bestmove_callback = nullptr;
if (on_connection_lost)
on_connection_lost();
}