1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use std::io::Write;
use std::mem::size_of;
use std::net::{TcpListener, TcpStream};
use std::process::exit;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{io, thread};

#[cfg(feature = "insecure-remote")]
const BIND_ADDR: &str = "0.0.0.0";
#[cfg(not(feature = "insecure-remote"))]
const BIND_ADDR: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 13579;

const CHAN_MAX_MESSAGES: usize = 32;
const LEN_FIELD_SIZE: usize = size_of::<u32>();
const WIRE_PROTOCOL_VERSION: u8 = 1;
const THREAD_ID_PREFIX: &str = "ThreadId";

static REMOTE_DEBUG: Mutex<Option<RemoteDebug>> = Mutex::new(None);

// *** msg / vals macros ***

/// Send a debug message to the remote viewer
///
/// ```dontrun
/// // Default port
/// let world = "world!";
/// rdbg::msg!("Hello {}", world);
/// flush();
///
/// // Custom port
/// let debug = rdbg::port(5000);
/// rdbg::msg!(&debug, ["Hello {}", world]);
/// debug.flush();
/// ```
#[cfg(feature = "enabled")]
#[macro_export]
macro_rules! msg {
    ($port:expr, [ $($arg:tt)* ]) => {
        $port.send_message(file!(), line!(), $crate::MsgPayload::Message(
            std::fmt::format(format_args!($($arg)*))
        ))
    };

    ($($arg:tt)*) => {
        $crate::msg!($crate::RemoteDebug::default(), [ $($arg)* ])
    };
}

#[cfg(not(feature = "enabled"))]
#[macro_export]
macro_rules! msg {
    ($port:expr, [ $($arg:tt)* ]) => {};
    ($($arg:tt)*) => {};
}

/// Send a debug message to the remote viewer (with flush)
///
/// ```dontrun
/// // Default port
/// let world = "world!";
/// rdbg::msgf!("Hello {}", world);
/// flush();
///
/// // Custom port
/// let debug = rdbg::port(5000);
/// rdbg::msgf!(&debug, ["Hello {}", world]);
/// debug.flush();
/// ```
#[cfg(feature = "enabled")]
#[macro_export]
macro_rules! msgf {
    ($port:expr, [ $($arg:tt)* ]) => {{
        $port.send_message(file!(), line!(), $crate::MsgPayload::Message(
            std::fmt::format(format_args!($($arg)*))
        ));
        $port.flush();
    }};

    ($($arg:tt)*) => {
        $crate::msgf!($crate::RemoteDebug::default(), [ $($arg)* ])
    };
}

#[cfg(not(feature = "enabled"))]
#[macro_export]
macro_rules! msgf {
    ($port:expr, [ $($arg:tt)* ]) => {};
    ($($arg:tt)*) => {};
}

/// Send debug expression name/value pairs to the remote viewer
///
/// ```dontrun
/// // Default port
/// let world = "world!";
/// rdbg::vals!(world, 1 + 1);
/// flush();
///
/// // Custom port
/// let debug = rdbg::port(5000);
/// rdbg::vals!(&debug, [world, 1 + 1]);
/// debug.flush();
/// ```
#[cfg(feature = "enabled")]
#[macro_export]
macro_rules! vals {
    ($port:expr, [ $($value:expr),+ $(,)? ]) => {
        $port.send_message(file!(), line!(), $crate::MsgPayload::Values(vec![$((
            match $value {
                val => {
                    (stringify!($value), format!("{:#?}", &val))
                }
            }
        )),+]))
    };

    ($($value:expr),+ $(,)?) => {
        $crate::vals!($crate::RemoteDebug::default(), [ $($value),+ ])
    };
}

#[cfg(not(feature = "enabled"))]
#[macro_export]
macro_rules! vals {
    ($port:expr, [ $($value:expr),+ $(,)? ]) => {};
    ($($value:expr),+ $(,)?) => {};
}

/// Send debug expression name/value pairs to the remote viewer (with flush)
///
/// ```dontrun
/// // Default port
/// let world = "world!";
/// rdbg::valsf!(world, 1 + 1);
/// flush();
///
/// // Custom port
/// let debug = rdbg::port(5000);
/// rdbg::valsf!(&debug, [world, 1 + 1]);
/// debug.flush();
/// ```
#[cfg(feature = "enabled")]
#[macro_export]
macro_rules! valsf {
    ($port:expr, [ $($value:expr),+ $(,)? ]) => {{
        $port.send_message(file!(), line!(), $crate::MsgPayload::Values(vec![$((
            match $value {
                val => {
                    (stringify!($value), format!("{:#?}", &val))
                }
            }
        )),+]));
        $port.flush();
    }};

    ($($value:expr),+ $(,)?) => {
        $crate::valsf!($crate::RemoteDebug::default(), [ $($value),+ ])
    };
}

#[cfg(not(feature = "enabled"))]
#[macro_export]
macro_rules! valsf {
    ($port:expr, [ $($value:expr),+ $(,)? ]) => {};
    ($($value:expr),+ $(,)?) => {};
}

// *** Message related functions ***

fn current_thread() -> String {
    // This has to be made into a string as there doesn't seem to be a way to get any
    // sort of integral version out of it (at least not in stable)
    parse_thread_id(format!("{:?}", thread::current().id()))
}

fn parse_thread_id(thread_id: String) -> String {
    // We optimistically assume the current format, but if it isn't just return
    // the initial string as-is
    let mut split = thread_id.split(&['(', ')']);
    if split.next() == Some(THREAD_ID_PREFIX) {
        match split.next() {
            Some(thread_id) if split.next() == Some("") => thread_id.to_string(),
            _ => thread_id,
        }
    } else {
        thread_id
    }
}

#[cfg(test)]
mod tests {
    use crate::parse_thread_id;

    #[test]
    fn parse_thread_current() {
        assert_eq!(parse_thread_id("ThreadId(1)".to_string()), "1".to_string());
    }

    #[test]
    fn parse_thread_changed() {
        assert_eq!(
            parse_thread_id("Thread(1)".to_string()),
            "Thread(1)".to_string()
        );
    }
}

fn current_time() -> u64 {
    // This can only really fail if time goes to before the epoch, which likely isn't possible
    // on today's system clocks
    // While this returns a u128, u64 ought to be large enough to hold all ms since the epoch
    // for our lifetimes
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis() as u64
}

#[inline]
fn required_str_capacity(s: &str) -> usize {
    s.as_bytes().len() + LEN_FIELD_SIZE
}

// *** MsgPayloadVal ***

#[repr(u8)]
enum MsgPayloadVal {
    Message = 1,
    Values = 2,
}

// *** MsgPayload ***

#[doc(hidden)]
#[derive(Clone, Debug)]
pub enum MsgPayload {
    // A formatted string
    Message(String),
    // A list of name/value pairs from expressions
    Values(Vec<(&'static str, String)>),
}

impl MsgPayload {
    fn required_capacity(&self) -> usize {
        (match self {
            MsgPayload::Message(msg) => required_str_capacity(msg),
            //  We start with 4 because we start by sending number of vec elements
            MsgPayload::Values(values) => {
                values.iter().fold(LEN_FIELD_SIZE, |acc, (name, value)| {
                    acc + required_str_capacity(name) + required_str_capacity(value)
                })
            }
        }) + size_of::<MsgPayloadVal>()
    }
}

// *** Message ***

#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct Message(Vec<u8>);

impl Message {
    pub fn new(filename: &str, line: u32, payload: MsgPayload) -> Self {
        let time = current_time();
        let thread_id = current_thread();

        // Msg length + time + thread id + filename len + line # + payload len
        let len = LEN_FIELD_SIZE // msg len
            + size_of::<u64>() // time
            + required_str_capacity(&thread_id)
            + required_str_capacity(filename)
            + size_of::<u32>() // line #
            + payload.required_capacity();

        let mut msg = Self(Vec::with_capacity(len));
        msg.write_u32(len as u32);
        msg.write_u64(time);
        msg.write_str(&thread_id);
        msg.write_str(filename);
        msg.write_u32(line);
        msg.write_payload(&payload);

        debug_assert_eq!(msg.0.len(), len, "Bad message length");
        msg
    }

    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }

    #[inline]
    fn write_str(&mut self, s: &str) {
        self.write_u32(s.len() as u32);
        self.0.extend(s.as_bytes());
    }

    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.0.extend(i.to_be_bytes());
    }

    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.0.extend(i.to_be_bytes());
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.0.extend(i.to_be_bytes());
    }

    fn write_payload(&mut self, payload: &MsgPayload) {
        match payload {
            MsgPayload::Message(msg) => {
                self.write_u8(MsgPayloadVal::Message as u8);
                self.write_str(msg);
            }
            MsgPayload::Values(values) => {
                self.write_u8(MsgPayloadVal::Values as u8);
                self.write_u32(values.len() as u32);

                for (name, value) in values {
                    self.write_str(name);
                    self.write_str(value);
                }
            }
        }
    }
}

// *** Event ***

enum Event {
    NewMessage(Message),
    Flush,
}

// *** Flushed ***

#[derive(Clone)]
struct Flushed(Arc<(Condvar, Mutex<bool>)>);

impl Flushed {
    #[inline]
    fn new() -> Self {
        Self(Arc::new((Default::default(), Mutex::new(false))))
    }

    fn flushed(&self) {
        let (var, lock) = &*self.0;
        // Panic if mutex is poisoned
        let mut flushed = lock.lock().unwrap();
        *flushed = true;
        var.notify_one();
    }

    fn flush_and_wait(&self, sender: &SyncSender<Event>) {
        match sender.send(Event::Flush) {
            Ok(_) => {
                let (var, lock) = &*self.0;
                // Panic if mutex is poisoned
                let mut flushed = lock.lock().unwrap();

                while !*flushed {
                    // Panic if mutex is poisoned
                    flushed = var.wait(flushed).unwrap();
                }

                // Reset before releasing lock
                *flushed = false;
            }
            Err(err) => {
                eprintln!("Unable to send quit event: {err}");
            }
        }
    }
}

// *** RemoteDebug ***

#[doc(hidden)]
#[derive(Clone)]
pub struct RemoteDebug {
    sender: SyncSender<Event>,
    flush: Flushed,
}

impl RemoteDebug {
    fn from_sender(sender: SyncSender<Event>) -> Self {
        Self {
            sender,
            flush: Flushed::new(),
        }
    }

    fn from_port(port: u16) -> Self {
        // Panic if mutex is poisoned
        let remote_debug = &mut *REMOTE_DEBUG.lock().unwrap();

        // If our global var is already inited, just return it otherwise do one time thread creation
        match remote_debug {
            Some(remote_debug) => remote_debug.clone(),
            None => {
                let debug = handle_connections(port);
                *remote_debug = Some(debug.clone());
                debug
            }
        }
    }

    pub fn send_message(&self, filename: &str, line: u32, payload: MsgPayload) {
        if let Err(err) = self
            .sender
            .send(Event::NewMessage(Message::new(filename, line, payload)))
        {
            eprintln!("Unable to send new message event: {err}");
        }
    }

    #[inline]
    pub fn flush(&self) {
        self.flush.flush_and_wait(&self.sender);
    }
}

impl Default for RemoteDebug {
    fn default() -> Self {
        Self::from_port(DEFAULT_PORT)
    }
}

// NOTE: This function isn't a part of RemoteDebug simply to make it a few less key strokes for the
// user in case they want to include this on every macro invocation.
/// Specify a custom port for the TCP socket to listen on when using the [msg] and [vals] macros.
///
/// NOTE: The first time this function or [msg] or [vals] macros are processed determines the port.
/// Once it is established it will not change no matter what `port` value is used.
///
/// ```dontrun
/// let world = "world!";
/// rdbg::msg!(rdbg::port(5000), ["Hello {}", world]);
///
/// rdbg::vals!(rdbg::port(5000), [world, 1 + 1]);
/// rdbg::flush();
/// ```
#[cfg(feature = "enabled")]
#[inline]
pub fn port(port: u16) -> RemoteDebug {
    RemoteDebug::from_port(port)
}

#[cfg(not(feature = "enabled"))]
#[inline]
pub fn port(_port: u16) {}

/// Flush msg/val queue for default port
///
/// NOTE: The first time this function or [msg] or [vals] macros are processed determines the port.
/// Once it is established it will not change even if custom port macros or functions are called.
///
/// ```dontrun
/// let world = "world!";
/// rdbg::msg!(rdbg::port(5000), ["Hello {}", world]);
///
/// rdbg::vals!(rdbg::port(5000), [world, 1 + 1]);
/// rdbg::flush();
/// ```
#[cfg(feature = "enabled")]
#[inline]
pub fn flush() {
    RemoteDebug::default().flush();
}

#[cfg(not(feature = "enabled"))]
#[inline]
pub fn flush() {}

// *** Connection related functions ***

fn handle_connections(port: u16) -> RemoteDebug {
    let (sender, receiver) = sync_channel::<Event>(CHAN_MAX_MESSAGES);
    let debug = RemoteDebug::from_sender(sender);
    let debug_clone = debug.clone();

    thread::spawn(move || {
        let mut curr_msg = None;

        loop {
            // We have no good way to report errors, so just unwrap and panic, if needed
            // (likely due to 'address in use' or 'permission denied', so we want to know about that
            // not mysteriously just not receive messages)
            match TcpListener::bind((BIND_ADDR, port)) {
                Ok(listener) => {
                    // Per docs, 'incoming' will always return an entry
                    if let Ok(mut stream) = listener.incoming().next().unwrap() {
                        if process_stream(&mut stream, &receiver, &mut curr_msg, &debug_clone) {
                            // Quit signalled - we are done
                            break;
                        }
                    }
                }
                Err(err) => {
                    eprintln!("Unable to listen on {BIND_ADDR}:{port}: {err}");
                    // We exit instead of panic because this is a separate thread. We want it very
                    // obvious if for some reason it can't listen on this port so we exit immediately
                    exit(1);
                }
            }
        }
    });

    debug
}

fn process_stream(
    stream: &mut TcpStream,
    receiver: &Receiver<Event>,
    curr_msg: &mut Option<Message>,
    debug: &RemoteDebug,
) -> bool {
    // If we hit an error writing out the version just return since we have no good way to report
    if write_to_stream(&WIRE_PROTOCOL_VERSION.to_be_bytes(), stream).is_err() {
        return false;
    }

    loop {
        // If we were interrupted sending last message then resend otherwise wait for a new message
        let msg = match &curr_msg {
            Some(msg) => msg,
            None => {
                // We have no good way to report errors, so just unwrap and panic, if needed
                // (this is likely impossible since our SyncSender is in a global var)
                match receiver.recv().unwrap() {
                    Event::NewMessage(msg) => {
                        *curr_msg = Some(msg);
                        // Can't fail, stored above
                        curr_msg.as_ref().unwrap()
                    }
                    Event::Flush => {
                        debug.flush.flushed();
                        continue;
                    }
                }
            }
        };

        match write_to_stream(msg.as_slice(), stream) {
            Ok(_) => {
                // Success, don't resend this message again
                *curr_msg = None;
            }
            Err(_) => {
                // Preserve current message and resend on next connection
                break;
            }
        }
    }

    false
}

fn write_to_stream(buffer: &[u8], stream: &mut TcpStream) -> io::Result<()> {
    let mut index = 0;

    // Keep writing until everything in the buffer has been written or we get an error
    while index < buffer.len() {
        match stream.write(&buffer[index..]) {
            Ok(wrote) => {
                index += wrote;
            }
            Err(err) => {
                return Err(err);
            }
        }
    }

    Ok(())
}