2023-11-20 16:19:12 +00:00
|
|
|
#![feature(coroutines, coroutine_trait, type_alias_impl_trait, impl_trait_in_assoc_type)]
|
2023-09-30 23:47:18 +00:00
|
|
|
|
2023-03-11 17:36:38 +00:00
|
|
|
mod proto;
|
|
|
|
|
2023-02-17 21:27:58 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
2023-02-28 11:12:03 +00:00
|
|
|
use std::io::BufReader as SyncBufReader;
|
2023-02-17 21:27:58 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-09-30 23:47:18 +00:00
|
|
|
use anyhow::anyhow;
|
2023-02-17 21:27:58 +00:00
|
|
|
use futures_util::future::join_all;
|
|
|
|
use prometheus::Registry as MetricsRegistry;
|
2023-03-05 21:04:28 +00:00
|
|
|
use quick_xml::events::{BytesDecl, Event};
|
|
|
|
use quick_xml::{NsReader, Writer};
|
2023-08-04 22:38:56 +00:00
|
|
|
use rustls_pemfile::{certs, read_one, Item as PemItem};
|
2023-02-17 21:27:58 +00:00
|
|
|
use serde::Deserialize;
|
2023-03-06 11:05:33 +00:00
|
|
|
use tokio::io::{AsyncBufRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter};
|
2023-02-17 21:27:58 +00:00
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
use tokio::sync::mpsc::channel;
|
|
|
|
use tokio_rustls::rustls::{Certificate, PrivateKey};
|
2023-02-28 11:12:03 +00:00
|
|
|
use tokio_rustls::TlsAcceptor;
|
2023-02-17 21:27:58 +00:00
|
|
|
|
2023-09-30 23:12:11 +00:00
|
|
|
use lavina_core::player::{PlayerConnection, PlayerId, PlayerRegistry};
|
2023-09-30 23:34:35 +00:00
|
|
|
use lavina_core::prelude::*;
|
2023-10-08 13:53:39 +00:00
|
|
|
use lavina_core::repo::Storage;
|
2023-10-13 20:09:08 +00:00
|
|
|
use lavina_core::room::RoomRegistry;
|
|
|
|
use lavina_core::terminator::Terminator;
|
|
|
|
use proto_xmpp::bind::{Name, Resource};
|
2023-09-30 15:43:46 +00:00
|
|
|
use proto_xmpp::stream::*;
|
|
|
|
use proto_xmpp::xml::{Continuation, FromXml, Parser, ToXml};
|
2023-10-13 14:54:08 +00:00
|
|
|
use sasl::AuthBody;
|
2023-02-17 21:27:58 +00:00
|
|
|
|
2023-10-13 20:09:08 +00:00
|
|
|
use self::proto::ClientPacket;
|
|
|
|
|
|
|
|
mod iq;
|
|
|
|
mod message;
|
|
|
|
mod presence;
|
|
|
|
mod updates;
|
2023-03-30 12:49:39 +00:00
|
|
|
|
2023-02-17 21:27:58 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
pub struct ServerConfig {
|
|
|
|
pub listen_on: SocketAddr,
|
|
|
|
pub cert: PathBuf,
|
|
|
|
pub key: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LoadedConfig {
|
|
|
|
cert: Certificate,
|
|
|
|
key: PrivateKey,
|
|
|
|
}
|
|
|
|
|
2023-04-09 21:31:43 +00:00
|
|
|
struct Authenticated {
|
2024-04-07 12:06:23 +00:00
|
|
|
/// Identifier of the authenticated player.
|
|
|
|
///
|
|
|
|
/// Used when communicating with lavina-core on behalf of the player.
|
2023-04-09 21:31:43 +00:00
|
|
|
player_id: PlayerId,
|
2024-04-07 12:06:23 +00:00
|
|
|
/// The user's XMPP name.
|
|
|
|
///
|
|
|
|
/// Used in `to` and `from` fields of XMPP messages.
|
2023-04-09 21:31:43 +00:00
|
|
|
xmpp_name: Name,
|
2024-04-07 12:06:23 +00:00
|
|
|
/// The resource given to this user by the server.
|
2023-04-09 21:31:43 +00:00
|
|
|
xmpp_resource: Resource,
|
2024-04-07 12:06:23 +00:00
|
|
|
/// The resource used by this user when joining MUCs.
|
2023-04-09 21:31:43 +00:00
|
|
|
xmpp_muc_name: Resource,
|
|
|
|
}
|
|
|
|
|
2023-10-13 20:06:40 +00:00
|
|
|
pub struct RunningServer {
|
|
|
|
pub addr: SocketAddr,
|
|
|
|
terminator: Terminator,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RunningServer {
|
|
|
|
pub async fn terminate(self) -> Result<()> {
|
|
|
|
self.terminator.terminate().await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 21:27:58 +00:00
|
|
|
pub async fn launch(
|
|
|
|
config: ServerConfig,
|
|
|
|
players: PlayerRegistry,
|
|
|
|
rooms: RoomRegistry,
|
|
|
|
metrics: MetricsRegistry,
|
2023-10-08 13:53:39 +00:00
|
|
|
storage: Storage,
|
2023-10-13 20:06:40 +00:00
|
|
|
) -> Result<RunningServer> {
|
2023-02-17 21:27:58 +00:00
|
|
|
log::info!("Starting XMPP projection");
|
|
|
|
|
|
|
|
let certs = certs(&mut SyncBufReader::new(File::open(config.cert)?))?;
|
|
|
|
let certs = certs.into_iter().map(Certificate).collect::<Vec<_>>();
|
|
|
|
|
2023-08-04 22:38:56 +00:00
|
|
|
let key = match read_one(&mut SyncBufReader::new(File::open(config.key)?))? {
|
|
|
|
Some(PemItem::ECKey(k) | PemItem::PKCS8Key(k) | PemItem::RSAKey(k)) => PrivateKey(k),
|
2024-01-22 15:13:19 +00:00
|
|
|
_ => return Err(fail("no keys in file")),
|
2023-08-04 22:38:56 +00:00
|
|
|
};
|
2023-02-17 21:27:58 +00:00
|
|
|
|
|
|
|
let loaded_config = Arc::new(LoadedConfig {
|
|
|
|
cert: certs.into_iter().next().expect("no certs in file"),
|
2023-08-04 22:38:56 +00:00
|
|
|
key,
|
2023-02-17 21:27:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let listener = TcpListener::bind(config.listen_on).await?;
|
2023-10-13 20:06:40 +00:00
|
|
|
let addr = listener.local_addr()?;
|
|
|
|
|
2023-02-17 21:27:58 +00:00
|
|
|
let terminator = Terminator::spawn(|mut termination| async move {
|
|
|
|
let (stopped_tx, mut stopped_rx) = channel(32);
|
|
|
|
let mut actors = HashMap::new();
|
|
|
|
loop {
|
|
|
|
select! {
|
|
|
|
biased;
|
|
|
|
_ = &mut termination => break,
|
|
|
|
stopped = stopped_rx.recv() => match stopped {
|
|
|
|
Some(stopped) => { let _ = actors.remove(&stopped); },
|
|
|
|
None => unreachable!(),
|
|
|
|
},
|
|
|
|
new_conn = listener.accept() => {
|
|
|
|
match new_conn {
|
|
|
|
Ok((stream, socket_addr)) => {
|
|
|
|
log::debug!("Incoming connection from {socket_addr}");
|
|
|
|
if actors.contains_key(&socket_addr) {
|
|
|
|
log::warn!("Already contains connection form {socket_addr}");
|
|
|
|
// TODO kill the older connection and restart it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let players = players.clone();
|
|
|
|
let rooms = rooms.clone();
|
2023-10-08 13:53:39 +00:00
|
|
|
let storage = storage.clone();
|
2023-02-17 21:27:58 +00:00
|
|
|
let terminator = Terminator::spawn(|termination| {
|
|
|
|
let stopped_tx = stopped_tx.clone();
|
|
|
|
let loaded_config = loaded_config.clone();
|
|
|
|
async move {
|
2023-10-08 13:53:39 +00:00
|
|
|
match handle_socket(loaded_config, stream, &socket_addr, players, rooms, storage, termination).await {
|
2023-02-17 21:27:58 +00:00
|
|
|
Ok(_) => log::info!("Connection terminated"),
|
|
|
|
Err(err) => log::warn!("Connection failed: {err}"),
|
|
|
|
}
|
|
|
|
stopped_tx.send(socket_addr).await?;
|
|
|
|
Ok(())
|
2023-02-28 11:12:03 +00:00
|
|
|
}
|
2023-02-17 21:27:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
actors.insert(socket_addr, terminator);
|
|
|
|
},
|
|
|
|
Err(err) => log::warn!("Failed to accept new connection: {err}"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log::info!("Stopping XMPP projection");
|
2023-09-30 23:47:18 +00:00
|
|
|
join_all(actors.into_iter().map(|(socket_addr, terminator)| async move {
|
|
|
|
log::debug!("Stopping XMPP connection at {socket_addr}");
|
|
|
|
match terminator.terminate().await {
|
|
|
|
Ok(_) => log::debug!("Stopped XMPP connection at {socket_addr}"),
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("XMPP connection to {socket_addr} finished with error: {err}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2023-02-28 11:12:03 +00:00
|
|
|
.await;
|
2023-02-17 21:27:58 +00:00
|
|
|
log::info!("Stopped XMPP projection");
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
log::info!("Started XMPP projection");
|
2023-10-13 20:06:40 +00:00
|
|
|
Ok(RunningServer { addr, terminator })
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_socket(
|
|
|
|
config: Arc<LoadedConfig>,
|
|
|
|
mut stream: TcpStream,
|
|
|
|
socket_addr: &SocketAddr,
|
2023-04-09 21:31:43 +00:00
|
|
|
mut players: PlayerRegistry,
|
2023-02-17 21:27:58 +00:00
|
|
|
rooms: RoomRegistry,
|
2023-10-08 13:53:39 +00:00
|
|
|
mut storage: Storage,
|
2023-02-17 21:27:58 +00:00
|
|
|
termination: Deferred<()>, // TODO use it to stop the connection gracefully
|
|
|
|
) -> Result<()> {
|
2023-09-22 23:12:03 +00:00
|
|
|
log::info!("Received an XMPP connection from {socket_addr}");
|
2023-03-05 21:04:28 +00:00
|
|
|
let mut reader_buf = vec![];
|
|
|
|
let (reader, writer) = stream.split();
|
|
|
|
let mut buf_reader = BufReader::new(reader);
|
|
|
|
let mut buf_writer = BufWriter::new(writer);
|
|
|
|
|
2023-03-06 11:05:33 +00:00
|
|
|
socket_force_tls(&mut buf_reader, &mut buf_writer, &mut reader_buf).await?;
|
2023-02-17 21:27:58 +00:00
|
|
|
|
2023-04-05 16:57:35 +00:00
|
|
|
let mut config = tokio_rustls::rustls::ServerConfig::builder()
|
2023-02-17 21:27:58 +00:00
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(vec![config.cert.clone()], config.key.clone())?;
|
2023-04-05 16:57:35 +00:00
|
|
|
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
|
2023-02-28 11:12:03 +00:00
|
|
|
|
2023-10-13 20:06:40 +00:00
|
|
|
log::debug!("Accepting TLS connection...");
|
2023-02-17 21:27:58 +00:00
|
|
|
let acceptor = TlsAcceptor::from(Arc::new(config));
|
2023-03-05 21:04:28 +00:00
|
|
|
let new_stream = acceptor.accept(stream).await?;
|
2023-02-17 21:27:58 +00:00
|
|
|
log::debug!("TLS connection established");
|
|
|
|
|
2023-03-05 21:04:28 +00:00
|
|
|
let (a, b) = tokio::io::split(new_stream);
|
2023-03-06 11:05:33 +00:00
|
|
|
let mut xml_reader = NsReader::from_reader(BufReader::new(a));
|
2024-04-06 09:23:12 +00:00
|
|
|
let mut xml_writer = Writer::new(BufWriter::new(b));
|
2023-03-05 21:04:28 +00:00
|
|
|
|
2024-02-18 16:46:29 +00:00
|
|
|
pin!(termination);
|
|
|
|
select! {
|
|
|
|
biased;
|
|
|
|
_ = &mut termination =>{
|
|
|
|
log::info!("Socket handling was terminated");
|
|
|
|
return Ok(())
|
|
|
|
},
|
|
|
|
authenticated = socket_auth(&mut xml_reader, &mut xml_writer, &mut reader_buf, &mut storage) => {
|
|
|
|
match authenticated {
|
|
|
|
Ok(authenticated) => {
|
|
|
|
let mut connection = players.connect_to_player(authenticated.player_id.clone()).await;
|
|
|
|
socket_final(
|
|
|
|
&mut xml_reader,
|
|
|
|
&mut xml_writer,
|
|
|
|
&mut reader_buf,
|
|
|
|
&authenticated,
|
|
|
|
&mut connection,
|
|
|
|
&rooms,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Authentication error: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2023-03-05 21:04:28 +00:00
|
|
|
|
|
|
|
let a = xml_reader.into_inner().into_inner();
|
2024-04-06 09:23:12 +00:00
|
|
|
let b = xml_writer.into_inner().into_inner();
|
2023-03-05 21:04:28 +00:00
|
|
|
a.unsplit(b).shutdown().await?;
|
2023-02-17 21:27:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-03-06 11:05:33 +00:00
|
|
|
|
|
|
|
async fn socket_force_tls(
|
|
|
|
reader: &mut (impl AsyncBufRead + Unpin),
|
|
|
|
writer: &mut (impl AsyncWrite + Unpin),
|
|
|
|
reader_buf: &mut Vec<u8>,
|
|
|
|
) -> Result<()> {
|
2023-09-30 15:43:46 +00:00
|
|
|
use proto_xmpp::tls::*;
|
2023-03-06 11:49:51 +00:00
|
|
|
let xml_reader = &mut NsReader::from_reader(reader);
|
|
|
|
let xml_writer = &mut Writer::new(writer);
|
2023-03-06 11:05:33 +00:00
|
|
|
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
|
|
|
|
|
|
|
let event = Event::Decl(BytesDecl::new("1.0", None, None));
|
|
|
|
xml_writer.write_event_async(event).await?;
|
|
|
|
let msg = ServerStreamStart {
|
|
|
|
from: "localhost".into(),
|
|
|
|
lang: "en".into(),
|
2023-04-05 12:31:44 +00:00
|
|
|
id: uuid::Uuid::new_v4().to_string(),
|
2023-03-06 11:05:33 +00:00
|
|
|
version: "1.0".into(),
|
|
|
|
};
|
|
|
|
msg.write_xml(xml_writer).await?;
|
|
|
|
let msg = Features {
|
|
|
|
start_tls: true,
|
|
|
|
mechanisms: false,
|
|
|
|
bind: false,
|
|
|
|
};
|
|
|
|
msg.write_xml(xml_writer).await?;
|
|
|
|
xml_writer.get_mut().flush().await?;
|
|
|
|
|
|
|
|
let StartTLS = StartTLS::parse(xml_reader, reader_buf).await?;
|
2023-03-06 11:49:51 +00:00
|
|
|
ProceedTLS.write_xml(xml_writer).await?;
|
2023-03-06 11:05:33 +00:00
|
|
|
xml_writer.get_mut().flush().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn socket_auth(
|
|
|
|
xml_reader: &mut NsReader<(impl AsyncBufRead + Unpin)>,
|
|
|
|
xml_writer: &mut Writer<(impl AsyncWrite + Unpin)>,
|
|
|
|
reader_buf: &mut Vec<u8>,
|
2023-10-08 13:53:39 +00:00
|
|
|
storage: &mut Storage,
|
2023-04-09 21:31:43 +00:00
|
|
|
) -> Result<Authenticated> {
|
2023-03-06 11:05:33 +00:00
|
|
|
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
|
|
|
|
2023-10-13 14:54:08 +00:00
|
|
|
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
|
2023-03-06 11:05:33 +00:00
|
|
|
ServerStreamStart {
|
|
|
|
from: "localhost".into(),
|
|
|
|
lang: "en".into(),
|
2023-04-05 12:31:44 +00:00
|
|
|
id: uuid::Uuid::new_v4().to_string(),
|
2023-03-06 11:05:33 +00:00
|
|
|
version: "1.0".into(),
|
|
|
|
}
|
|
|
|
.write_xml(xml_writer)
|
|
|
|
.await?;
|
|
|
|
Features {
|
|
|
|
start_tls: false,
|
|
|
|
mechanisms: true,
|
|
|
|
bind: false,
|
|
|
|
}
|
|
|
|
.write_xml(xml_writer)
|
|
|
|
.await?;
|
|
|
|
xml_writer.get_mut().flush().await?;
|
|
|
|
|
2023-10-08 13:53:39 +00:00
|
|
|
let auth: proto_xmpp::sasl::Auth = proto_xmpp::sasl::Auth::parse(xml_reader, reader_buf).await?;
|
2023-09-30 15:43:46 +00:00
|
|
|
proto_xmpp::sasl::Success.write_xml(xml_writer).await?;
|
2024-04-06 09:23:12 +00:00
|
|
|
xml_writer.get_mut().flush().await?;
|
2023-04-13 22:38:26 +00:00
|
|
|
|
2023-10-08 13:53:39 +00:00
|
|
|
match AuthBody::from_str(&auth.body) {
|
|
|
|
Ok(logopass) => {
|
|
|
|
let name = &logopass.login;
|
|
|
|
let stored_user = storage.retrieve_user_by_name(name).await?;
|
|
|
|
|
|
|
|
let stored_user = match stored_user {
|
|
|
|
Some(u) => u,
|
|
|
|
None => {
|
|
|
|
log::info!("User '{}' not found", name);
|
|
|
|
return Err(fail("no user found"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// TODO return proper XML errors to the client
|
|
|
|
|
|
|
|
if stored_user.password.is_none() {
|
|
|
|
log::info!("Password not defined for user '{}'", name);
|
|
|
|
return Err(fail("password is not defined"));
|
|
|
|
}
|
|
|
|
if stored_user.password.as_deref() != Some(&logopass.password) {
|
|
|
|
log::info!("Incorrect password supplied for user '{}'", name);
|
|
|
|
return Err(fail("passwords do not match"));
|
|
|
|
}
|
|
|
|
|
2024-04-07 12:06:23 +00:00
|
|
|
let name: Str = name.as_str().into();
|
|
|
|
|
2023-10-08 13:53:39 +00:00
|
|
|
Ok(Authenticated {
|
2024-04-07 12:06:23 +00:00
|
|
|
player_id: PlayerId::from(name.clone())?,
|
|
|
|
xmpp_name: Name(name.clone()),
|
|
|
|
xmpp_resource: Resource(name.clone()),
|
|
|
|
xmpp_muc_name: Resource(name.clone()),
|
2023-10-08 13:53:39 +00:00
|
|
|
})
|
2023-10-13 20:09:08 +00:00
|
|
|
}
|
|
|
|
Err(e) => return Err(e),
|
2023-10-08 13:53:39 +00:00
|
|
|
}
|
2023-03-06 11:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn socket_final(
|
|
|
|
xml_reader: &mut NsReader<(impl AsyncBufRead + Unpin)>,
|
|
|
|
xml_writer: &mut Writer<(impl AsyncWrite + Unpin)>,
|
|
|
|
reader_buf: &mut Vec<u8>,
|
2023-04-09 21:31:43 +00:00
|
|
|
authenticated: &Authenticated,
|
|
|
|
user_handle: &mut PlayerConnection,
|
2023-04-11 16:28:03 +00:00
|
|
|
rooms: &RoomRegistry,
|
2023-03-06 11:05:33 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
|
|
|
|
2023-10-13 14:54:08 +00:00
|
|
|
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
|
2023-03-06 11:05:33 +00:00
|
|
|
ServerStreamStart {
|
|
|
|
from: "localhost".into(),
|
|
|
|
lang: "en".into(),
|
2023-04-05 12:31:44 +00:00
|
|
|
id: uuid::Uuid::new_v4().to_string(),
|
2023-03-06 11:05:33 +00:00
|
|
|
version: "1.0".into(),
|
|
|
|
}
|
|
|
|
.write_xml(xml_writer)
|
|
|
|
.await?;
|
|
|
|
Features {
|
|
|
|
start_tls: false,
|
|
|
|
mechanisms: false,
|
|
|
|
bind: true,
|
|
|
|
}
|
|
|
|
.write_xml(xml_writer)
|
|
|
|
.await?;
|
|
|
|
xml_writer.get_mut().flush().await?;
|
2023-03-11 23:30:48 +00:00
|
|
|
|
|
|
|
let mut parser = proto::ClientPacket::parse();
|
2023-04-11 14:42:09 +00:00
|
|
|
let mut events = vec![];
|
|
|
|
reader_buf.clear();
|
|
|
|
let mut next_xml_event = Box::pin(xml_reader.read_resolved_event_into_async(reader_buf));
|
|
|
|
|
|
|
|
'outer: loop {
|
2023-10-13 20:09:08 +00:00
|
|
|
let mut conn = XmppConnection {
|
|
|
|
user: authenticated,
|
|
|
|
user_handle,
|
|
|
|
rooms,
|
|
|
|
};
|
2023-04-11 14:42:09 +00:00
|
|
|
let should_recreate_xml_future = select! {
|
|
|
|
biased;
|
|
|
|
res = &mut next_xml_event => 's: {
|
|
|
|
let (ns, event) = res?;
|
|
|
|
if let Event::Text(ref e) = event {
|
|
|
|
if e.iter().all(|x| *x == 0xA) {
|
|
|
|
break 's true;
|
|
|
|
}
|
2023-03-28 22:36:12 +00:00
|
|
|
}
|
2023-04-11 14:42:09 +00:00
|
|
|
match parser.consume(ns, &event) {
|
|
|
|
Continuation::Final(res) => {
|
|
|
|
let res = res?;
|
2023-10-13 20:09:08 +00:00
|
|
|
let stop = conn.handle_packet(&mut events, res).await?;
|
2023-04-11 14:42:09 +00:00
|
|
|
for i in &events {
|
|
|
|
xml_writer.write_event_async(i).await?;
|
|
|
|
}
|
|
|
|
events.clear();
|
|
|
|
xml_writer.get_mut().flush().await?;
|
|
|
|
if stop {
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
parser = proto::ClientPacket::parse();
|
|
|
|
}
|
|
|
|
Continuation::Continue(p) => parser = p,
|
|
|
|
}
|
|
|
|
true
|
|
|
|
},
|
2023-10-13 20:09:08 +00:00
|
|
|
update = conn.user_handle.receiver.recv() => {
|
2023-04-11 14:42:09 +00:00
|
|
|
if let Some(update) = update {
|
2023-10-13 20:09:08 +00:00
|
|
|
conn.handle_update(&mut events, update).await?;
|
2023-04-11 14:42:09 +00:00
|
|
|
for i in &events {
|
|
|
|
xml_writer.write_event_async(i).await?;
|
|
|
|
}
|
|
|
|
events.clear();
|
|
|
|
xml_writer.get_mut().flush().await?;
|
|
|
|
} else {
|
|
|
|
log::warn!("Player is terminated, must terminate the connection");
|
2023-04-05 16:57:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-04-11 14:42:09 +00:00
|
|
|
false
|
2023-03-11 23:30:48 +00:00
|
|
|
}
|
2023-04-11 14:42:09 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
if should_recreate_xml_future {
|
|
|
|
drop(next_xml_event);
|
|
|
|
next_xml_event = Box::pin(xml_reader.read_resolved_event_into_async(reader_buf));
|
2023-03-11 23:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 16:57:35 +00:00
|
|
|
Ok(())
|
2023-03-30 12:49:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 20:09:08 +00:00
|
|
|
struct XmppConnection<'a> {
|
|
|
|
user: &'a Authenticated,
|
|
|
|
user_handle: &'a mut PlayerConnection,
|
|
|
|
rooms: &'a RoomRegistry,
|
2023-03-30 12:49:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 20:09:08 +00:00
|
|
|
impl<'a> XmppConnection<'a> {
|
|
|
|
async fn handle_packet(&mut self, output: &mut Vec<Event<'static>>, packet: ClientPacket) -> Result<bool> {
|
|
|
|
let res = match packet {
|
2024-04-04 17:49:03 +00:00
|
|
|
ClientPacket::Iq(iq) => {
|
2023-10-13 20:09:08 +00:00
|
|
|
self.handle_iq(output, iq).await;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ClientPacket::Message(m) => {
|
|
|
|
self.handle_message(output, m).await?;
|
|
|
|
false
|
|
|
|
}
|
2024-04-04 17:49:03 +00:00
|
|
|
ClientPacket::Presence(p) => {
|
2023-10-13 20:09:08 +00:00
|
|
|
self.handle_presence(output, p).await?;
|
|
|
|
false
|
|
|
|
}
|
2024-04-04 17:49:03 +00:00
|
|
|
ClientPacket::StreamEnd => {
|
2023-10-13 20:09:08 +00:00
|
|
|
ServerStreamEnd.serialize(output);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(res)
|
2023-03-30 12:49:39 +00:00
|
|
|
}
|
2023-03-30 12:31:20 +00:00
|
|
|
}
|