Просмотр исходного кода

net: make dnet! macro more compact (and rename to dnetev!)

x 3 лет назад
Родитель
Сommit
25b84e18f6
4 измененных файлов с 74 добавлено и 45 удалено
  1. 11 17
      src/net/channel.rs
  2. 25 8
      src/net/dnet.rs
  3. 15 13
      src/net/session/outbound_session.rs
  4. 23 7
      src/rpc/from_impl.rs

+ 11 - 17
src/net/channel.rs

@@ -28,7 +28,7 @@ use smol::Executor;
 use url::Url;
 use url::Url;
 
 
 use super::{
 use super::{
-    dnet::{self, dnet, DnetEvent},
+    dnet::{self, dnetev, DnetEvent},
     message,
     message,
     message::Packet,
     message::Packet,
     message_subscriber::{MessageSubscription, MessageSubsystem},
     message_subscriber::{MessageSubscription, MessageSubsystem},
@@ -207,14 +207,11 @@ impl Channel {
     async fn send_message<M: message::Message>(&self, message: &M) -> Result<()> {
     async fn send_message<M: message::Message>(&self, message: &M) -> Result<()> {
         let packet = Packet { command: M::NAME.to_string(), payload: serialize(message) };
         let packet = Packet { command: M::NAME.to_string(), payload: serialize(message) };
 
 
-        dnet!(self,
-            let event = DnetEvent::SendMessage(dnet::MessageInfo {
-                chan: self.info.clone(),
-                cmd: packet.command.clone(),
-                time: NanoTimestamp::current_time(),
-            });
-            self.p2p().dnet_notify(event).await;
-        );
+        dnetev!(self, SendMessage, {
+            chan: self.info.clone(),
+            cmd: packet.command.clone(),
+            time: NanoTimestamp::current_time(),
+        });
 
 
         let stream = &mut *self.writer.lock().await;
         let stream = &mut *self.writer.lock().await;
         let _ = message::send_packet(stream, packet).await?;
         let _ = message::send_packet(stream, packet).await?;
@@ -288,14 +285,11 @@ impl Channel {
                 }
                 }
             };
             };
 
 
-            dnet!(self,
-                let event = DnetEvent::RecvMessage(dnet::MessageInfo {
-                    chan: self.info.clone(),
-                    cmd: packet.command.clone(),
-                    time: NanoTimestamp::current_time(),
-                });
-                self.p2p().dnet_notify(event).await;
-            );
+            dnetev!(self, RecvMessage, {
+                chan: self.info.clone(),
+                cmd: packet.command.clone(),
+                time: NanoTimestamp::current_time(),
+            });
 
 
             // Send result to our subscribers
             // Send result to our subscribers
             self.message_subsystem.notify(&packet.command, &packet.payload).await;
             self.message_subsystem.notify(&packet.command, &packet.payload).await;

+ 25 - 8
src/net/dnet.rs

@@ -20,16 +20,17 @@ use super::channel::ChannelInfo;
 use crate::util::time::NanoTimestamp;
 use crate::util::time::NanoTimestamp;
 use url::Url;
 use url::Url;
 
 
-macro_rules! dnet {
-    ($self:expr, $($code:tt)*) => {
+macro_rules! dnetev {
+    ($self:expr, $event_name:ident, $($code:tt)*) => {
         {
         {
             if *$self.p2p().dnet_enabled.lock().await {
             if *$self.p2p().dnet_enabled.lock().await {
-                $($code)*
+                let event = DnetEvent::$event_name(dnet::$event_name $($code)*);
+                $self.p2p().dnet_notify(event).await;
             }
             }
         }
         }
     };
     };
 }
 }
-pub(crate) use dnet;
+pub(crate) use dnetev;
 
 
 #[derive(Clone, Debug)]
 #[derive(Clone, Debug)]
 pub struct MessageInfo {
 pub struct MessageInfo {
@@ -38,18 +39,34 @@ pub struct MessageInfo {
     pub time: NanoTimestamp,
     pub time: NanoTimestamp,
 }
 }
 
 
+// Needed by the macro
+pub type SendMessage = MessageInfo;
+pub type RecvMessage = MessageInfo;
+
+#[derive(Clone, Debug)]
+pub struct OutboundConnecting {
+    pub slot: u32,
+    pub addr: Url,
+}
+
 #[derive(Clone, Debug)]
 #[derive(Clone, Debug)]
-pub struct OutboundConnect {
+pub struct OutboundConnected {
     pub slot: u32,
     pub slot: u32,
     pub addr: Url,
     pub addr: Url,
     pub channel_id: u32,
     pub channel_id: u32,
 }
 }
 
 
+#[derive(Clone, Debug)]
+pub struct OutboundDisconnected {
+    pub slot: u32,
+    pub err: String,
+}
+
 #[derive(Clone, Debug)]
 #[derive(Clone, Debug)]
 pub enum DnetEvent {
 pub enum DnetEvent {
     SendMessage(MessageInfo),
     SendMessage(MessageInfo),
     RecvMessage(MessageInfo),
     RecvMessage(MessageInfo),
-    //OutboundConnecting(OutboundConnect),
-    OutboundConnected(OutboundConnect),
-    OutboundDisconnected(u32),
+    OutboundConnecting(OutboundConnecting),
+    OutboundConnected(OutboundConnected),
+    OutboundDisconnected(OutboundDisconnected),
 }
 }

+ 15 - 13
src/net/session/outbound_session.rs

@@ -38,7 +38,7 @@ use super::{
     super::{
     super::{
         channel::ChannelPtr,
         channel::ChannelPtr,
         connector::Connector,
         connector::Connector,
-        dnet::{self, dnet, DnetEvent},
+        dnet::{self, dnetev, DnetEvent},
         message::GetAddrsMessage,
         message::GetAddrsMessage,
         p2p::{P2p, P2pPtr},
         p2p::{P2p, P2pPtr},
     },
     },
@@ -169,10 +169,10 @@ impl OutboundSession {
                         slot, e,
                         slot, e,
                     );
                     );
 
 
-                    dnet!(self,
-                        let event = DnetEvent::OutboundDisconnected(slot);
-                        self.p2p().dnet_notify(event).await;
-                    );
+                    dnetev!(self, OutboundDisconnected, {
+                        slot,
+                        err: e.to_string()
+                    });
                 }
                 }
             }
             }
         }
         }
@@ -206,6 +206,11 @@ impl OutboundSession {
             slot, addr,
             slot, addr,
         );
         );
 
 
+        dnetev!(self, OutboundConnecting, {
+            slot,
+            addr: addr.clone(),
+        });
+
         match connector.connect(&addr).await {
         match connector.connect(&addr).await {
             Ok((url, channel)) => {
             Ok((url, channel)) => {
                 info!(
                 info!(
@@ -214,14 +219,11 @@ impl OutboundSession {
                     slot, url
                     slot, url
                 );
                 );
 
 
-                dnet!(self,
-                    let event = DnetEvent::OutboundConnected(dnet::OutboundConnect {
-                        slot,
-                        addr: addr.clone(),
-                        channel_id: channel.info.id
-                    });
-                    self.p2p().dnet_notify(event).await;
-                );
+                dnetev!(self, OutboundConnected, {
+                    slot,
+                    addr: addr.clone(),
+                    channel_id: channel.info.id
+                });
 
 
                 let stop_sub =
                 let stop_sub =
                     channel.subscribe_stop().await.expect("Channel should not be stopped");
                     channel.subscribe_stop().await.expect("Channel should not be stopped");

+ 23 - 7
src/rpc/from_impl.rs

@@ -41,15 +41,22 @@ impl From<net::dnet::MessageInfo> for JsonValue {
     fn from(info: net::dnet::MessageInfo) -> JsonValue {
     fn from(info: net::dnet::MessageInfo) -> JsonValue {
         json_map([
         json_map([
             ("chan", info.chan.into()),
             ("chan", info.chan.into()),
-            ("cmd", JsonStr(info.cmd.clone())),
+            ("cmd", JsonStr(info.cmd)),
             ("time", JsonStr(info.time.0.to_string())),
             ("time", JsonStr(info.time.0.to_string())),
         ])
         ])
     }
     }
 }
 }
 
 
 #[cfg(feature = "net")]
 #[cfg(feature = "net")]
-impl From<net::dnet::OutboundConnect> for JsonValue {
-    fn from(info: net::dnet::OutboundConnect) -> JsonValue {
+impl From<net::dnet::OutboundConnecting> for JsonValue {
+    fn from(info: net::dnet::OutboundConnecting) -> JsonValue {
+        json_map([("slot", JsonNum(info.slot.into())), ("addr", JsonStr(info.addr.to_string()))])
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<net::dnet::OutboundConnected> for JsonValue {
+    fn from(info: net::dnet::OutboundConnected) -> JsonValue {
         json_map([
         json_map([
             ("slot", JsonNum(info.slot.into())),
             ("slot", JsonNum(info.slot.into())),
             ("addr", JsonStr(info.addr.to_string())),
             ("addr", JsonStr(info.addr.to_string())),
@@ -58,6 +65,13 @@ impl From<net::dnet::OutboundConnect> for JsonValue {
     }
     }
 }
 }
 
 
+#[cfg(feature = "net")]
+impl From<net::dnet::OutboundDisconnected> for JsonValue {
+    fn from(info: net::dnet::OutboundDisconnected) -> JsonValue {
+        json_map([("slot", JsonNum(info.slot.into())), ("err", JsonStr(info.err))])
+    }
+}
+
 #[cfg(feature = "net")]
 #[cfg(feature = "net")]
 impl From<net::dnet::DnetEvent> for JsonValue {
 impl From<net::dnet::DnetEvent> for JsonValue {
     fn from(event: net::dnet::DnetEvent) -> JsonValue {
     fn from(event: net::dnet::DnetEvent) -> JsonValue {
@@ -68,13 +82,15 @@ impl From<net::dnet::DnetEvent> for JsonValue {
             net::dnet::DnetEvent::RecvMessage(info) => {
             net::dnet::DnetEvent::RecvMessage(info) => {
                 json_map([("event", json_str("recv")), ("info", info.into())])
                 json_map([("event", json_str("recv")), ("info", info.into())])
             }
             }
+            net::dnet::DnetEvent::OutboundConnecting(info) => {
+                json_map([("event", json_str("outbound_connecting")), ("info", info.into())])
+            }
             net::dnet::DnetEvent::OutboundConnected(info) => {
             net::dnet::DnetEvent::OutboundConnected(info) => {
                 json_map([("event", json_str("outbound_connected")), ("info", info.into())])
                 json_map([("event", json_str("outbound_connected")), ("info", info.into())])
             }
             }
-            net::dnet::DnetEvent::OutboundDisconnected(slot) => json_map([
-                ("event", json_str("outbound_disconnected")),
-                ("slot", JsonNum(slot.into())),
-            ]),
+            net::dnet::DnetEvent::OutboundDisconnected(info) => {
+                json_map([("event", json_str("outbound_disconnected")), ("info", info.into())])
+            }
         }
         }
     }
     }
 }
 }