Preparing terminal communication
This commit is contained in:
Generated
+21
@@ -29,6 +29,12 @@ version = "3.20.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.67"
|
||||
@@ -446,6 +452,16 @@ version = "1.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.118"
|
||||
@@ -463,8 +479,13 @@ version = "1.52.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ pulse-ui = { workspace = true }
|
||||
pulse-wire = { workspace = true }
|
||||
|
||||
chrono = "0.4.45"
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "net", "fs", "io-util"] }
|
||||
crossterm = { workspace = true }
|
||||
|
||||
[workspace]
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub mod terminal;
|
||||
|
||||
pub fn server_path() -> PathBuf {
|
||||
PathBuf::from("/tmp/pulse-engine.sock")
|
||||
}
|
||||
|
||||
pub trait PulseWire {
|
||||
fn to_com(&self) -> Vec<u8>;
|
||||
fn from_com(_com: &mut Vec<u8>) -> Self;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod terminal;
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use pulse_wire::PulseWire;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
use tokio::{
|
||||
io::AsyncReadExt,
|
||||
net::{
|
||||
UnixListener,
|
||||
unix::{OwnedReadHalf, OwnedWriteHalf},
|
||||
},
|
||||
};
|
||||
|
||||
pub struct TerminalServer {
|
||||
clients: Vec<OwnedWriteHalf>,
|
||||
}
|
||||
|
||||
impl TerminalServer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
clients: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> tokio::io::Result<()> {
|
||||
let path = pulse_wire::server_path();
|
||||
|
||||
if path.exists() {
|
||||
tokio::fs::remove_file(&path).await?;
|
||||
}
|
||||
|
||||
let listener = UnixListener::bind(&path)?;
|
||||
|
||||
println!("Terminal server listening on {:?}", path);
|
||||
|
||||
loop {
|
||||
let (stream, _) = listener.accept().await?;
|
||||
|
||||
let (reader, writer) = stream.into_split();
|
||||
|
||||
self.clients.push(writer);
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = Self::handle_client(reader).await {
|
||||
eprintln!("Terminal connection error: {err}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_client(mut reader: OwnedReadHalf) -> tokio::io::Result<()> {
|
||||
let input = tokio::spawn(async move {
|
||||
let mut buffer = [0u8; 2048];
|
||||
|
||||
loop {
|
||||
let size = reader.read(&mut buffer).await?;
|
||||
|
||||
if size == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
println!("Received {} bytes", size);
|
||||
}
|
||||
|
||||
Ok::<(), tokio::io::Error>(())
|
||||
});
|
||||
|
||||
let _ = tokio::try_join!(input)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn broadcast(&mut self) -> tokio::io::Result<()> {
|
||||
// for client in &mut self.clients {}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user