summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Ledbetter <timledbetter@gmail.com>2023-04-02 21:02:17 +0100
committerSam Atkins <atkinssj@gmail.com>2023-04-20 09:59:18 +0100
commit680d4e34d5daa0bbe7caa34b18a4ad18cfe55faf (patch)
treea8ac707799befe434408af2ff750af7531c7e39b
parentbf320e4826ece33b0190332dda3e135786b48c89 (diff)
downloadserenity-680d4e34d5daa0bbe7caa34b18a4ad18cfe55faf.zip
LibChess: Allow UCIEndpoint to handle unexpected disconnections
-rw-r--r--Userland/Libraries/LibChess/UCIEndpoint.cpp13
-rw-r--r--Userland/Libraries/LibChess/UCIEndpoint.h5
2 files changed, 18 insertions, 0 deletions
diff --git a/Userland/Libraries/LibChess/UCIEndpoint.cpp b/Userland/Libraries/LibChess/UCIEndpoint.cpp
index 7693d0e3df..865da51521 100644
--- a/Userland/Libraries/LibChess/UCIEndpoint.cpp
+++ b/Userland/Libraries/LibChess/UCIEndpoint.cpp
@@ -56,14 +56,27 @@ void Endpoint::event(Core::Event& event)
case Command::Type::Quit:
return handle_quit();
default:
+ Object::event(event);
break;
}
}
+void Endpoint::custom_event(Core::CustomEvent& custom_event)
+{
+ if (custom_event.custom_type() == EndpointEventType::UnexpectedEof)
+ handle_unexpected_eof();
+}
+
void Endpoint::set_in_notifier()
{
m_in_notifier = Core::Notifier::construct(m_in->fd(), Core::Notifier::Read);
m_in_notifier->on_ready_to_read = [this] {
+ if (!m_in->can_read_line()) {
+ Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(EndpointEventType::UnexpectedEof));
+ m_in_notifier->set_enabled(false);
+ return;
+ }
+
while (m_in->can_read_line())
Core::EventLoop::current().post_event(*this, read_command());
};
diff --git a/Userland/Libraries/LibChess/UCIEndpoint.h b/Userland/Libraries/LibChess/UCIEndpoint.h
index 00ae61ef39..18f4f4db34 100644
--- a/Userland/Libraries/LibChess/UCIEndpoint.h
+++ b/Userland/Libraries/LibChess/UCIEndpoint.h
@@ -31,6 +31,7 @@ public:
virtual void handle_bestmove(BestMoveCommand const&) { }
virtual void handle_info(InfoCommand const&) { }
virtual void handle_quit() { }
+ virtual void handle_unexpected_eof() { }
void send_command(Command const&);
@@ -49,8 +50,12 @@ public:
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();
NonnullOwnPtr<Command> read_command();