mirror of
https://github.com/Astatin3/unshell-nodes-rs.git
synced 2026-06-09 00:28:00 -06:00
Move files around, add UI
This commit is contained in:
+2
-1
@@ -4,5 +4,6 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.140"
|
||||
# libc = "0.2.172"
|
||||
unshell-rs = { path = "../" }
|
||||
unshell-rs-lib = { path = "../unshell-rs-lib" }
|
||||
|
||||
+55
-11
@@ -2,20 +2,64 @@
|
||||
// mod execute;
|
||||
|
||||
use std::error::Error;
|
||||
use unshell_rs::{
|
||||
networkers::{TCPClient, TCPConnection},
|
||||
payload::run_client,
|
||||
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
// /// Pipe streams are blocking, we need separate threads to monitor them without blocking the primary thread.
|
||||
// fn child_stream_to_vec<R>(mut stream: R) -> Arc<Mutex<Vec<u8>>>
|
||||
// where
|
||||
// R: Read + Send + 'static,
|
||||
// {
|
||||
// let out = Arc::new(Mutex::new(Vec::new()));
|
||||
// let vec = out.clone();
|
||||
use unshell_rs_lib::{
|
||||
networkers::{ClientTrait, Connection, TCPClient, TCPConnection},
|
||||
packets::Packet,
|
||||
};
|
||||
|
||||
// }
|
||||
// Generic client function
|
||||
pub fn run_client<C, Cl>(address: &str) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
Cl: ClientTrait<C>,
|
||||
C: Connection + 'static,
|
||||
Cl::Error: std::error::Error + 'static,
|
||||
C::Error: std::error::Error + 'static,
|
||||
{
|
||||
let recv_conn = Arc::new(Mutex::new(Cl::connect(address)?));
|
||||
let transmit_vec: Arc<Mutex<Vec<Packet>>> = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
let transmit_conn = Arc::clone(&recv_conn);
|
||||
let transmit_vec_clone = Arc::clone(&transmit_vec);
|
||||
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
let mut transmit_vec_lock = transmit_vec.lock().unwrap();
|
||||
if transmit_vec_lock.len() > 0 {
|
||||
let mut conn_lock = recv_conn.lock().unwrap();
|
||||
if let Ok(json) = serde_json::to_string(&transmit_vec_lock.pop().unwrap()) {
|
||||
conn_lock.write(&json).expect("Failed to send packet!");
|
||||
}
|
||||
} else {
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
let mut conn_lock = transmit_conn.lock().unwrap();
|
||||
let data = conn_lock.read();
|
||||
drop(conn_lock);
|
||||
match data {
|
||||
Ok(data_json) => {
|
||||
if data_json.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let packet = serde_json::from_str::<Packet>(data_json.as_str());
|
||||
println!("{:?}", packet);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading, {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
run_client::<TCPConnection, TCPClient>("127.0.0.1:3000")?;
|
||||
|
||||
Reference in New Issue
Block a user