dnet.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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. use url::Url;
  19. use super::channel::ChannelInfo;
  20. use crate::util::time::NanoTimestamp;
  21. macro_rules! dnetev {
  22. ($self:expr, $event_name:ident, $($code:tt)*) => {
  23. {
  24. if $self.p2p().dnet_enabled.load(std::sync::atomic::Ordering::SeqCst) {
  25. let event = DnetEvent::$event_name(dnet::$event_name $($code)*);
  26. $self.p2p().dnet_notify(event).await;
  27. }
  28. }
  29. };
  30. }
  31. pub(crate) use dnetev;
  32. #[derive(Clone, Debug)]
  33. pub struct MessageInfo {
  34. pub chan: ChannelInfo,
  35. pub cmd: String,
  36. pub time: NanoTimestamp,
  37. }
  38. // Needed by the dnetev!() macro
  39. pub type SendMessage = MessageInfo;
  40. pub type RecvMessage = MessageInfo;
  41. #[derive(Clone, Debug)]
  42. pub struct InboundInfo {
  43. pub addr: Url,
  44. pub channel_id: u32,
  45. }
  46. pub type InboundConnected = InboundInfo;
  47. pub type InboundDisconnected = InboundInfo;
  48. #[derive(Clone, Debug)]
  49. pub struct OutboundSlotSleeping {
  50. pub slot: u32,
  51. }
  52. #[derive(Clone, Debug)]
  53. pub struct OutboundSlotConnecting {
  54. pub slot: u32,
  55. pub addr: Url,
  56. }
  57. #[derive(Clone, Debug)]
  58. pub struct OutboundSlotConnected {
  59. pub slot: u32,
  60. pub addr: Url,
  61. pub channel_id: u32,
  62. }
  63. #[derive(Clone, Debug)]
  64. pub struct OutboundSlotDisconnected {
  65. pub slot: u32,
  66. pub err: String,
  67. }
  68. #[derive(Clone, Debug)]
  69. pub struct OutboundPeerDiscovery {
  70. pub attempt: u32,
  71. pub state: &'static str,
  72. }
  73. #[derive(Clone, Debug)]
  74. pub struct DirectConnecting {
  75. pub connect_addr: Url,
  76. }
  77. #[derive(Clone, Debug)]
  78. pub struct DirectConnected {
  79. pub connect_addr: Url,
  80. pub addr: Url,
  81. pub channel_id: u32,
  82. }
  83. #[derive(Clone, Debug)]
  84. pub struct DirectDisconnected {
  85. pub connect_addr: Url,
  86. pub err: String,
  87. }
  88. pub type DirectPeerDiscovery = OutboundPeerDiscovery;
  89. #[derive(Clone, Debug)]
  90. pub enum DnetEvent {
  91. SendMessage(MessageInfo),
  92. RecvMessage(MessageInfo),
  93. InboundConnected(InboundConnected),
  94. InboundDisconnected(InboundDisconnected),
  95. OutboundSlotSleeping(OutboundSlotSleeping),
  96. OutboundSlotConnecting(OutboundSlotConnecting),
  97. OutboundSlotConnected(OutboundSlotConnected),
  98. OutboundSlotDisconnected(OutboundSlotDisconnected),
  99. OutboundPeerDiscovery(OutboundPeerDiscovery),
  100. DirectConnecting(DirectConnecting),
  101. DirectConnected(DirectConnected),
  102. DirectDisconnected(DirectDisconnected),
  103. DirectPeerDiscovery(DirectPeerDiscovery),
  104. }