UCIEndpoint.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. * Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibChess/UCICommand.h>
  9. #include <LibCore/EventReceiver.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/Notifier.h>
  12. namespace Chess::UCI {
  13. class Endpoint : public Core::EventReceiver {
  14. C_OBJECT(Endpoint)
  15. public:
  16. virtual ~Endpoint() override = default;
  17. Function<void(ByteString, Error)> on_command_read_error;
  18. virtual void handle_uci() { }
  19. virtual void handle_debug(DebugCommand const&) { }
  20. virtual void handle_isready() { }
  21. virtual void handle_setoption(SetOptionCommand const&) { }
  22. virtual void handle_position(PositionCommand const&) { }
  23. virtual void handle_go(GoCommand const&) { }
  24. virtual void handle_stop() { }
  25. virtual void handle_id(IdCommand const&) { }
  26. virtual void handle_uciok() { }
  27. virtual void handle_readyok() { }
  28. virtual void handle_bestmove(BestMoveCommand const&) { }
  29. virtual void handle_info(InfoCommand const&) { }
  30. virtual void handle_quit() { }
  31. virtual void handle_ucinewgame() { }
  32. virtual void handle_unexpected_eof() { }
  33. void send_command(Command const&);
  34. virtual void event(Core::Event&) override;
  35. ErrorOr<void> set_in(NonnullOwnPtr<Core::File> in)
  36. {
  37. m_in_fd = in->fd();
  38. m_in = TRY(Core::InputBufferedFile::create(move(in)));
  39. set_in_notifier();
  40. return {};
  41. }
  42. void set_out(NonnullOwnPtr<Core::File> out) { m_out = move(out); }
  43. protected:
  44. Endpoint() = default;
  45. virtual void custom_event(Core::CustomEvent&) override;
  46. private:
  47. enum EndpointEventType {
  48. UnexpectedEof
  49. };
  50. void set_in_notifier();
  51. ErrorOr<NonnullOwnPtr<Command>> read_command(StringView line) const;
  52. Optional<int> m_in_fd {};
  53. OwnPtr<Core::InputBufferedFile> m_in;
  54. OwnPtr<Core::File> m_out;
  55. RefPtr<Core::Notifier> m_in_notifier;
  56. };
  57. }