Skip to content

Commit

Permalink
use unix sockets instead of tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
jstkdng committed Mar 5, 2023
1 parent 21fb516 commit 647a965
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
1 change: 0 additions & 1 deletion include/flags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Flags
Flags() = default;
~Flags() = default;

int tcp_port = 56988;
bool no_stdin = false;
bool force_x11 = false;
bool force_sixel = false;
Expand Down
2 changes: 1 addition & 1 deletion include/tmux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace tmux

int get_statusbar_offset();

void handle_hook(std::string_view hook, const Flags& flags);
void handle_hook(std::string_view hook);
void register_hooks();
void unregister_hooks();
}
Expand Down
3 changes: 2 additions & 1 deletion include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace util
auto get_b2_hash(const std::string& str) -> std::string;
auto get_cache_path() -> std::string;
auto get_log_filename() -> std::string;
void send_tcp_message(std::string_view msg, int port);
void send_tcp_message(std::string_view msg);
auto get_socket_path() -> std::string;
}

#endif
5 changes: 3 additions & 2 deletions scripts/sockets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
import time
import os

cmd1 = """
{"action":"add","identifier":"preview","max_height":21,"max_width":118,"path":"/tmp/img1.png","x":10,"y":15}
Expand All @@ -13,11 +14,11 @@
{"action":"remove","identifier":"preview"}
"""

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
connected = False
while not connected:
try:
s.connect(("127.0.0.1", 56988))
s.connect(f"/tmp/ueberzug_{os.getenv('USER')}.sock")
connected = True
except Exception:
time.sleep(0.05)
Expand Down
11 changes: 3 additions & 8 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,16 @@ flags(flags)
if (!fs::exists(cache_path)) fs::create_directories(cache_path);
tmux::register_hooks();
tcp_thread = std::jthread([&] {
logger->info("Listenning on port {}.", flags.tcp_port);
logger->info("Listenning for commands on socket {}.", util::get_socket_path());
tcp_loop();
});
cv::ocl::Context ctx = cv::ocl::Context::getDefault();
if (ctx.ptr()) {
auto device = ctx.device(0);
logger->info("OpenCL supported. Using device {}.", device.name());
}
}

Application::~Application()
{
canvas->clear();
if (f_stderr) std::fclose(f_stderr);
util::send_tcp_message("EXIT", flags.tcp_port);
util::send_tcp_message("EXIT");
tmux::unregister_hooks();
}

Expand Down Expand Up @@ -165,7 +160,7 @@ void Application::tcp_loop()
{
zmq::context_t context(1);
zmq::socket_t socket(context, zmq::socket_type::stream);
socket.bind(fmt::format("tcp://127.0.0.1:{}", flags.tcp_port));
socket.bind(fmt::format("ipc://{}", util::get_socket_path()));
int data[256];
auto idbuf = zmq::buffer(data);
while (true) {
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ int main(int argc, char *argv[])
CLI::App *layer_command = program.add_subcommand("layer", "Display images on the terminal.");
layer_command->add_flag("-s,--silent", flags.silent, "Print stderr to /dev/null.");
layer_command->add_flag("--no-stdin", flags.no_stdin, "Don't listen on stdin for commands.");
layer_command->add_option("--tcp-port", flags.tcp_port, "Change tcp port used.");
layer_command->add_flag("--x11", flags.force_x11, "Force X11 output.");
layer_command->add_flag("--sixel", flags.force_sixel, "Force sixel output")->excludes("--x11");
layer_command->add_option("-p,--parser", nullptr, "**UNUSED**, only present for backwards compatibility.");
Expand Down Expand Up @@ -99,7 +98,7 @@ int main(int argc, char *argv[])
if (tmux_command->parsed()) {
try {
auto positionals = tmux_command->remaining();
tmux::handle_hook(positionals.at(0), flags);
tmux::handle_hook(positionals.at(0));
} catch (const std::out_of_range& oor) {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tmux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ int tmux::get_statusbar_offset()
return std::stoi(output[0]);
}

void tmux::handle_hook(std::string_view hook, const Flags& flags)
void tmux::handle_hook(std::string_view hook)
{
auto msg = fmt::format("{{\"action\": \"tmux\", \"hook\": \"{}\"}}\n", hook);
util::send_tcp_message(msg, flags.tcp_port);
util::send_tcp_message(msg);
}

void tmux::register_hooks()
Expand Down
12 changes: 10 additions & 2 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@

#include <memory>
#include <regex>
#include <filesystem>
#include <xcb/xcb.h>
#include <botan/hash.h>
#include <botan/hex.h>
#include <fmt/format.h>
#include <zmq.hpp>

namespace fs = std::filesystem;

auto util::str_split(const std::string& str, const std::string& delim) -> std::vector<std::string>
{
std::regex re {delim};
Expand Down Expand Up @@ -64,9 +67,14 @@ auto util::get_log_filename() -> std::string
return fmt::format("ueberzug_{}.log", os::getenv("USER").value());
}

void util::send_tcp_message(std::string_view msg, int port)
auto util::get_socket_path() -> std::string
{
return fmt::format("{}/ueberzug_{}.sock", fs::temp_directory_path().string(), os::getenv("USER").value());
}

void util::send_tcp_message(std::string_view msg)
{
auto endpoint = fmt::format("tcp://127.0.0.1:{}", port);
auto endpoint = fmt::format("ipc://{}", get_socket_path());
zmq::context_t context(1);
zmq::socket_t socket(context, zmq::socket_type::stream);
socket.set(zmq::sockopt::linger, 2);
Expand Down

0 comments on commit 647a965

Please sign in to comment.