]> git.lizzy.rs Git - connect-rs.git/blobdiff - examples/tcp-echo-server/src/main.rs
refactor to have datagram already serialized in memory
[connect-rs.git] / examples / tcp-echo-server / src / main.rs
index 04e1e122cdf968e06f6c2c80a5ad160a68a7fd8c..c2603856751f7e98f991ac0edae85356653ea104 100644 (file)
@@ -28,16 +28,16 @@ async fn main() -> anyhow::Result<()> {
         info!("Handling connection from {}", conn.peer_addr());
 
         task::spawn(async move {
-            while let Some(mut envelope) = conn.reader().next().await {
+            while let Some(envelope) = conn.reader().next().await {
                 // handle message based on intended recipient
-                if envelope.recipient() == 65535 {
+                if envelope.tag() == 65535 {
                     // if recipient is 65535, we do custom processing
-                    let data = envelope.take_data().unwrap();
+                    let data = envelope.data().to_vec();
                     let msg =
                         String::from_utf8(data).expect("could not build String from payload bytes");
                     info!("Received a message \"{}\" from {}", msg, conn.peer_addr());
 
-                    let reply = ConnectDatagram::new(envelope.recipient(), msg.into_bytes())
+                    let reply = ConnectDatagram::with_tag(envelope.tag(), msg.into_bytes())
                         .expect("could not construct new datagram from built String");
 
                     conn.writer()
@@ -49,7 +49,7 @@ async fn main() -> anyhow::Result<()> {
                     // if recipient is anything else, we just send it back
                     warn!(
                         "Received a message for unknown recipient {} from {}",
-                        envelope.recipient(),
+                        envelope.tag(),
                         conn.peer_addr()
                     );