1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Server runner - main loop for the daemon process
//!
//! The server:
//! 1. Binds to IPC sockets (data + control)
//! 2. Accepts client connections
//! 3. Performs handshake (version check, environment exchange)
//! 4. Relays input from clients to the editor
//! 5. Sends rendered output back to clients
use std::io;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::server::capture_backend::{terminal_setup_sequences, terminal_teardown_sequences};
use crate::server::input_parser::InputParser;
use crate::server::ipc::{ServerConnection, ServerListener, SocketPaths};
use crate::server::protocol::{
ClientControl, ServerControl, ServerHello, TermSize, VersionMismatch, PROTOCOL_VERSION,
};
use crossterm::event::Event;
/// Server configuration
#[derive(Debug, Clone)]
pub struct ServerConfig {
/// Working directory for this session
pub working_dir: std::path::PathBuf,
/// Optional session name (otherwise derived from working_dir)
pub session_name: Option<String>,
/// Idle timeout before auto-shutdown (None = never)
pub idle_timeout: Option<Duration>,
/// Whether mouse hover (and mode 1003 on Windows) is enabled
pub mouse_hover_enabled: bool,
}
/// Server state
pub struct Server {
config: ServerConfig,
listener: ServerListener,
clients: Vec<ConnectedClient>,
/// Last time a client was connected (for idle timeout)
last_client_activity: Instant,
/// Flag to signal shutdown
shutdown: Arc<AtomicBool>,
}
/// A connected client
pub struct ConnectedClient {
/// Client connection (data + control sockets)
conn: ServerConnection,
/// Client's terminal size
term_size: TermSize,
/// Client's environment
#[allow(dead_code)]
env: std::collections::HashMap<String, Option<String>>,
/// Client ID for logging
id: u64,
/// Input parser for converting raw bytes to events
input_parser: InputParser,
}
impl Server {
/// Create a new server for the given working directory
pub fn new(config: ServerConfig) -> io::Result<Self> {
let socket_paths = if let Some(ref name) = config.session_name {
SocketPaths::for_session_name(name)?
} else {
SocketPaths::for_working_dir(&config.working_dir)?
};
let listener = ServerListener::bind(socket_paths)?;
Ok(Self {
config,
listener,
clients: Vec::new(),
last_client_activity: Instant::now(),
shutdown: Arc::new(AtomicBool::new(false)),
})
}
/// Get a handle to request shutdown
pub fn shutdown_handle(&self) -> Arc<AtomicBool> {
self.shutdown.clone()
}
/// Run the server main loop
///
/// This is a basic implementation that handles connections and handshakes.
/// The full implementation will integrate with the Editor.
pub fn run(&mut self) -> io::Result<()> {
tracing::info!("Server starting for {:?}", self.config.working_dir);
let mut next_client_id = 1u64;
loop {
// Check for shutdown
if self.shutdown.load(Ordering::SeqCst) {
tracing::info!("Shutdown requested");
break;
}
// Check idle timeout
if let Some(timeout) = self.config.idle_timeout {
if self.clients.is_empty() && self.last_client_activity.elapsed() > timeout {
tracing::info!("Idle timeout reached, shutting down");
break;
}
}
// Try to accept new connections
match self.listener.accept() {
Ok(Some(conn)) => match self.handle_new_connection(conn, next_client_id) {
Ok(client) => {
tracing::info!("Client {} connected", client.id);
self.clients.push(client);
self.last_client_activity = Instant::now();
next_client_id += 1;
}
Err(e) => {
tracing::warn!("Failed to complete handshake: {}", e);
}
},
Ok(None) => {
// No pending connection
}
Err(e) => {
tracing::error!("Accept error: {}", e);
}
}
// Process existing clients
let input_events = self.process_clients()?;
// TODO: Send input events to the Editor
// For now, just update activity timestamp if we got events
if !input_events.is_empty() {
self.last_client_activity = Instant::now();
}
// Brief sleep to avoid busy-waiting
std::thread::sleep(Duration::from_millis(10));
}
// Send quit message to all clients
self.disconnect_all_clients("Server shutting down")?;
Ok(())
}
/// Handle a new client connection (perform handshake)
fn handle_new_connection(
&self,
conn: ServerConnection,
client_id: u64,
) -> io::Result<ConnectedClient> {
// Read client hello (blocking read)
// On Windows, named pipes are already in blocking mode from accept().
// On Unix, we need to explicitly set blocking mode for the handshake.
#[cfg(not(windows))]
conn.control.set_nonblocking(false)?;
let hello_json = conn
.read_control()?
.ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "No hello received"))?;
let client_msg: ClientControl = serde_json::from_str(&hello_json)
.map_err(|e| io::Error::other(format!("Invalid hello: {}", e)))?;
let hello = match client_msg {
ClientControl::Hello(h) => h,
_ => {
return Err(io::Error::other("Expected Hello message"));
}
};
// Check protocol version
if hello.protocol_version != PROTOCOL_VERSION {
let mismatch = VersionMismatch {
server_version: env!("CARGO_PKG_VERSION").to_string(),
client_version: hello.client_version.clone(),
action: if hello.protocol_version > PROTOCOL_VERSION {
"upgrade_server".to_string()
} else {
"restart_server".to_string()
},
message: format!(
"Protocol version mismatch: server={}, client={}",
PROTOCOL_VERSION, hello.protocol_version
),
};
let response = serde_json::to_string(&ServerControl::VersionMismatch(mismatch))
.map_err(|e| io::Error::other(e.to_string()))?;
conn.write_control(&response)?;
return Err(io::Error::other("Version mismatch"));
}
// Send server hello
let session_id = self.config.session_name.clone().unwrap_or_else(|| {
crate::workspace::encode_path_for_filename(&self.config.working_dir)
});
let server_hello = ServerHello::new(session_id);
let response = serde_json::to_string(&ServerControl::Hello(server_hello))
.map_err(|e| io::Error::other(e.to_string()))?;
conn.write_control(&response)?;
tracing::debug!(
"Client {} hello: term_size={}x{}, TERM={:?}",
client_id,
hello.term_size.cols,
hello.term_size.rows,
hello.term()
);
// Set sockets back to non-blocking for normal operation.
// On Windows, keep pipes in blocking mode — try_read_nonblocking()
// uses PeekNamedPipe to check data availability before doing a
// blocking read. Setting PIPE_NOWAIT on Windows can trigger
// STATUS_STACK_BUFFER_OVERRUN (0xc0000409).
#[cfg(not(windows))]
conn.control.set_nonblocking(true)?;
// Send terminal setup sequences to initialize the client's terminal
let setup = terminal_setup_sequences(self.config.mouse_hover_enabled);
conn.write_data(&setup)?;
Ok(ConnectedClient {
conn,
term_size: hello.term_size,
env: hello.env,
id: client_id,
input_parser: InputParser::new(),
})
}
/// Process messages from connected clients
///
/// Returns a vector of (client_id, events) for the caller to handle.
fn process_clients(&mut self) -> io::Result<Vec<(u64, Vec<Event>)>> {
let mut disconnected = Vec::new();
let mut control_messages: Vec<(usize, ClientControl)> = Vec::new();
let mut input_events: Vec<(u64, Vec<Event>)> = Vec::new();
for (idx, client) in self.clients.iter_mut().enumerate() {
// Read from data socket (input from client)
let mut buf = [0u8; 4096];
match client.conn.read_data(&mut buf) {
Ok(0) => {
// On Windows named pipes, Ok(0) in non-blocking mode might mean "no data"
// Only treat as EOF on Unix where it definitively means disconnected
#[cfg(not(windows))]
{
disconnected.push(idx);
continue;
}
// On Windows, ignore Ok(0) - we'll detect disconnection via write errors
}
Ok(n) => {
// Parse raw bytes into events
let events = client.input_parser.parse(&buf[..n]);
if !events.is_empty() {
tracing::trace!(
"Client {} sent {} bytes -> {} events",
client.id,
n,
events.len()
);
input_events.push((client.id, events));
}
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
// No data available
}
Err(e) => {
tracing::warn!("Client {} read error: {}", client.id, e);
disconnected.push(idx);
continue;
}
}
// Try to read a control message (non-blocking)
// Use try_read which internally handles non-blocking mode and Windows pipe quirks
let mut buf = [0u8; 4096];
match client.conn.control.try_read(&mut buf) {
Ok(0) => {
// On Windows, Ok(0) in non-blocking mode might mean "no data"
// Only treat as EOF if we're sure the pipe is closed
#[cfg(not(windows))]
{
// On Unix, Ok(0) means EOF
disconnected.push(idx);
}
// On Windows, just ignore - we'll detect actual disconnection
// when write fails or we get a real error
}
Ok(n) => {
// Parse the bytes as a line (looking for newline)
let data = &buf[..n];
if let Ok(text) = std::str::from_utf8(data) {
for line in text.lines() {
let line = line.trim();
if !line.is_empty() {
if let Ok(msg) = serde_json::from_str::<ClientControl>(line) {
control_messages.push((idx, msg));
}
}
}
}
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
// No message available
}
Err(e) => {
tracing::warn!("Client {} control read error: {}", client.id, e);
}
}
}
// Update activity timestamp if we had any messages
if !control_messages.is_empty() {
self.last_client_activity = Instant::now();
}
// Process control messages (now we can borrow self mutably)
for (idx, msg) in control_messages {
if !disconnected.contains(&idx) {
self.handle_control_message(idx, msg)?;
}
}
// Remove disconnected clients (in reverse order to preserve indices)
for idx in disconnected.into_iter().rev() {
let client = self.clients.remove(idx);
tracing::info!("Client {} disconnected", client.id);
}
Ok(input_events)
}
/// Handle a control message from a client
fn handle_control_message(&mut self, client_idx: usize, msg: ClientControl) -> io::Result<()> {
let client = match self.clients.get_mut(client_idx) {
Some(c) => c,
None => return Ok(()), // Client already disconnected
};
match msg {
ClientControl::Hello(_) => {
// Shouldn't happen after initial handshake
tracing::warn!("Client {} sent unexpected Hello", client.id);
}
ClientControl::Resize { cols, rows } => {
client.term_size = TermSize::new(cols, rows);
tracing::debug!("Client {} resized to {}x{}", client.id, cols, rows);
// TODO: Trigger re-render
}
ClientControl::Ping => {
let pong = serde_json::to_string(&ServerControl::Pong)
.map_err(|e| io::Error::other(e.to_string()))?;
client.conn.write_control(&pong)?;
}
ClientControl::Detach => {
tracing::info!("Client {} detached", client.id);
// Will be removed on next process_clients() call
}
ClientControl::Quit => {
let client_id = client.id;
tracing::info!("Client {} requested quit", client_id);
// TODO: Check for unsaved changes, prompt, etc.
self.shutdown.store(true, Ordering::SeqCst);
}
ClientControl::OpenFiles { .. } => {
// This runner doesn't have an editor, so we can't open files
tracing::warn!(
"Client {} sent OpenFiles but no editor is running",
client.id
);
}
}
Ok(())
}
/// Disconnect all clients
fn disconnect_all_clients(&mut self, reason: &str) -> io::Result<()> {
let teardown = terminal_teardown_sequences();
for client in &mut self.clients {
// Send terminal teardown sequences to restore client's terminal
drop(client.conn.write_data(&teardown));
// Send quit control message
let quit_msg = serde_json::to_string(&ServerControl::Quit {
reason: reason.to_string(),
})
.unwrap_or_default();
drop(client.conn.write_control(&quit_msg));
}
self.clients.clear();
Ok(())
}
/// Send output to all connected clients
#[allow(dead_code)]
pub fn broadcast_output(&mut self, data: &[u8]) -> io::Result<()> {
for client in &mut self.clients {
if let Err(e) = client.conn.write_data(data) {
tracing::warn!("Failed to send to client {}: {}", client.id, e);
}
}
Ok(())
}
/// Get the number of connected clients
pub fn client_count(&self) -> usize {
self.clients.len()
}
/// Get the socket paths
pub fn socket_paths(&self) -> &SocketPaths {
self.listener.paths()
}
}
#[allow(dead_code)]
impl ConnectedClient {
/// Get the client's terminal size
pub fn term_size(&self) -> TermSize {
self.term_size
}
/// Get the client's TERM environment variable
pub fn term(&self) -> Option<&str> {
self.env.get("TERM").and_then(|v| v.as_deref())
}
/// Check if the client supports truecolor
pub fn supports_truecolor(&self) -> bool {
self.env
.get("COLORTERM")
.and_then(|v| v.as_deref())
.map(|v| v == "truecolor" || v == "24bit")
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_server_config_defaults() {
let config = ServerConfig {
working_dir: PathBuf::from("/tmp/test"),
session_name: None,
idle_timeout: Some(Duration::from_secs(3600)),
mouse_hover_enabled: true,
};
assert_eq!(config.working_dir, PathBuf::from("/tmp/test"));
assert_eq!(config.idle_timeout, Some(Duration::from_secs(3600)));
}
#[test]
fn test_connected_client_term_helpers() {
use crate::server::protocol::{ClientHello, TermSize};
use std::collections::HashMap;
// Test TERM accessor and truecolor detection via ClientHello
// (same logic used by ConnectedClient)
let mut env = HashMap::new();
env.insert("TERM".to_string(), Some("xterm-256color".to_string()));
env.insert("COLORTERM".to_string(), Some("truecolor".to_string()));
let mut hello = ClientHello::new(TermSize::new(80, 24));
hello.env = env;
assert_eq!(hello.term(), Some("xterm-256color"));
assert!(hello.supports_truecolor());
}
#[test]
fn test_term_size() {
use crate::server::protocol::TermSize;
let size = TermSize::new(120, 40);
assert_eq!(size.cols, 120);
assert_eq!(size.rows, 40);
}
}