-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
625 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::core::service::PacketHandler; | ||
use crate::protocol::NetPacket; | ||
use anyhow::Context; | ||
use futures_util::{SinkExt, StreamExt}; | ||
use std::net::SocketAddr; | ||
use tokio::net::TcpStream; | ||
use tokio::sync::mpsc::channel; | ||
use tokio_tungstenite::accept_async; | ||
use tokio_tungstenite::tungstenite::Message; | ||
|
||
pub async fn handle_websocket_connection( | ||
stream: TcpStream, | ||
addr: SocketAddr, | ||
handler: PacketHandler, | ||
) { | ||
tokio::spawn(async move { | ||
if let Err(e) = handle_websocket_connection0(stream, addr, handler).await { | ||
log::warn!("websocket err {:?} {}", e, addr); | ||
} | ||
}); | ||
} | ||
|
||
async fn handle_websocket_connection0( | ||
stream: TcpStream, | ||
addr: SocketAddr, | ||
handler: PacketHandler, | ||
) -> anyhow::Result<()> { | ||
let ws_stream = accept_async(stream) | ||
.await | ||
.with_context(|| format!("Error during WebSocket handshake {}", addr))?; | ||
|
||
let (mut ws_write, mut ws_read) = ws_stream.split(); | ||
|
||
let (sender, mut receiver) = channel::<Vec<u8>>(100); | ||
tokio::spawn(async move { | ||
while let Some(data) = receiver.recv().await { | ||
if let Err(e) = ws_write.send(Message::Binary(data)).await { | ||
log::warn!("websocket err {:?} {}", e, addr); | ||
break; | ||
} | ||
} | ||
let _ = ws_write.close().await; | ||
}); | ||
let sender = Some(sender); | ||
while let Some(msg) = ws_read.next().await { | ||
let msg = msg.with_context(|| format!("Error during WebSocket read {}", addr))?; | ||
match msg { | ||
Message::Text(txt) => log::info!("Received text message: {} {}", txt, addr), | ||
Message::Binary(mut data) => { | ||
let packet = NetPacket::new0(data.len(), &mut data)?; | ||
if let Some(rs) = handler.handle(packet, addr, &sender).await { | ||
if sender | ||
.as_ref() | ||
.unwrap() | ||
.send(rs.buffer().to_vec()) | ||
.await | ||
.is_err() | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
Message::Ping(_) | Message::Pong(_) => (), | ||
Message::Close(_) => break, | ||
_ => {} | ||
} | ||
} | ||
return Ok(()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.