on master help sign in
dap: support TCP clients
fabee03Dmitry Sharshakov
helix-dap/examples/dap-basic.rs+3−1
@@ -13,7 +13,9 @@ pub async fn main() -> Result<()> {
13 .apply()
13 .apply()
14 .expect("Failed to set up logging");
14 .expect("Failed to set up logging");
15
15
16− let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?;
16+ let client = Client::tcp("127.0.0.1:7777".parse::<std::net::SocketAddr>().unwrap(), 0).await;
17+ println!("create: {:?}", client);
18+ let mut client = client?;
17
19
18 println!("init: {:?}", client.initialize().await);
20 println!("init: {:?}", client.initialize().await);
19 println!("caps: {:#?}", client.capabilities());
21 println!("caps: {:#?}", client.capabilities());
helix-dap/src/client.rs+35−14
@@ -7,7 +7,8 @@ use serde_json::{from_value, to_value, Value};
7use std::process::Stdio;
7use std::process::Stdio;
8use std::sync::atomic::{AtomicU64, Ordering};
8use std::sync::atomic::{AtomicU64, Ordering};
9use tokio::{
9use tokio::{
10− io::{BufReader, BufWriter},
10+ io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
11+ net::TcpStream,
11 process::{Child, Command},
12 process::{Child, Command},
12 sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
13 sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
13};
14};
@@ -257,7 +258,7 @@ struct VariablesResponseBody {
257#[derive(Debug)]
258#[derive(Debug)]
258pub struct Client {
259pub struct Client {
259 id: usize,
260 id: usize,
260− _process: Child,
261+ _process: Option<Child>,
261 server_tx: UnboundedSender<Request>,
262 server_tx: UnboundedSender<Request>,
262 server_rx: UnboundedReceiver<Payload>,
263 server_rx: UnboundedReceiver<Payload>,
263 request_counter: AtomicU64,
264 request_counter: AtomicU64,
@@ -265,7 +266,33 @@ pub struct Client {
265}
266}
266
267
267impl Client {
268impl Client {
269+ pub fn streams(
270+ rx: Box<dyn AsyncBufRead + Unpin + Send>,
271+ tx: Box<dyn AsyncWrite + Unpin + Send>,
272+ id: usize,
273+ process: Option<Child>,
274+ ) -> Result<Self> {
275+ let (server_rx, server_tx) = Transport::start(rx, tx, id);
276+
277+ let client = Self {
278+ id,
279+ _process: process,
280+ server_tx,
281+ server_rx,
282+ request_counter: AtomicU64::new(0),
283+ capabilities: None,
284+ };
285+
286+ Ok(client)
287+ }
288+
289+ pub async fn tcp(addr: std::net::SocketAddr, id: usize) -> Result<Self> {
290+ let stream = TcpStream::connect(addr).await?;
291+ let (rx, tx) = stream.into_split();
292+ Self::streams(Box::new(BufReader::new(rx)), Box::new(tx), id, None)
293+ }
294+
268− pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result<Self> {
295+ pub fn stdio(cmd: &str, args: Vec<&str>, id: usize) -> Result<Self> {
269 let process = Command::new(cmd)
296 let process = Command::new(cmd)
270 .args(args)
297 .args(args)
271 .stdin(Stdio::piped())
298 .stdin(Stdio::piped())
@@ -280,18 +307,12 @@ impl Client {
280 let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
307 let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
281 let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
308 let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
282
309
283− let (server_rx, server_tx) = Transport::start(Box::new(reader), Box::new(writer), id);
310+ Self::streams(
284−
311+ Box::new(BufReader::new(reader)),
285− let client = Self {
312+ Box::new(writer),
286 id,
313 id,
287− _process: process,
314+ Some(process),
288− server_tx,
315+ )
289− server_rx,
290− request_counter: AtomicU64::new(0),
291− capabilities: None,
292− };
293−
294− Ok(client)
295 }
316 }
296
317
297 pub fn id(&self) -> usize {
318 pub fn id(&self) -> usize {