Small remote code cleanup

This commit is contained in:
Maxime Coste 2016-11-29 19:12:56 +00:00
parent db86c6cbc9
commit 0dff8a7d3f
3 changed files with 21 additions and 25 deletions

View File

@ -465,14 +465,9 @@ int run_client(StringView session, StringView init_command, UIType ui_type)
while (true)
event_manager.handle_next_events(EventMode::Normal);
}
catch (remote_error&)
catch (remote_error& e)
{
write_stderr("disconnected from server\n");
return -1;
}
catch (connection_failed& e)
{
write_stderr(format("{}\n", e.what()));
write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1;
}
return 0;
@ -713,9 +708,9 @@ int run_pipe(StringView session)
{
send_command(session, command);
}
catch (connection_failed& e)
catch (remote_error& e)
{
write_stderr(format("{}\n", e.what()));
write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1;
}
return 0;

View File

@ -49,10 +49,18 @@ public:
~MsgWriter() noexcept(false)
{
*reinterpret_cast<uint32_t*>(m_stream.data()+1) = (uint32_t)m_stream.size();
int res = ::write(m_socket, m_stream.data(), m_stream.size());
if (res == 0)
throw remote_error{"peer disconnected"};
uint32_t count = (uint32_t)m_stream.size();
char* data = m_stream.data();
*reinterpret_cast<uint32_t*>(data+1) = count;
while (count > 0)
{
int res = ::write(m_socket, data, count);
if (res <= 0)
throw remote_error{res ? "peer disconnected"
: format("socket write failed: {}", strerror(errno))};
data += res;
count -= res;
}
}
void write(const char* val, size_t size)
@ -226,10 +234,9 @@ private:
void read_from_socket(int sock, size_t size)
{
int res = ::read(sock, m_stream.data() + m_write_pos, size);
if (res == 0)
throw remote_error{"peer disconnected"};
if (res < 0)
throw remote_error{format("socket error, read returned {}", res)};
if (res <= 0)
throw remote_error{res ? "peer disconnected"
: format("socket read failed: {}", strerror(errno))};
m_write_pos += res;
}
@ -238,6 +245,7 @@ private:
uint32_t m_write_pos = 0;
uint32_t m_read_pos = header_size;
};
template<>
String MsgReader::read<String>()
{
@ -493,7 +501,7 @@ static int connect_to(StringView session)
fcntl(sock, F_SETFD, FD_CLOEXEC);
sockaddr_un addr = session_addr(session);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw connection_failed(addr.sun_path);
throw remote_error(format("connect to {} failed", addr.sun_path));
return sock;
}

View File

@ -19,13 +19,6 @@ struct remote_error : runtime_error
{}
};
struct connection_failed : runtime_error
{
connection_failed(StringView filename)
: runtime_error{format("connect to {} failed", filename)}
{}
};
class FDWatcher;
// A remote client handle communication between a client running on the server