aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/connection.rs
blob: 7c81aa2dc5228cd5081151fc3ab254a5b2ba1a25 (plain)
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
use smol::{channel::Sender, lock::Mutex};
use std::{collections::VecDeque, fmt, sync::Arc};

use karyons_core::async_utils::CondVar;

use karyons_net::Conn;

/// Defines the direction of a network connection.
#[derive(Clone, Debug)]
pub enum ConnDirection {
    Inbound,
    Outbound,
}

impl fmt::Display for ConnDirection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ConnDirection::Inbound => write!(f, "Inbound"),
            ConnDirection::Outbound => write!(f, "Outbound"),
        }
    }
}

pub struct NewConn {
    pub direction: ConnDirection,
    pub conn: Conn,
    pub disconnect_signal: Sender<()>,
}

/// Connection queue
pub struct ConnQueue {
    queue: Mutex<VecDeque<NewConn>>,
    conn_available: CondVar,
}

impl ConnQueue {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            queue: Mutex::new(VecDeque::new()),
            conn_available: CondVar::new(),
        })
    }

    /// Push a connection into the queue and wait for the disconnect signal
    pub async fn handle(&self, conn: Conn, direction: ConnDirection) {
        let (disconnect_signal, chan) = smol::channel::bounded(1);
        let new_conn = NewConn {
            direction,
            conn,
            disconnect_signal,
        };
        self.queue.lock().await.push_back(new_conn);
        self.conn_available.signal();
        let _ = chan.recv().await;
    }

    /// Receive the next connection in the queue
    pub async fn next(&self) -> NewConn {
        let mut queue = self.queue.lock().await;
        while queue.is_empty() {
            queue = self.conn_available.wait(queue).await;
        }
        queue.pop_front().unwrap()
    }
}