Skip to content

Commit

Permalink
Merge pull request #13 from NoahShomette/request_work
Browse files Browse the repository at this point in the history
Update to Bevy 0.13 / Fix Request/Response, add Request/Response documentation, update older leftover documentation
  • Loading branch information
jamescarterbell committed Feb 19, 2024
2 parents 5f84180 + c949895 commit 94d7bc8
Show file tree
Hide file tree
Showing 8 changed files with 984 additions and 517 deletions.
1,179 changes: 711 additions & 468 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "bevy_eventwork"
version = "0.8.0"
authors = ["James <[email protected]>", "Neikos <[email protected]>", "Noah <[email protected]>"]
authors = [
"James <[email protected]>",
"Neikos <[email protected]>",
"Noah <[email protected]>",
]
edition = "2021"
description = "Event based networking library for Bevy"
readme = "README.md"
Expand Down Expand Up @@ -29,7 +33,7 @@ name = "server"

[dependencies]
# This is a bevy plugin
bevy = { version = "0.12.0", features = [
bevy = { version = "0.13.0", features = [
"multi-threaded",
], default-features = false }
# Used for on wire serialization
Expand All @@ -49,4 +53,4 @@ async-net = { version = "2.0.0", optional = true }
futures-lite = "2.0.0"

[dev-dependencies]
bevy = { version = "0.12.0", features = ["default_font"] }
bevy = { version = "0.13.0", features = ["default_font"] }
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn handle_incoming_whisper_messages(

## Request/Response

Starting with version 0.7.1, you can now automatically handle Request/Response style messaging with event work! Check the documentation for more info!
Starting with version 0.7.1, you can now automatically handle Request/Response style messaging with event work! Check the [documentation](https://docs.rs/bevy_eventwork/latest/bevy_eventwork/managers/network_request/index.html) for more info!

## Bevy Version Compatibility

Expand Down
4 changes: 2 additions & 2 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ fn setup_ui(mut commands: Commands, _materials: ResMut<Assets<ColorMaterial>>) {
..default()
},
)
.with_alignment(TextAlignment::Center),
.with_justify(JustifyText::Center),
..Default::default()
});
});
Expand All @@ -411,7 +411,7 @@ fn setup_ui(mut commands: Commands, _materials: ResMut<Assets<ColorMaterial>>) {
..default()
},
)
.with_alignment(TextAlignment::Center),
.with_justify(JustifyText::Center),
..Default::default()
});
});
Expand Down
12 changes: 5 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Then, register which kind of messages can be received through [`managers::networ
as well as which provider you wantto handle these messages and you
can start receiving packets as events of [`NetworkData<T>`].
This plugin also supports Request/Response style messages, see that modules documentation for further info: **[Request Documentation](https://docs.rs/bevy_eventwork/latest/bevy_eventwork/managers/network_request/index.html)**
## Example Client
```rust,no_run
use bevy::prelude::*;
Expand Down Expand Up @@ -139,7 +141,7 @@ fn handle_connection_events(
```
As you can see, they are both quite similar, and provide everything a basic networked game needs.
Currently, Bevy's [TaskPool] is the default runtime used by Eventwork.
Currently, Bevy's [TaskPool](bevy::tasks::TaskPool) is the default runtime used by Eventwork.
*/

/// Contains error enum.
Expand Down Expand Up @@ -189,10 +191,6 @@ impl<T> AsyncChannel<T> {

#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
/// A [`ConnectionId`] denotes a single connection
///
/// Use [`ConnectionId::is_server`] whether it is a connection to a server
/// or another. In most client/server applications this is not required as there
/// is no ambiguity.
pub struct ConnectionId {
/// The key of the connection.
pub id: u32,
Expand Down Expand Up @@ -220,7 +218,7 @@ impl Debug for NetworkPacket {
}

#[derive(Debug, Event)]
/// A network event originating from a [`NetworkClient`]
/// A network event originating from another eventwork app
pub enum NetworkEvent {
/// A new client has connected
Connected(ConnectionId),
Expand Down Expand Up @@ -274,7 +272,7 @@ impl Connection {
}
}
#[derive(Default, Copy, Clone, Debug)]
/// The plugin to add to your bevy [`App`](bevy::prelude::App) when you want
/// The plugin to add to your bevy [`App``] when you want
/// to instantiate a server
pub struct EventworkPlugin<NP: NetworkProvider, RT: Runtime = bevy::tasks::TaskPool>(
PhantomData<(NP, RT)>,
Expand Down
24 changes: 15 additions & 9 deletions src/managers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ use crate::{

/// Contains logic for using [`Network`]
pub mod network;
/// Contains logic for making requests with expected responses to a server
/// Contains logic for making requests with expected responses
pub mod network_request;

/// An instance of a [`NetworkServer`] is used to listen for new client connections
/// using [`NetworkServer::listen`]
/// An instance of a Network that uses the provided [`NetworkProvider`] to drive itself.
///
/// You can use this resource to interact with the network in Bevy systems.
///
/// - Listen for new client connections using [`Network::listen`]
/// - Connect to a server using [`Network::connect`]
/// - Send new messages using [`Network::send_message`]
/// - Send broadcasts to all connected clients using [`Network::broadcast`]
#[derive(Resource)]
pub struct Network<NP: NetworkProvider> {
recv_message_map: Arc<DashMap<&'static str, Vec<(ConnectionId, Vec<u8>)>>>,
Expand All @@ -30,15 +36,15 @@ pub struct Network<NP: NetworkProvider> {
connection_count: u32,
}

/// A trait used by [`NetworkServer`] to drive a server, this is responsible
/// for generating the futures that carryout the underlying server logic.
/// A trait used to drive the network. This is responsible
/// for generating the futures that carryout the underlying app network logic.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait NetworkProvider: 'static + Send + Sync {
/// This is to configure particular protocols
type NetworkSettings: Resource + Clone;

/// The type that acts as a combined sender and reciever for a client.
/// The type that acts as a combined sender and reciever for the network.
/// This type needs to be able to be split.
type Socket: Send;

Expand All @@ -54,7 +60,7 @@ pub trait NetworkProvider: 'static + Send + Sync {
/// Info necessary to start a connection, an [`std::net::SocketAddr`] for instance
type AcceptInfo: Send;

/// The output type of [`accept_loop`]
/// The output type of [`Self::accept_loop`]
type AcceptStream: Stream<Item = Self::Socket> + Unpin + Send;

/// This will be spawned as a background operation to continuously add new connections.
Expand All @@ -69,14 +75,14 @@ pub trait NetworkProvider: 'static + Send + Sync {
network_settings: Self::NetworkSettings,
) -> Result<Self::Socket, NetworkError>;

/// Recieves messages from the client, forwards them to Eventwork via a sender.
/// Recieves messages over the network, forwards them to Eventwork via a sender.
async fn recv_loop(
read_half: Self::ReadHalf,
messages: Sender<NetworkPacket>,
settings: Self::NetworkSettings,
);

/// Sends messages to the client, receives packages from Eventwork via receiver.
/// Sends messages over the network, receives packages from Eventwork via receiver.
async fn send_loop(
write_half: Self::WriteHalf,
messages: Receiver<NetworkPacket>,
Expand Down
4 changes: 2 additions & 2 deletions src/managers/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ pub(crate) fn handle_new_incoming_connections<NP: NetworkProvider, RT: Runtime>(
}
}

/// A utility trait on [`App`] to easily register [`ServerMessage`]s
/// A utility trait on [`App`] to easily register [`NetworkMessage`]s
pub trait AppNetworkMessage {
/// Register a server message type
/// Register a network message type
///
/// ## Details
/// This will:
Expand Down
Loading

0 comments on commit 94d7bc8

Please sign in to comment.