1pub mod process;
2pub mod runtime;
3
4#[cfg(unix)]
5mod unix;
6#[cfg(windows)]
7mod windows;
8#[cfg(windows)]
9pub use windows::NamedPipeListener;
10
11#[cfg(unix)]
12use std::path::PathBuf;
13
14use anyhow::Result;
15#[cfg(unix)]
16use anyhow::anyhow;
17#[cfg(unix)]
18use tokio::io::AsyncWriteExt;
19
20#[derive(Debug, Clone)]
22pub enum DaemonAddr {
23 #[cfg(unix)]
24 Unix(PathBuf),
25 #[cfg(windows)]
26 NamedPipe(String),
27}
28
29impl DaemonAddr {
30 pub fn default_for_current_os() -> Self {
31 #[cfg(unix)]
32 {
33 Self::Unix(unix::default_socket_path())
34 }
35 #[cfg(windows)]
36 {
37 Self::NamedPipe(windows::default_pipe_name())
38 }
39 }
40
41 pub fn display(&self) -> String {
42 match self {
43 #[cfg(unix)]
44 Self::Unix(p) => p.display().to_string(),
45 #[cfg(windows)]
46 Self::NamedPipe(n) => n.clone(),
47 }
48 }
49
50 pub fn is_listening(&self) -> bool {
52 match self {
53 #[cfg(unix)]
54 Self::Unix(p) => p.exists(),
55 #[cfg(windows)]
56 Self::NamedPipe(name) => windows::pipe_exists(name),
57 }
58 }
59}
60
61pub fn cleanup(addr: &DaemonAddr) {
63 match addr {
64 #[cfg(unix)]
65 DaemonAddr::Unix(p) => {
66 if p.exists() {
67 let _ = std::fs::remove_file(p);
68 }
69 }
70 #[cfg(windows)]
71 DaemonAddr::NamedPipe(_) => {
72 }
74 }
75}
76
77#[cfg(unix)]
79pub fn bind_listener(addr: &DaemonAddr) -> Result<tokio::net::UnixListener> {
80 match addr {
81 DaemonAddr::Unix(path) => unix::bind_listener(path),
82 }
83}
84
85#[cfg(unix)]
87pub async fn connect(addr: &DaemonAddr) -> Result<tokio::net::UnixStream> {
88 match addr {
89 DaemonAddr::Unix(path) => unix::connect(path).await,
90 }
91}
92
93#[cfg(unix)]
95pub async fn connect_enterprise(
96 path: &std::path::Path,
97 handshake: &lean_ctx_protocol::runtime_handshake::RuntimeHandshake,
98) -> Result<tokio::net::UnixStream> {
99 let mut stream = unix::connect(path).await?;
100 let frame = handshake.encode_frame().map_err(|error| anyhow!(error))?;
101 stream.write_all(&frame).await?;
102 stream.flush().await?;
103 Ok(stream)
104}
105
106#[cfg(windows)]
108pub fn bind_listener(addr: &DaemonAddr) -> Result<NamedPipeListener> {
109 match addr {
110 DaemonAddr::NamedPipe(name) => NamedPipeListener::bind(name),
111 }
112}
113
114#[cfg(windows)]
116pub async fn connect(
117 addr: &DaemonAddr,
118) -> Result<tokio::net::windows::named_pipe::NamedPipeClient> {
119 match addr {
120 DaemonAddr::NamedPipe(name) => windows::connect(name).await,
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 #[test]
129 fn daemon_addr_display_non_empty() {
130 let addr = DaemonAddr::default_for_current_os();
131 let display = addr.display();
132 assert!(!display.is_empty(), "`display` is empty");
133 }
134
135 #[test]
136 fn cleanup_nonexistent_does_not_panic() {
137 #[cfg(unix)]
138 {
139 let addr = DaemonAddr::Unix(std::path::PathBuf::from(
140 "/tmp/lean-ctx-test-nonexistent.sock",
141 ));
142 cleanup(&addr);
143 }
144 }
145
146 #[cfg(unix)]
147 #[tokio::test]
148 async fn enterprise_connect_sends_versioned_frame() {
149 let dir = tempfile::tempdir().unwrap();
150 let path = dir.path().join("enterprise.sock");
151 let listener = unix::bind_listener(&path).unwrap();
152 let handshake = lean_ctx_protocol::runtime_handshake::RuntimeHandshake::public([]);
153 let client = connect_enterprise(&path, &handshake);
154 let server = listener.accept();
155 let (client, accepted) = tokio::join!(client, server);
156 let _client = client.unwrap();
157 let (mut stream, _) = accepted.unwrap();
158 use tokio::io::AsyncReadExt;
159 let mut length = [0_u8; 4];
160 stream.read_exact(&mut length).await.unwrap();
161 let len = u32::from_be_bytes(length) as usize;
162 assert_eq!(len, handshake.encode_frame().unwrap().len() - 4);
163 }
164}