refactor: store weak client in the nodes

This commit is contained in:
Nova
2022-05-30 19:00:19 -04:00
parent a2e61f9b78
commit 09588ab31d
5 changed files with 93 additions and 66 deletions

View File

@@ -1,27 +1,36 @@
use super::scenegraph::Scenegraph;
use libstardustxr::messenger::Messenger;
use mio::net::UnixStream;
use std::rc::{Rc, Weak};
use rccell::{RcCell, WeakCell};
pub struct Client<'a> {
messenger: Rc<Messenger<'a>>,
pub scenegraph: Option<Scenegraph<'a>>,
weak_ref: WeakCell<Client<'a>>,
messenger: Messenger<'a>,
scenegraph: Option<Scenegraph<'a>>,
}
impl<'a> Client<'a> {
pub fn from_connection(connection: UnixStream) -> Self {
let mut client = Client {
pub fn from_connection(connection: UnixStream) -> RcCell<Self> {
let client = RcCell::new(Client {
weak_ref: WeakCell::new(),
scenegraph: None,
messenger: Rc::new(Messenger::new(connection)),
};
client.scenegraph = Some(Scenegraph::new(&mut client));
messenger: Messenger::new(connection),
});
client.borrow_mut().weak_ref = client.downgrade();
client.borrow_mut().scenegraph = Some(Scenegraph::new(client.clone()));
client
}
pub fn dispatch(&self) -> Result<(), std::io::Error> {
self.messenger.dispatch(self.scenegraph.as_ref().unwrap())
}
pub fn get_weak_messenger(&self) -> Weak<Messenger<'a>> {
Rc::downgrade(&self.messenger)
pub fn get_messenger(&self) -> &Messenger<'a> {
&self.messenger
}
pub fn get_scenegraph(&self) -> &Scenegraph<'a> {
self.scenegraph.as_ref().unwrap()
}
pub fn get_scenegraph_mut(&mut self) -> &mut Scenegraph<'a> {
self.scenegraph.as_mut().unwrap()
}
}

View File

@@ -3,6 +3,7 @@ use libstardustxr::server;
use mio::net::UnixListener;
use mio::unix::pipe;
use mio::{Events, Interest, Poll, Token};
use rccell::RcCell;
use slab::Slab;
use std::io::Write;
use std::thread::{self, JoinHandle};
@@ -22,7 +23,7 @@ impl EventLoop {
let mut socket = UnixListener::bind(socket_path.clone())?;
let (sender, mut receiver) = pipe::new()?;
let join_handle = Some(thread::spawn(move || -> Result<()> {
let mut clients: Slab<Option<Client>> = Slab::new();
let mut clients: Slab<Option<RcCell<Client>>> = Slab::new();
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(1024);
const LISTENER: Token = Token(usize::MAX - 1);
@@ -57,7 +58,14 @@ impl EventLoop {
},
STOP => return Ok(()),
token => loop {
match clients.get(token.0).unwrap().as_ref().unwrap().dispatch() {
match clients
.get(token.0)
.unwrap()
.as_ref()
.unwrap()
.borrow()
.dispatch()
{
Ok(_) => continue,
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock {

View File

@@ -3,7 +3,7 @@ use crate::nodes::spatial::Spatial;
use anyhow::Result;
use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError;
use rccell::{RcCell, WeakCell};
use rccell::RcCell;
use std::collections::HashMap;
pub struct Scenegraph<'a> {
@@ -11,7 +11,7 @@ pub struct Scenegraph<'a> {
}
impl<'a> Scenegraph<'a> {
pub fn new(client: &mut Client<'a>) -> Self {
pub fn new(client: RcCell<Client<'a>>) -> Self {
// root: Spatial::new(Some(client), "/", Default::default()),
// hmd: Spatial::new(Some(client), "/hmd", Default::default()),
Scenegraph {
@@ -20,6 +20,14 @@ impl<'a> Scenegraph<'a> {
}
}
impl<'a> Default for Scenegraph<'a> {
fn default() -> Self {
Scenegraph {
spatial_nodes: HashMap::new(),
}
}
}
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.spatial_nodes