Add flag to toggle socket flushing

This commit is contained in:
timvisee
2023-12-29 13:52:00 +01:00
parent 5e2eb9af21
commit c15949b6f0
4 changed files with 27 additions and 11 deletions

View File

@@ -51,6 +51,10 @@ pub struct Arguments {
/// Use binary mode to set pixels (`PB` protocol extension) [default: off]
#[arg(short, long, alias = "bin")]
binary: bool,
/// Do not flush socket after each pixel [default: on]
#[arg(short, long)]
no_flush: bool,
}
/// CLI argument handler.
@@ -107,4 +111,9 @@ impl ArgHandler {
pub fn binary(&self) -> bool {
self.data.binary
}
/// Whether to prevent flushing after each pixel.
pub fn no_flush(&self) -> bool {
self.data.no_flush
}
}

View File

@@ -44,6 +44,7 @@ fn start(arg_handler: &ArgHandler) {
size,
arg_handler.offset(),
arg_handler.binary(),
!arg_handler.no_flush(),
);
// Load the image manager
@@ -56,7 +57,7 @@ fn start(arg_handler: &ArgHandler) {
/// Gather important facts about the host.
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> {
// Set up a client, and get the screen size
let size = Client::connect(arg_handler.host().to_string(), false)?.read_screen_size()?;
let size = Client::connect(arg_handler.host().to_string(), false, false)?.read_screen_size()?;
// Print status
println!("Gathered screen size: {}x{}", size.0, size.1);

View File

@@ -27,6 +27,7 @@ impl Canvas {
size: (u16, u16),
offset: (u16, u16),
binary: bool,
flush: bool,
) -> Canvas {
// Initialize the object
let mut canvas = Canvas {
@@ -41,14 +42,14 @@ impl Canvas {
println!("Starting painter threads...");
// Spawn some painters
canvas.spawn_painters(binary);
canvas.spawn_painters(binary, flush);
// Return the canvas
canvas
}
/// Spawn the painters for this canvas
fn spawn_painters(&mut self, binary: bool) {
fn spawn_painters(&mut self, binary: bool, flush: bool) {
// Spawn some painters
for i in 0..self.painter_count {
// Determine the slice width
@@ -58,12 +59,12 @@ impl Canvas {
let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1);
// Spawn the painter
self.spawn_painter(painter_area, binary);
self.spawn_painter(painter_area, binary, flush);
}
}
/// Spawn a single painter in a thread.
fn spawn_painter(&mut self, area: Rect, binary: bool) {
fn spawn_painter(&mut self, area: Rect, binary: bool, flush: bool) {
// Get the host that will be used
let host = self.host.to_string();
@@ -80,7 +81,7 @@ impl Canvas {
loop {
// Connect
match Client::connect(host.clone(), binary) {
match Client::connect(host.clone(), binary, flush) {
Ok(client) => {
painter.set_client(Some(client));

View File

@@ -30,21 +30,25 @@ pub struct Client {
/// Whether to use binary mode (PB) instead of (PX).
binary: bool,
/// Whether to flush the stream after each pixel.
flush: bool,
}
impl Client {
/// Create a new client instance.
pub fn new(stream: TcpStream, binary: bool) -> Client {
pub fn new(stream: TcpStream, binary: bool, flush: bool) -> Client {
Client {
stream: BufStream::new(stream),
binary,
flush,
}
}
/// Create a new client instane from the given host, and connect to it.
pub fn connect(host: String, binary: bool) -> Result<Client, Error> {
pub fn connect(host: String, binary: bool, flush: bool) -> Result<Client, Error> {
// Create a new stream, and instantiate the client
Ok(Client::new(create_stream(host)?, binary))
Ok(Client::new(create_stream(host)?, binary, flush))
}
/// Write a pixel to the given stream.
@@ -108,9 +112,10 @@ impl Client {
// Flush, make sure to clear the send buffer
// TODO: only flush each 100 pixels?
// TODO: make flushing configurable?
// TODO: make buffer size configurable?
if self.flush {
self.stream.flush()?;
}
// Everything seems to be ok
Ok(())