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]
name = "vented"
description = "Event driven encrypted tcp communicaton"
version = "0.10.1"
version = "0.10.2"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"

@ -1,10 +1,10 @@
use std::io::{Read, Write};
use std::net::TcpStream;
use std::net::{Shutdown, TcpStream};
use std::sync::Arc;
use byteorder::{BigEndian, ByteOrder};
use crypto_box::{ChaChaBox, SecretKey};
use crypto_box::aead::{Aead, Payload};
use crypto_box::{ChaChaBox, SecretKey};
use generic_array::GenericArray;
use parking_lot::Mutex;
use sha2::Digest;
@ -99,6 +99,14 @@ impl CryptoStream {
pub fn receiver_node(&self) -> &String {
&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>

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

Loading…
Cancel
Save