Add Send as JSON

This commit is contained in:
Sarah Jamie Lewis 2021-01-13 20:51:25 -08:00
parent 84d017795e
commit d0221ba54a
2 changed files with 16 additions and 1 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "tapir-cwtch"
version = "0.1.8"
version = "0.1.9"
authors = ["Sarah Jamie Lewis <sarah@openprivacy.ca>"]
edition = "2018"
license = "MIT"

View File

@ -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<T>(&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<Direction> {
Connection {
conn: self.conn.try_clone().unwrap(),