Repository navigation
Support multiple reliable and unreliable data channels - #64
Conversation
johanhelsing
left a comment
There was a problem hiding this comment.
So, I finally got back to a computer, so hopefully I'll be able to better explain what I mean :)
I'm not sure I like the Enhanced pre-/postfix and extra public struct.
It feels like the public API would be better simply with:
impl WebRtcSocket {
fn send_reliable(peer: PeerId, data: Packet)
fn send_unreliable(peer: PeerId, data: Packet)
fn receive_reliable() -> Vec<(PeerId, Packet)>
fn receive_unreliable() -> Vec<(PeerId, Packet)>
}
That should avoid confusion around to why messages are not received when you thought they would be, or alternatively why messages are re-transmitted when the you don't expect them to be. It would also avoid having two different Packet types in the public api.
Alternative: Exposing channels through optional config
However, as we've been discussing this, the other option that I'm starting to seriously reconsider, is actually exposing the concept of channels to some degree.
We could let the socket config take a Vec of data channel configs, something like this:
pub struct WebRtcSocketConfig {
pub room_url: String,
pub ice_server: RtcIceServerConfig,
/// the configuration for the data channels that will be created, the first
/// channel is the default channel, which will be used by
/// [`WebRtcSocket::send`] and [`WebRtcSocket::receive`].
/// It's possible to send on a specific channel by using
/// [WebRtcSocket::`send_on_channel`] and
/// [WebRtcSocket::`receive_on_channel`].
pub channels: Vec<ChannelConfig>,
}
pub struct ChannelConfig {
pub ordered: bool,
pub max_retransmits: Option<u16>,
}
impl ChannelConfig {
pub fn unreliable() -> Self {
ChannelConfig {
ordered: false,
max_retransmits: Some(0),
}
}
pub fn reliable() -> Self {
ChannelConfig {
ordered: true,
max_retransmits: None,
}
}
}
impl Default for WebRtcSocketConfig {
fn default() -> Self {
WebRtcSocketConfig {
room_url: "ws://localhost:3536/example_room".to_string(),
ice_server: RtcIceServerConfig::default(),
// use a single udp-like channel by default
channel_config: vec![ChannelConfig::unreliable()],
}
}
}Pros
The API for simple udp-like applications stays unchanged, they can use the default config and be happy: simply WebRtcSocket::new(room_url)
If you only need reliable channels, you can do so by creating the socket with:
let config = WebRtcSocketConfig {
room_url,
channels: vec![ChannelConfig::reliable()],
..default()
};
let socket = WebRtcSocket::new_with_config(config);And avoid the overhead and potential error-source of opening unreliable channels you don't need.
If you have some other weird use case, that's entirely possible as well. For instance you could have one unreliable channel for game state updates, one reliable for chat, and another reliable for rpcs.
Since it's more of a 1-to-1 mapping with the webrtc API, we will have to fight less to shoehorn it into some other API.
Cons
- If you don't have matching channel configs on both sides, it will fail... Most likely you are creating a p2p app, and it's the same code on both sides, so probably not a huge issue.
- Slightly more boilerplate to get reliable connections up and running
- Potentially confusing that
WebRtcSocket::sendsends on the "first" channel"?- a) alternative here, is to label the channels instead of indexing them. Perhaps if a channel is labeled "default" it's the one that will be sent on "by default"?
- b) or remove the concept of a default channel, and have
sendandreceivealways take a channel index/label?
|
btw, sorry, for being so back-and-forth about this. I hope you don't feel like you've done a lot of wasted work. For what it's worth, discussing this and reading through your PRs has been really useful in order to start and think about what-why-and-how the API will be used. |
|
Also, FYI: I'm fixing a quite serious regression in #65 that has conflicts wit this PR. I'll probably merge it within the next day or two unless someone has comments. |
|
Thanks for your replies - and I don't feel that I wasted work because I learned a lot in the process and I would oppositely only regret to merge something that doesn't meet the quality standards and ease of use I am used from this library so I am happy about all of your suggestions. I am back and forth myself and have to apologise myself for giving you so many different things to review :) Also I am in no hurry because for my game I use my own fork and am happy with it for now |
6733c33 to
cbc7171
Compare
|
I finally found time to implement a working version following your suggestions. I believe this API makes matchbox much more powerful while still keeping it fairly simple. After a lot of thinking I actually don't like any of my suggestions better than yours and I guess adding more features will make the API inevitably more complex as well. Also this keeps everything very flexible in the backend if one day someone finds a more beautiful API! Also thanks a lot for your explanations in #66 - it was like I already expected but having you confirm it gave me the confidence and focus to dig through it once more and finally find a point of leverage from where on I could deconstruct the multiplexing single sender/receiver pair into separate ones which was a prerequisite for this implementation - following compiler suggestions until it worked! I tested it locally with multiple reliable and unreliable data channels both natively and wasm and my (simple) tests were successful. Both demos work fine natively and my game works well with the wasm version. I am getting more confident already with webrtc and async programming but I still expect that there might have been a few things I missed so I am looking forward to your feedback |
johanhelsing
left a comment
There was a problem hiding this comment.
Hey :) I didn't have time to finish the review now, hopefully I will have some time tomorrow.
Just wanted to tell you I'm really excited about this change, and it's looking great so far!
I agree that this iteration it has flexible internals that allows for further on the public API 👍
Co-authored-by: Johan Klokkhammer Helsing <johanhelsing@gmail.com>
Co-authored-by: Johan Klokkhammer Helsing <johanhelsing@gmail.com>
Co-authored-by: Johan Klokkhammer Helsing <johanhelsing@gmail.com>
Co-authored-by: Johan Klokkhammer Helsing <johanhelsing@gmail.com>
|
Looking much better now! From my side I would be confident to merge but please feel free to also test it on your own because the change was quite big. |
|
Okay, I think this should be almost good now. Just the one cleanup. I still wonder whether to include it in 0.5 or not. I intended to release that as soon as the demo was updated for bevy 0.9. I'm currently leaning towards merging it right after 0.5, then hopefully I'll have some time to dogfood it in my own project before we release it on crates.io |
Co-authored-by: Johan Klokkhammer Helsing <johanhelsing@gmail.com>
|
It's fine to wait a bit and I agree it's probably better to not include it in 0.5 but let's better merge it not too late because anybody forking the repo at this point will face a lot of merge conflicts |
Exploring your suggestions in #63 regarding #25 I tried an alternative implementation.
The two main differences beside some fixes are:
I decided to open two PRs for both variants because I am not sure which one is better. I like the simplicity of #63 and I tried to keep it in this PR - it is still possible to simply use the send() and receive() methods to not have to worry about the big world of WebRTC at all and remain blissfully oblivious of the data channels used backstage.
If the library user would like to have more influence or information over how the packet is or was sent I added two methods:
Of course there would be more convenient APIs if it would be a common usecase to open several data channels where each has its own specific settings and use cases but I love matchbox for its simplicity and this is I think the simplest API for simple use cases.
Please let me know what you think and if this is going more in a direction that is convenient for you as well.