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

@@ -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?
self.stream.flush()?;
if self.flush {
self.stream.flush()?;
}
// Everything seems to be ok
Ok(())