Fix handling of remote errors in the accepter

This commit is contained in:
Maxime Coste 2016-09-05 13:47:56 +01:00
parent a6b55074d6
commit 21a76d135c

View File

@ -38,8 +38,6 @@ enum class MessageType : char
Key Key
}; };
struct socket_error{};
class MsgWriter class MsgWriter
{ {
public: public:
@ -231,7 +229,7 @@ private:
if (res == 0) if (res == 0)
throw remote_error{"peer disconnected"}; throw remote_error{"peer disconnected"};
if (res < 0) if (res < 0)
throw socket_error{}; throw remote_error{format("socket error, read returned {}", res)};
m_write_pos += res; m_write_pos += res;
} }
@ -359,7 +357,7 @@ RemoteUI::RemoteUI(int socket, CharCoord dimensions)
disconnect = true; disconnect = true;
} }
} }
catch (remote_error& err) catch (const remote_error& err)
{ {
write_to_debug_buffer(format("Error while reading remote message: {}", err.what())); write_to_debug_buffer(format("Error while reading remote message: {}", err.what()));
disconnect = true; disconnect = true;
@ -629,17 +627,19 @@ private:
void handle_available_input() void handle_available_input()
{ {
const int sock = m_socket_watcher.fd(); const int sock = m_socket_watcher.fd();
do try
{ {
m_reader.read_available(sock); do
} {
while (fd_readable(sock) and not m_reader.ready()); m_reader.read_available(sock);
}
while (fd_readable(sock) and not m_reader.ready());
if (not m_reader.ready()) if (not m_reader.ready())
return; return;
switch (m_reader.type()) switch (m_reader.type())
{ {
case MessageType::Connect: case MessageType::Connect:
{ {
auto init_command = m_reader.read<String>(); auto init_command = m_reader.read<String>();
@ -652,7 +652,7 @@ private:
ui->set_client(client); ui->set_client(client);
Server::instance().remove_accepter(this); Server::instance().remove_accepter(this);
return; break;
} }
case MessageType::Command: case MessageType::Command:
{ {
@ -662,20 +662,26 @@ private:
Context context{Context::EmptyContextFlag{}}; Context context{Context::EmptyContextFlag{}};
CommandManager::instance().execute(command, context); CommandManager::instance().execute(command, context);
} }
catch (runtime_error& e) catch (const runtime_error& e)
{ {
write_to_debug_buffer(format("error running command '{}': {}", write_to_debug_buffer(format("error running command '{}': {}",
command, e.what())); command, e.what()));
} }
close(sock); close(sock);
Server::instance().remove_accepter(this); Server::instance().remove_accepter(this);
return; break;
} }
default: default:
write_to_debug_buffer("Invalid introduction message received"); write_to_debug_buffer("Invalid introduction message received");
close(sock); close(sock);
Server::instance().remove_accepter(this); Server::instance().remove_accepter(this);
break; }
}
catch (const remote_error& err)
{
write_to_debug_buffer(format("accepting connection failed: {}", err.what()));
close(sock);
Server::instance().remove_accepter(this);
} }
} }