Ensure we return 0 on exit from graceful disconnection

Fixes #1042
This commit is contained in:
Maxime Coste 2016-12-20 10:34:48 +00:00
parent 4b696836d1
commit b7a0aa7546
3 changed files with 18 additions and 15 deletions

View File

@ -464,10 +464,10 @@ int run_client(StringView session, StringView init_cmds, UIType ui_type)
while (true) while (true)
event_manager.handle_next_events(EventMode::Normal); event_manager.handle_next_events(EventMode::Normal);
} }
catch (remote_error& e) catch (disconnected& e)
{ {
write_stderr(format("{}\ndisconnecting\n", e.what())); write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1; return e.m_graceful ? 0 : -1;
} }
return 0; return 0;
} }
@ -702,10 +702,10 @@ int run_pipe(StringView session)
{ {
send_command(session, command); send_command(session, command);
} }
catch (remote_error& e) catch (disconnected& e)
{ {
write_stderr(format("{}\ndisconnecting\n", e.what())); write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1; return e.m_graceful ? 0 : -1;
} }
return 0; return 0;
} }

View File

@ -146,7 +146,7 @@ public:
if (m_write_pos == header_size) if (m_write_pos == header_size)
{ {
if (size() < header_size) if (size() < header_size)
throw remote_error{"invalid message received"}; throw disconnected{"invalid message received"};
m_stream.resize(size()); m_stream.resize(size());
} }
} }
@ -174,7 +174,7 @@ public:
void read(char* buffer, size_t size) void read(char* buffer, size_t size)
{ {
if (m_read_pos + size > m_stream.size()) if (m_read_pos + size > m_stream.size())
throw remote_error{"tried to read after message end"}; throw disconnected{"tried to read after message end"};
memcpy(buffer, m_stream.data() + m_read_pos, size); memcpy(buffer, m_stream.data() + m_read_pos, size);
m_read_pos += size; m_read_pos += size;
} }
@ -232,8 +232,8 @@ private:
kak_assert(m_write_pos + size <= m_stream.size()); kak_assert(m_write_pos + size <= m_stream.size());
int res = ::read(sock, m_stream.data() + m_write_pos, size); int res = ::read(sock, m_stream.data() + m_write_pos, size);
if (res <= 0) if (res <= 0)
throw remote_error{res ? "peer disconnected" throw disconnected{res ? format("socket read failed: {}", strerror(errno))
: format("socket read failed: {}", strerror(errno))}; : "peer disconnected", res == 0};
m_write_pos += res; m_write_pos += res;
} }
@ -346,8 +346,8 @@ static bool send_data(int fd, RemoteBuffer& buffer)
{ {
int res = ::write(fd, buffer.data(), buffer.size()); int res = ::write(fd, buffer.data(), buffer.size());
if (res <= 0) if (res <= 0)
throw remote_error{res ? "peer disconnected" throw disconnected{res ? format("socket write failed: {}", strerror(errno))
: format("socket write failed: {}", strerror(errno))}; : "peer disconnected", res == 0};
buffer.erase(buffer.begin(), buffer.begin() + res); buffer.erase(buffer.begin(), buffer.begin() + res);
} }
return buffer.empty(); return buffer.empty();
@ -382,7 +382,7 @@ RemoteUI::RemoteUI(int socket, DisplayCoord dimensions)
m_on_key(key); m_on_key(key);
} }
} }
catch (const remote_error& err) catch (const disconnected& err)
{ {
write_to_debug_buffer(format("Error while transfering remote messages: {}", err.what())); write_to_debug_buffer(format("Error while transfering remote messages: {}", err.what()));
ClientManager::instance().remove_client(*m_client, false); ClientManager::instance().remove_client(*m_client, false);
@ -507,7 +507,7 @@ static int connect_to(StringView session)
fcntl(sock, F_SETFD, FD_CLOEXEC); fcntl(sock, F_SETFD, FD_CLOEXEC);
sockaddr_un addr = session_addr(session); sockaddr_un addr = session_addr(session);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1) if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw remote_error(format("connect to {} failed", addr.sun_path)); throw disconnected(format("connect to {} failed", addr.sun_path));
return sock; return sock;
} }
@ -695,7 +695,7 @@ private:
Server::instance().remove_accepter(this); Server::instance().remove_accepter(this);
} }
} }
catch (const remote_error& err) catch (const disconnected& err)
{ {
write_to_debug_buffer(format("accepting connection failed: {}", err.what())); write_to_debug_buffer(format("accepting connection failed: {}", err.what()));
close(sock); close(sock);

View File

@ -11,9 +11,12 @@
namespace Kakoune namespace Kakoune
{ {
struct remote_error : runtime_error struct disconnected : runtime_error
{ {
using runtime_error::runtime_error; disconnected(String what, bool graceful = false)
: runtime_error{std::move(what)}, m_graceful{graceful} {}
const bool m_graceful;
}; };
class FDWatcher; class FDWatcher;