2020-08-19 14:59:31 -06:00
|
|
|
/*
|
2022-02-26 09:06:06 -07:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2023-04-30 17:05:11 +01:00
|
|
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
2020-08-19 14:59:31 -06:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-19 14:59:31 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibChess/UCICommand.h>
|
2023-08-06 18:09:39 +02:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2023-05-07 00:53:19 +02:00
|
|
|
#include <LibCore/File.h>
|
2020-08-19 14:59:31 -06:00
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
|
|
|
|
|
|
namespace Chess::UCI {
|
|
|
|
|
|
2023-08-06 18:09:39 +02:00
|
|
|
class Endpoint : public Core::EventReceiver {
|
2020-08-19 14:59:31 -06:00
|
|
|
C_OBJECT(Endpoint)
|
|
|
|
|
public:
|
2022-02-26 09:06:06 -07:00
|
|
|
virtual ~Endpoint() override = default;
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Function<void(ByteString, Error)> on_command_read_error;
|
2023-04-30 17:05:11 +01:00
|
|
|
|
2020-08-19 14:59:31 -06:00
|
|
|
virtual void handle_uci() { }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void handle_debug(DebugCommand const&) { }
|
2020-08-19 14:59:31 -06:00
|
|
|
virtual void handle_isready() { }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void handle_setoption(SetOptionCommand const&) { }
|
|
|
|
|
virtual void handle_position(PositionCommand const&) { }
|
|
|
|
|
virtual void handle_go(GoCommand const&) { }
|
2020-08-19 14:59:31 -06:00
|
|
|
virtual void handle_stop() { }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void handle_id(IdCommand const&) { }
|
2020-08-19 14:59:31 -06:00
|
|
|
virtual void handle_uciok() { }
|
|
|
|
|
virtual void handle_readyok() { }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void handle_bestmove(BestMoveCommand const&) { }
|
|
|
|
|
virtual void handle_info(InfoCommand const&) { }
|
2023-04-01 23:15:53 +01:00
|
|
|
virtual void handle_quit() { }
|
2023-04-26 19:00:48 +01:00
|
|
|
virtual void handle_ucinewgame() { }
|
2023-04-02 21:02:17 +01:00
|
|
|
virtual void handle_unexpected_eof() { }
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void send_command(Command const&);
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2021-10-31 23:32:56 +01:00
|
|
|
virtual void event(Core::Event&) override;
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2023-05-07 01:59:10 +02:00
|
|
|
ErrorOr<void> set_in(NonnullOwnPtr<Core::File> in)
|
2020-08-19 14:59:31 -06:00
|
|
|
{
|
2023-05-07 00:53:19 +02:00
|
|
|
m_in_fd = in->fd();
|
2023-05-03 18:45:18 -04:00
|
|
|
m_in = TRY(Core::InputBufferedFile::create(move(in)));
|
2020-08-19 14:59:31 -06:00
|
|
|
set_in_notifier();
|
2023-05-07 01:59:10 +02:00
|
|
|
return {};
|
2020-08-19 14:59:31 -06:00
|
|
|
}
|
2023-05-07 00:53:19 +02:00
|
|
|
void set_out(NonnullOwnPtr<Core::File> out) { m_out = move(out); }
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2021-10-31 23:38:04 +01:00
|
|
|
protected:
|
2022-02-26 09:06:06 -07:00
|
|
|
Endpoint() = default;
|
2023-04-02 21:02:17 +01:00
|
|
|
virtual void custom_event(Core::CustomEvent&) override;
|
2021-10-31 23:38:04 +01:00
|
|
|
|
2020-08-19 14:59:31 -06:00
|
|
|
private:
|
2023-04-02 21:02:17 +01:00
|
|
|
enum EndpointEventType {
|
|
|
|
|
UnexpectedEof
|
|
|
|
|
};
|
2020-08-19 14:59:31 -06:00
|
|
|
void set_in_notifier();
|
2023-04-30 17:05:11 +01:00
|
|
|
ErrorOr<NonnullOwnPtr<Command>> read_command(StringView line) const;
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2023-05-07 00:53:19 +02:00
|
|
|
Optional<int> m_in_fd {};
|
2023-05-03 18:45:18 -04:00
|
|
|
OwnPtr<Core::InputBufferedFile> m_in;
|
2023-05-07 00:53:19 +02:00
|
|
|
OwnPtr<Core::File> m_out;
|
2020-08-19 14:59:31 -06:00
|
|
|
RefPtr<Core::Notifier> m_in_notifier;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|