8 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
Johannes Wendel
baee4ab135 Remove unused file 2024-12-30 19:35:18 +01:00
Johannes Wendel
1ace2e5730 Make cli-parameter work 2024-12-30 19:20:25 +01:00
Jonas Zeunert
0ffc57ae78 Switch to binary msg 2024-12-30 16:20:19 +01:00
Jonas Zeunert
439c76c196 First working version 2024-12-30 15:45:22 +01:00
Jonas Zeunert
5bd7626843 First naive implementation of tcp server. Not working right now 2024-12-30 15:42:10 +01:00
30f374bfc4 Merge pull request 'Make nix compatible' (#1) from feature/jojo-nix-cmaptibility into master
Reviewed-on: rhetenor/fluter#1
2024-12-30 14:26:35 +01:00
5 changed files with 130 additions and 9 deletions

View File

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

BIN
breakwater.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

67
src/flutr.rs Normal file
View File

@@ -0,0 +1,67 @@
use crate::pixelmsgs::PixelStringMsgs;
use address::Host;
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 {
stream: TcpStream,
stream_fd: i32,
msgs: PixelStringMsgs,
}
impl FlutR {
pub fn new(host: Host, port: u16, msgs: PixelStringMsgs) -> Result<Self, std::io::Error> {
let stream = TcpStream::connect(format!("{host}:{port}"))?;
let stream_fd = stream.as_raw_fd();
Ok(FlutR {
stream,
stream_fd,
msgs,
})
}
pub fn flut(&mut self) {
unsafe {
self.write_io_uring();
//self.write_sequential();
}
}
fn write_sequential(&mut self) {
loop {
for msg in self.msgs.clone() {
let _ = self.stream.write(&msg);
}
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

@@ -1,26 +1,38 @@
use address::Host;
use std::path::PathBuf;
use image::{ImageReader, Rgb};
use image::ImageReader;
use clap::Parser;
type Pixel<'a> = (u32, u32, &'a Rgb<u8>);
mod flutr;
mod pixelmsgs;
use flutr::FlutR;
use pixelmsgs::Add;
use pixelmsgs::PixelStringMsgs;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
image_path: PathBuf,
addr: Host,
host: Host,
#[arg(short, long, default_value_t = 1234)]
#[arg(default_value_t = 1234)]
port: u16,
}
fn main() {
let args = Args::parse();
let image_result = parse_image(&args.image_path);
let pixel_msgs = parse_image_to_msgs(&args.image_path);
let mut flutr = FlutR::new(args.host, args.port, pixel_msgs).unwrap();
flutr.flut();
}
fn parse_image_to_msgs(path: &PathBuf) -> PixelStringMsgs {
let image_result = parse_image(path);
let image = match image_result {
Ok(img) => img,
@@ -28,13 +40,13 @@ fn main() {
};
let rgb_image = image.into_rgb8();
let mut result = PixelStringMsgs::with_capacity(rgb_image.len());
for pixel in rgb_image.enumerate_pixels() {
write_pixel(pixel);
result.add(pixel);
}
//println!("{:?}", &image);
}
fn write_pixel(pixel: Pixel) {}
result
}
fn parse_image(path: &PathBuf) -> Result<image::DynamicImage, image::error::ImageError> {
ImageReader::open(path)?.with_guessed_format()?.decode()

40
src/pixelmsgs.rs Normal file
View File

@@ -0,0 +1,40 @@
use image::Rgb;
pub type PixelBinaryMsg = [u8; 11];
pub type PixelBinaryMsgs = Vec<PixelBinaryMsg>;
pub type PixelStringMsg = Box<[u8]>;
pub type PixelStringMsgs = Vec<PixelStringMsg>;
pub trait Add {
fn add(&mut self, item: (u32, u32, &Rgb<u8>));
}
impl Add for PixelBinaryMsgs {
fn add(&mut self, item: (u32, u32, &Rgb<u8>)) {
let pb: &[u8] = &[80, 66];
let x = (item.0 as u16).to_le_bytes();
let y = (item.1 as u16).to_le_bytes();
let color: &Rgb<u8> = item.2;
let r = color.0[0].to_le_bytes();
let g = color.0[1].to_le_bytes();
let b = color.0[2].to_le_bytes();
let msg = [pb, &x, &y, &r, &g, &b, &[0]].concat();
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);
}
}