Add flag to toggle socket flushing
This commit is contained in:
@@ -51,6 +51,10 @@ pub struct Arguments {
|
|||||||
/// Use binary mode to set pixels (`PB` protocol extension) [default: off]
|
/// Use binary mode to set pixels (`PB` protocol extension) [default: off]
|
||||||
#[arg(short, long, alias = "bin")]
|
#[arg(short, long, alias = "bin")]
|
||||||
binary: bool,
|
binary: bool,
|
||||||
|
|
||||||
|
/// Do not flush socket after each pixel [default: on]
|
||||||
|
#[arg(short, long)]
|
||||||
|
no_flush: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CLI argument handler.
|
/// CLI argument handler.
|
||||||
@@ -107,4 +111,9 @@ impl ArgHandler {
|
|||||||
pub fn binary(&self) -> bool {
|
pub fn binary(&self) -> bool {
|
||||||
self.data.binary
|
self.data.binary
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether to prevent flushing after each pixel.
|
||||||
|
pub fn no_flush(&self) -> bool {
|
||||||
|
self.data.no_flush
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ fn start(arg_handler: &ArgHandler) {
|
|||||||
size,
|
size,
|
||||||
arg_handler.offset(),
|
arg_handler.offset(),
|
||||||
arg_handler.binary(),
|
arg_handler.binary(),
|
||||||
|
!arg_handler.no_flush(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Load the image manager
|
// Load the image manager
|
||||||
@@ -56,7 +57,7 @@ fn start(arg_handler: &ArgHandler) {
|
|||||||
/// Gather important facts about the host.
|
/// Gather important facts about the host.
|
||||||
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> {
|
fn gather_host_facts(arg_handler: &ArgHandler) -> Result<(u16, u16), Error> {
|
||||||
// Set up a client, and get the screen size
|
// 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
|
// Print status
|
||||||
println!("Gathered screen size: {}x{}", size.0, size.1);
|
println!("Gathered screen size: {}x{}", size.0, size.1);
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ impl Canvas {
|
|||||||
size: (u16, u16),
|
size: (u16, u16),
|
||||||
offset: (u16, u16),
|
offset: (u16, u16),
|
||||||
binary: bool,
|
binary: bool,
|
||||||
|
flush: bool,
|
||||||
) -> Canvas {
|
) -> Canvas {
|
||||||
// Initialize the object
|
// Initialize the object
|
||||||
let mut canvas = Canvas {
|
let mut canvas = Canvas {
|
||||||
@@ -41,14 +42,14 @@ impl Canvas {
|
|||||||
println!("Starting painter threads...");
|
println!("Starting painter threads...");
|
||||||
|
|
||||||
// Spawn some painters
|
// Spawn some painters
|
||||||
canvas.spawn_painters(binary);
|
canvas.spawn_painters(binary, flush);
|
||||||
|
|
||||||
// Return the canvas
|
// Return the canvas
|
||||||
canvas
|
canvas
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn the painters for this 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
|
// Spawn some painters
|
||||||
for i in 0..self.painter_count {
|
for i in 0..self.painter_count {
|
||||||
// Determine the slice width
|
// Determine the slice width
|
||||||
@@ -58,12 +59,12 @@ impl Canvas {
|
|||||||
let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1);
|
let painter_area = Rect::from((i as u16) * width, 0, width, self.size.1);
|
||||||
|
|
||||||
// Spawn the painter
|
// Spawn the painter
|
||||||
self.spawn_painter(painter_area, binary);
|
self.spawn_painter(painter_area, binary, flush);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn a single painter in a thread.
|
/// 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
|
// Get the host that will be used
|
||||||
let host = self.host.to_string();
|
let host = self.host.to_string();
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ impl Canvas {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Connect
|
// Connect
|
||||||
match Client::connect(host.clone(), binary) {
|
match Client::connect(host.clone(), binary, flush) {
|
||||||
Ok(client) => {
|
Ok(client) => {
|
||||||
painter.set_client(Some(client));
|
painter.set_client(Some(client));
|
||||||
|
|
||||||
|
|||||||
@@ -30,21 +30,25 @@ pub struct Client {
|
|||||||
|
|
||||||
/// Whether to use binary mode (PB) instead of (PX).
|
/// Whether to use binary mode (PB) instead of (PX).
|
||||||
binary: bool,
|
binary: bool,
|
||||||
|
|
||||||
|
/// Whether to flush the stream after each pixel.
|
||||||
|
flush: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Create a new client instance.
|
/// Create a new client instance.
|
||||||
pub fn new(stream: TcpStream, binary: bool) -> Client {
|
pub fn new(stream: TcpStream, binary: bool, flush: bool) -> Client {
|
||||||
Client {
|
Client {
|
||||||
stream: BufStream::new(stream),
|
stream: BufStream::new(stream),
|
||||||
binary,
|
binary,
|
||||||
|
flush,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new client instane from the given host, and connect to it.
|
/// 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
|
// 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.
|
/// Write a pixel to the given stream.
|
||||||
@@ -108,9 +112,10 @@ impl Client {
|
|||||||
|
|
||||||
// Flush, make sure to clear the send buffer
|
// Flush, make sure to clear the send buffer
|
||||||
// TODO: only flush each 100 pixels?
|
// TODO: only flush each 100 pixels?
|
||||||
// TODO: make flushing configurable?
|
|
||||||
// TODO: make buffer size configurable?
|
// TODO: make buffer size configurable?
|
||||||
|
if self.flush {
|
||||||
self.stream.flush()?;
|
self.stream.flush()?;
|
||||||
|
}
|
||||||
|
|
||||||
// Everything seems to be ok
|
// Everything seems to be ok
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user