Skip to content

Commit

Permalink
finished minimum functionality for bevy interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob-JB committed Jun 16, 2024
1 parent ccd13ec commit 4c72783
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 140 deletions.
9 changes: 6 additions & 3 deletions crates/bevy_interface/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ fn connect(endpoint_q: Query<Entity, With<ExampleEndpoint>>, mut connections: Co
let connection_entity = connections
.connect::<QuinnEndpoint>(
endpoint_entity,
(
Description::new_connect_description::<QuinnEndpoint>((
quinn_client_config,
"127.0.0.1:27018".parse().unwrap(),
"dev.drewridley.com".into(),
),
)),
)
.unwrap()
.unwrap();
Expand All @@ -134,7 +134,7 @@ fn send_message(
let mut connection = endpoint.connection_mut(connection_entity).unwrap();

let stream_id = connection
.open_stream(Description::new::<QuinnStreamId>(
.open_stream(Description::new_open_description::<QuinnStreamId>(
nevy_quic::quinn_proto::Dir::Uni,
))
.unwrap()
Expand Down Expand Up @@ -172,6 +172,9 @@ fn send_stream_data(

loop {
if stream_queue.buffer.len() == 0 {
stream.close(Description::new_send_close_description::<QuinnStreamId>(
None,
));
commands.entity(stream_entity).despawn();
break;
}
Expand Down
13 changes: 4 additions & 9 deletions crates/bevy_interface/src/connections/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use transport_interface::*;

mod stream_access;
mod stream_description;
mod stream_id;
pub use stream_access::*;
pub use stream_description::*;
pub use stream_id::*;

#[derive(Debug)]
pub struct MismatchedType {
pub expected: &'static str,
}
use crate::{description::Description, MismatchedType};

trait BevyConnectionInner<'c> {
fn open_stream(
&mut self,
description: StreamDescription,
description: Description,
) -> Result<Option<BevyStreamId>, MismatchedType>;

fn send_stream(
Expand All @@ -42,7 +37,7 @@ where
{
fn open_stream(
&mut self,
description: StreamDescription,
description: Description,
) -> Result<Option<BevyStreamId>, MismatchedType> {
let description = description.downcast()?;

Expand Down Expand Up @@ -96,7 +91,7 @@ impl<'c> BevyConnectionMut<'c> {

pub fn open_stream(
&mut self,
description: StreamDescription,
description: Description,
) -> Result<Option<BevyStreamId>, MismatchedType> {
self.inner.open_stream(description)
}
Expand Down
23 changes: 16 additions & 7 deletions crates/bevy_interface/src/connections/stream_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;

use transport_interface::*;

use super::{MismatchedType, StreamDescription};
use crate::{description::Description, MismatchedType};

pub trait StreamError {
fn is_fatal(&self) -> bool;
Expand All @@ -23,7 +23,7 @@ impl<E: ErrorFatality + 'static> StreamError for E {
pub(crate) trait BevySendStreamInner<'s> {
fn send(&mut self, data: &[u8]) -> Result<usize, Box<dyn StreamError>>;

fn close(&mut self, description: StreamDescription) -> Result<Result<(), ()>, MismatchedType>;
fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType>;

fn is_open(&self) -> bool;
}
Expand All @@ -39,7 +39,7 @@ impl<'s, S: SendStreamMut<'s>> BevySendStreamInner<'s> for S {
.map_err(|err| -> Box<dyn StreamError> { Box::new(err) })
}

fn close(&mut self, description: StreamDescription) -> Result<Result<(), ()>, MismatchedType> {
fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType> {
let description = description.downcast()?;

Ok(self.close(description))
Expand All @@ -61,10 +61,7 @@ impl<'s> BevySendStream<'s> {
self.inner.send(data)
}

pub fn close(
&mut self,
description: StreamDescription,
) -> Result<Result<(), ()>, MismatchedType> {
pub fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType> {
self.inner.close(description)
}

Expand All @@ -76,6 +73,8 @@ impl<'s> BevySendStream<'s> {
pub(crate) trait BevyRecvStreamInner<'s> {
fn recv(&mut self, limit: usize) -> Result<Box<[u8]>, Box<dyn StreamError>>;

fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType>;

fn is_open(&self) -> bool;
}

Expand All @@ -90,6 +89,12 @@ impl<'s, S: RecvStreamMut<'s>> BevyRecvStreamInner<'s> for S {
.map_err(|err| -> Box<dyn StreamError> { Box::new(err) })
}

fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType> {
let description = description.downcast()?;

Ok(self.close(description))
}

fn is_open(&self) -> bool {
self.is_open()
}
Expand All @@ -106,6 +111,10 @@ impl<'s> BevyRecvStream<'s> {
self.inner.recv(limit)
}

pub fn close(&mut self, description: Description) -> Result<Result<(), ()>, MismatchedType> {
self.inner.close(description)
}

pub fn is_open(&self) -> bool {
self.inner.is_open()
}
Expand Down
86 changes: 0 additions & 86 deletions crates/bevy_interface/src/connections/stream_description.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/bevy_interface/src/connections/stream_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;

use transport_interface::*;

use super::MismatchedType;
use crate::MismatchedType;

pub(crate) trait BevyStreamIdInner: Send + Sync + 'static {
fn into_any(self: Box<Self>) -> Box<dyn Any>;
Expand Down
110 changes: 110 additions & 0 deletions crates/bevy_interface/src/description.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::any::Any;

use transport_interface::*;

use crate::MismatchedType;

/// type erased description use for operations that don't have type info
///
/// because it is type erased it can be given to plugins and they don't need to have type info
///
/// if a plugin needs to use a description more than once see [CloneableStreamDescription]
pub struct Description {
description: Box<dyn Any>,
}

impl Description {
/// creates a new description used for connecting on an endpoint `E`
pub fn new_connect_description<E: Endpoint>(description: E::ConnectDescription) -> Self {
Description {
description: Box::new(description),
}
}

/// creates a new description used for opening a stream of `S`
pub fn new_open_description<S: StreamId>(description: S::OpenDescription) -> Self {
Description {
description: Box::new(description),
}
}

/// creates a new description used for closing a send stream of `S`
pub fn new_send_close_description<'s, S: StreamId>(
description: <S::SendMut<'s> as SendStreamMut<'s>>::CloseDescription,
) -> Self {
Description {
description: Box::new(description),
}
}

/// creates a new description used for closing a recv stream of `S`
pub fn new_recv_close_description<S: StreamId>(description: S::OpenDescription) -> Self
where
for<'a> <S::RecvMut<'a> as RecvStreamMut<'a>>::CloseDescription: 'static,
{
Description {
description: Box::new(description),
}
}

pub(crate) fn downcast<T: 'static>(self) -> Result<T, MismatchedType> {
match self.description.downcast() {
Ok(downcasted) => Ok(*downcasted),
Err(_) => Err(MismatchedType {
expected: std::any::type_name::<T>(),
}),
}
}
}

trait CloneableDescriptionInner {
fn clone(&self) -> Box<dyn CloneableDescriptionInner>;

fn into_any(self: Box<Self>) -> Box<dyn Any>;
}

/// type erased description used for opening and closing streams
///
/// because it is type erased it can be given to plugins and they don't need to have type info
///
/// this type implements Into<[Description]>
pub struct CloneableDescription {
description: Box<dyn CloneableDescriptionInner>,
}

impl<T: Clone + 'static> CloneableDescriptionInner for T {
fn clone(&self) -> Box<dyn CloneableDescriptionInner> {
Box::new(self.clone())
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}

impl Clone for CloneableDescription {
fn clone(&self) -> Self {
CloneableDescription {
description: self.description.clone(),
}
}
}

impl From<CloneableDescription> for Description {
fn from(value: CloneableDescription) -> Self {
Description {
description: value.description.into_any(),
}
}
}

impl CloneableDescription {
pub fn new<S: StreamId>(description: S::OpenDescription) -> Self
where
S::OpenDescription: Clone + 'static,
{
CloneableDescription {
description: Box::new(description),
}
}
}
Loading

0 comments on commit 4c72783

Please sign in to comment.