Skip to content
This repository was archived by the owner on Jan 28, 2024. It is now read-only.

Commit bac26e1

Browse files
author
Itsusinn
committed
refactor: use replace remote address with local port
1 parent 9201dbb commit bac26e1

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/server/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ use crate::{
1313
};
1414

1515
pub type WsConn = Sender<tungstenite::Message>;
16-
pub type WsConnId = Arc<SocketAddr>;
16+
pub type WsConnId = u16;
1717

1818
pub use websocket::{ws, wss};
1919

2020
pub async fn receive_packets(
2121
data: Vec<u8>,
2222
conn: Sender<tungstenite::Message>,
23-
conn_id: Arc<SocketAddr>,
23+
conn_id: u16,
2424
) -> Result<()> {
2525
let pkt: Packet = ciborium::de::from_reader(&*data)?;
2626
#[cfg(debug_assertions)]

src/server/websocket.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub async fn wss(certs: &(Vec<Certificate>, PrivateKey)) -> Result<()> {
2525
let acceptor = tokio_rustls::TlsAcceptor::from(Arc::new(config));
2626
while let Some((stream, _)) = listener.accept().await.log() {
2727
let acceptor = acceptor.clone();
28-
if let Some(peer_address) = stream.peer_addr().log()
28+
if let Some(peer_address) = stream.local_addr().log()
2929
&& let Some(stream) = acceptor.accept(stream).await.log() {
30-
accept_connection(stream, peer_address).await.eyre_log();
30+
accept_connection(stream, peer_address.port()).await.log();
3131
};
3232
}
3333
info!("wss listening stopped");
@@ -36,29 +36,26 @@ pub async fn wss(certs: &(Vec<Certificate>, PrivateKey)) -> Result<()> {
3636
pub async fn ws() -> Result<()> {
3737
let listener = TcpListener::bind(&ws_server_addr()).await?;
3838
while let Some((stream, _)) = listener.accept().await.log() {
39-
if let Some(peer_address) = stream.peer_addr().log() {
40-
accept_connection(stream, peer_address).await.eyre_log();
39+
if let Some(local_address) = stream.local_addr().log() {
40+
accept_connection(stream, local_address.port()).await.log();
4141
};
4242
}
4343
info!("ws listening stopped");
4444
Ok(())
4545
}
4646

47-
pub async fn accept_connection<S>(stream: S, peer_address: SocketAddr) -> Result<()>
47+
pub async fn accept_connection<S>(stream: S, peer_id: u16) -> Result<()>
4848
where
4949
S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
5050
{
5151
// handshake happens here
5252
let ws_stream = tokio_tungstenite::accept_async(stream).await?;
5353

54-
info!("New WebSocket connection: {}", peer_address);
55-
56-
let conn_id = Arc::new(peer_address);
54+
info!("New WebSocket connection: {}", peer_id);
5755

5856
let (tx, mut rx) = mpsc::channel(128);
5957
let (write, mut read) = ws_stream.split();
6058

61-
let conn_id_clone = conn_id.clone();
6259
tokio::spawn(async move {
6360
let mut write = write;
6461
while let Some(ws_message) = rx.recv().await {
@@ -74,14 +71,14 @@ where
7471
Ok(_) => {}
7572
};
7673
}
77-
info!("ws disconnected {}", conn_id_clone);
74+
info!("ws disconnected {}", peer_id);
7875
rx.close();
7976
});
8077
tokio::spawn(async move {
8178
let tx = tx;
8279
while let Some(next) = read.next().await {
8380
let tx = tx.clone();
84-
let conn_id = conn_id.clone();
81+
8582
match next {
8683
Err(ws::Error::ConnectionClosed) | Err(ws::Error::AlreadyClosed) => {
8784
break;
@@ -93,7 +90,7 @@ where
9390
Err(e) => error!("{:?}", e.to_eyre()),
9491
Ok(ws::Message::Binary(data)) => {
9592
tokio::spawn(async move {
96-
receive_packets(data, tx, conn_id).await.log();
93+
receive_packets(data, tx, peer_id).await.log();
9794
});
9895
}
9996
Ok(ws::Message::Ping(data)) => {
@@ -102,7 +99,7 @@ where
10299
Ok(msg) => warn!("unexpected message {}", msg),
103100
}
104101
}
105-
info!("ws disconnected {}", conn_id)
102+
info!("ws disconnected {}", peer_id)
106103
});
107104
Ok(())
108105
}

0 commit comments

Comments
 (0)