]> git.lizzy.rs Git - connect-rs.git/blobdiff - examples/tcp-client/src/main.rs
remove dependency on protobuf and introduce basic custom wire format
[connect-rs.git] / examples / tcp-client / src / main.rs
index c65a45548cb32894155e443b1409105b67237902..5e15adad32afb1e1733955535a2c61661c4b9447 100644 (file)
@@ -1,9 +1,5 @@
-pub mod schema;
-
-use crate::schema::hello_world::HelloWorld;
-use connect::{Connection, SinkExt, StreamExt};
+use connect::{ConnectDatagram, Connection, SinkExt, StreamExt};
 use log::*;
-use protobuf::well_known_types::Any;
 use std::env;
 
 #[async_std::main]
@@ -24,21 +20,20 @@ async fn main() -> anyhow::Result<()> {
     let mut conn = Connection::tcp_client(ip_address).await?;
 
     // send a message to the server
-    let raw_msg = String::from("Hello world");
-
-    let mut msg = HelloWorld::new();
-    msg.set_message(raw_msg.clone());
+    let msg = String::from("Hello world");
+    info!("Sending message: {}", msg);
 
-    conn.writer().send(msg).await?;
-    info!("Sent message: {}", raw_msg);
+    let envelope = ConnectDatagram::new(65535, msg.into_bytes())?;
+    conn.writer().send(envelope).await?;
 
     // wait for the server to reply with an ack
-    while let Some(reply) = conn.reader().next().await {
+    if let Some(mut reply) = conn.reader().next().await {
         info!("Received message");
 
-        let msg: HelloWorld = Any::unpack(&reply)?.unwrap();
+        let data = reply.take_data().unwrap();
+        let msg = String::from_utf8(data)?;
 
-        info!("Unpacked reply: {}", msg.get_message());
+        info!("Unpacked reply: {}", msg);
     }
 
     Ok(())