record-daemon: fix obs recording

This commit is contained in:
2026-03-20 20:34:44 +01:00
parent dbb224e118
commit 1166424c29
12 changed files with 3717 additions and 515 deletions

View File

@@ -118,7 +118,8 @@ impl IpcServer {
info!("Starting IPC server at {:?}", self.config.socket_path);
let listener = PlatformListener::bind(&self.config.socket_path).map_err(IpcError::BindError)?;
let listener =
PlatformListener::bind(&self.config.socket_path).map_err(IpcError::BindError)?;
self.listener = Some(listener);
@@ -146,7 +147,8 @@ impl IpcServer {
// Accept new connection
match listener.accept().await {
Ok((stream, addr)) => {
self.handle_new_connection(stream, format!("{:?}", addr)).await;
self.handle_new_connection(stream, format!("{:?}", addr))
.await;
}
Err(e) => {
warn!("Failed to accept connection: {}", e);
@@ -182,7 +184,8 @@ impl IpcServer {
tokio::spawn(async move {
*client_count.write().await += 1;
if let Err(e) = Self::handle_connection(stream, handlers, shutdown.clone(), notification_tx).await
if let Err(e) =
Self::handle_connection(stream, handlers, shutdown.clone(), notification_tx).await
{
error!("Connection error: {}", e);
}
@@ -232,8 +235,11 @@ impl IpcServer {
Ok(msg) => msg,
Err(e) => {
warn!("Failed to parse message: {}", e);
let response = IpcResponse::error(uuid::Uuid::nil(), format!("Parse error: {}", e));
writer.send(response.to_json()?).await
let response =
IpcResponse::error(uuid::Uuid::nil(), format!("Parse error: {}", e));
writer
.send(response.to_json()?)
.await
.map_err(|e| IpcError::CodecError(e.to_string()))?;
continue;
}
@@ -241,9 +247,7 @@ impl IpcServer {
// Handle message
let response = match message.message_type {
MessageType::Request => {
handlers.handle(message).await
}
MessageType::Request => handlers.handle(message).await,
MessageType::Notification => {
// Notifications don't get responses
trace!("Received notification: {:?}", message);
@@ -258,7 +262,9 @@ impl IpcServer {
// Send response
let response_json = response.to_json()?;
writer.send(response_json).await
writer
.send(response_json)
.await
.map_err(|e| IpcError::CodecError(e.to_string()))?;
}
@@ -268,13 +274,12 @@ impl IpcServer {
/// Stop the IPC server.
pub async fn stop(&mut self) -> Result<()> {
*self.shutdown.write().await = true;
// Remove socket file
if self.config.socket_path.exists() {
std::fs::remove_file(&self.config.socket_path)
.map_err(IpcError::BindError)?;
std::fs::remove_file(&self.config.socket_path).map_err(IpcError::BindError)?;
}
info!("IPC server stopped");
Ok(())
}
@@ -353,7 +358,7 @@ impl IpcServer {
// On Windows, we don't need to bind in advance for named pipes
// The server will create pipe instances on demand
info!("Starting IPC server at {:?}", self.config.socket_path);
info!("IPC server started successfully");
Ok(())
}
@@ -362,10 +367,13 @@ impl IpcServer {
pub async fn run(&self) -> Result<()> {
use tokio::net::windows::named_pipe::ServerOptions;
info!("IPC server listening for connections on {:?}", self.config.socket_path);
info!(
"IPC server listening for connections on {:?}",
self.config.socket_path
);
let pipe_name: String = self.config.socket_path.to_string_lossy().into_owned();
loop {
if *self.shutdown.read().await {
info!("IPC server shutting down");
@@ -383,7 +391,7 @@ impl IpcServer {
match server.connect().await {
Ok(()) => {
debug!("New IPC client connected");
// Spawn handler for this connection
let handlers = self.handlers.clone();
let shutdown = self.shutdown.clone();
@@ -465,19 +473,23 @@ impl IpcServer {
Ok(msg) => msg,
Err(e) => {
warn!("Failed to parse message: {}", e);
let response = IpcResponse::error(uuid::Uuid::nil(), format!("Parse error: {}", e));
writer.write_all(response.to_json()?.as_bytes()).await
let response =
IpcResponse::error(uuid::Uuid::nil(), format!("Parse error: {}", e));
writer
.write_all(response.to_json()?.as_bytes())
.await
.map_err(IpcError::WriteError)?;
writer
.write_all(b"\n")
.await
.map_err(IpcError::WriteError)?;
writer.write_all(b"\n").await.map_err(IpcError::WriteError)?;
continue;
}
};
// Handle message
let response = match message.message_type {
MessageType::Request => {
handlers.handle(message).await
}
MessageType::Request => handlers.handle(message).await,
MessageType::Notification => {
// Notifications don't get responses
trace!("Received notification: {:?}", message);
@@ -492,8 +504,14 @@ impl IpcServer {
// Send response
let response_json = response.to_json()?;
writer.write_all(response_json.as_bytes()).await.map_err(IpcError::WriteError)?;
writer.write_all(b"\n").await.map_err(IpcError::WriteError)?;
writer
.write_all(response_json.as_bytes())
.await
.map_err(IpcError::WriteError)?;
writer
.write_all(b"\n")
.await
.map_err(IpcError::WriteError)?;
}
Ok(())
@@ -527,7 +545,10 @@ mod tests {
#[test]
fn test_ipc_server_config_default() {
let config = IpcServerConfig::default();
assert!(config.socket_path.to_string_lossy().contains("record-daemon"));
assert!(config
.socket_path
.to_string_lossy()
.contains("record-daemon"));
assert_eq!(config.max_connections, 10);
assert_eq!(config.timeout_secs, 30);
}