// Copyright (C) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only use { crate::interaction::shared::Shared, std::sync::Arc, tokio::{ net::TcpStream, sync::{mpsc, Mutex}, }, tokio_util::codec::{BytesCodec, Framed}, }; pub struct Peer { pub bytes: Framed, pub rx: mpsc::UnboundedReceiver, } impl Peer { pub async fn new( state: Arc>, bytes: Framed, username: String, ) -> std::io::Result { let (tx, rx) = mpsc::unbounded_channel(); state.lock().await.peers.insert(username, tx); Ok(Self { bytes, rx }) } pub async fn _change_username( self, state: Arc>, username: &str, new_username: &str, ) { // Remove peer from peers { state.lock().await.peers.remove(username); } // Add the peer back with the new username Self::new(state, self.bytes, new_username.to_string()).await.unwrap(); } }