Simplify RemoteClient creation code
This commit is contained in:
parent
a3b29b1f11
commit
80b50cd737
|
@ -299,10 +299,8 @@ int run_client(StringView session, StringView init_command)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
EventManager event_manager;
|
EventManager event_manager;
|
||||||
auto client = connect_to(session,
|
RemoteClient client{session, make_unique<NCursesUI>(),
|
||||||
std::unique_ptr<UserInterface>{new NCursesUI{}},
|
get_env_vars(), init_command};
|
||||||
get_env_vars(),
|
|
||||||
init_command);
|
|
||||||
while (true)
|
while (true)
|
||||||
event_manager.handle_next_events();
|
event_manager.handle_next_events();
|
||||||
}
|
}
|
||||||
|
|
|
@ -407,27 +407,39 @@ void RemoteUI::set_input_callback(InputCallback callback)
|
||||||
m_input_callback = std::move(callback);
|
m_input_callback = std::move(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
RemoteClient::RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
|
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
|
||||||
const EnvVarMap& env_vars,
|
const EnvVarMap& env_vars, StringView init_command)
|
||||||
StringView init_command)
|
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions())
|
||||||
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions()),
|
|
||||||
m_socket_watcher{socket, [this](FDWatcher&){ process_available_messages(); }}
|
|
||||||
{
|
{
|
||||||
Message msg(socket);
|
auto filename = "/tmp/kak-" + session;
|
||||||
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 sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
(int)m_dimensions.column) };
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
||||||
msg.write(key);
|
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_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()
|
void RemoteClient::process_available_messages()
|
||||||
{
|
{
|
||||||
int socket = m_socket_watcher.fd();
|
int socket = m_socket_watcher->fd();
|
||||||
timeval tv{ 0, 0 };
|
timeval tv{ 0, 0 };
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
|
|
||||||
|
@ -441,7 +453,7 @@ void RemoteClient::process_available_messages()
|
||||||
|
|
||||||
void RemoteClient::process_next_message()
|
void RemoteClient::process_next_message()
|
||||||
{
|
{
|
||||||
int socket = m_socket_watcher.fd();
|
int socket = m_socket_watcher->fd();
|
||||||
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
|
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
|
||||||
switch (msg)
|
switch (msg)
|
||||||
{
|
{
|
||||||
|
@ -490,7 +502,7 @@ void RemoteClient::process_next_message()
|
||||||
|
|
||||||
void RemoteClient::write_next_key()
|
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
|
// 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());
|
||||||
|
@ -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)
|
void send_command(StringView session, StringView command)
|
||||||
{
|
{
|
||||||
auto filename = "/tmp/kak-" + session;
|
auto filename = "/tmp/kak-" + session;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define remote_hh_INCLUDED
|
#define remote_hh_INCLUDED
|
||||||
|
|
||||||
#include "display_buffer.hh"
|
#include "display_buffer.hh"
|
||||||
#include "event_manager.hh"
|
|
||||||
#include "user_interface.hh"
|
#include "user_interface.hh"
|
||||||
#include "env_vars.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
|
// A remote client handle communication between a client running on the server
|
||||||
// and a user interface running on the local process.
|
// and a user interface running on the local process.
|
||||||
class RemoteClient
|
class RemoteClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RemoteClient(int socket, std::unique_ptr<UserInterface>&& ui,
|
RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
|
||||||
const EnvVarMap& env_vars, StringView init_command);
|
const EnvVarMap& env_vars, StringView init_command);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -33,12 +34,8 @@ private:
|
||||||
|
|
||||||
std::unique_ptr<UserInterface> m_ui;
|
std::unique_ptr<UserInterface> m_ui;
|
||||||
CharCoord m_dimensions;
|
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);
|
void send_command(StringView session, StringView command);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user