Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: use SocketListener::accept_future instead of SocketService #137

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions examples/gio-echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -36,25 +36,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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,
glib::Object::NONE,
)?;
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(())
}
Loading