]> git.lizzy.rs Git - connect-rs.git/blobdiff - examples/tls-echo-server/src/main.rs
remove dependency on protobuf and introduce basic custom wire format
[connect-rs.git] / examples / tls-echo-server / src / main.rs
index bf9d476f2579363c4b35d231e22c0a89c0f9a94e..2a9abc8eb79afe229c583232973ede8d0f64e1a5 100644 (file)
@@ -1,11 +1,8 @@
-mod schema;
-
-use crate::schema::hello_world::HelloWorld;
 use async_std::{io, task};
 use connect::tls::rustls::internal::pemfile::{certs, rsa_private_keys};
 use connect::tls::rustls::{NoClientAuth, ServerConfig};
 use connect::tls::TlsListener;
-use connect::{SinkExt, StreamExt};
+use connect::{ConnectDatagram, SinkExt, StreamExt};
 use log::*;
 use std::env;
 use std::fs::File;
@@ -38,23 +35,36 @@ async fn main() -> anyhow::Result<()> {
         info!("Handling connection from {}", conn.peer_addr());
 
         task::spawn(async move {
-            while let Some(msg) = conn.reader().next().await {
-                if msg.is::<HelloWorld>() {
-                    if let Ok(Some(contents)) = msg.unpack::<HelloWorld>() {
-                        info!(
-                            "Received a message \"{}\" from {}",
-                            contents.get_message(),
-                            conn.peer_addr()
-                        );
-
-                        conn.writer()
-                            .send(contents)
-                            .await
-                            .expect("Could not send message back to source connection");
-                        info!("Sent message back to original sender");
-                    }
+            while let Some(mut envelope) = conn.reader().next().await {
+                // handle message based on intended recipient
+                if envelope.recipient() == 65535 {
+                    // if recipient is 65535, we do custom processing
+                    let data = envelope.take_data().unwrap();
+                    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())
+                        .expect("could not construct new datagram from built String");
+
+                    conn.writer()
+                        .send(reply)
+                        .await
+                        .expect("Could not send message back to source connection");
+                    info!("Sent message back to original sender");
                 } else {
-                    error!("Received a message of unknown type")
+                    // if recipient is anything else, we just send it back
+                    warn!(
+                        "Received a message for unknown recipient {} from {}",
+                        envelope.recipient(),
+                        conn.peer_addr()
+                    );
+
+                    conn.writer()
+                        .send(envelope)
+                        .await
+                        .expect("Could not send message back to source connection");
+                    info!("Sent message back to original sender");
                 }
             }
         });