Initial commit (history reset)

This commit is contained in:
2025-07-09 07:18:46 -05:00
commit da8a4ce5a5
8 changed files with 366 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
use std::{
io::{self, Write},
os::unix::net::UnixStream,
path::Path,
process::Command,
};
use crate::read_line;
pub trait BinderArg {
fn from_u8_bind(b: u8) -> Self;
}
pub struct DynLib {
stream: UnixStream,
}
impl DynLib {
pub fn load(dyn_lib: &str) -> io::Result<Self> {
if !Path::new(&format!("/tmp/{dyn_lib}")).exists() {
let path = format!("./{dyn_lib}.dyn");
Command::new(path).spawn().unwrap();
std::thread::sleep(std::time::Duration::from_millis(100));
}
Ok(Self {
stream: UnixStream::connect(format!("/tmp/{dyn_lib}"))?,
})
}
pub fn call<T: BinderArg>(&mut self, fn_id: u8) -> Option<T> {
self.stream.write_all(&[fn_id, 255]).ok()?;
self.stream.flush().ok()?;
let msg = read_line(&mut self.stream)?;
Some(T::from_u8_bind(msg[0]))
}
}
impl BinderArg for () {
fn from_u8_bind(_: u8) -> Self {}
}
+75
View File
@@ -0,0 +1,75 @@
pub mod binder;
use std::{
fs,
io::{self, Read, Write},
os::unix::net::UnixListener,
};
pub trait Lib {
fn init(&mut self);
fn call(&mut self, id: u8);
}
pub fn run<L: Lib>(mut dyn_lib: L, dyn_lib_id: &str) -> io::Result<()> {
let path = &format!("/tmp/{dyn_lib_id}");
let _ = fs::remove_file(path);
let listener = UnixListener::bind(path)?;
println!("Listening on {path}");
dyn_lib.init();
for stream in listener.incoming() {
match stream {
Ok(mut sock) => loop {
match read_line(&mut sock) {
Some(id) => {
println!("Calling {id:?}");
dyn_lib.call(id[0]);
sock.write_all(&[0x52, 255])?;
sock.flush()?;
}
None => {
break;
}
}
},
Err(e) => {
eprintln!("Connection failed: {e}");
}
}
}
Ok(())
}
pub fn read_line<T: Read>(stream: &mut T) -> Option<Vec<u8>> {
let mut buffer = Vec::new();
loop {
let mut tmp = [0u8; 1];
stream.read_exact(&mut tmp).ok()?;
buffer.extend_from_slice(&tmp);
if tmp[0] == 255 {
break;
}
}
Some(buffer)
}
#[macro_export]
macro_rules! lib {
($(fn $id:literal() $(-> $t:ty)? $body:block)*) => {
fn call(&mut self, id: u8) {
match id {
$($id => $body)*
_ => {}
}
}
};
}
+32
View File
@@ -0,0 +1,32 @@
use std::io;
use dynll::{lib, Lib};
pub struct MyLib;
impl Lib for MyLib {
fn init(&mut self) {}
lib! {
fn 0x24() {}
}
}
const APP: &str = "Test-1.4.2";
fn main() -> io::Result<()> {
// dynll::run(MyLib, APP)?;
let mut test = dynll::binder::DynLib::load(APP)?;
println!("Connected");
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
test.call::<()>(0x23).unwrap();
println!("Done");
Ok(())
}