Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/sandstar-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use sandstar_ipc::protocol::{read_frame, write_frame};
use sandstar_ipc::types::{EngineCommand, EngineResponse};
use std::io;
use std::net::TcpStream;
use std::time::Duration;

/// Longest wait for the server, per read or write. Above the server's own
/// deadlines (5 s read, 10 s engine, 5 s write), so the server's error reply
/// arrives first; below it the CLI would give up on a busy but healthy engine.
const IO_TIMEOUT: Duration = Duration::from_secs(20);

/// Send a command to the engine server and return the response.
pub fn send_command(address: &str, cmd: &EngineCommand) -> io::Result<EngineResponse> {
Expand All @@ -28,15 +34,22 @@ pub fn send_command(address: &str, cmd: &EngineCommand) -> io::Result<EngineResp
fn connect(address: &str) -> io::Result<ConnStream> {
if !address.contains(':') {
let stream = std::os::unix::net::UnixStream::connect(address)?;
stream.set_read_timeout(Some(IO_TIMEOUT))?;
stream.set_write_timeout(Some(IO_TIMEOUT))?;
return Ok(ConnStream::Unix(stream));
}
let stream = TcpStream::connect(address)?;
stream.set_read_timeout(Some(IO_TIMEOUT))?;
stream.set_write_timeout(Some(IO_TIMEOUT))?;
Ok(ConnStream::Tcp(stream))
}

#[cfg(not(unix))]
fn connect(address: &str) -> io::Result<TcpStream> {
TcpStream::connect(address)
let stream = TcpStream::connect(address)?;
stream.set_read_timeout(Some(IO_TIMEOUT))?;
stream.set_write_timeout(Some(IO_TIMEOUT))?;
Ok(stream)
}

// --- Platform-specific stream type ---
Expand Down
2 changes: 1 addition & 1 deletion crates/sandstar-ipc/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{de::DeserializeOwned, Serialize};
use std::io;

/// Maximum frame size (1 MB). Prevents OOM from malformed length prefixes.
const MAX_FRAME_SIZE: u32 = 1_048_576;
pub const MAX_FRAME_SIZE: u32 = 1_048_576;

/// Write a serializable value as a length-prefixed bincode frame.
///
Expand Down
31 changes: 13 additions & 18 deletions crates/sandstar-server/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//! Command dispatch: deserialize IPC commands, execute on engine, send response.
//! Command dispatch: run an IPC command on the engine (main loop side).
//!
//! The connection itself (framing, deadlines) lives in `crate::ipc` (R-28).

use std::io;
use std::time::Instant;

use sandstar_engine::{Engine, EngineValue};
use sandstar_hal::{HalDiagnostics, HalRead, HalWrite};
use sandstar_ipc::protocol::{read_frame, write_frame};
use sandstar_ipc::types::*;
use tracing::{debug, info, warn};

use crate::config::ServerConfig;
use crate::history::HistoryStore;
use crate::ipc::Stream;
use crate::reload;

/// Result of handling an IPC connection.
/// What the main loop must do after an IPC command.
pub enum ConnectionResult {
/// Normal command processed, continue accepting connections.
Continue,
Expand All @@ -24,19 +23,15 @@ pub enum ConnectionResult {
PollNow,
}

/// Handle a single IPC connection: read one command, dispatch, write response.
pub fn handle_connection<H: HalRead + HalWrite + HalDiagnostics>(
mut stream: Stream,
/// Run one IPC command on the engine: what the main loop must do next, and
/// the reply for the client.
pub fn run<H: HalRead + HalWrite + HalDiagnostics>(
cmd: EngineCommand,
engine: &mut Engine<H>,
config: &ServerConfig,
start_time: Instant,
history_store: &HistoryStore,
) -> io::Result<ConnectionResult> {
let cmd: EngineCommand = match read_frame(&mut stream)? {
Some(cmd) => cmd,
None => return Ok(ConnectionResult::Continue), // clean disconnect
};

) -> (ConnectionResult, EngineResponse) {
debug!(?cmd, "received command");
crate::metrics::metrics()
.ipc_requests
Expand All @@ -47,10 +42,10 @@ pub fn handle_connection<H: HalRead + HalWrite + HalDiagnostics>(
EngineCommand::PollNow => ConnectionResult::PollNow,
_ => ConnectionResult::Continue,
};
let response = execute(cmd, engine, config, start_time, history_store);
write_frame(&mut stream, &response)?;

Ok(result)
(
result,
execute(cmd, engine, config, start_time, history_store),
)
}

/// Execute a command against the engine and produce a response.
Expand Down
Loading
Loading