From b92231a88b396416c506c0547f28c7caf67952eb Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Tue, 31 Dec 2024 11:58:45 +0100 Subject: [PATCH] Add string and binary msgs --- Cargo.toml | 1 + src/flutr.rs | 32 +++++++++++++++++++++++--------- src/pixelmsgs.rs | 19 ++++++++++++++++--- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 175ad17..ef5745b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" address = "0.11.0" clap = { version = "4.5.23", features = ["derive"] } image = "0.25.5" +io-uring = "0.7.3" tokio = "1.42.0" diff --git a/src/flutr.rs b/src/flutr.rs index 840a305..9003ef9 100644 --- a/src/flutr.rs +++ b/src/flutr.rs @@ -1,26 +1,40 @@ use crate::pixelmsgs::PixelMsgs; use address::Host; -use std::{io::Write, net::TcpStream}; +use io_uring::{opcode, types, IoUring}; +use std::{io::Write, net::TcpStream, os::fd::AsRawFd}; pub struct FlutR { stream: TcpStream, + stream_fd: i32, msgs: PixelMsgs, } impl FlutR { pub fn new(host: Host, port: u16, msgs: PixelMsgs) -> Result { let stream = TcpStream::connect(format!("{host}:{port}"))?; - Ok(FlutR { stream, msgs }) + let stream_fd = stream.as_raw_fd(); + Ok(FlutR { + stream, + stream_fd, + msgs, + }) } pub fn flut(&mut self) { - for msg in self.msgs.clone() { - let result = self.stream.write(&msg); - match result { - Ok(_) => (), - Err(err) => eprintln!("{err}"), - } - let _ = self.stream.flush(); + loop { + //self.write_sequential(); + self.write_io_uring(); } } + + fn write_sequential(&mut self) { + for msg in self.msgs.clone() { + let _ = self.stream.write(&msg); + } + let _ = self.stream.flush(); + } + + fn write_io_uring(&mut self) { + for msg in self.msgs.clone() {} + } } diff --git a/src/pixelmsgs.rs b/src/pixelmsgs.rs index 6ba35a9..604280c 100644 --- a/src/pixelmsgs.rs +++ b/src/pixelmsgs.rs @@ -1,13 +1,15 @@ use image::Rgb; -pub type PixelMsg = [u8; 10]; -pub type PixelMsgs = Vec; +pub type PixelBinaryMsg = [u8; 11]; +pub type PixelBinaryMsgs = Vec; +pub type PixelStringMsg<'a> = &'a [u8]; +pub type PixelStringMsgs<'a> = Vec>; pub trait Add { fn add(&mut self, item: (u32, u32, &Rgb)); } -impl Add for PixelMsgs { +impl Add for PixelBinaryMsgs { fn add(&mut self, item: (u32, u32, &Rgb)) { let pb: &[u8] = &[80, 66]; let x = (item.0 as u16).to_le_bytes(); @@ -22,3 +24,14 @@ impl Add for PixelMsgs { self.push(msg.try_into().unwrap()); } } + +impl<'a> Add for PixelStringMsgs<'a> { + fn add(&mut self, item: (u32, u32, &Rgb)) { + let color: &Rgb = item.2; + let r = color.0[0]; + let g = color.0[1]; + let b = color.0[2]; + let msg = format!("PX {} {} {r:02x}{g:02x}{b:02x}\n", item.0, item.1).as_bytes(); + self.push(msg.to_owned()); + } +}