UCIEndpoint.h 1.8 KB

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