This commit is contained in:
John Smith 2021-11-26 12:01:22 -05:00
parent 7718ca84a7
commit 8e23cb1d98
2 changed files with 23 additions and 24 deletions

View File

@ -26,9 +26,9 @@ impl DummyNetworkConnection {
pub enum NetworkConnection { pub enum NetworkConnection {
Dummy(DummyNetworkConnection), Dummy(DummyNetworkConnection),
RawTcp(tcp::RawTcpNetworkConnection), RawTcp(tcp::RawTcpNetworkConnection),
WSAccepted(ws::WebSocketNetworkConnectionAccepted), WsAccepted(ws::WebSocketNetworkConnectionAccepted),
WS(ws::WebsocketNetworkConnectionWS), Ws(ws::WebsocketNetworkConnectionWS),
WSS(ws::WebsocketNetworkConnectionWSS), Wss(ws::WebsocketNetworkConnectionWSS),
//WebRTC(wrtc::WebRTCNetworkConnection), //WebRTC(wrtc::WebRTCNetworkConnection),
} }
@ -37,27 +37,27 @@ impl NetworkConnection {
match self { match self {
Self::Dummy(d) => d.protocol_type(), Self::Dummy(d) => d.protocol_type(),
Self::RawTcp(t) => t.protocol_type(), Self::RawTcp(t) => t.protocol_type(),
Self::WSAccepted(w) => w.protocol_type(), Self::WsAccepted(w) => w.protocol_type(),
Self::WS(w) => w.protocol_type(), Self::Ws(w) => w.protocol_type(),
Self::WSS(w) => w.protocol_type(), Self::Wss(w) => w.protocol_type(),
} }
} }
pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), ()>> { pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), ()>> {
match self { match self {
Self::Dummy(d) => d.send(message), Self::Dummy(d) => d.send(message),
Self::RawTcp(t) => t.send(message), Self::RawTcp(t) => t.send(message),
Self::WSAccepted(w) => w.send(message), Self::WsAccepted(w) => w.send(message),
Self::WS(w) => w.send(message), Self::Ws(w) => w.send(message),
Self::WSS(w) => w.send(message), Self::Wss(w) => w.send(message),
} }
} }
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, ()>> { pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, ()>> {
match self { match self {
Self::Dummy(d) => d.recv(), Self::Dummy(d) => d.recv(),
Self::RawTcp(t) => t.recv(), Self::RawTcp(t) => t.recv(),
Self::WSAccepted(w) => w.recv(), Self::WsAccepted(w) => w.recv(),
Self::WS(w) => w.recv(), Self::Ws(w) => w.recv(),
Self::WSS(w) => w.recv(), Self::Wss(w) => w.recv(),
} }
} }
} }

View File

@ -73,9 +73,9 @@ where
{ {
pub fn new(tls: bool, ws_stream: WebSocketStream<T>) -> Self { pub fn new(tls: bool, ws_stream: WebSocketStream<T>) -> Self {
Self { Self {
tls: tls, tls,
inner: Arc::new(AsyncMutex::new(WebSocketNetworkConnectionInner { inner: Arc::new(AsyncMutex::new(WebSocketNetworkConnectionInner {
ws_stream: ws_stream, ws_stream,
})), })),
} }
} }
@ -153,11 +153,11 @@ impl WebsocketProtocolHandler {
}; };
let inner = WebsocketProtocolHandlerInner { let inner = WebsocketProtocolHandlerInner {
tls: tls, tls,
network_manager: network_manager, network_manager,
local_address: local_address, local_address,
request_path: path.as_bytes().to_vec(), request_path: path.as_bytes().to_vec(),
connection_initial_timeout: connection_initial_timeout, connection_initial_timeout,
}; };
Self { Self {
inner: Arc::new(inner), inner: Arc::new(inner),
@ -170,8 +170,7 @@ impl WebsocketProtocolHandler {
socket_addr: SocketAddr, socket_addr: SocketAddr,
) -> Result<bool, ()> { ) -> Result<bool, ()> {
let request_path_len = self.inner.request_path.len() + 2; let request_path_len = self.inner.request_path.len() + 2;
let mut peekbuf: Vec<u8> = Vec::with_capacity(request_path_len); let mut peekbuf: Vec<u8> = vec![0u8; request_path_len];
peekbuf.resize(request_path_len, 0u8);
match io::timeout( match io::timeout(
Duration::from_micros(self.inner.connection_initial_timeout), Duration::from_micros(self.inner.connection_initial_timeout),
ps.peek_exact(&mut peekbuf), ps.peek_exact(&mut peekbuf),
@ -217,7 +216,7 @@ impl WebsocketProtocolHandler {
protocol_type, protocol_type,
); );
let conn = NetworkConnection::WSAccepted(WebsocketNetworkConnection::new( let conn = NetworkConnection::WsAccepted(WebsocketNetworkConnection::new(
self.inner.tls, self.inner.tls,
ws_stream, ws_stream,
)); ));
@ -225,7 +224,7 @@ impl WebsocketProtocolHandler {
.network_manager .network_manager
.clone() .clone()
.on_new_connection( .on_new_connection(
ConnectionDescriptor::new(peer_addr, self.inner.local_address.clone()), ConnectionDescriptor::new(peer_addr, self.inner.local_address),
conn, conn,
) )
.await?; .await?;
@ -269,7 +268,7 @@ impl WebsocketProtocolHandler {
let connector = TlsConnector::default(); let connector = TlsConnector::default();
let tls_stream = connector.connect(domain, tcp_stream).await.map_err(drop)?; let tls_stream = connector.connect(domain, tcp_stream).await.map_err(drop)?;
let (ws_stream, _response) = client_async(request, tls_stream).await.map_err(drop)?; let (ws_stream, _response) = client_async(request, tls_stream).await.map_err(drop)?;
let conn = NetworkConnection::WSS(WebsocketNetworkConnection::new(tls, ws_stream)); let conn = NetworkConnection::Wss(WebsocketNetworkConnection::new(tls, ws_stream));
network_manager network_manager
.on_new_connection( .on_new_connection(
ConnectionDescriptor::new(peer_addr, local_addr), ConnectionDescriptor::new(peer_addr, local_addr),
@ -279,7 +278,7 @@ impl WebsocketProtocolHandler {
Ok(conn) Ok(conn)
} else { } else {
let (ws_stream, _response) = client_async(request, tcp_stream).await.map_err(drop)?; let (ws_stream, _response) = client_async(request, tcp_stream).await.map_err(drop)?;
let conn = NetworkConnection::WS(WebsocketNetworkConnection::new(tls, ws_stream)); let conn = NetworkConnection::Ws(WebsocketNetworkConnection::new(tls, ws_stream));
network_manager network_manager
.on_new_connection( .on_new_connection(
ConnectionDescriptor::new(peer_addr, local_addr), ConnectionDescriptor::new(peer_addr, local_addr),