@@ -2,15 +2,22 @@ use crate::{
2 transport::{Event, Payload, Request, Response, Transport},
2 transport::{Event, Payload, Request, Response, Transport},
5+use log::{error, info};
5use serde::{Deserialize, Serialize};
6use serde::{Deserialize, Serialize};
6use serde_json::{from_value, to_value, Value};
7use serde_json::{from_value, to_value, Value};
7−use std::process::Stdio;
8+use std::sync::{
8−use std::sync::atomic::{AtomicU64, Ordering};
9+ atomic::{AtomicU64, Ordering},
12+use std::{collections::HashMap, process::Stdio};
9use tokio::{
13use tokio::{
10 io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
14 io::{AsyncBufRead, AsyncWrite, BufReader, BufWriter},
11 net::TcpStream,
15 net::TcpStream,
12 process::{Child, Command},
16 process::{Child, Command},
13− sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
18+ mpsc::{channel, Receiver, Sender, UnboundedReceiver, UnboundedSender},
16#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
23#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@@ -252,9 +259,9 @@ pub struct Client {
252 id: usize,
259 id: usize,
253 _process: Option<Child>,
260 _process: Option<Child>,
254 server_tx: UnboundedSender<Request>,
261 server_tx: UnboundedSender<Request>,
255− server_rx: UnboundedReceiver<Payload>,
256 request_counter: AtomicU64,
262 request_counter: AtomicU64,
257 capabilities: Option<DebuggerCapabilities>,
263 capabilities: Option<DebuggerCapabilities>,
264+ awaited_events: Arc<Mutex<HashMap<String, Sender<Event>>>>,
260impl Client {
267impl Client {
@@ -270,14 +277,52 @@ impl Client {
271 _process: process,
278 _process: process,
272 server_tx,
279 server_tx,
274 request_counter: AtomicU64::new(0),
280 request_counter: AtomicU64::new(0),
275 capabilities: None,
281 capabilities: None,
282+ awaited_events: Arc::new(Mutex::new(HashMap::default())),
285+ tokio::spawn(Self::recv(Arc::clone(&client.awaited_events), server_rx));
278 Ok(client)
287 Ok(client)
291+ awaited_events: Arc<Mutex<HashMap<String, Sender<Event>>>>,
292+ mut server_rx: UnboundedReceiver<Payload>,
294+ while let Some(msg) = server_rx.recv().await {
296+ Payload::Event(ev) => {
297+ let name = ev.event.clone();
298+ let tx = awaited_events.lock().await.remove(&name);
301+ Some(tx) => match tx.send(ev).await {
304+ "Tried sending event into a closed channel (name={:?})",
309+ info!("unhandled event");
310+ // client_tx.send(Payload::Event(ev)).expect("Failed to send");
314+ Payload::Response(_) => unreachable!(),
315+ Payload::Request(_) => todo!(),
320+ pub async fn listen_for_event(&self, name: String) -> Receiver<Event> {
321+ let (rx, tx) = channel(1);
322+ self.awaited_events.lock().await.insert(name.clone(), rx);
281 pub async fn tcp(addr: std::net::SocketAddr, id: usize) -> Result<Self> {
326 pub async fn tcp(addr: std::net::SocketAddr, id: usize) -> Result<Self> {
282 let stream = TcpStream::connect(addr).await?;
327 let stream = TcpStream::connect(addr).await?;
283 let (rx, tx) = stream.into_split();
328 let (rx, tx) = stream.into_split();
@@ -373,45 +418,25 @@ impl Client {
375 pub async fn launch(&mut self, args: impl Serialize) -> Result<()> {
420 pub async fn launch(&mut self, args: impl Serialize) -> Result<()> {
421+ let mut initialized = self.listen_for_event("initialized".to_owned()).await;
376 self.request("launch".to_owned(), to_value(args).ok())
423 self.request("launch".to_owned(), to_value(args).ok())
382− .await
426+ initialized.recv().await;
383− .expect("Expected initialized event")
427+
385− Payload::Event(Event { event, .. }) => {
386− if event == *"initialized" {
392− _ => unreachable!(),
396 pub async fn attach(&mut self, args: impl Serialize) -> Result<()> {
431 pub async fn attach(&mut self, args: impl Serialize) -> Result<()> {
432+ let mut initialized = self.listen_for_event("initialized".to_owned()).await;
397 self.request("attach".to_owned(), to_value(args).ok())
434 self.request("attach".to_owned(), to_value(args).ok())
403− .await
437+ initialized.recv().await;
404− .expect("Expected initialized event")
438+
406− Payload::Event(Event { event, .. }) => {
407− if event == *"initialized" {
413− _ => unreachable!(),
417 pub async fn set_breakpoints(
442 pub async fn set_breakpoints(
@@ -447,19 +472,6 @@ impl Client {
450− pub async fn wait_for_stopped(&mut self) -> Result<()> {
451− match self.server_rx.recv().await.expect("Expected stopped event") {
452− Payload::Event(Event { event, .. }) => {
453− if event == *"stopped" {
459− _ => unreachable!(),
463 pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> {
475 pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> {
464 let args = ContinueArguments { thread_id };
476 let args = ContinueArguments { thread_id };