Simplify RemoteClient creation code

This commit is contained in:
Maxime Coste 2014-11-05 13:57:12 +00:00
parent a3b29b1f11
commit 80b50cd737
3 changed files with 33 additions and 46 deletions

View File

@ -299,10 +299,8 @@ int run_client(StringView session, StringView init_command)
try
{
EventManager event_manager;
auto client = connect_to(session,
std::unique_ptr<UserInterface>{new NCursesUI{}},
get_env_vars(),
init_command);
RemoteClient client{session, make_unique<NCursesUI>(),
get_env_vars(), init_command};
while (true)
event_manager.handle_next_events();
}

View File

@ -407,27 +407,39 @@ void RemoteUI::set_input_callback(InputCallback callback)
m_input_callback = std::move(callback);
}
RemoteClient::RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars,
StringView init_command)
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions()),
m_socket_watcher{socket, [this](FDWatcher&){ process_available_messages(); }}
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command)
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions())
{
Message msg(socket);
msg.write(init_command.data(), (int)init_command.length());
msg.write((char)0);
msg.write(env_vars);
auto filename = "/tmp/kak-" + session;
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) |
(int)m_dimensions.column) };
msg.write(key);
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
fcntl(sock, F_SETFD, FD_CLOEXEC);
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw connection_failed(filename);
{
Message msg(sock);
msg.write(init_command.data(), (int)init_command.length());
msg.write((char)0);
msg.write(env_vars);
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) |
(int)m_dimensions.column) };
msg.write(key);
}
m_ui->set_input_callback([this]{ write_next_key(); });
m_socket_watcher.reset(new FDWatcher{sock, [this](FDWatcher&){ process_available_messages(); }});
}
void RemoteClient::process_available_messages()
{
int socket = m_socket_watcher.fd();
int socket = m_socket_watcher->fd();
timeval tv{ 0, 0 };
fd_set rfds;
@ -441,7 +453,7 @@ void RemoteClient::process_available_messages()
void RemoteClient::process_next_message()
{
int socket = m_socket_watcher.fd();
int socket = m_socket_watcher->fd();
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
switch (msg)
{
@ -490,7 +502,7 @@ void RemoteClient::process_next_message()
void RemoteClient::write_next_key()
{
Message msg(m_socket_watcher.fd());
Message msg(m_socket_watcher->fd());
// do that before checking dimensions as get_key may
// handle a resize event.
msg.write(m_ui->get_key());
@ -505,26 +517,6 @@ void RemoteClient::write_next_key()
}
}
std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars,
StringView init_command)
{
auto filename = "/tmp/kak-" + session;
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
fcntl(sock, F_SETFD, FD_CLOEXEC);
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename.c_str(), sizeof(addr.sun_path) - 1);
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
throw connection_failed(filename);
return std::unique_ptr<RemoteClient>{new RemoteClient{sock, std::move(ui),
env_vars,
init_command}};
}
void send_command(StringView session, StringView command)
{
auto filename = "/tmp/kak-" + session;

View File

@ -2,7 +2,6 @@
#define remote_hh_INCLUDED
#include "display_buffer.hh"
#include "event_manager.hh"
#include "user_interface.hh"
#include "env_vars.hh"
@ -18,12 +17,14 @@ struct connection_failed : runtime_error
{}
};
class FDWatcher;
// A remote client handle communication between a client running on the server
// and a user interface running on the local process.
class RemoteClient
{
public:
RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command);
private:
@ -33,12 +34,8 @@ private:
std::unique_ptr<UserInterface> m_ui;
CharCoord m_dimensions;
FDWatcher m_socket_watcher;
std::unique_ptr<FDWatcher> m_socket_watcher;
};
std::unique_ptr<RemoteClient> connect_to(StringView session,
std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars,
StringView init_command);
void send_command(StringView session, StringView command);