DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Git (a651bad838)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#[macro_use]
extern crate log;

use std::thread;
use ws::{self, Builder, CloseCode, Handler, Handshake};

enum Message {
    ShutdownServer,
}

pub struct Sender(crossbeam_channel::Sender<Message>);

struct Connection {
    sender: ws::Sender,
}

impl Handler for Connection {
    fn on_open(&mut self, _: Handshake) -> ws::Result<()> {
        debug!("Connection opened.");
        Ok(())
    }

    fn on_close(&mut self, _: CloseCode, _: &str) {
        debug!("Connection closed.");
    }

    fn on_message(&mut self, message: ws::Message) -> ws::Result<()> {
        self.sender.send(message)
    }
}

pub fn start_server(port: u16) -> Sender {
    debug!("Starting server.");
    let (sender, receiver) = crossbeam_channel::unbounded();
    thread::Builder::new()
        .name("debugger".to_owned())
        .spawn(move || {
            let socket = Builder::new()
                .build(|sender: ws::Sender| Connection { sender: sender })
                .unwrap();
            let sender = socket.broadcaster();
            thread::Builder::new()
                .name("debugger-websocket".to_owned())
                .spawn(move || {
                    socket.listen(("127.0.0.1", port)).unwrap();
                })
                .expect("Thread spawning failed");
            while let Ok(message) = receiver.recv() {
                match message {
                    Message::ShutdownServer => {
                        break;
                    },
                }
            }
            sender.shutdown().unwrap();
        })
        .expect("Thread spawning failed");
    Sender(sender)
}

pub fn shutdown_server(sender: &Sender) {
    debug!("Shutting down server.");
    let &Sender(ref sender) = sender;
    if let Err(_) = sender.send(Message::ShutdownServer) {
        warn!("Failed to shut down server.");
    }
}