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

Refactor BRP to allow for 3rd-party transports #15438

Merged
merged 28 commits into from
Sep 27, 2024

Conversation

LiamGallagher737
Copy link
Contributor

@LiamGallagher737 LiamGallagher737 commented Sep 26, 2024

Objective

Closes #15408 (somewhat)

Solution

  • Moved the existing HTTP transport to its own module with its own plugin (RemoteHttpPlugin) (disabled on WASM)
  • Swapped out the smol crate for the smaller crates it re-exports to make it easier to keep out non-wasm code (HTTP transport needs async-io which can't build on WASM)
  • Added a new public BrpSender resource holding the matching sender for the BrpReceiver' (formally BrpMailbox). This allows other crates to send BrpMessage's to the "mailbox".

Testing

TODO

@LiamGallagher737 LiamGallagher737 marked this pull request as ready for review September 26, 2024 08:25
@LiamGallagher737 LiamGallagher737 marked this pull request as draft September 26, 2024 08:26
@IQuick143 IQuick143 added C-Feature A new feature, making something new possible A-Dev-Tools Tools used to debug Bevy applications. D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels Sep 26, 2024
@alice-i-cecile alice-i-cecile added the X-Controversial There is active debate or serious implications around merging this PR label Sep 26, 2024
@alice-i-cecile
Copy link
Member

I think this is a good idea, but it's a non-trivial maintenance burden due to the addition of another language. Does it make more sense to maintain this out-of-tree for now until the protocol is stabilized further?

@BenjaminBrienen BenjaminBrienen added the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Sep 26, 2024
@LiamGallagher737
Copy link
Contributor Author

I could edit this PR to only fix the building on WASM by extracting the HTTP in to its own module and then move the JS bindings in to my own crate.

@LiamGallagher737 LiamGallagher737 changed the title JS Bindings for BRP Refactor BRP to allow for 3rd-party transports Sep 26, 2024
@alice-i-cecile
Copy link
Member

I could edit this PR to only fix the building on WASM by extracting the HTTP in to its own module and then move the JS bindings in to my own crate.

Let's do that! That seems like a nice refactor anyways, and will be able to be merged uncontroversially.

@LiamGallagher737
Copy link
Contributor Author

Do you think the HTTP transport should have its own plugin rather than being so tied in with RemotePlugin? It won't make a difference now but if more transports do get added we will want a way to select only the ones we want.

@BenjaminBrienen BenjaminBrienen added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Controversial There is active debate or serious implications around merging this PR labels Sep 26, 2024
@alice-i-cecile
Copy link
Member

Yes, please split it out into its own plugin :)

@LiamGallagher737 LiamGallagher737 marked this pull request as ready for review September 27, 2024 09:51
crates/bevy_remote/src/http.rs Outdated Show resolved Hide resolved
crates/bevy_remote/src/http.rs Outdated Show resolved Hide resolved
crates/bevy_remote/src/lib.rs Outdated Show resolved Hide resolved
// Fetch the handler for the method. If there's no such handler
// registered, return an error.
let methods = world.resource::<RemoteMethods>();

let Some(handler) = methods.0.get(&message.method) else {
let _ = message.sender.send_blocking(Err(BrpError {
let _ = message.sender.force_send(Err(BrpError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the rationale for the change to force_send?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this ensures errors are reported even if the queue is full. This will overwrite regular messages to ensure the error message is always sent. Did I get that right @LiamGallagher737 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous send_blocking is not available on WASM. Another option is to use try_send but I don't think it is necessary as the channel is written to from in this function and only once. I could add some docs to support this.

Copy link
Contributor

@mweatherley mweatherley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor doc stuff and a small question.

Copy link
Contributor

@djeedai djeedai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, a few comments but nothing blocking.


impl Plugin for RemoteHttpPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(HostAddress(self.address))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overwrites any previous value. Should we insert only if the user didn't, to allow them to overwrite the default address and port?

I appreciate you can configure via the methods, but since world resources are "public", I think the more common pattern in Bevy is to let the user manually insert them before a plugin if they want to override any default. @alice-i-cecile any opinion on that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd prefer a manual insertion with docs.


/// A resource containing the port number that Bevy will listen on.
///
/// Currently, changing this while the application is running has no effect; this merely
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to previous comment, why are we using a resource at all / why make the field public, if this has no effect? It feels users will get confused.

It's also arguable whether we need 2 separate resources for IP and port; we won't use one without the other.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, and I think it especially makes sense now that the transport has its own plugin.

@mweatherley mweatherley added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Sep 27, 2024
@alice-i-cecile alice-i-cecile added the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Sep 27, 2024
@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Sep 27, 2024
@alice-i-cecile alice-i-cecile removed the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Sep 27, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 27, 2024
auto-merge was automatically disabled September 27, 2024 20:10

Head branch was pushed to by a user without write access

Merged via the queue into bevyengine:main with commit 60cf7ca Sep 27, 2024
29 of 30 checks passed
@LiamGallagher737 LiamGallagher737 deleted the brp-js-bindings branch September 27, 2024 20:29
rudderbucky pushed a commit to rudderbucky/bevy that referenced this pull request Sep 27, 2024
## Objective

Closes bevyengine#15408 (somewhat)

## Solution

- Moved the existing HTTP transport to its own module with its own
plugin (`RemoteHttpPlugin`) (disabled on WASM)
- Swapped out the `smol` crate for the smaller crates it re-exports to
make it easier to keep out non-wasm code (HTTP transport needs
`async-io` which can't build on WASM)
- Added a new public `BrpSender` resource holding the matching sender
for the `BrpReceiver`' (formally `BrpMailbox`). This allows other crates
to send `BrpMessage`'s to the "mailbox".

## Testing

TODO

---------

Co-authored-by: Matty <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Dev-Tools Tools used to debug Bevy applications. C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JS Bindings for BRP
6 participants