From 6030fe38623970b7c33a9f4e0427eecbe6fbc8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 7 Jul 2024 21:55:31 +0200 Subject: [PATCH] examples: use SocketListener::accept_future instead of SocketService `SocketService` wraps `SocketListener` to accept connections asynchronously and pass them along in the callback. This is unnecessary in an async world as we have `SocketListener::accept_future` which allows us to accept connections asynchronously in a loop. --- examples/gio-echo-server.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/gio-echo-server.rs b/examples/gio-echo-server.rs index bbef8e1..cdad4d7 100644 --- a/examples/gio-echo-server.rs +++ b/examples/gio-echo-server.rs @@ -3,7 +3,7 @@ use std::{env, net::SocketAddr}; use async_tungstenite::{gio::accept_async, tungstenite::Result}; use futures::prelude::*; use gio::{ - prelude::*, InetSocketAddress, SocketConnection, SocketProtocol, SocketService, SocketType, + prelude::*, InetSocketAddress, SocketConnection, SocketListener, SocketProtocol, SocketType, }; async fn accept_connection(stream: SocketConnection) -> Result<()> { @@ -36,8 +36,8 @@ fn main() -> Result<(), Box> { let sockaddr: SocketAddr = addr.parse()?; let inetaddr: InetSocketAddress = sockaddr.into(); - let service = SocketService::new(); - service.add_address( + let listener = SocketListener::new(); + listener.add_address( &inetaddr, SocketType::Stream, SocketProtocol::Tcp, @@ -45,16 +45,14 @@ fn main() -> Result<(), Box> { )?; println!("Listening on: {}", inetaddr.to_string()); - service.connect_incoming(|_service, connection, _| { - let stream = connection.clone(); - glib::MainContext::default().spawn_local(async move { - accept_connection(stream).await.unwrap(); - }); - false - }); - let main_loop = glib::MainLoop::new(None, false); - main_loop.run(); + main_loop.context().block_on(async move { + while let Ok((stream, _)) = listener.accept_future().await { + glib::MainContext::default().spawn_local(async move { + accept_connection(stream).await.unwrap(); + }); + } + }); Ok(()) }