dnet.rs
1 /* This file is part of DarkFi (https://dark.fi) 2 * 3 * Copyright (C) 2020-2025 Dyne.org foundation 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Affero General Public License for more details. 14 * 15 * You should have received a copy of the GNU Affero General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 use url::Url; 20 21 use super::channel::ChannelInfo; 22 use crate::util::time::NanoTimestamp; 23 24 macro_rules! dnetev { 25 ($self:expr, $event_name:ident, $($code:tt)*) => { 26 { 27 if $self.p2p().dnet_enabled.load(std::sync::atomic::Ordering::SeqCst) { 28 let event = DnetEvent::$event_name(dnet::$event_name $($code)*); 29 $self.p2p().dnet_notify(event).await; 30 } 31 } 32 }; 33 } 34 pub(crate) use dnetev; 35 36 #[derive(Clone, Debug)] 37 pub struct MessageInfo { 38 pub chan: ChannelInfo, 39 pub cmd: String, 40 pub time: NanoTimestamp, 41 } 42 43 // Needed by the dnetev!() macro 44 pub type SendMessage = MessageInfo; 45 pub type RecvMessage = MessageInfo; 46 47 #[derive(Clone, Debug)] 48 pub struct InboundInfo { 49 pub addr: Url, 50 pub channel_id: u32, 51 } 52 53 pub type InboundConnected = InboundInfo; 54 pub type InboundDisconnected = InboundInfo; 55 56 #[derive(Clone, Debug)] 57 pub struct OutboundSlotSleeping { 58 pub slot: u32, 59 } 60 61 #[derive(Clone, Debug)] 62 pub struct OutboundSlotConnecting { 63 pub slot: u32, 64 pub addr: Url, 65 } 66 67 #[derive(Clone, Debug)] 68 pub struct OutboundSlotConnected { 69 pub slot: u32, 70 pub addr: Url, 71 pub channel_id: u32, 72 } 73 74 #[derive(Clone, Debug)] 75 pub struct OutboundSlotDisconnected { 76 pub slot: u32, 77 pub err: String, 78 } 79 80 #[derive(Clone, Debug)] 81 pub struct OutboundPeerDiscovery { 82 pub attempt: u32, 83 pub state: &'static str, 84 } 85 86 #[derive(Clone, Debug)] 87 pub struct DirectConnecting { 88 pub connect_addr: Url, 89 } 90 91 #[derive(Clone, Debug)] 92 pub struct DirectConnected { 93 pub connect_addr: Url, 94 pub addr: Url, 95 pub channel_id: u32, 96 } 97 98 #[derive(Clone, Debug)] 99 pub struct DirectDisconnected { 100 pub connect_addr: Url, 101 pub err: String, 102 } 103 104 pub type DirectPeerDiscovery = OutboundPeerDiscovery; 105 106 #[derive(Clone, Debug)] 107 pub enum DnetEvent { 108 SendMessage(MessageInfo), 109 RecvMessage(MessageInfo), 110 InboundConnected(InboundConnected), 111 InboundDisconnected(InboundDisconnected), 112 OutboundSlotSleeping(OutboundSlotSleeping), 113 OutboundSlotConnecting(OutboundSlotConnecting), 114 OutboundSlotConnected(OutboundSlotConnected), 115 OutboundSlotDisconnected(OutboundSlotDisconnected), 116 OutboundPeerDiscovery(OutboundPeerDiscovery), 117 DirectConnecting(DirectConnecting), 118 DirectConnected(DirectConnected), 119 DirectDisconnected(DirectDisconnected), 120 DirectPeerDiscovery(DirectPeerDiscovery), 121 }