Core LibraryalphaAPI Reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

method libtmux-cxx latest

Server.at_socket_path

-S path: the socket file, used verbatim.

Source and package
Qualified name
libtmux::Server::at_socket_path
Namespace
libtmux
Declared in
Server
Package
libtmux-cxx
Source
include/libtmux/server.hpp
[[C++ nodiscard attributeAsks the compiler to warn when a result is discarded. An explicit cast to void acknowledges that the result is intentionally ignored.Language documentation]] static expected<Server, CommandFailure> at_socket_path(std::string_view path, CommandObserver observer = {}, ExecutionPolicy policy = {});template <typename Path> requires std::same_as<std::remove_cvref_t<Path>, std::filesystem::path> [[C++ nodiscard attributeAsks the compiler to warn when a result is discarded. An explicit cast to void acknowledges that the result is intentionally ignored.Language documentation]] static expected<Server, CommandFailure> at_socket_path(Path&& path, CommandObserver observer = {}, ExecutionPolicy policy = {});

Description#

These report CommandFailure, the same type every other call reports, rather than the SocketError the argument builders use: a factory that failed differently is a factory nothing can be chained onto. The reason a selector was rejected is in the diagnostic, and socket_path_arguments() still returns the enum for a caller that wants to branch on it.

An observer, if given, is told about every command this server runs. It is fixed at construction because the connection is immutable afterwards, and that is what makes a Server safe to copy between threads. The policy is fixed for the same reason, and says what a call gets when it names no timeout or limit of its own.

Examples#

Connect to an owned server. Construct a Server handle for an explicit socket. Construction sends no command; sessions() reads the running server. The fixture controls daemon lifetime, while copies of Server share the connection. libtmux-cxx is not published to a package registry yet, so this builds it from its repository. Use Clang 18 or newer with libc++, CMake 3.25 or newer, Ninja, Git, and tmux 3.3a or newer on Linux. In an empty directory, fetch the documented library revision:

Terminal window
$ git clone https://github.com/libtmux/libtmux-cxx.git libtmux-source && \
git -C libtmux-source checkout 542fdc204ff3cfa56f5484327cda0c69bad2307d

Build and install the C++23 library and its public testing component into this directory:

Terminal window
$ cmake -S libtmux-source -B libtmux-build -G Ninja \
-DCMAKE_TOOLCHAIN_FILE="$PWD/libtmux-source/cmake/toolchains/clang-libcxx.cmake" \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE=Release \
-DLIBTMUX_BUILD_TESTS=OFF \
-DLIBTMUX_BUILD_EXAMPLES=OFF \
-DLIBTMUX_BUILD_TESTING_LIBRARY=ON && \
cmake --build libtmux-build --parallel 2 && \
cmake --install libtmux-build --prefix "$PWD/libtmux-prefix"

Save this complete consumer project as CMakeLists.txt: View source

cmake_minimum_required(VERSION 3.25)
project(libtmux_api_example LANGUAGES CXX)
find_package(libtmux REQUIRED COMPONENTS testing)
add_executable(api_example main.cpp)
target_link_libraries(api_example PRIVATE libtmux::libtmux libtmux::testing)

Save this complete program as main.cpp. ScopedTmuxServer owns a private daemon and stops it when the program exits. Ordinary applications use Server for their own socket; these examples use the fixture until setup moves to ordinary server objects. View source

#include <cstdio>
#include <libtmux/libtmux.hpp>
#include <libtmux/testing/scoped_server.hpp>
int main() {
// The fixture owns this private server and cleans it up on every return path.
auto fixture = libtmux::test::ScopedTmuxServer::start({
.session_name = "api",
.socket_namespace = libtmux::test::SocketNamespace::consumer("api"),
});
if (!fixture.has_value()) {
std::fprintf(stderr, "%s\n", fixture.error().c_str());
return 1;
}
auto server = libtmux::Server::at_socket_path(fixture->socket_path());
if (!server.has_value()) {
std::fprintf(stderr, "%s\n", server.error().diagnostic.c_str());
return 1;
}
// Copying the handle keeps the same connection; it does not start another server.
const libtmux::Server copy = *server;
const auto sessions = copy.sessions();
if (!sessions.has_value()) {
std::fprintf(stderr, "%s\n", sessions.error().diagnostic.c_str());
return 1;
}
if (copy.socket_path() != fixture->socket_path().string() || sessions->size() != 1 ||
sessions->front().name() != "api") {
std::fputs("unexpected server or session\n", stderr);
return 1;
}
std::puts("Connected to the private api session.");
return 0;
}

Build against the installed library and run the saved program. An operation error produces a failing exit status:

Terminal window
$ cmake -S . -B build -G Ninja \
-DCMAKE_PREFIX_PATH="$PWD/libtmux-prefix" \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_CXX_FLAGS=-stdlib=libc++ && \
cmake --build build --parallel 2 && \
./build/api_example

Expected output:

Connected to the private api session.
Type parameters
  • Path # for at_socket_path(path, observer, policy) [overload 2]

Parameters

Guides#

Pages that discuss at_socket_path.

Esc

Type to search.

Reference previewOpen reference