diff --git a/src/tscore/src/connection/client.rs b/src/tscore/src/connection/client.rs index 1da8c13..3e15d71 100644 --- a/src/tscore/src/connection/client.rs +++ b/src/tscore/src/connection/client.rs @@ -159,6 +159,14 @@ impl Client { self.client_id } + pub fn shared_secret(&self) -> &Option { + &self.shared_secret + } + + pub fn key_cache_mut(&mut self) -> &mut KeyCache { + &mut self.key_cache + } + /// 开始连接握手 pub fn start_handshake(&mut self) -> Result, ProtocolError> { self.state_machine diff --git a/src/tscore/src/connection/session.rs b/src/tscore/src/connection/session.rs index e6b6d11..80f5396 100644 --- a/src/tscore/src/connection/session.rs +++ b/src/tscore/src/connection/session.rs @@ -4,7 +4,7 @@ use tokio::sync::mpsc; use super::client::{ChannelEntry, Client, ClientConfig, ClientEntry, CommandEvent, HandleResult}; use super::state::ConnectionState; -use crate::protocol::Command; +use crate::protocol::{parse_voice_packet, Command, Direction, InPacket, PacketType, VoiceData}; use crate::ProtocolError; pub enum SessionCommand { @@ -67,6 +67,12 @@ pub enum SessionEvent { clients_online: u16, channels_online: u16, }, + VoiceData { + codec: u8, + packet_id: u16, + audio_data: Vec, + is_whisper: bool, + }, Error(String), Disconnected, } @@ -256,7 +262,18 @@ impl Session { tokio::select! { result = self.socket.recv(&mut buf) => { let len = result?; - let handle_result = self.client.handle_data(&buf[..len])?; + let data = &buf[..len]; + + if let Ok(packet) = InPacket::parse(Direction::S2C, data) { + let packet_type = packet.header.flags.packet_type(); + + if packet_type == PacketType::Voice || packet_type == PacketType::VoiceWhisper { + self.handle_voice_packet(&packet).await; + continue; + } + } + + let handle_result = self.client.handle_data(data)?; for response in &handle_result.responses { self.socket.send(response).await?; } @@ -280,6 +297,38 @@ impl Session { } } + async fn handle_voice_packet(&mut self, packet: &InPacket) { + let voice_data = match parse_voice_packet(packet) { + Ok(v) => v, + Err(_) => return, + }; + + match voice_data { + VoiceData::Normal(voice) => { + let _ = self + .event_tx + .send(SessionEvent::VoiceData { + codec: voice.codec.to_u8(), + packet_id: voice.packet_id, + audio_data: voice.audio_data, + is_whisper: false, + }) + .await; + } + VoiceData::Whisper(whisper) => { + let _ = self + .event_tx + .send(SessionEvent::VoiceData { + codec: whisper.codec.to_u8(), + packet_id: whisper.packet_id, + audio_data: whisper.audio_data, + is_whisper: true, + }) + .await; + } + } + } + async fn emit_session_event(&self, event: CommandEvent) { let session_event = match event { CommandEvent::InitServer {