diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-11-05 13:57:12 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-11-05 13:57:12 +0000 |
| commit | 80b50cd737c485545d300d90763668c2f52df3cb (patch) | |
| tree | 04efc20ed6f90a155a4f43dfb588a336482d8524 | |
| parent | a3b29b1f1184cec2f1af33415556d3f7009b4696 (diff) | |
Simplify RemoteClient creation code
| -rw-r--r-- | src/main.cc | 6 | ||||
| -rw-r--r-- | src/remote.cc | 62 | ||||
| -rw-r--r-- | src/remote.hh | 11 |
3 files changed, 33 insertions, 46 deletions
diff --git a/src/main.cc b/src/main.cc index f6154404..056d55ee 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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(); } diff --git a/src/remote.cc b/src/remote.cc index 561b3cf0..d46527ae 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -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; diff --git a/src/remote.hh b/src/remote.hh index 2d56011c..90aa14da 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -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); |
