From 673eed4e80ebd418fffccd65f73ae21d087fdd20 Mon Sep 17 00:00:00 2001 From: Filip Kojro Date: Sun, 16 Aug 2026 15:41:18 +0200 Subject: [PATCH] using standardized tcp requests and responses --- client/src/main.rs | 50 +++++++++++++++++++++++------------ server/src/main.rs | 65 ++++++++++++++++++++++++++-------------------- shared/src/lib.rs | 44 +++++++++++++++++++++++++++++-- 3 files changed, 113 insertions(+), 46 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 1e4a3b1..cdbd08d 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -3,36 +3,54 @@ use ringbuf::{ HeapRb, traits::{Consumer, Observer, Producer, Split}, }; -use shared::{ConectionResponse, ResponseStatus}; +use shared::{ResponseStatus, TcpRequest, TcpResponse, TcpResponseData}; use std::sync::Arc; -use tokio::net::{TcpStream, UdpSocket}; +use tokio::{ + io::AsyncWriteExt, + net::{TcpStream, UdpSocket}, +}; #[tokio::main] async fn main() { let server_address = "127.0.0.1:8080"; - let tcp_stream = TcpStream::connect(server_address) + let mut tcp_stream = TcpStream::connect(server_address) .await .expect("failed to connect using tcp"); + let _ = tcp_stream + .write_all( + serde_json::to_string(&TcpRequest { + client_id: 0, + data: shared::TcpRequestData::NewConnection, + }) + .expect("failed to parse initial request") + .as_bytes(), + ) + .await + .expect("failed to send initial message"); + tcp_stream.readable().await.expect("idk"); let mut tcp_buf = [0; 4096]; let mut session_client_id: u32 = 0; match tcp_stream.try_read(&mut tcp_buf) { - Ok(n) => { - let connection_response: ConectionResponse = - serde_json::from_slice(&tcp_buf[0..n]).unwrap(); - - session_client_id = connection_response.client_id; - - println!( - "tcp response status Ok: {}, clientId: {}", - connection_response.status == ResponseStatus::Ok, - connection_response.client_id - ); - } + Ok(n) => match serde_json::from_slice::(&tcp_buf[0..n]) { + Ok(response) => { + println!("connection Ok: {}", response.status == ResponseStatus::Ok); + match response.data { + TcpResponseData::NewConnection { client_id } => { + session_client_id = client_id; + println!("new client id: {}", client_id) + } + _ => { + println!("wrong response") + } + } + } + Err(e) => eprintln!("failed to parse tcp packet: {}", e), + }, Err(e) => eprintln!("failed to receive tcp packet: {}", e), } @@ -133,7 +151,7 @@ async fn main() { } let id_bytes: [u8; 4] = receive_buffer[0..4].try_into().unwrap(); - let client_id = u32::from_be_bytes(id_bytes); + let stream_id = u32::from_be_bytes(id_bytes); let audio_packet = &receive_buffer[4..len]; diff --git a/server/src/main.rs b/server/src/main.rs index 59d39ec..b8cbafa 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,4 +1,4 @@ -use shared::{ConectionResponse, ResponseStatus}; +use shared::{AudioStreamInfo, ResponseStatus, TcpRequest, TcpRequestData, TcpResponse}; use std::sync::Arc; use std::{collections::HashMap, net::SocketAddr}; use tokio::io::AsyncWriteExt; @@ -10,6 +10,7 @@ use tokio::{ struct Client { tcp_addr: SocketAddr, udp_addr: Option, + audio_streams: Option>, } type State = Arc>>; @@ -66,34 +67,42 @@ async fn main() { let (mut tcp_stream, tcp_address) = tcp_listener.accept().await.expect("failed to accept tcp"); - let client_id = next_client_id; - next_client_id += 1; + tcp_stream.readable().await.expect("idk"); + let mut tcp_buf = [0; 4096]; - tcp_state.write().await.insert( - client_id, - Client { - tcp_addr: tcp_address, - udp_addr: None, + match tcp_stream.try_read(&mut tcp_buf) { + Ok(n) => match serde_json::from_slice::(&tcp_buf[0..n]) { + Ok(request) => match request.data { + TcpRequestData::NewConnection => { + let client_id = next_client_id; + next_client_id += 1; + tcp_state.write().await.insert( + client_id, + Client { + tcp_addr: tcp_address, + udp_addr: None, + audio_streams: None, + }, + ); + println!("new client: {}, from: {}", client_id, tcp_address); + let _ = tcp_stream + .write_all( + serde_json::to_string(&TcpResponse { + status: ResponseStatus::Ok, + data: shared::TcpResponseData::NewConnection { + client_id: client_id, + }, + }) + .expect("failed to parse response json") + .as_bytes(), + ) + .await; + } + _ => eprintln!("i dont support other requests yet"), + }, + Err(e) => eprintln!("failed to parse tcp packet: {}", e), }, - ); - - let welcome_msg = ConectionResponse { - status: ResponseStatus::Ok, - client_id: client_id, - }; - - let _ = tcp_stream - .write_all( - serde_json::to_string(&welcome_msg) - .expect("failed to parse json") - .as_bytes(), - ) - .await; - - println!( - "new client: {}, from: {}", - client_id, - tcp_state.read().await.get(&client_id).unwrap().tcp_addr - ); + Err(e) => eprintln!("failed to receive tcp packet: {}", e), + } } } diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 513c4bc..b35a7b3 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -7,7 +7,47 @@ pub enum ResponseStatus { } #[derive(Serialize, Deserialize)] -pub struct ConectionResponse { +pub struct AudioStreamInfo { + pub stream_id: u32, + pub channels: u8, + pub sample_rate: u32, +} + +#[derive(Serialize, Deserialize)] +pub struct TcpRequest { + pub client_id: u32, + pub data: TcpRequestData, +} + +#[derive(Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum TcpRequestData { + NewConnection, + DropConnection, + NewAudioStream { audio_stream_info: AudioStreamInfo }, + DropAudioStream { audio_stream_id: u32 }, +} + +#[derive(Serialize, Deserialize)] +pub struct TcpResponse { pub status: ResponseStatus, - pub client_id: u32, + pub data: TcpResponseData, +} + +#[derive(Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum TcpResponseData { + NewConnection { + client_id: u32, + }, + Updated, + UserJoined { + user_id: u32, + }, + UserDisconnected { + user_id: u32, + }, + AudioStreamsUpdate { + audio_stream_info: Vec, + }, }