dap: support TCP clients

imgbot
Dmitry Sharshakov 3 years ago committed by Blaž Hrastnik
parent 26a55dcefd
commit fabee03983

@ -13,7 +13,9 @@ pub async fn main() -> Result<()> {
.apply() .apply()
.expect("Failed to set up logging"); .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!("init: {:?}", client.initialize().await);
println!("caps: {:#?}", client.capabilities()); println!("caps: {:#?}", client.capabilities());

@ -7,7 +7,8 @@ use serde_json::{from_value, to_value, Value};
use std::process::Stdio; use std::process::Stdio;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use tokio::{ use tokio::{
io::{BufReader, BufWriter}, io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
net::TcpStream,
process::{Child, Command}, process::{Child, Command},
sync::mpsc::{channel, UnboundedReceiver, UnboundedSender}, sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
}; };
@ -257,7 +258,7 @@ struct VariablesResponseBody {
#[derive(Debug)] #[derive(Debug)]
pub struct Client { pub struct Client {
id: usize, id: usize,
_process: Child, _process: Option<Child>,
server_tx: UnboundedSender<Request>, server_tx: UnboundedSender<Request>,
server_rx: UnboundedReceiver<Payload>, server_rx: UnboundedReceiver<Payload>,
request_counter: AtomicU64, request_counter: AtomicU64,
@ -265,7 +266,33 @@ pub struct Client {
} }
impl 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) let process = Command::new(cmd)
.args(args) .args(args)
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -280,18 +307,12 @@ impl Client {
let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin")); 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 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); Self::streams(
Box::new(BufReader::new(reader)),
let client = Self { Box::new(writer),
id, id,
_process: process, Some(process),
server_tx, )
server_rx,
request_counter: AtomicU64::new(0),
capabilities: None,
};
Ok(client)
} }
pub fn id(&self) -> usize { pub fn id(&self) -> usize {

Loading…
Cancel
Save