dnet.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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.lock().await {
  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 enum DnetEvent {
  75. SendMessage(MessageInfo),
  76. RecvMessage(MessageInfo),
  77. InboundConnected(InboundConnected),
  78. InboundDisconnected(InboundDisconnected),
  79. OutboundSlotSleeping(OutboundSlotSleeping),
  80. OutboundSlotConnecting(OutboundSlotConnecting),
  81. OutboundSlotConnected(OutboundSlotConnected),
  82. OutboundSlotDisconnected(OutboundSlotDisconnected),
  83. OutboundPeerDiscovery(OutboundPeerDiscovery),
  84. }