Add Send as JSON
This commit is contained in:
parent
84d017795e
commit
d0221ba54a
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tapir-cwtch"
|
name = "tapir-cwtch"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
authors = ["Sarah Jamie Lewis <sarah@openprivacy.ca>"]
|
authors = ["Sarah Jamie Lewis <sarah@openprivacy.ca>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -2,6 +2,7 @@ use hashbrown::HashSet;
|
||||||
use integer_encoding::{FixedInt, VarInt};
|
use integer_encoding::{FixedInt, VarInt};
|
||||||
use secretbox::CipherType::Salsa20;
|
use secretbox::CipherType::Salsa20;
|
||||||
use secretbox::SecretBox;
|
use secretbox::SecretBox;
|
||||||
|
use serde::Serialize;
|
||||||
use std::io::{Error, Read, Write};
|
use std::io::{Error, Read, Write};
|
||||||
use std::net::{Shutdown, TcpStream};
|
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> {
|
pub fn try_clone(&self) -> Connection<Direction> {
|
||||||
Connection {
|
Connection {
|
||||||
conn: self.conn.try_clone().unwrap(),
|
conn: self.conn.try_clone().unwrap(),
|
||||||
|
|
Loading…
Reference in New Issue