Fix server greeting

This commit is contained in:
Joscha 2022-02-27 15:20:00 +01:00
parent 2f56ea804f
commit 3cd027cd15

View file

@ -6,13 +6,13 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use anyhow::anyhow; use anyhow::{anyhow, bail};
use cove_core::conn::{self, ConnMaintenance, ConnRx, ConnTx}; use cove_core::conn::{self, ConnMaintenance, ConnRx, ConnTx};
use cove_core::packets::{ use cove_core::packets::{
Cmd, JoinNtf, NickCmd, NickNtf, NickRpl, Packet, PartNtf, SendCmd, SendNtf, SendRpl, WhoCmd, Cmd, IdentifyCmd, IdentifyRpl, JoinNtf, NickCmd, NickNtf, NickRpl, Packet, PartNtf, RoomCmd,
WhoRpl, RoomRpl, SendCmd, SendNtf, SendRpl, WhoCmd, WhoRpl,
}; };
use cove_core::{Message, MessageId, Session, SessionId}; use cove_core::{Identity, Message, MessageId, Session, SessionId};
use log::{info, warn}; use log::{info, warn};
use rand::Rng; use rand::Rng;
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
@ -235,92 +235,105 @@ impl Server {
.clone() .clone()
} }
// async fn handle_hello( async fn negotiate_room(tx: &ConnTx, rx: &mut ConnRx) -> anyhow::Result<String> {
// &self, loop {
// tx: &ConnTx, match rx.recv().await? {
// id: u64, Some(Packet::Cmd {
// cmd: IdentifyCmd, id,
// ) -> anyhow::Result<Option<(String, Session)>> { cmd: Cmd::Room(RoomCmd { name }),
// if let Some(reason) = util::check_room(&cmd.room) { }) => {
// tx.send(&Packet::rpl(id, IdentifyRpl::InvalidRoom { reason }))?; if let Some(reason) = util::check_room(&name) {
// return Ok(None); tx.send(&Packet::rpl(id, RoomRpl::InvalidRoom { reason }))?;
// } continue;
// if let Some(reason) = util::check_nick(&cmd.nick) { }
// tx.send(&Packet::rpl(id, IdentifyRpl::InvalidNick { reason }))?; tx.send(&Packet::rpl(id, RoomRpl::Success))?;
// return Ok(None); return Ok(name);
// } }
// if let Some(reason) = util::check_identity(&cmd.identity) { Some(_) => bail!("invalid packet during room negotiation"),
// tx.send(&Packet::rpl(id, IdentifyRpl::InvalidIdentity { reason }))?; None => bail!("connection closed during room negotiation"),
// return Ok(None); }
// } }
}
// let session = Session { async fn negotiate_identity(tx: &ConnTx, rx: &mut ConnRx) -> anyhow::Result<(u64, Session)> {
// id: SessionId::of(&format!("{}", rand::thread_rng().gen::<u64>())), loop {
// nick: cmd.nick, match rx.recv().await? {
// identity: Identity::of(&cmd.identity), Some(Packet::Cmd {
// }; id,
cmd: Cmd::Identify(IdentifyCmd { nick, identity }),
}) => {
if let Some(reason) = util::check_identity(&identity) {
tx.send(&Packet::rpl(id, IdentifyRpl::InvalidNick { reason }))?;
continue;
}
if let Some(reason) = util::check_nick(&nick) {
tx.send(&Packet::rpl(id, IdentifyRpl::InvalidNick { reason }))?;
continue;
}
let session = Session {
id: SessionId::of(&format!("{}", rand::thread_rng().gen::<u64>())),
nick,
identity: Identity::of(&identity),
};
return Ok((id, session));
}
Some(_) => bail!("invalid packet during room negotiation"),
None => bail!("connection closed during room negotiation"),
}
}
}
// Ok(Some((cmd.room, session))) fn welcome(id: u64, you: Session, room: &Room, tx: &ConnTx) -> anyhow::Result<()> {
// } let others = room
.clients
.values()
.map(|client| client.session.clone())
.collect::<Vec<_>>();
let last_message = room.last_message;
// async fn greet(&self, tx: ConnTx, mut rx: ConnRx) -> anyhow::Result<ServerSession> { tx.send(&Packet::rpl(
// let (id, room, session) = loop { id,
// let (id, cmd) = match rx.recv().await? { IdentifyRpl::Success {
// Some(Packet::Cmd { you,
// id, others,
// cmd: Cmd::Hello(cmd), last_message,
// }) => (id, cmd), },
// Some(_) => return Err(anyhow!("not a Hello packet")), ))?;
// None => return Err(anyhow!("connection closed during greeting")),
// };
// if let Some((room, session)) = self.handle_hello(&tx, id, cmd).await? { Ok(())
// break (id, room, session); }
// }
// };
// let room = self.room(room).await; async fn greet(&self, tx: ConnTx, mut rx: ConnRx) -> anyhow::Result<ServerSession> {
let room = Self::negotiate_room(&tx, &mut rx).await?;
let (id, session) = Self::negotiate_identity(&tx, &mut rx).await?;
// { let room = self.room(room).await;
// let mut room = room.lock().await; {
let mut room = room.lock().await;
// Reply to successful identify command in the same lock as joining
// the room so the client doesn' miss any messages.
Self::welcome(id, session.clone(), &*room, &tx)?;
// Join room only after welcome so current session is not yet
// present in room during welcome.
room.join(Client {
session: session.clone(),
send: tx.clone(),
});
}
// let you = session.clone(); Ok(ServerSession {
// let others = room tx,
// .clients rx,
// .values() room,
// .map(|client| client.session.clone()) session,
// .collect::<Vec<_>>(); })
// let last_message = room.last_message; }
// tx.send(&Packet::rpl(
// id,
// IdentifyRpl::Success {
// you,
// others,
// last_message,
// },
// ))?;
// room.join(Client {
// session: session.clone(),
// send: tx.clone(),
// });
// }
// Ok(ServerSession {
// tx,
// rx,
// room,
// session,
// })
// }
async fn greet_and_run(&self, tx: ConnTx, rx: ConnRx) -> anyhow::Result<()> { async fn greet_and_run(&self, tx: ConnTx, rx: ConnRx) -> anyhow::Result<()> {
// let mut session = self.greet(tx, rx).await?; let mut session = self.greet(tx, rx).await?;
// let result = session.run().await; let result = session.run().await;
// session.room.lock().await.part(session.session.id); session.room.lock().await.part(session.session.id);
// result result
todo!()
} }
/// Wrapper for [`ConnMaintenance::perform`] so it returns an /// Wrapper for [`ConnMaintenance::perform`] so it returns an