|
|
@ -1,5 +1,5 @@
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
transport::{Event, Payload, Request, Transport},
|
|
|
|
transport::{Payload, Request, Transport},
|
|
|
|
types::*,
|
|
|
|
types::*,
|
|
|
|
Result,
|
|
|
|
Result,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -9,19 +9,13 @@ use std::{
|
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
|
|
path::PathBuf,
|
|
|
|
path::PathBuf,
|
|
|
|
process::Stdio,
|
|
|
|
process::Stdio,
|
|
|
|
sync::{
|
|
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
|
|
|
|
Arc,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use tokio::{
|
|
|
|
use tokio::{
|
|
|
|
io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
|
|
|
|
io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
|
|
|
|
net::TcpStream,
|
|
|
|
net::TcpStream,
|
|
|
|
process::{Child, Command},
|
|
|
|
process::{Child, Command},
|
|
|
|
sync::{
|
|
|
|
sync::mpsc::{channel, unbounded_channel, UnboundedReceiver, UnboundedSender},
|
|
|
|
mpsc::{channel, Receiver, Sender, UnboundedReceiver, UnboundedSender},
|
|
|
|
|
|
|
|
Mutex,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
time,
|
|
|
|
time,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -32,8 +26,6 @@ pub struct Client {
|
|
|
|
server_tx: UnboundedSender<Request>,
|
|
|
|
server_tx: UnboundedSender<Request>,
|
|
|
|
request_counter: AtomicU64,
|
|
|
|
request_counter: AtomicU64,
|
|
|
|
capabilities: Option<DebuggerCapabilities>,
|
|
|
|
capabilities: Option<DebuggerCapabilities>,
|
|
|
|
awaited_events: Arc<Mutex<HashMap<String, Sender<Event>>>>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
pub breakpoints: HashMap<PathBuf, Vec<SourceBreakpoint>>,
|
|
|
|
pub breakpoints: HashMap<PathBuf, Vec<SourceBreakpoint>>,
|
|
|
|
// TODO: multiple threads support
|
|
|
|
// TODO: multiple threads support
|
|
|
@ -46,8 +38,9 @@ impl Client {
|
|
|
|
tx: Box<dyn AsyncWrite + Unpin + Send>,
|
|
|
|
tx: Box<dyn AsyncWrite + Unpin + Send>,
|
|
|
|
id: usize,
|
|
|
|
id: usize,
|
|
|
|
process: Option<Child>,
|
|
|
|
process: Option<Child>,
|
|
|
|
) -> Result<Self> {
|
|
|
|
) -> Result<(Self, UnboundedReceiver<Payload>)> {
|
|
|
|
let (server_rx, server_tx) = Transport::start(rx, tx, id);
|
|
|
|
let (server_rx, server_tx) = Transport::start(rx, tx, id);
|
|
|
|
|
|
|
|
let (client_rx, client_tx) = unbounded_channel();
|
|
|
|
|
|
|
|
|
|
|
|
let client = Self {
|
|
|
|
let client = Self {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
@ -55,24 +48,30 @@ impl Client {
|
|
|
|
server_tx,
|
|
|
|
server_tx,
|
|
|
|
request_counter: AtomicU64::new(0),
|
|
|
|
request_counter: AtomicU64::new(0),
|
|
|
|
capabilities: None,
|
|
|
|
capabilities: None,
|
|
|
|
awaited_events: Arc::new(Mutex::new(HashMap::default())),
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
breakpoints: HashMap::new(),
|
|
|
|
breakpoints: HashMap::new(),
|
|
|
|
stack_pointer: None,
|
|
|
|
stack_pointer: None,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
tokio::spawn(Self::recv(Arc::clone(&client.awaited_events), server_rx));
|
|
|
|
tokio::spawn(Self::recv(server_rx, client_rx));
|
|
|
|
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
Ok((client, client_tx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn tcp(addr: std::net::SocketAddr, id: usize) -> Result<Self> {
|
|
|
|
pub async fn tcp(
|
|
|
|
|
|
|
|
addr: std::net::SocketAddr,
|
|
|
|
|
|
|
|
id: usize,
|
|
|
|
|
|
|
|
) -> Result<(Self, UnboundedReceiver<Payload>)> {
|
|
|
|
let stream = TcpStream::connect(addr).await?;
|
|
|
|
let stream = TcpStream::connect(addr).await?;
|
|
|
|
let (rx, tx) = stream.into_split();
|
|
|
|
let (rx, tx) = stream.into_split();
|
|
|
|
Self::streams(Box::new(BufReader::new(rx)), Box::new(tx), id, None)
|
|
|
|
Self::streams(Box::new(BufReader::new(rx)), Box::new(tx), id, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn stdio(cmd: &str, args: Vec<&str>, id: usize) -> Result<Self> {
|
|
|
|
pub fn stdio(
|
|
|
|
|
|
|
|
cmd: &str,
|
|
|
|
|
|
|
|
args: Vec<&str>,
|
|
|
|
|
|
|
|
id: usize,
|
|
|
|
|
|
|
|
) -> Result<(Self, UnboundedReceiver<Payload>)> {
|
|
|
|
let process = Command::new(cmd)
|
|
|
|
let process = Command::new(cmd)
|
|
|
|
.args(args)
|
|
|
|
.args(args)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
@ -114,7 +113,7 @@ impl Client {
|
|
|
|
args: Vec<&str>,
|
|
|
|
args: Vec<&str>,
|
|
|
|
port_format: &str,
|
|
|
|
port_format: &str,
|
|
|
|
id: usize,
|
|
|
|
id: usize,
|
|
|
|
) -> Result<Self> {
|
|
|
|
) -> Result<(Self, UnboundedReceiver<Payload>)> {
|
|
|
|
let port = Self::get_port().await.unwrap();
|
|
|
|
let port = Self::get_port().await.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let process = Command::new(cmd)
|
|
|
|
let process = Command::new(cmd)
|
|
|
@ -145,41 +144,20 @@ impl Client {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn recv(
|
|
|
|
async fn recv(mut server_rx: UnboundedReceiver<Payload>, client_tx: UnboundedSender<Payload>) {
|
|
|
|
awaited_events: Arc<Mutex<HashMap<String, Sender<Event>>>>,
|
|
|
|
|
|
|
|
mut server_rx: UnboundedReceiver<Payload>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
while let Some(msg) = server_rx.recv().await {
|
|
|
|
while let Some(msg) = server_rx.recv().await {
|
|
|
|
match msg {
|
|
|
|
match msg {
|
|
|
|
Payload::Event(ev) => {
|
|
|
|
Payload::Event(ev) => {
|
|
|
|
let name = ev.event.clone();
|
|
|
|
client_tx.send(Payload::Event(ev)).expect("Failed to send");
|
|
|
|
let hashmap = awaited_events.lock().await;
|
|
|
|
|
|
|
|
let tx = hashmap.get(&name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match tx {
|
|
|
|
|
|
|
|
Some(tx) => match tx.send(ev).await {
|
|
|
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
|
|
|
Err(_) => error!(
|
|
|
|
|
|
|
|
"Tried sending event into a closed channel (name={:?})",
|
|
|
|
|
|
|
|
name
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
None => {
|
|
|
|
|
|
|
|
info!("unhandled event {}", name);
|
|
|
|
|
|
|
|
// client_tx.send(Payload::Event(ev)).expect("Failed to send");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Payload::Response(_) => unreachable!(),
|
|
|
|
Payload::Response(_) => unreachable!(),
|
|
|
|
Payload::Request(_) => todo!(),
|
|
|
|
Payload::Request(req) => {
|
|
|
|
|
|
|
|
client_tx
|
|
|
|
|
|
|
|
.send(Payload::Request(req))
|
|
|
|
|
|
|
|
.expect("Failed to send");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn listen_for_event(&self, name: String) -> Receiver<Event> {
|
|
|
|
|
|
|
|
let (rx, tx) = channel(1);
|
|
|
|
|
|
|
|
self.awaited_events.lock().await.insert(name.clone(), rx);
|
|
|
|
|
|
|
|
tx
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
@ -248,8 +226,6 @@ impl Client {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
|
|
|
|
pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
|
|
|
|
// TODO: buffer these until initialized arrives
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response = self.request::<requests::Launch>(args).await?;
|
|
|
|
let response = self.request::<requests::Launch>(args).await?;
|
|
|
|
log::error!("launch response {}", response);
|
|
|
|
log::error!("launch response {}", response);
|
|
|
|
|
|
|
|
|
|
|
@ -257,8 +233,6 @@ impl Client {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
|
|
|
|
pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
|
|
|
|
// TODO: buffer these until initialized arrives
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response = self.request::<requests::Attach>(args).await?;
|
|
|
|
let response = self.request::<requests::Attach>(args).await?;
|
|
|
|
log::error!("attach response {}", response);
|
|
|
|
log::error!("attach response {}", response);
|
|
|
|
|
|
|
|
|
|
|
|