RemoteClient owns the FDWatcher of it's socket

This commit is contained in:
Maxime Coste 2013-01-11 18:44:02 +01:00
parent 914ede7a82
commit d2f0e2de66
3 changed files with 17 additions and 26 deletions

View File

@ -703,17 +703,6 @@ RemoteClient* connect_to(const String& pid, const String& init_command)
NCursesUI* ui = new NCursesUI{}; NCursesUI* ui = new NCursesUI{};
RemoteClient* remote_client = new RemoteClient{sock, ui, init_command}; RemoteClient* remote_client = new RemoteClient{sock, ui, init_command};
new FDWatcher{sock, [=](FDWatcher&) {
try
{
remote_client->process_next_message();
}
catch (Kakoune::runtime_error& error)
{
ui->print_status(error.description(), -1);
}
}};
new FDWatcher{0, [=](FDWatcher& ev) { new FDWatcher{0, [=](FDWatcher& ev) {
try try
{ {

View File

@ -294,7 +294,8 @@ DisplayCoord RemoteUI::dimensions()
RemoteClient::RemoteClient(int socket, UserInterface* ui, RemoteClient::RemoteClient(int socket, UserInterface* ui,
const String& init_command) const String& init_command)
: m_socket(socket), m_ui(ui), m_dimensions(ui->dimensions()) : m_ui(ui), m_dimensions(ui->dimensions()),
m_socket_watcher{socket, [this](FDWatcher&){ process_next_message(); }}
{ {
Message msg(socket); Message msg(socket);
msg.write(init_command); msg.write(init_command);
@ -304,35 +305,36 @@ RemoteClient::RemoteClient(int socket, UserInterface* ui,
void RemoteClient::process_next_message() void RemoteClient::process_next_message()
{ {
RemoteUIMsg msg = read<RemoteUIMsg>(m_socket); int socket = m_socket_watcher.fd();
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
switch (msg) switch (msg)
{ {
case RemoteUIMsg::PrintStatus: case RemoteUIMsg::PrintStatus:
{ {
auto status = read<String>(m_socket); auto status = read<String>(socket);
auto cursor_pos = read<CharCount>(m_socket); auto cursor_pos = read<CharCount>(socket);
m_ui->print_status(status, cursor_pos); m_ui->print_status(status, cursor_pos);
break; break;
} }
case RemoteUIMsg::MenuShow: case RemoteUIMsg::MenuShow:
{ {
auto choices = read_vector<String>(m_socket); auto choices = read_vector<String>(socket);
auto anchor = read<DisplayCoord>(m_socket); auto anchor = read<DisplayCoord>(socket);
auto style = read<MenuStyle>(m_socket); auto style = read<MenuStyle>(socket);
m_ui->menu_show(choices, anchor, style); m_ui->menu_show(choices, anchor, style);
break; break;
} }
case RemoteUIMsg::MenuSelect: case RemoteUIMsg::MenuSelect:
m_ui->menu_select(read<int>(m_socket)); m_ui->menu_select(read<int>(socket));
break; break;
case RemoteUIMsg::MenuHide: case RemoteUIMsg::MenuHide:
m_ui->menu_hide(); m_ui->menu_hide();
break; break;
case RemoteUIMsg::InfoShow: case RemoteUIMsg::InfoShow:
{ {
auto choices = read<String>(m_socket); auto choices = read<String>(socket);
auto anchor = read<DisplayCoord>(m_socket); auto anchor = read<DisplayCoord>(socket);
auto style = read<MenuStyle>(m_socket); auto style = read<MenuStyle>(socket);
m_ui->info_show(choices, anchor, style); m_ui->info_show(choices, anchor, style);
break; break;
} }
@ -341,8 +343,8 @@ void RemoteClient::process_next_message()
break; break;
case RemoteUIMsg::Draw: case RemoteUIMsg::Draw:
{ {
DisplayBuffer display_buffer = read<DisplayBuffer>(m_socket); DisplayBuffer display_buffer = read<DisplayBuffer>(socket);
String mode_line = read<String>(m_socket); String mode_line = read<String>(socket);
m_ui->draw(display_buffer, mode_line); m_ui->draw(display_buffer, mode_line);
break; break;
} }
@ -351,7 +353,7 @@ void RemoteClient::process_next_message()
void RemoteClient::write_next_key() void RemoteClient::write_next_key()
{ {
Message msg(m_socket); Message msg(m_socket_watcher.fd());
// do that before checking dimensions as get_key may // do that before checking dimensions as get_key may
// handle a resize event. // handle a resize event.
msg.write(m_ui->get_key()); msg.write(m_ui->get_key());

View File

@ -22,9 +22,9 @@ public:
void write_next_key(); void write_next_key();
private: private:
int m_socket;
std::unique_ptr<UserInterface> m_ui; std::unique_ptr<UserInterface> m_ui;
DisplayCoord m_dimensions; DisplayCoord m_dimensions;
FDWatcher m_socket_watcher;
}; };
} }