Skip to content

Commit

Permalink
Handle communication errors #12
Browse files Browse the repository at this point in the history
  • Loading branch information
sakhnik committed Oct 16, 2021
1 parent 7ebcf3f commit cba2dbd
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/IWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ struct IWindow

virtual void Present() = 0;
virtual void SessionEnd() = 0;
virtual void SetError(const char *) = 0;
};
15 changes: 12 additions & 3 deletions src/MsgPackRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <iostream>


MsgPackRpc::MsgPackRpc(uv_stream_t *stdin_stream, uv_stream_t *stdout_stream)
MsgPackRpc::MsgPackRpc(uv_stream_t *stdin_stream, uv_stream_t *stdout_stream,
OnErrorT on_error)
: _stdin_stream{stdin_stream}
, _stdout_stream{stdout_stream}
, _on_error{on_error}
{
_stdout_stream->data = this;

Expand All @@ -17,10 +19,13 @@ MsgPackRpc::MsgPackRpc(uv_stream_t *stdin_stream, uv_stream_t *stdout_stream)
};

auto read_astream = [](uv_stream_t* stream, ssize_t nread, const uv_buf_t *buf) {
MsgPackRpc *self = reinterpret_cast<MsgPackRpc*>(stream->data);
if (nread > 0)
{
MsgPackRpc *self = reinterpret_cast<MsgPackRpc*>(stream->data);
self->_handle_data(buf->base, nread);
else
{
Logger().error("Failed to read: {}", uv_strerror(nread));
self->_on_error(uv_strerror(nread));
}
};

Expand Down Expand Up @@ -52,7 +57,11 @@ void MsgPackRpc::Request(PackRequestT pack_request, OnResponseT on_response)
auto cb = [](uv_write_t* req, int status) {
Write *w = reinterpret_cast<Write *>(req);
if (status < 0)
{
Logger().error("Failed to write {} bytes: {}", w->buffer.size(), uv_strerror(status));
MsgPackRpc *self = reinterpret_cast<MsgPackRpc *>(req->data);
self->_on_error(uv_strerror(status));
}
delete w;
};

Expand Down
5 changes: 4 additions & 1 deletion src/MsgPackRpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
class MsgPackRpc
{
public:
MsgPackRpc(uv_stream_t *, uv_stream_t *);
using OnErrorT = std::function<void(const char *)>;

MsgPackRpc(uv_stream_t *, uv_stream_t *, OnErrorT);
~MsgPackRpc();

using OnNotificationT = std::function<void(std::string_view, msgpack::object)>;
Expand All @@ -31,6 +33,7 @@ class MsgPackRpc
private:
uv_stream_t *_stdin_stream;
uv_stream_t *_stdout_stream;
OnErrorT _on_error;
OnNotificationT _on_notification;

// Capture any non msgpack-rpc output, as it may be text output from --version or alike
Expand Down
18 changes: 17 additions & 1 deletion src/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

void Session::_Init(uv_stream_t *in, uv_stream_t *out)
{
_rpc.reset(new MsgPackRpc(in, out));
auto onError = [this](const char *error) { _OnError(error); };
_rpc.reset(new MsgPackRpc(in, out, onError));
_renderer.reset(new Renderer{uv_default_loop(), _rpc.get()});
_redraw_handler.reset(new RedrawHandler{_rpc.get(), _renderer.get()});

Expand Down Expand Up @@ -45,3 +46,18 @@ void Session::SetWindow(IWindow *window)
_window = window;
_renderer->SetWindow(window);
}

void Session::_OnError(const char *error)
{
_Exit();
if (_window && error)
_window->SetError(error);
}

void Session::_Exit()
{
_nvim_exited.store(true, std::memory_order_relaxed);
uv_stop(uv_default_loop());
if (_window)
_window->SessionEnd();
}
2 changes: 2 additions & 0 deletions src/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ class Session
IWindow *_window = nullptr;

void _Init(uv_stream_t *in, uv_stream_t *out);
void _OnError(const char *);
void _Exit();
};
7 changes: 2 additions & 5 deletions src/SessionSpawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ SessionSpawn::SessionSpawn(int argc, char *argv[])
//});

auto on_exit = [](uv_process_t *proc, int64_t exit_status, int signal) {
SessionSpawn *session = reinterpret_cast<SessionSpawn *>(proc->data);
Logger().info("Exit: status={} signal={}", exit_status, signal);
session->_nvim_exited.store(true, std::memory_order_relaxed);
uv_stop(uv_default_loop());
if (session->_window)
session->_window->SessionEnd();
SessionSpawn *session = reinterpret_cast<SessionSpawn *>(proc->data);
session->_Exit();
};

uv_process_options_t options{};
Expand Down
2 changes: 1 addition & 1 deletion src/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Window
void DrawCursor(cairo_t *, int row, int col, unsigned fg, std::string_view mode);
void SessionEnd() override;

void SetError(const char *error);
void SetError(const char *error) override;

private:
GtkApplication *_app;
Expand Down

0 comments on commit cba2dbd

Please sign in to comment.