dnet.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 darkfi_serial::{SerialDecodable, SerialEncodable};
  19. use url::Url;
  20. use super::channel::ChannelInfo;
  21. use crate::util::time::NanoTimestamp;
  22. macro_rules! dnetev {
  23. ($self:expr, $event_name:ident, $($code:tt)*) => {
  24. {
  25. if *$self.p2p().dnet_enabled.lock().await {
  26. let event = DnetEvent::$event_name(dnet::$event_name $($code)*);
  27. $self.p2p().dnet_notify(event).await;
  28. }
  29. }
  30. };
  31. }
  32. pub(crate) use dnetev;
  33. #[derive(Clone, Debug)]
  34. pub struct MessageInfo {
  35. pub chan: ChannelInfo,
  36. pub cmd: String,
  37. pub time: NanoTimestamp,
  38. }
  39. // Needed by the macro
  40. pub type SendMessage = MessageInfo;
  41. pub type RecvMessage = MessageInfo;
  42. #[derive(Clone, Debug)]
  43. pub struct OutboundConnecting {
  44. pub slot: u32,
  45. pub addr: Url,
  46. }
  47. #[derive(Clone, Debug)]
  48. pub struct OutboundConnected {
  49. pub slot: u32,
  50. pub addr: Url,
  51. pub channel_id: u32,
  52. }
  53. #[derive(Clone, Debug)]
  54. pub struct OutboundDisconnected {
  55. pub slot: u32,
  56. pub err: String,
  57. }
  58. #[derive(Clone, Debug)]
  59. pub enum DnetEvent {
  60. SendMessage(MessageInfo),
  61. RecvMessage(MessageInfo),
  62. OutboundConnecting(OutboundConnecting),
  63. OutboundConnected(OutboundConnected),
  64. OutboundDisconnected(OutboundDisconnected),
  65. }