resonator 0.1.0

This crate allows 2 devices to send live PCM audio data to each other through a server
Documentation
use std::{sync::{Arc, Mutex}, net::TcpStream, io::{Write, Read}, thread};

use crate::{common::audiobuffer::AudioBuffer, config::BUFFER_SIZE};

pub struct ResonatorClient {
    send_port: u16,                                  //The port which is used for sending outbound audio buffers
    receive_port: u16,                               //The port which is used for receiving inbound audio buffers
    outgoning_buffers: Arc<Mutex<Vec<AudioBuffer>>>, //The buffers which are to be sent to the server
    incoming_buffers: Arc<Mutex<Vec<AudioBuffer>>>,  //The buffers which have been received from the server, but not yet processed
    id: i32,                                         //The id of this client
    reciever_id: i32                                 //The id of the client which this client is sending audio to
}

impl ResonatorClient {
    pub fn new(send_port: u16, receive_port: u16, id: i32, reciever_id: i32) -> ResonatorClient {
        ResonatorClient {
            send_port,
            receive_port,
            outgoning_buffers: Arc::new(Mutex::new(Vec::new())),
            incoming_buffers: Arc::new(Mutex::new(Vec::new())),
            id,
            reciever_id
        }
    }

    //Getter function for returning the vector of recieved audio buffers
    pub fn get_incoming_buffers(&self) -> Arc<Mutex<Vec<AudioBuffer>>> {
        self.incoming_buffers.clone()
    }

    //Returns the first buffer in the incoming buffers vec, and removes it
    pub fn process_buffer(&self) -> Option<AudioBuffer> {
        if self.incoming_buffers.lock().unwrap().len() > 0 {
            return Some(self.incoming_buffers.lock().unwrap().remove(0));
        }

        None
    }

    //Pushes a buffer to the outgoing buffers vec to be sent to the server
    pub fn push_buffer(&self, audio_buffer: AudioBuffer) {
        self.outgoning_buffers.lock().unwrap().push(audio_buffer);
    }

    //Begins the client, spawning the reciever and sender threads
    pub fn begin(&self) {
        println!("Starting client on ports {} and {}", self.send_port, self.receive_port);

        {
            let receive_port = self.receive_port.clone();
            let incoming_buffers = self.incoming_buffers.clone();
            let id = self.id.clone();
            thread::spawn(move || {
                ResonatorClient::_reciever_thread(receive_port.clone(), incoming_buffers.clone(), id.clone());
                loop {}
            });
        }

        {
            let send_port = self.send_port.clone();
            let outgoning_buffers = self.outgoning_buffers.clone();
            let id = self.id.clone();
            let receiver_id = self.reciever_id.clone();

            thread::spawn(move || {
                ResonatorClient::_sender_thread(send_port.clone(), outgoning_buffers.clone(), id.clone(), receiver_id.clone());
                loop {}
            });
        }
    }

    fn _reciever_thread(port: u16, incoming_buffers: Arc<Mutex<Vec<AudioBuffer>>>, id: i32) {
        let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).unwrap();
        stream.write_all(&id.to_le_bytes()).unwrap();

        let mut buffer = [0; BUFFER_SIZE];
        loop {
            match stream.read(&mut buffer) {
                Ok(size) if size == 0 => {
                    println!("Client closed connection");
                    break;
                }
                Ok(size) => size,
                Err(_) => {
                    println!("Client error");
                    break;
                }
            };

            //Parsed buffer ready to be played
            let audio_buffer = AudioBuffer::from_bytes(&buffer);
            incoming_buffers.lock().unwrap().push(audio_buffer.clone());
            println!("Received buffer!");
            //println!("{:?}", audio_buffer);
        }
    }

    fn _sender_thread(port: u16, outgoning_buffers: Arc<Mutex<Vec<AudioBuffer>>>, id: i32, reciever_id: i32) {
        let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port)).unwrap();

        //Send buffer every 10 secs
        loop {
            //If no outgoing buffers in the queue, skip
            if outgoning_buffers.lock().unwrap().len() == 0 {
                continue;
            }

            //Otherwise send the first buffer in the outgoing buffers vec
            println!("Sending buffer from client");
            let audio_buffer = outgoning_buffers.lock().unwrap().remove(0);
            let raw_buffer = audio_buffer.as_buffer().unwrap().raw_buffer_with_header(id, reciever_id);
            stream.write_all(&raw_buffer).unwrap();
        }
    }
}