Small remote code cleanup
This commit is contained in:
parent
db86c6cbc9
commit
0dff8a7d3f
13
src/main.cc
13
src/main.cc
|
@ -465,14 +465,9 @@ int run_client(StringView session, StringView init_command, UIType ui_type)
|
||||||
while (true)
|
while (true)
|
||||||
event_manager.handle_next_events(EventMode::Normal);
|
event_manager.handle_next_events(EventMode::Normal);
|
||||||
}
|
}
|
||||||
catch (remote_error&)
|
catch (remote_error& e)
|
||||||
{
|
{
|
||||||
write_stderr("disconnected from server\n");
|
write_stderr(format("{}\ndisconnecting\n", e.what()));
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
catch (connection_failed& e)
|
|
||||||
{
|
|
||||||
write_stderr(format("{}\n", e.what()));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -713,9 +708,9 @@ int run_pipe(StringView session)
|
||||||
{
|
{
|
||||||
send_command(session, command);
|
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 -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -49,10 +49,18 @@ public:
|
||||||
|
|
||||||
~MsgWriter() noexcept(false)
|
~MsgWriter() noexcept(false)
|
||||||
{
|
{
|
||||||
*reinterpret_cast<uint32_t*>(m_stream.data()+1) = (uint32_t)m_stream.size();
|
uint32_t count = (uint32_t)m_stream.size();
|
||||||
int res = ::write(m_socket, m_stream.data(), m_stream.size());
|
char* data = m_stream.data();
|
||||||
if (res == 0)
|
*reinterpret_cast<uint32_t*>(data+1) = count;
|
||||||
throw remote_error{"peer disconnected"};
|
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)
|
void write(const char* val, size_t size)
|
||||||
|
@ -226,10 +234,9 @@ private:
|
||||||
void read_from_socket(int sock, size_t size)
|
void read_from_socket(int sock, size_t 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{"peer disconnected"};
|
throw remote_error{res ? "peer disconnected"
|
||||||
if (res < 0)
|
: format("socket read failed: {}", strerror(errno))};
|
||||||
throw remote_error{format("socket error, read returned {}", res)};
|
|
||||||
m_write_pos += res;
|
m_write_pos += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,6 +245,7 @@ private:
|
||||||
uint32_t m_write_pos = 0;
|
uint32_t m_write_pos = 0;
|
||||||
uint32_t m_read_pos = header_size;
|
uint32_t m_read_pos = header_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
String MsgReader::read<String>()
|
String MsgReader::read<String>()
|
||||||
{
|
{
|
||||||
|
@ -493,7 +501,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 connection_failed(addr.sun_path);
|
throw remote_error(format("connect to {} failed", addr.sun_path));
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
class FDWatcher;
|
||||||
|
|
||||||
// A remote client handle communication between a client running on the server
|
// A remote client handle communication between a client running on the server
|
||||||
|
|
Loading…
Reference in New Issue
Block a user