naia-lib / naia

a cross-platform (including Wasm!) networking library built in Rust. Intended to make multiplayer game development dead-simple & lightning-fast
Apache License 2.0
878 stars 58 forks source link

Decouple naia from webrtc #138

Closed cBournhonesque closed 1 year ago

cBournhonesque commented 1 year ago

The goal of this PR is to make naia compatible with any unreliable transport layer (WebRtc, UDP, QUIC).

This is accomplished by enabling feature-flags in the naia-socket crates.

Transport layers will need to implement a couple things:

(ideally there would be a trait, but it didn't seem worth the effort yet, it would be clearer once we actually try to bring a different transport layer)

Migration Guide

connorcarpenter commented 1 year ago

This is great! Will solve this issue: https://github.com/naia-lib/naia/issues/107

connorcarpenter commented 1 year ago

Hmm, after a bit more consideration, I really would like to go with another approach here.

I think the socket crates should remain as is. If anything, I might like to return them to their own GitHub repo (it was like this a while ago, I took advice against my better judgement to combine them into this monorepo). They seem to perform very well as a single responsibility crate collection.

naia-socket crates:

.. and that's it. Its only a transport layer implementation.

Now, naia_client and naia_server and other crates above that, I think should remain with the implementation you've come up with here. They could have webrtc_transport, udp_transport, quic_transport, ect. feature flags.

Server.listen() would be changed to fn listen<S: Into<Box<dyn naia_server::Socket>>>(socket: S) and likewise Client.connect() would be changed to fn connect<S: Into<Box<dyn naia_client::Socket>>>(socket: S)

Then, like you've done, there would be, in both the client and server directories, a transports directory with webrtc.rs udp.rs quic.rs that contain the trait implementations for their respective sockets.

The key here is that naia_client and naia_server should be able to compile without any *_transport feature flag enabled. I've really wanted to allow people to be able to swap out the transport layer with anything they want, even if it's defined in their own crate or a third party crate. I think this will be a better organization going forward, and I'd rather jump straight to my ideal.

Thank you for your effort on this, it'll help me jumpstart the process when I tackle this soon :+1: