Skip to main content

rmux_client/commands/
server.rs

1use std::path::Path;
2
3use rmux_proto::{
4    DaemonStatusRequest, KillServerRequest, LockClientRequest, LockServerRequest,
5    LockSessionRequest, Request, Response, ServerAccessRequest, SessionName, ShutdownIfIdleRequest,
6};
7
8use crate::{
9    auto_start::{ensure_server_running_with_config, AutoStartConfig, AutoStartError},
10    connection::{connect, Connection},
11    ClientError,
12};
13
14impl Connection {
15    /// Ensures the server is available, honouring top-level no-start-server behavior.
16    pub fn start_server(
17        socket_path: &Path,
18        no_start_server: bool,
19        config: AutoStartConfig,
20    ) -> Result<Self, StartServerError> {
21        if no_start_server {
22            return connect(socket_path).map_err(StartServerError::Client);
23        }
24
25        ensure_server_running_with_config(socket_path, config).map_err(StartServerError::AutoStart)
26    }
27
28    /// Sends a `kill-server` request over the detached RPC channel.
29    pub fn kill_server(&mut self) -> Result<Response, ClientError> {
30        self.roundtrip(&Request::KillServer(KillServerRequest))
31    }
32
33    /// Sends a `kill-server` request and returns after the frame is written.
34    ///
35    /// Windows package clients use this for the tmux-style fire-and-forget
36    /// shutdown path, where waiting for the daemon's final cleanup dominates
37    /// the command latency and the named pipe does not need client-side socket
38    /// removal.
39    pub fn kill_server_after_write(&mut self) -> Result<(), ClientError> {
40        self.write_request(&Request::KillServer(KillServerRequest))
41    }
42
43    /// Sends a legacy wire-v1 `kill-server` request for pre-0.6 daemon cleanup.
44    pub fn kill_server_legacy_wire_v1(&mut self) -> Result<(), ClientError> {
45        self.write_legacy_wire_v1_request(&Request::KillServer(KillServerRequest))
46    }
47
48    /// Sends an internal daemon status request over the detached RPC channel.
49    pub fn daemon_status(&mut self) -> Result<Response, ClientError> {
50        self.roundtrip(&Request::DaemonStatus(DaemonStatusRequest))
51    }
52
53    /// Sends an internal idle-only daemon shutdown request.
54    pub fn shutdown_if_idle(&mut self) -> Result<Response, ClientError> {
55        self.roundtrip(&Request::ShutdownIfIdle(ShutdownIfIdleRequest))
56    }
57
58    /// Sends a `lock-server` request over the detached RPC channel.
59    pub fn lock_server(&mut self) -> Result<Response, ClientError> {
60        self.roundtrip(&Request::LockServer(LockServerRequest))
61    }
62
63    /// Sends a `lock-session` request over the detached RPC channel.
64    pub fn lock_session(&mut self, target: SessionName) -> Result<Response, ClientError> {
65        self.roundtrip(&Request::LockSession(LockSessionRequest { target }))
66    }
67
68    /// Sends a `lock-client` request over the detached RPC channel.
69    pub fn lock_client(&mut self, target_client: String) -> Result<Response, ClientError> {
70        self.roundtrip(&Request::LockClient(LockClientRequest { target_client }))
71    }
72
73    /// Sends a `server-access` request over the detached RPC channel.
74    pub fn server_access(&mut self, request: ServerAccessRequest) -> Result<Response, ClientError> {
75        self.roundtrip(&Request::ServerAccess(request))
76    }
77}
78
79/// Client-side `start-server` failure surface.
80#[derive(Debug)]
81pub enum StartServerError {
82    /// Connecting to an already-running server failed.
83    Client(ClientError),
84    /// Auto-starting the server failed.
85    AutoStart(AutoStartError),
86}