From d0221ba54ad6f5d5a6f9618f3aa297fc4374dc1a Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 13 Jan 2021 20:51:25 -0800 Subject: [PATCH] Add Send as JSON --- Cargo.toml | 2 +- src/connections/mod.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e03149d..4e43289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tapir-cwtch" -version = "0.1.8" +version = "0.1.9" authors = ["Sarah Jamie Lewis "] edition = "2018" license = "MIT" diff --git a/src/connections/mod.rs b/src/connections/mod.rs index e2be753..e7753d8 100644 --- a/src/connections/mod.rs +++ b/src/connections/mod.rs @@ -2,6 +2,7 @@ use hashbrown::HashSet; use integer_encoding::{FixedInt, VarInt}; use secretbox::CipherType::Salsa20; use secretbox::SecretBox; +use serde::Serialize; use std::io::{Error, Read, Write}; use std::net::{Shutdown, TcpStream}; @@ -162,6 +163,20 @@ where } } + /// Send anything implemented Serialize as JSON + pub fn send_json_encrypted(&mut self, data: T) -> Result<(), Error> + where + T: Serialize, + { + let mut msg = vec![]; + let mut len = [0u8; 2]; + let json = serde_json::to_string(&data).unwrap(); + (json.len() as u16).encode_fixed(&mut len); + msg.extend_from_slice(len.as_slice()); + msg.extend_from_slice(json.as_bytes()); + self.send_encrypted(msg) + } + pub fn try_clone(&self) -> Connection { Connection { conn: self.conn.try_clone().unwrap(),