unsafeimpl Send for ConnHandle {} unsafeimpl Sync for ConnHandle {}
impl Drop for ConnHandle { fn drop(&mutself) { unsafe {
ffi::dbus_connection_close(self.0);
ffi::dbus_connection_unref(self.0);
}
}
}
/// Experimental rewrite of Connection [unstable / experimental] /// /// Slightly lower level, with better support for async operations. /// Also, this struct is Send + Sync. /// /// Blocking operations should be clearly marked as such, although if you /// try to access the connection from several threads at the same time, /// blocking might occur due to an internal mutex inside the dbus library. /// /// This version avoids dbus_connection_dispatch, and thus avoids /// callbacks from that function. Instead the same functionality needs to be /// implemented by these bindings somehow - this is not done yet. #[derive(Debug)] pubstruct TxRx {
handle: ConnHandle,
}
/* No, we don't want our app to suddenly quit if dbus goes down */ unsafe { ffi::dbus_connection_set_exit_on_disconnect(ptr, 0) };
let c = TxRx { handle };
Ok(c)
}
/// Creates a new D-Bus connection. /// /// Blocking: until the connection is up and running. pubfn get_private(bus: BusType) -> Result<TxRx, Error> { letmut e = Error::empty(); let conn = unsafe { ffi::dbus_bus_get_private(bus, e.get_mut()) }; if conn == ptr::null_mut() { return Err(e)
} Self::conn_from_ptr(conn)
}
/// Creates a new D-Bus connection to a remote address. /// /// Note: for all common cases (System / Session bus) you probably want "get_private" instead. /// /// Blocking: until the connection is established. pubfn open_private(address: &str) -> Result<TxRx, Error> { letmut e = Error::empty(); let conn = unsafe { ffi::dbus_connection_open_private(to_c_str(address).as_ptr(), e.get_mut()) }; if conn == ptr::null_mut() { return Err(e)
} Self::conn_from_ptr(conn)
}
/// Registers a new D-Bus connection with the bus. /// /// Note: `get_private` does this automatically, useful with `open_private` /// /// Blocking: until a "Hello" response is received from the server. pubfn register(&mutself) -> Result<(), Error> { // This function needs to take &mut self, because it changes unique_name and unique_name takes a &self letmut e = Error::empty(); ifunsafe { ffi::dbus_bus_register(self.conn(), e.get_mut()) == 0 } {
Err(e)
} else {
Ok(())
}
}
/// Gets whether the connection is currently open. pubfn is_connected(&self) -> bool { unsafe { ffi::dbus_connection_get_is_connected(self.conn()) != 0 }
}
/// Get the connection's unique name. /// /// It's usually something like ":1.54" pubfn unique_name(&self) -> Option<&str> { let c = unsafe { ffi::dbus_bus_get_unique_name(self.conn()) }; if c == ptr::null_mut() { return None; } let s = unsafe { CStr::from_ptr(c) };
str::from_utf8(s.to_bytes()).ok()
}
/// Puts a message into libdbus out queue. Use "flush" or "read_write" to make sure it is sent over the wire. /// /// Returns a serial number than can be used to match against a reply. pubfn send(&self, msg: Message) -> Result<u32, ()> { letmut serial = 0u32; let r = unsafe { ffi::dbus_connection_send(self.conn(), msg.ptr(), &mut serial) }; if r == 0 { return Err(()); }
Ok(serial)
}
/// Flush the queue of outgoing messages. /// /// Blocking: until the outgoing queue is empty. pubfn flush(&self) { unsafe { ffi::dbus_connection_flush(self.conn()) } }
/// Read and write to the connection. /// /// Incoming messages are put in the internal queue, outgoing messages are written. /// /// Blocking: If there are no messages, for up to timeout_ms milliseconds, or forever if timeout_ms is None. /// For non-blocking behaviour, set timeout_ms to Some(0). pubfn read_write(&self, timeout_ms: Option<i32>) -> Result<(), ()> { let t = timeout_ms.unwrap_or(-1); ifunsafe { ffi::dbus_connection_read_write(self.conn(), t) == 0 } {
Err(())
} else {
Ok(())
}
}
/// Removes a message from the incoming queue, or returns None if the queue is empty. /// /// Use "read_write" first, so that messages are put into the incoming queue. /// For unhandled messages, please call MessageDispatcher::default_dispatch to return /// default replies for method calls. pubfn pop_message(&self) -> Option<Message> { let mptr = unsafe { ffi::dbus_connection_pop_message(self.conn()) }; if mptr == ptr::null_mut() {
None
} else {
Some(Message::from_ptr(mptr, false))
}
}
/// Get an up-to-date list of file descriptors to watch. /// /// Might be changed into something that allows for callbacks when the watch list is changed. pubfn watch_fds(&mutself) -> Result<Vec<Watch>, ()> { extern"C"fn add_watch_cb(watch: *mut ffi::DBusWatch, data: *mut c_void) -> u32 { unsafe { let wlist: &mut Vec<Watch> = &mut *(data as *mut _);
wlist.push(Watch::from_raw(watch));
} 1
} letmut r = vec!(); ifunsafe { ffi::dbus_connection_set_watch_functions(self.conn(),
Some(add_watch_cb), None, None, &mut r as *mut _ as *mut _, None) } == 0{ return Err(()) }
assert!(unsafe { ffi::dbus_connection_set_watch_functions(self.conn(),
None, None, None, ptr::null_mut(), None) } != 0);
Ok(r)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.