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

Async socket creation #136

Closed
wants to merge 1 commit into from

Conversation

garryod
Copy link
Collaborator

@garryod garryod commented Mar 11, 2023

Trying out creating the socket asynchronously, this should allow us to send the PeerId plus some arbitrary setup data before the socket is returned. This works really nicely if we're in an async context already, not as nice if we're in sync (e.g. bevy)

@garryod
Copy link
Collaborator Author

garryod commented Mar 11, 2023

Looks like we just come across a new clippy false positive

@johanhelsing
Copy link
Owner

I still have an idea to get around the PeerId issue without going async in connect. Are there other reasons we'd want it to be async?

@garryod
Copy link
Collaborator Author

garryod commented Mar 12, 2023

I still have an idea to get around the PeerId issue without going async in connect. Are there other reasons we'd want it to be async?

Transfering PeerId is the main motivating factor, though having something we can await whilst the connection is being established would also be good. What was your thought?

Edit: Just seen #140

@simbleau
Copy link
Collaborator

simbleau commented Mar 12, 2023

Just curious, how could you use the socket in Bevy as a resource if you do this?

I currently have something like this:

#[derive(Resource, Default)]
struct SocketResource {
    pub mb_socket: Option<WebRtcSocket>,
}

Which I can then use to write Bevy events. I call these "SilkSocketEvents" (my internal abstraction library is called Silk).

Here's an exact reference how I wrote that abstraction layer:

fn event_writer(
    mut socket_res: ResMut<SocketResource>,
    mut event_wtr: EventWriter<SilkSocketEvent>,
    mut connection_state: ResMut<State<ConnectionState>>,
) {
    let socket_res = socket_res.as_mut();
    if let Some(ref mut socket) = socket_res.mb_socket {
        // Connection state updates
        for (id, state) in socket.update_peers() {
            match state {
                matchbox_socket::PeerState::Connected => {
                    connection_state.set(ConnectionState::Connected).unwrap();
                    event_wtr.send(SilkSocketEvent::ConnectedToHost(id));
                }
                matchbox_socket::PeerState::Disconnected => {
                    connection_state
                        .set(ConnectionState::Disconnected)
                        .unwrap();
                    event_wtr.send(SilkSocketEvent::DisconnectedFromHost);
                }
            }
        }

        // Unreliable messages
        event_wtr.send_batch(
            socket
                .receive_on_channel(SilkSocketConfig::UNRELIABLE_CHANNEL_INDEX)
                .into_iter()
                .map(SilkSocketEvent::Message),
        );

        // Reliable messages
        event_wtr.send_batch(
            socket
                .receive_on_channel(SilkSocketConfig::RELIABLE_CHANNEL_INDEX)
                .into_iter()
                .map(SilkSocketEvent::Message),
        );

        // Id changed events
        if let Some(id) = socket.id() {
            if socket_res.id.is_none() {
                socket_res.id.replace(id.clone());
                event_wtr.send(SilkSocketEvent::IdAssigned(id));
            }
        }
    }
}

This is essentially how I translate my events for my games and projects. I'm not sure how I could that anymore.

@garryod
Copy link
Collaborator Author

garryod commented Mar 12, 2023

Just curious, how could you use the socket in Bevy as a resource if you do this?

Apologies if I'm missing something, but I believe my changes to the bevy ggrs example demonstrate this with a channel used to return the socket to the bevy startup system and subsequently insert it as a resource

@simbleau
Copy link
Collaborator

Just curious, how could you use the socket in Bevy as a resource if you do this?

Apologies if I'm missing something, but I believe my changes to the bevy ggrs example demonstrate this with a channel used to return the socket to the bevy startup system and subsequently insert it as a resource

I totally glanced over that. :) +1

@simbleau simbleau mentioned this pull request Mar 12, 2023
@garryod garryod closed this Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants