xmpp-rs/tokio-xmpp/examples/echo_server.rs
Jonas Schäfer 4cfe4f8429 xmlstream: implement simple timeout logic
This allows to detect and handle dying streams without getting stuck
forever.

Timeouts are always wrong, though, so we put the burden of choosing the
right values (mostly) on the creator of a stream.
2024-09-01 10:02:36 +02:00

44 lines
1.4 KiB
Rust

use futures::{SinkExt, StreamExt};
use tokio::{self, io, net::TcpSocket};
use tokio_xmpp::parsers::stream_features::StreamFeatures;
use tokio_xmpp::xmlstream::{accept_stream, StreamHeader, Timeouts};
#[tokio::main]
async fn main() -> Result<(), io::Error> {
// TCP socket
let address = "127.0.0.1:5222".parse().unwrap();
let socket = TcpSocket::new_v4()?;
socket.bind(address)?;
let listener = socket.listen(1024)?;
// Main loop, accepts incoming connections
loop {
let (stream, _addr) = listener.accept().await?;
let stream = accept_stream(
tokio::io::BufStream::new(stream),
tokio_xmpp::parsers::ns::DEFAULT_NS,
Timeouts::default(),
)
.await?;
let stream = stream.send_header(StreamHeader::default()).await?;
let mut stream = stream
.send_features::<minidom::Element>(&StreamFeatures::default())
.await?;
tokio::spawn(async move {
while let Some(packet) = stream.next().await {
match packet {
Ok(packet) => {
println!("Received packet: {:?}", packet);
stream.send(&packet).await.unwrap();
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
});
}
}