refactor and make tcp work

This commit is contained in:
John Smith
2022-01-05 12:01:02 -05:00
parent 3035bc079f
commit b66aca0ce0
25 changed files with 339 additions and 447 deletions
+38 -44
View File
@@ -5,41 +5,34 @@ use crate::*;
///////////////////////////////////////////////////////////
// Accept
cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
use async_std::net::*;
use utils::async_peek_stream::*;
pub trait ProtocolAcceptHandler: ProtocolAcceptHandlerClone + Send + Sync {
fn on_accept(
&self,
stream: AsyncPeekStream,
peer_addr: SocketAddr,
) -> SystemPinBoxFuture<Result<Option<NetworkConnection>, String>>;
}
pub trait ProtocolAcceptHandler: ProtocolAcceptHandlerClone + Send + Sync {
fn on_accept(
&self,
stream: AsyncPeekStream,
peer_addr: SocketAddr,
) -> SystemPinBoxFuture<Result<Option<NetworkConnection>, String>>;
}
pub trait ProtocolAcceptHandlerClone {
fn clone_box(&self) -> Box<dyn ProtocolAcceptHandler>;
}
pub trait ProtocolAcceptHandlerClone {
fn clone_box(&self) -> Box<dyn ProtocolAcceptHandler>;
}
impl<T> ProtocolAcceptHandlerClone for T
where
T: 'static + ProtocolAcceptHandler + Clone,
{
fn clone_box(&self) -> Box<dyn ProtocolAcceptHandler> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn ProtocolAcceptHandler> {
fn clone(&self) -> Box<dyn ProtocolAcceptHandler> {
self.clone_box()
}
}
pub type NewProtocolAcceptHandler =
dyn Fn(VeilidConfig, bool, SocketAddr) -> Box<dyn ProtocolAcceptHandler> + Send;
impl<T> ProtocolAcceptHandlerClone for T
where
T: 'static + ProtocolAcceptHandler + Clone,
{
fn clone_box(&self) -> Box<dyn ProtocolAcceptHandler> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn ProtocolAcceptHandler> {
fn clone(&self) -> Box<dyn ProtocolAcceptHandler> {
self.clone_box()
}
}
pub type NewProtocolAcceptHandler =
dyn Fn(VeilidConfig, bool, SocketAddr) -> Box<dyn ProtocolAcceptHandler> + Send;
///////////////////////////////////////////////////////////
// Dummy protocol network connection for testing
@@ -64,7 +57,6 @@ impl DummyNetworkConnection {
#[derive(Debug)]
struct NetworkConnectionInner {
protocol_connection: ProtocolNetworkConnection,
last_message_sent_time: Option<u64>,
last_message_recv_time: Option<u64>,
}
@@ -73,7 +65,8 @@ struct NetworkConnectionInner {
struct NetworkConnectionArc {
descriptor: ConnectionDescriptor,
established_time: u64,
inner: AsyncMutex<NetworkConnectionInner>,
protocol_connection: ProtocolNetworkConnection,
inner: Mutex<NetworkConnectionInner>,
}
#[derive(Clone, Debug)]
@@ -89,9 +82,8 @@ impl PartialEq for NetworkConnection {
impl Eq for NetworkConnection {}
impl NetworkConnection {
fn new_inner(protocol_connection: ProtocolNetworkConnection) -> NetworkConnectionInner {
fn new_inner() -> NetworkConnectionInner {
NetworkConnectionInner {
protocol_connection,
last_message_sent_time: None,
last_message_recv_time: None,
}
@@ -103,7 +95,8 @@ impl NetworkConnection {
NetworkConnectionArc {
descriptor,
established_time: intf::get_timestamp(),
inner: AsyncMutex::new(Self::new_inner(protocol_connection)),
protocol_connection,
inner: Mutex::new(Self::new_inner()),
}
}
@@ -135,23 +128,24 @@ impl NetworkConnection {
}
pub async fn close(&self) -> Result<(), String> {
let mut inner = self.arc.inner.lock().await;
inner.protocol_connection.close().await
self.arc.protocol_connection.close().await
}
pub async fn send(&self, message: Vec<u8>) -> Result<(), String> {
let mut inner = self.arc.inner.lock().await;
let out = inner.protocol_connection.send(message).await;
let ts = intf::get_timestamp();
let out = self.arc.protocol_connection.send(message).await;
if out.is_ok() {
inner.last_message_sent_time = Some(intf::get_timestamp());
let mut inner = self.arc.inner.lock();
inner.last_message_sent_time.max_assign(Some(ts));
}
out
}
pub async fn recv(&self) -> Result<Vec<u8>, String> {
let mut inner = self.arc.inner.lock().await;
let out = inner.protocol_connection.recv().await;
let ts = intf::get_timestamp();
let out = self.arc.protocol_connection.recv().await;
if out.is_ok() {
inner.last_message_recv_time = Some(intf::get_timestamp());
let mut inner = self.arc.inner.lock();
inner.last_message_recv_time.max_assign(Some(ts));
}
out
}