Continue implementing rooms

Changing lots of things along the way... But that's how it is: Make one
change, make more changes to fix the resulting errors and so on.
This commit is contained in:
Joscha 2022-02-19 01:01:52 +01:00
parent 31ffa5cd67
commit 992e84e67e
12 changed files with 791 additions and 141 deletions

View file

@ -14,4 +14,6 @@ sha2 = "0.10.1"
thiserror = "1.0.30"
tokio = { version = "1.16.1", features = ["full"] }
tokio-stream = "0.1.8"
tokio-tungstenite = "0.16.1"
tokio-tungstenite = { version = "0.16.1", features = [
"rustls-tls-native-roots",
] }

View file

@ -12,7 +12,7 @@ use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex;
use tokio_stream::wrappers::UnboundedReceiverStream;
use tokio_tungstenite::tungstenite::{self, Message};
use tokio_tungstenite::WebSocketStream;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use crate::packets::Packet;
@ -34,9 +34,10 @@ pub enum Error {
pub type Result<T> = result::Result<T, Error>;
type WsStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
#[derive(Clone)]
pub struct ConnTx {
peer_addr: SocketAddr,
tx: UnboundedSender<Message>,
}
@ -49,16 +50,14 @@ impl fmt::Debug for ConnTx {
impl ConnTx {
pub fn send(&self, packet: &Packet) -> Result<()> {
let str = serde_json::to_string(packet).expect("unserializable packet");
// TODO Format somewhat nicer?
debug!("<{}> ↑ {}", self.peer_addr, str.trim());
debug!("↑ {}", str.trim()); // TODO Format somewhat nicer?
self.tx.send(Message::Text(str))?;
Ok(())
}
}
pub struct ConnRx {
peer_addr: SocketAddr,
ws_rx: SplitStream<WebSocketStream<TcpStream>>,
ws_rx: SplitStream<WsStream>,
last_ping_payload: Arc<Mutex<Vec<u8>>>,
}
@ -92,8 +91,7 @@ impl ConnRx {
let packet = serde_json::from_str(&str)?;
// TODO Format somewhat nicer?
debug!("<{}> ↓ {}", self.peer_addr, str.trim());
debug!("↓ {}", str.trim()); // TODO Format somewhat nicer?
return Ok(Some(packet));
}
@ -103,7 +101,7 @@ impl ConnRx {
pub struct ConnMaintenance {
// Shoveling packets into the WS connection
rx: UnboundedReceiver<Message>,
ws_tx: SplitSink<WebSocketStream<TcpStream>, Message>,
ws_tx: SplitSink<WsStream, Message>,
// Pinging and ponging
tx: UnboundedSender<Message>,
ping_delay: Duration,
@ -127,7 +125,7 @@ impl ConnMaintenance {
async fn shovel(
rx: UnboundedReceiver<Message>,
ws_tx: SplitSink<WebSocketStream<TcpStream>, Message>,
ws_tx: SplitSink<WsStream, Message>,
) -> Result<()> {
UnboundedReceiverStream::new(rx)
.map(Ok)
@ -163,22 +161,13 @@ impl ConnMaintenance {
}
}
pub fn new(
stream: WebSocketStream<TcpStream>,
ping_delay: Duration,
) -> Result<(ConnTx, ConnRx, ConnMaintenance)> {
let peer_addr = stream.get_ref().peer_addr()?;
pub fn new(stream: WsStream, ping_delay: Duration) -> Result<(ConnTx, ConnRx, ConnMaintenance)> {
let (ws_tx, ws_rx) = stream.split();
let (tx, rx) = mpsc::unbounded_channel();
let last_ping_payload = Arc::new(Mutex::new(vec![]));
let conn_tx = ConnTx {
peer_addr,
tx: tx.clone(),
};
let conn_tx = ConnTx { tx: tx.clone() };
let conn_rx = ConnRx {
peer_addr,
ws_rx,
last_ping_payload: last_ping_payload.clone(),
};

View file

@ -4,23 +4,30 @@ use crate::macros::packets;
use crate::{Message, MessageId, Session};
#[derive(Debug, Deserialize, Serialize)]
pub struct HelloCmd {
pub room: String,
pub struct RoomCmd {
pub name: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub enum RoomRpl {
Success,
InvalidRoom { reason: String },
}
#[derive(Debug, Deserialize, Serialize)]
pub struct IdentifyCmd {
pub nick: String,
pub identity: String,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum HelloRpl {
pub enum IdentifyRpl {
Success {
you: Session,
others: Vec<Session>,
last_message: MessageId,
},
InvalidRoom {
reason: String,
},
InvalidNick {
reason: String,
},
@ -37,7 +44,7 @@ pub struct NickCmd {
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum NickRpl {
Success,
Success { you: Session },
InvalidNick { reason: String },
}
@ -86,7 +93,8 @@ pub struct SendNtf {
// Create a Cmd enum for all commands, a Rpl enum for all replies and a Ntf enum
// for all notifications, as well as TryFrom impls for the individual structs.
packets! {
cmd Hello(HelloCmd, HelloRpl),
cmd Room(RoomCmd, RoomRpl),
cmd Identify(IdentifyCmd, IdentifyRpl),
cmd Nick(NickCmd, NickRpl),
cmd Send(SendCmd, SendRpl),
cmd Who(WhoCmd, WhoRpl),