From fabee039835e18ec464164f60182689f7489cfe7 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Fri, 13 Aug 2021 20:13:27 +0300 Subject: [PATCH] dap: support TCP clients --- helix-dap/examples/dap-basic.rs | 4 ++- 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 81decd1a4..68e8ab545 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::().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 08f7704eb..cbb24d15d 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, server_tx: UnboundedSender, server_rx: UnboundedReceiver, request_counter: AtomicU64, @@ -265,7 +266,33 @@ pub struct Client { } impl Client { - pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result { + pub fn streams( + rx: Box, + tx: Box, + id: usize, + process: Option, + ) -> Result { + 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 { + 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 { 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 {