Skip to content

Commit

Permalink
Wip configure host for tcp debug adapter
Browse files Browse the repository at this point in the history
Co-Authored-By: Anthony Eid <[email protected]>
  • Loading branch information
RemcoSmitsDev and Anthony-Eid committed Jul 15, 2024
1 parent ffa0609 commit d9461c3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
39 changes: 27 additions & 12 deletions crates/dap/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use parking_lot::{Mutex, MutexGuard};
use serde_json::Value;
use smol::{
io::BufReader,
net::TcpStream,
net::{TcpListener, TcpStream},
process::{self, Child},
};
use std::{
collections::HashMap,
net::{Ipv4Addr, SocketAddrV4},
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
path::PathBuf,
process::Stdio,
sync::{
Expand All @@ -34,7 +34,7 @@ use std::{
},
time::Duration,
};
use task::{DebugAdapterConfig, DebugConnectionType, DebugRequestType};
use task::{DebugAdapterConfig, DebugConnectionType, DebugRequestType, TCPHost};
use util::ResultExt;

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -78,9 +78,9 @@ impl DebugAdapterClient {
project_path: PathBuf,
cx: &mut AsyncAppContext,
) -> Result<Self> {
match config.connection {
DebugConnectionType::TCP => {
Self::create_tcp_client(id, config, command, args, project_path, cx).await
match config.connection.clone() {
DebugConnectionType::TCP(host) => {
Self::create_tcp_client(id, config, host, command, args, project_path, cx).await
}
DebugConnectionType::STDIO => {
Self::create_stdio_client(id, config, command, args, project_path, cx).await
Expand All @@ -91,11 +91,17 @@ impl DebugAdapterClient {
async fn create_tcp_client(
id: DebugAdapterClientId,
config: DebugAdapterConfig,
host: TCPHost,
command: &str,
args: Vec<&str>,
project_path: PathBuf,
cx: &mut AsyncAppContext,
) -> Result<Self> {
let mut port = host.port;
if port.is_none() {
port = Self::get_port().await;
}

let mut command = process::Command::new(command);
command
.current_dir(project_path)
Expand All @@ -114,7 +120,10 @@ impl DebugAdapterClient {
.timer(Duration::from_millis(1000))
.await;

let address = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), config.port);
let address = SocketAddrV4::new(
host.host.unwrap_or_else(|| Ipv4Addr::new(127, 0, 0, 1)),
port.unwrap(),
);

let (rx, tx) = TcpStream::connect(address).await?.split();

Expand All @@ -129,6 +138,17 @@ impl DebugAdapterClient {
)
}

async fn get_port() -> Option<u16> {
Some(
TcpListener::bind(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0))
.await
.ok()?
.local_addr()
.ok()?
.port(),
)
}

async fn create_stdio_client(
id: DebugAdapterClientId,
config: DebugAdapterConfig,
Expand All @@ -150,11 +170,6 @@ impl DebugAdapterClient {
.spawn()
.with_context(|| "failed to spawn command.")?;

// give the adapter some time to start std
cx.background_executor()
.timer(Duration::from_millis(1000))
.await;

let stdin = process
.stdin
.take()
Expand Down
2 changes: 1 addition & 1 deletion crates/debugger_ui/src/debugger_panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use dap::client::{self, DebugAdapterClientId, ThreadState, ThreadStatus};
use dap::client::{DebugAdapterClientId, ThreadState, ThreadStatus};
use dap::requests::{Disconnect, Scopes, StackTrace, Variables};
use dap::{client::DebugAdapterClient, transport::Events};
use dap::{
Expand Down
4 changes: 2 additions & 2 deletions crates/task/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::str::FromStr;
use std::{borrow::Cow, path::Path};

pub use task_template::{
DebugAdapterConfig, DebugConnectionType, DebugRequestType, RevealStrategy, TaskTemplate,
TaskTemplates, TaskType,
DebugAdapterConfig, DebugConnectionType, DebugRequestType, RevealStrategy, TCPHost,
TaskTemplate, TaskTemplates, TaskType,
};
pub use vscode_format::VsCodeTaskFile;

Expand Down
27 changes: 20 additions & 7 deletions crates/task/src/task_template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{net::Ipv4Addr, path::PathBuf};

use anyhow::{bail, Context};
use collections::{HashMap, HashSet};
Expand Down Expand Up @@ -70,16 +70,31 @@ pub enum TaskType {
}

/// Represents the type of the debugger adapter connection
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum DebugConnectionType {
/// Connect to the debug adapter via TCP
#[default]
TCP,
#[serde(flatten)]
TCP(TCPHost),
/// Connect to the debug adapter via STDIO
STDIO,
}

impl Default for DebugConnectionType {
fn default() -> Self {
DebugConnectionType::TCP(TCPHost::default())
}
}

/// Represents the host information of the debug adapter
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
pub struct TCPHost {
/// The port that the debug adapter is listening on
pub port: Option<u16>,
/// The host that the debug adapter is listening too
pub host: Option<Ipv4Addr>,
}

/// Represents the type that will determine which request to call on the debug adapter
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
Expand All @@ -99,10 +114,8 @@ pub struct DebugAdapterConfig {
/// that will be send with the `initialize` request
pub id: String,
/// The type of connection the adapter should use
#[serde(default)]
#[serde(default, flatten)]
pub connection: DebugConnectionType,
/// The port that the debug adapter is listening on
pub port: u16,
/// The type of request that should be called on the debug adapter
#[serde(default)]
pub request: DebugRequestType,
Expand Down

0 comments on commit d9461c3

Please sign in to comment.