aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/network.rs
blob: 7950dc75292101ceab007341de011cc6843fd488 (plain) (blame)
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
pub mod tcp;
pub mod udp;

use futures_util::FutureExt;
use log::*;
use std::{future::Future, net::SocketAddr};
use tokio::{join, select, sync::{oneshot, watch}};

use crate::state::StatePhase;

#[derive(Clone, Debug)]
pub struct ConnectionInfo {
    socket_addr: SocketAddr,
    hostname: String,
    accept_invalid_cert: bool,
}

impl ConnectionInfo {
    pub fn new(socket_addr: SocketAddr, hostname: String, accept_invalid_cert: bool) -> Self {
        Self {
            socket_addr,
            hostname,
            accept_invalid_cert,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum VoiceStreamType {
    TCP,
    UDP,
}

async fn run_until<F>(
    phase_checker: impl Fn(StatePhase) -> bool,
    fut: F,
    mut phase_watcher: watch::Receiver<StatePhase>,
) where
    F: Future<Output = ()>,
{
    let (tx, rx) = oneshot::channel();
    let phase_transition_block = async {
        loop {
            phase_watcher.changed().await.unwrap();
            if phase_checker(*phase_watcher.borrow()) {
                break;
            }
        }
        if tx.send(true).is_err() {
            warn!("future resolved before it could be cancelled");
        }
    };

    let main_block = async {
        let rx = rx.fuse();
        //pin_mut!(rx);
        let fut = fut.fuse();
        //pin_mut!(fut);
        select! {
            _ = fut => (),
            _ = rx => (),
        };
    };

    join!(main_block, phase_transition_block);
}