1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:18:12 +00:00
serenity/Userland/Libraries/LibChess/UCIEndpoint.h
Tim Ledbetter 56dde3df54 LibChess+ChessEngine: Don't crash on error when reading UCI commands
ChessEngine and the chess GUI will no longer crash on error while
reading UCI commands. ChessEngine will print a message to the standard
output, while the GUI will ignore unknown commands. Both will print
the error to the debug log if the UCI_DEBUG flag is enabled.

Trailing and preceding whitespace is now stripped from commands before
they are parsed. Commands which are just whitespace no longer produce
errors.
2023-05-03 08:31:34 +01:00

71 lines
2 KiB
C++

/*
* Copyright (c) 2020-2022, the SerenityOS developers.
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibChess/UCICommand.h>
#include <LibCore/IODevice.h>
#include <LibCore/Notifier.h>
#include <LibCore/Object.h>
namespace Chess::UCI {
class Endpoint : public Core::Object {
C_OBJECT(Endpoint)
public:
virtual ~Endpoint() override = default;
Function<void(DeprecatedString, Error)> on_command_read_error;
virtual void handle_uci() { }
virtual void handle_debug(DebugCommand const&) { }
virtual void handle_isready() { }
virtual void handle_setoption(SetOptionCommand const&) { }
virtual void handle_position(PositionCommand const&) { }
virtual void handle_go(GoCommand const&) { }
virtual void handle_stop() { }
virtual void handle_id(IdCommand const&) { }
virtual void handle_uciok() { }
virtual void handle_readyok() { }
virtual void handle_bestmove(BestMoveCommand const&) { }
virtual void handle_info(InfoCommand const&) { }
virtual void handle_quit() { }
virtual void handle_ucinewgame() { }
virtual void handle_unexpected_eof() { }
void send_command(Command const&);
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)
{
m_in = in;
set_in_notifier();
}
void set_out(RefPtr<Core::IODevice> out) { m_out = out; }
protected:
Endpoint() = default;
Endpoint(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out);
virtual void custom_event(Core::CustomEvent&) override;
private:
enum EndpointEventType {
UnexpectedEof
};
void set_in_notifier();
ErrorOr<NonnullOwnPtr<Command>> read_command(StringView line) const;
RefPtr<Core::IODevice> m_in;
RefPtr<Core::IODevice> m_out;
RefPtr<Core::Notifier> m_in_notifier;
};
}