Skip to content

Commit

Permalink
allow infinite wait.
Browse files Browse the repository at this point in the history
To not break existing API, create a new method with a long name
  • Loading branch information
dancerj committed Feb 13, 2025
1 parent d84298b commit 871e27b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 15 additions & 1 deletion dbus/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,21 @@ impl $c {
///
/// Returns true when there was a message to process, and false when time out reached.
pub fn process(&self, timeout: Duration) -> Result<bool, Error> {
if let Some(msg) = self.channel.blocking_pop_message(timeout)? {
self.process_with_optional_timeout(Some(timeout))
}

/// Tries to handle an incoming message if there is one. If there isn't one,
/// it will wait up to timeout. If timeout is None, it will block forever.
///
/// This method only takes "&self" instead of "&mut self", but it is a logic error to call
/// it recursively and might lead to panics or deadlocks.
///
/// For `SyncConnection`: It is also a logic error to call this method from one thread, while
/// calling this or other methods from other threads. This can lead to messages being lost.
///
/// Returns true when there was a message to process, and false when time out reached.
pub fn process_with_optional_timeout(&self, timeout: Option<Duration>) -> Result<bool, Error> {
if let Some(msg) = self.channel.blocking_pop_message_with_optional_timeout(timeout)? {
if self.all_signal_matches.load(Ordering::Acquire) && msg.msg_type() == MessageType::Signal {
// If it's a signal and the mode is enabled, send a copy of the message to all
// matching filters.
Expand Down
8 changes: 7 additions & 1 deletion dbus/src/channel/ffichannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,14 @@ impl Channel {
/// Removes a message from the incoming queue, or waits until timeout if the queue is empty.
///
pub fn blocking_pop_message(&self, timeout: Duration) -> Result<Option<Message>, Error> {
self.blocking_pop_message_with_optional_timeout(Some(timeout))
}

/// Removes a message from the incoming queue, or waits until timeout if the queue is empty. If timeout is None, it will wait forever.
///
pub fn blocking_pop_message_with_optional_timeout(&self, timeout: Option<Duration>) -> Result<Option<Message>, Error> {
if let Some(msg) = self.pop_message() { return Ok(Some(msg)) }
self.read_write(Some(timeout)).map_err(|_|
self.read_write(timeout).map_err(|_|
Error::new_failed("Failed to read/write data, disconnected from D-Bus?")
)?;
Ok(self.pop_message())
Expand Down
9 changes: 8 additions & 1 deletion dbus/src/channel/nativechannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ impl Channel {
});
}

/// Removes a message from the incoming queue, or waits until timeout if the queue is empty. TODO: actually time out.
///
pub fn blocking_pop_message(&self, timeout: Duration) -> Result<Option<Message>, Error> {
self.blocking_pop_message_with_optional_timeout(Some(timeout))
}

/// Removes a message from the incoming queue, or waits until timeout if the queue is empty.
/// If timeout is None, it will wait forever. TODO: actually time out
///
pub fn blocking_pop_message(&self, _timeout: Duration) -> Result<Option<Message>, Error> {
pub fn blocking_pop_message_with_optional_timeout(&self, _timeout: Option<Duration>) -> Result<Option<Message>, Error> {
if let Some(msg) = self.pop_message() { return Ok(Some(msg)) }
// TODO: Timeout
block_on(async {
Expand Down

0 comments on commit 871e27b

Please sign in to comment.