diff options
-rw-r--r-- | helix-dap/examples/dap-basic.rs | 4 | ||||
-rw-r--r-- | helix-dap/src/client.rs | 49 |
2 files changed, 38 insertions, 15 deletions
diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-basic.rs index 81decd1a..68e8ab54 100644 --- a/helix-dap/examples/dap-basic.rs +++ b/helix-dap/examples/dap-basic.rs @@ -13,7 +13,9 @@ pub async fn main() -> Result<()> { .apply() .expect("Failed to set up logging"); - let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?; + let client = Client::tcp("127.0.0.1:7777".parse::<std::net::SocketAddr>().unwrap(), 0).await; + println!("create: {:?}", client); + let mut client = client?; println!("init: {:?}", client.initialize().await); println!("caps: {:#?}", client.capabilities()); diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs index 08f7704e..cbb24d15 100644 --- a/helix-dap/src/client.rs +++ b/helix-dap/src/client.rs @@ -7,7 +7,8 @@ use serde_json::{from_value, to_value, Value}; use std::process::Stdio; use std::sync::atomic::{AtomicU64, Ordering}; use tokio::{ - io::{BufReader, BufWriter}, + io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter}, + net::TcpStream, process::{Child, Command}, sync::mpsc::{channel, UnboundedReceiver, UnboundedSender}, }; @@ -257,7 +258,7 @@ struct VariablesResponseBody { #[derive(Debug)] pub struct Client { id: usize, - _process: Child, + _process: Option<Child>, server_tx: UnboundedSender<Request>, server_rx: UnboundedReceiver<Payload>, request_counter: AtomicU64, @@ -265,7 +266,33 @@ pub struct Client { } impl Client { - pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result<Self> { + pub fn streams( + rx: Box<dyn AsyncBufRead + Unpin + Send>, + tx: Box<dyn AsyncWrite + Unpin + Send>, + id: usize, + process: Option<Child>, + ) -> Result<Self> { + let (server_rx, server_tx) = Transport::start(rx, tx, id); + + let client = Self { + id, + _process: process, + server_tx, + server_rx, + request_counter: AtomicU64::new(0), + capabilities: None, + }; + + Ok(client) + } + + pub async fn tcp(addr: std::net::SocketAddr, id: usize) -> Result<Self> { + let stream = TcpStream::connect(addr).await?; + let (rx, tx) = stream.into_split(); + Self::streams(Box::new(BufReader::new(rx)), Box::new(tx), id, None) + } + + pub fn stdio(cmd: &str, args: Vec<&str>, id: usize) -> Result<Self> { let process = Command::new(cmd) .args(args) .stdin(Stdio::piped()) @@ -280,18 +307,12 @@ impl Client { let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin")); let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout")); - let (server_rx, server_tx) = Transport::start(Box::new(reader), Box::new(writer), id); - - let client = Self { + Self::streams( + Box::new(BufReader::new(reader)), + Box::new(writer), id, - _process: process, - server_tx, - server_rx, - request_counter: AtomicU64::new(0), - capabilities: None, - }; - - Ok(client) + Some(process), + ) } pub fn id(&self) -> usize { |