bevy_stokes

Crates.iobevy_stokes
lib.rsbevy_stokes
version0.1.0-alpha.2
sourcesrc
created_at2022-02-15 05:12:08.565396
updated_at2022-02-18 12:12:28.521464
descriptionA bevy plugin providing a thin and ergonomic wrapper around laminar.
homepagehttps://github.com/hlb8122/bevy_stokes
repositoryhttps://github.com/hlb8122/bevy_stokes
max_upload_size
id532569
size97,927
Harry Barber (hlb8122)

documentation

README

bevy_stokes

A bevy plugin providing a thin and ergonomic wrapper around laminar.

License Cargo Documentation

Example

Ping

fn setup(mut commands: Commands) {
    let addr: SocketAddr = PING_ADDR.parse().unwrap();
    let socket_bundle = bind(addr, Duration::from_millis(10)).unwrap();
    commands.spawn_bundle(socket_bundle);
}

fn ping(mut socket_query: Query<&mut SendQueue, With<SocketMarker>>) {
    let mut packet_queue = socket_query.single_mut();
    let ping = Packet::reliable_unordered(PONG_ADDR.parse().unwrap(), b"DEADBEEF".to_vec());
    packet_queue.send(ping);
    info!("sent ping");
}

fn pong(mut connection_query: Query<&mut ReceiveQueue, With<ConnectionMarker>>) {
    if let Ok(mut queue) = connection_query.get_single_mut() {
        for _ in queue.drain() {
            info!("received pong");
        }
    }
}

Pong

fn setup(mut commands: Commands) {
    let addr: SocketAddr = PONG_ADDR.parse().unwrap();
    let socket_bundle = bind(addr, Duration::from_millis(10)).unwrap();
    commands.spawn_bundle(socket_bundle);
}

fn pong(
    mut socket_query: Query<&mut SendQueue, With<SocketMarker>>,
    mut connection_query: Query<
        (&SocketId, &ConnectionAddress, &mut ReceiveQueue),
        With<ConnectionMarker>,
    >,
) {
    if let Ok((socket_id, conn_addr, mut queue)) = connection_query.get_single_mut() {
        for ping in queue.drain() {
            info!("received ping");

            let mut packet_queue = socket_query.get_mut(socket_id.0).unwrap();
            let pong = Packet::reliable_unordered(conn_addr.0, ping.payload().to_vec());
            packet_queue.send(pong);
            info!("returned pong");
        }
    }
}
Commit count: 11

cargo fmt