Skip to main content

rmux_client/commands/
server.rs

1use std::fmt;
2use std::path::Path;
3
4use rmux_proto::{
5    DaemonStatusRequest, KillServerRequest, LockClientRequest, LockServerRequest,
6    LockSessionRequest, Request, Response, ServerAccessRequest, SessionName, ShutdownIfIdleRequest,
7};
8
9use crate::{
10    auto_start::{ensure_server_running_with_config, AutoStartConfig, AutoStartError},
11    connection::{connect, Connection},
12    ClientError,
13};
14
15impl Connection {
16    /// Ensures the server is available, honouring top-level no-start-server behavior.
17    pub fn start_server(
18        socket_path: &Path,
19        no_start_server: bool,
20        config: AutoStartConfig,
21    ) -> Result<Self, StartServerError> {
22        if no_start_server {
23            return connect(socket_path).map_err(StartServerError::Client);
24        }
25
26        ensure_server_running_with_config(socket_path, config).map_err(StartServerError::AutoStart)
27    }
28
29    /// Sends a `kill-server` request over the detached RPC channel.
30    pub fn kill_server(&mut self) -> Result<Response, ClientError> {
31        self.roundtrip(&Request::KillServer(KillServerRequest))
32    }
33
34    /// Sends a `kill-server` request and returns after the frame is written.
35    ///
36    /// Windows package clients use this because the daemon can close its reply
37    /// pipe during shutdown. Callers must then drop this connection and wait
38    /// for endpoint release with [`crate::wait_for_server_endpoint_cleanup`].
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 `kill-server` request for stale daemon cleanup.
44    pub fn kill_server_legacy_wire(&mut self, wire_version: u32) -> Result<(), ClientError> {
45        self.write_legacy_wire_request(&Request::KillServer(KillServerRequest), wire_version)
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}
87
88impl fmt::Display for StartServerError {
89    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
90        match self {
91            Self::Client(error) => fmt::Display::fmt(error, formatter),
92            Self::AutoStart(error) => fmt::Display::fmt(error, formatter),
93        }
94    }
95}
96
97impl std::error::Error for StartServerError {
98    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
99        match self {
100            Self::Client(error) => Some(error),
101            Self::AutoStart(error) => Some(error),
102        }
103    }
104}