From c956cb7da50430acc7e98584e839b733858dd0ba Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 25 Apr 2022 16:51:28 +0800 Subject: [PATCH] xous: add `Envelope.forward()` to forward messages Messages should be able to be forwarded from one Server to another. This is a wrapper around the machinery that would do that. Signed-off-by: Sean Cross --- xous-rs/src/definitions/messages/envelope.rs | 63 +++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/xous-rs/src/definitions/messages/envelope.rs b/xous-rs/src/definitions/messages/envelope.rs index 885e85048..75da635b5 100644 --- a/xous-rs/src/definitions/messages/envelope.rs +++ b/xous-rs/src/definitions/messages/envelope.rs @@ -1,4 +1,4 @@ -use super::{Message, MessageSender}; +use super::{Message, MessageId, MessageSender}; use crate::CID; #[repr(C)] @@ -28,6 +28,14 @@ impl Envelope { ] } + /// Take the message, throwing away the sender information. If the message is + /// Blocking (i.e. if message.is_blocking() is `true`), then the process that + /// sent this message will not automatically get a response. You will need to + /// manually send a response. + /// + /// If the message is non-blocking, then this has no side effects. + /// + /// This is equivalent to "leaking" the message, which is not strictly unsafe. pub fn take_message(self) -> Message { use core::mem::ManuallyDrop; // Convert `Self` into something that won't have its "Drop" method called @@ -37,29 +45,68 @@ impl Envelope { unsafe { core::ptr::read(&manual_self.body) } } - pub fn send(self, connection: CID) -> Result { + /// Take this message and forward it to another server. + /// + /// **Note**: For blocking messages, this will block until the other server responds to + /// the message. In the future, this may be turned into a nonblocking operation where + /// this will return immediately and allow the target Server to respond directly. + /// + /// ## Result + /// + /// If the result is successful, then nothing is returned. + /// + /// If there is an error, then the original Envelope is returned along with the resulting + /// error. + pub fn forward(mut self, connection: CID, id: MessageId) -> Result<(), (Envelope, crate::Error)> { use core::mem::ManuallyDrop; + + // Update our ID to match the newly-sent message. Reuse the same message struct. + self.body.set_id(id); + // Convert `Self` into something that won't have its "Drop" method called let manual_self = ManuallyDrop::new(self); - // This function only works on memory messages + // Unsafe because there are now two things that are pointing at "self.body". However, + // this is fine since these two pointers are never used at the same time. let body = unsafe { core::ptr::read(&manual_self.body) }; + + // Different messages have different kinds of lifetimes, so they must all be + // handled differently. match body { Message::Move(_) => { let result = crate::send_message(connection, body); + + // If the Move was successful, return so. if let Ok(crate::Result::Ok) = result { - return Ok(crate::Result::Ok); + // `self` goes out of scope here without having `Drop` called on it + return Ok(()); } + + // If there's an error, reconstitute ourselves and return. + if let Err(e) = result { + return Err((ManuallyDrop::into_inner(manual_self), e)); + } + return Err(( ManuallyDrop::into_inner(manual_self), crate::Error::MemoryInUse, )); } _ => { - return Err(( - ManuallyDrop::into_inner(manual_self), - crate::Error::MemoryInUse, - )) + let result = crate::send_message(connection, body); + let new_self = ManuallyDrop::into_inner(manual_self); + + if let Ok(crate::Result::Ok) = result { + // `new_self` will have its Drop() called + return Ok(()); + } + + // If there's an error, reconstitute ourselves and return. + if let Err(e) = result { + return Err((new_self, e)); + } + + return Err((new_self, crate::Error::MemoryInUse)); } } }