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

net: fix command/payload naming and remove redundant buffer

draoi 2 лет назад
Родитель
Сommit
f604f0054f
2 измененных файлов с 12 добавлено и 13 удалено
  1. 10 11
      src/net/channel.rs
  2. 2 2
      src/net/message_subscriber.rs

+ 10 - 11
src/net/channel.rs

@@ -223,8 +223,7 @@ impl Channel {
         assert!(std::mem::size_of::<usize>() <= std::mem::size_of::<u64>());
 
         let stream = &mut *self.writer.lock().await;
-        let mut name_buffer = Vec::<u8>::new();
-        let mut msg_buffer = Vec::<u8>::new();
+        let mut buffer = Vec::<u8>::new();
         let mut written: usize = 0;
 
         dnetev!(self, SendMessage, {
@@ -239,26 +238,26 @@ impl Channel {
         trace!(target: "net::channel::send_message()", "Sent magic");
         trace!(target: "net::channel::send_message()", "Sending command...");
 
-        // First encode the name to an intermediate buffer.
-        M::NAME.to_string().encode_async(&mut name_buffer).await?;
+        // First encode the command to an intermediate buffer.
+        M::NAME.to_string().encode_async(&mut buffer).await?;
 
         // Then extract the length of the intermediate buffer as a VarInt
-        // and write to the stream. This is the length of the name message.
-        // Then encode the name itself to the stream.
-        written += VarInt(name_buffer.len() as u64).encode_async(stream).await?;
+        // and write to the stream. This is the length of the command message.
+        // Then encode the command itself to the stream.
+        written += VarInt(buffer.len() as u64).encode_async(stream).await?;
         written += M::NAME.to_string().encode_async(stream).await?;
 
         trace!(target: "net::channel::send_message()", "Sent command: {}", M::NAME.to_string());
         trace!(target: "net::channel::send_message()", "Sending payload...");
 
-        // Do the same proceedure for the Message.
-        message.encode_async(&mut msg_buffer).await?;
+        // Do the same proceedure for the Message payload.
+        message.encode_async(&mut buffer).await?;
 
-        written += VarInt(msg_buffer.len() as u64).encode_async(stream).await?;
+        written += VarInt(buffer.len() as u64).encode_async(stream).await?;
         written += message.encode_async(stream).await?;
 
         trace!(target: "net::channel::send_message()", "Sent payload {} bytes, total bytes {}",
-            msg_buffer.len(), written);
+            buffer.len(), written);
 
         stream.flush().await?;
 

+ 2 - 2
src/net/message_subscriber.rs

@@ -203,8 +203,8 @@ impl<M: Message> MessageDispatcherInterface for MessageDispatcher<M> {
 
                 // Deserialize stream into type, send down the pipes.
                 match M::decode_async(&mut take).await {
-                    Ok(message) => {
-                        let message = Ok(Arc::new(message));
+                    Ok(payload) => {
+                        let message = Ok(Arc::new(payload));
                         self._trigger_all(message).await
                     }