Skip to content

Commit

Permalink
Add loop server/proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrice Desclaux <[email protected]>
  • Loading branch information
serpilliere committed Jan 19, 2023
1 parent 5f65881 commit 3b85311
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
11 changes: 10 additions & 1 deletion sanzu/src/bin/sanzu_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate log;

use clap::{Arg, Command};
use clap::{Arg, ArgAction, Command};
use sanzu::{config::read_server_config, proxy, utils::ArgumentsProxy};
use sanzu_common::proto::VERSION;
use std::net::IpAddr;
Expand Down Expand Up @@ -106,6 +106,13 @@ Protocol version: {:?}
.long("shm_is_xwd")
.num_args(0),
)
.arg(
Arg::new("loop")
.help("Endless server clients")
.short('d')
.long("loop")
.action(ArgAction::SetTrue),
)
.get_matches();

let server_addr = matches
Expand All @@ -129,6 +136,7 @@ Protocol version: {:?}
let vsock = matches.get_flag("vsock");
let import_video_shm = matches.get_one::<String>("import_video_shm").cloned();
let shm_is_xwd = matches.get_flag("shm_is_xwd");
let endless_loop = matches.get_flag("loop");

let arguments = ArgumentsProxy {
vsock,
Expand All @@ -141,6 +149,7 @@ Protocol version: {:?}
audio,
video_shared_mem: import_video_shm,
shm_is_xwd,
endless_loop,
};

if let Err(err) = proxy::run(&conf, &arguments) {
Expand Down
9 changes: 9 additions & 0 deletions sanzu/src/bin/sanzu_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ Protocol version: {:?}
.long("rdonly")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("loop")
.help("Endless server clients")
.short('d')
.long("loop")
.action(ArgAction::SetTrue),
)
.get_matches();

let address = matches.get_one::<String>("listen").unwrap();
Expand All @@ -188,6 +195,7 @@ Protocol version: {:?}
let extern_img_source = matches.get_one::<String>("use_extern_img_source").cloned();
let avoid_img_extraction = matches.get_flag("avoid_img_extraction");
let rdonly = matches.get_flag("rdonly");
let endless_loop = matches.get_flag("loop");

let arguments = ArgumentsSrv {
vsock,
Expand All @@ -206,6 +214,7 @@ Protocol version: {:?}
extern_img_source,
avoid_img_extraction,
rdonly,
endless_loop,
};

if let Err(err) = server::run(&conf, &arguments) {
Expand Down
15 changes: 15 additions & 0 deletions sanzu/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,22 @@ macro_rules! send_srv_msg_type {
}};
}

/// Exec main loop
///
pub fn run(config: &ConfigServer, arguments: &ArgumentsProxy) -> Result<()> {
if arguments.endless_loop {
loop {
if let Err(err) = run_server(config, arguments) {
error!("Server error");
err.chain().for_each(|cause| error!(" - due to {}", cause));
}
}
} else {
run_server(config, arguments)
}
}

pub fn run_server(config: &ConfigServer, arguments: &ArgumentsProxy) -> Result<()> {
let codec_name = get_encoder_category(&arguments.encoder_name)?;

let mut sound_encoder = opus::Encoder::new(
Expand Down
17 changes: 16 additions & 1 deletion sanzu/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ fn auth_client(
Ok((tls_conn, username))
}

/// Exec main loop
///
pub fn run(config: &ConfigServer, arguments: &ArgumentsSrv) -> Result<()> {
if arguments.endless_loop {
loop {
if let Err(err) = run_server(config, arguments) {
error!("Server error");
err.chain().for_each(|cause| error!(" - due to {}", cause));
}
}
} else {
run_server(config, arguments)
}
}

/// Server main loop
///
/// The loop is composed of the following actions:
Expand All @@ -106,7 +121,7 @@ fn auth_client(
/// - encode image
/// - serialize / send events to client
/// - receive / handle client events
pub fn run(config: &ConfigServer, arguments: &ArgumentsSrv) -> Result<()> {
pub fn run_server(config: &ConfigServer, arguments: &ArgumentsSrv) -> Result<()> {
info!("Start server");

let mut sock: Box<dyn ReadWrite> = match (arguments.vsock, arguments.stdio, arguments.unixsock)
Expand Down
2 changes: 2 additions & 0 deletions sanzu/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct ArgumentsSrv<'a> {
pub extern_img_source: Option<String>,
pub avoid_img_extraction: bool,
pub rdonly: bool,
pub endless_loop: bool,
}

pub struct ArgumentsClient<'a> {
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct ArgumentsProxy<'a> {
pub audio: bool,
pub video_shared_mem: Option<String>,
pub shm_is_xwd: bool,
pub endless_loop: bool,
}

const MAX_HEADER_SIZE: u32 = 0x100;
Expand Down

0 comments on commit 3b85311

Please sign in to comment.