Compare commits

2 Commits

Author SHA1 Message Date
Jonas Zeunert
e4fe5472aa First messy io_uring impl 2025-01-07 21:41:13 +01:00
Jonas Zeunert
b92231a88b Add string and binary msgs 2024-12-31 11:58:45 +01:00
5 changed files with 75 additions and 17 deletions

View File

@@ -7,4 +7,6 @@ edition = "2021"
address = "0.11.0" address = "0.11.0"
clap = { version = "4.5.23", features = ["derive"] } clap = { version = "4.5.23", features = ["derive"] }
image = "0.25.5" image = "0.25.5"
io-uring = "0.7.3"
libc = "0.2.169"
tokio = "1.42.0" tokio = "1.42.0"

BIN
breakwater.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@@ -1,26 +1,67 @@
use crate::pixelmsgs::PixelMsgs; use crate::pixelmsgs::PixelStringMsgs;
use address::Host; use address::Host;
use std::{io::Write, net::TcpStream}; use io_uring::{opcode, types, IoUring, Submitter};
use libc::c_void;
use libc::iovec;
use std::{io::Write, net::TcpStream, os::fd::AsRawFd};
pub struct FlutR { pub struct FlutR {
stream: TcpStream, stream: TcpStream,
msgs: PixelMsgs, stream_fd: i32,
msgs: PixelStringMsgs,
} }
impl FlutR { impl FlutR {
pub fn new(host: Host, port: u16, msgs: PixelMsgs) -> Result<Self, std::io::Error> { pub fn new(host: Host, port: u16, msgs: PixelStringMsgs) -> Result<Self, std::io::Error> {
let stream = TcpStream::connect(format!("{host}:{port}"))?; 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) { pub fn flut(&mut self) {
for msg in self.msgs.clone() { unsafe {
let result = self.stream.write(&msg); self.write_io_uring();
match result { //self.write_sequential();
Ok(_) => (), }
Err(err) => eprintln!("{err}"), }
fn write_sequential(&mut self) {
loop {
for msg in self.msgs.clone() {
let _ = self.stream.write(&msg);
} }
let _ = self.stream.flush(); let _ = self.stream.flush();
} }
} }
unsafe fn write_io_uring(&mut self) {
let mut ring = IoUring::new(8).unwrap();
let buffer_len: usize = self.msgs.len() * 11;
let buffer: iovec = iovec {
iov_base: self.msgs.as_mut_ptr() as *mut c_void,
iov_len: buffer_len,
};
ring.submitter().register_buffers(&[buffer]);
let write_op = opcode::WriteFixed::new(
io_uring::types::Fd(self.stream_fd),
buffer.iov_base as *const u8,
buffer_len as u32,
0,
)
.build();
loop {
while ring.submission().is_full() {
ring.submission().sync();
}
ring.submission().push(&write_op).unwrap();
ring.submit().unwrap();
}
}
} }

View File

@@ -10,7 +10,7 @@ mod pixelmsgs;
use flutr::FlutR; use flutr::FlutR;
use pixelmsgs::Add; use pixelmsgs::Add;
use pixelmsgs::PixelMsgs; use pixelmsgs::PixelStringMsgs;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@@ -31,9 +31,7 @@ fn main() {
flutr.flut(); flutr.flut();
} }
fn parse_image_to_msgs(path: &PathBuf) -> PixelMsgs { fn parse_image_to_msgs(path: &PathBuf) -> PixelStringMsgs {
let mut result = PixelMsgs::default();
let image_result = parse_image(path); let image_result = parse_image(path);
let image = match image_result { let image = match image_result {
@@ -42,6 +40,7 @@ fn parse_image_to_msgs(path: &PathBuf) -> PixelMsgs {
}; };
let rgb_image = image.into_rgb8(); let rgb_image = image.into_rgb8();
let mut result = PixelStringMsgs::with_capacity(rgb_image.len());
for pixel in rgb_image.enumerate_pixels() { for pixel in rgb_image.enumerate_pixels() {
result.add(pixel); result.add(pixel);
} }

View File

@@ -1,13 +1,15 @@
use image::Rgb; use image::Rgb;
pub type PixelMsg = [u8; 10]; pub type PixelBinaryMsg = [u8; 11];
pub type PixelMsgs = Vec<PixelMsg>; pub type PixelBinaryMsgs = Vec<PixelBinaryMsg>;
pub type PixelStringMsg = Box<[u8]>;
pub type PixelStringMsgs = Vec<PixelStringMsg>;
pub trait Add { pub trait Add {
fn add(&mut self, item: (u32, u32, &Rgb<u8>)); fn add(&mut self, item: (u32, u32, &Rgb<u8>));
} }
impl Add for PixelMsgs { impl Add for PixelBinaryMsgs {
fn add(&mut self, item: (u32, u32, &Rgb<u8>)) { fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
let pb: &[u8] = &[80, 66]; let pb: &[u8] = &[80, 66];
let x = (item.0 as u16).to_le_bytes(); let x = (item.0 as u16).to_le_bytes();
@@ -22,3 +24,17 @@ impl Add for PixelMsgs {
self.push(msg.try_into().unwrap()); self.push(msg.try_into().unwrap());
} }
} }
impl Add for PixelStringMsgs {
fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
let color: &Rgb<u8> = 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()
.to_vec()
.into_boxed_slice();
self.push(msg);
}
}