Improve stream read write error handling

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent e59e26c72f
commit 9a1c1df91d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,7 +1,7 @@
[package] [package]
name = "vented" name = "vented"
description = "Event driven encrypted tcp communicaton" description = "Event driven encrypted tcp communicaton"
version = "0.10.1" version = "0.10.2"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

@ -1,10 +1,10 @@
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::TcpStream; use std::net::{Shutdown, TcpStream};
use std::sync::Arc; use std::sync::Arc;
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use crypto_box::{ChaChaBox, SecretKey};
use crypto_box::aead::{Aead, Payload}; use crypto_box::aead::{Aead, Payload};
use crypto_box::{ChaChaBox, SecretKey};
use generic_array::GenericArray; use generic_array::GenericArray;
use parking_lot::Mutex; use parking_lot::Mutex;
use sha2::Digest; use sha2::Digest;
@ -99,19 +99,27 @@ impl CryptoStream {
pub fn receiver_node(&self) -> &String { pub fn receiver_node(&self) -> &String {
&self.recv_node_id &self.recv_node_id
} }
/// Closes both streams
pub fn shutdown(&self) -> VentedResult<()> {
self.send_stream.lock().shutdown(Shutdown::Both)?;
self.recv_stream.lock().shutdown(Shutdown::Both)?;
Ok(())
}
} }
pub struct EncryptionBox<T> pub struct EncryptionBox<T>
where where
T: Aead, T: Aead,
{ {
inner: T, inner: T,
counter: u128, counter: u128,
} }
impl<T> EncryptionBox<T> impl<T> EncryptionBox<T>
where where
T: Aead, T: Aead,
{ {
/// Creates a new encryption box with the given inner value /// Creates a new encryption box with the given inner value
pub fn new(inner: T) -> Self { pub fn new(inner: T) -> Self {

@ -97,6 +97,9 @@ impl ConcurrentStreamManager {
} }
future.resolve(()); future.resolve(());
} }
if let Err(e) = stream.shutdown() {
log::error!("Failed to shutdown stream: {}", e);
}
emitters.lock().remove(&recv_id); emitters.lock().remove(&recv_id);
threads.lock().remove(&thread::current().id()); threads.lock().remove(&thread::current().id());
} }
@ -112,14 +115,26 @@ impl ConcurrentStreamManager {
let wg = WaitGroup::clone(&wg); let wg = WaitGroup::clone(&wg);
move || { move || {
mem::drop(wg); mem::drop(wg);
while let Ok(mut event) = stream.read() { loop {
event.origin = Some(stream.receiver_node().clone()); match stream.read() {
Ok(mut event) => {
if let Err(e) = sender.send((stream.receiver_node().clone(), event)) { event.origin = Some(stream.receiver_node().clone());
log::trace!("Failed to get event from {}: {}", recv_id, e);
break; if let Err(e) = sender.send((stream.receiver_node().clone(), event))
{
log::trace!("Failed to get event from {}: {}", recv_id, e);
break;
}
}
Err(e) => {
log::error!("Failed to send event: {}", e);
break;
}
} }
} }
if let Err(e) = stream.shutdown() {
log::error!("Failed to shutdown stream: {}", e);
}
threads.lock().remove(&thread::current().id()); threads.lock().remove(&thread::current().id());
} }
})?; })?;

Loading…
Cancel
Save