Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Upcoming release
================

* Deleted deprecated API.
* Changed API to enforce sending startup information using the type system.

0.4.5 (2026-09-30)
==================
Expand Down
4 changes: 2 additions & 2 deletions crates/fleetspeak/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn main() {
// SAFETY: We call `from_env` at the beginning of `main` so nothing could
// have tampered with it. We also never invoke it again anywhere else.
let fleetspeak = unsafe {
fleetspeak::Comms::from_env()
fleetspeak::from_env()
}.expect("failed to handshake Fleetspeak connection");

fleetspeak.startup("0.0.1")
let fleetspeak = fleetspeak.startup("0.0.1")
.expect("failed to send Fleetspeak startup message");

for packet in fleetspeak.receiver()
Expand Down
129 changes: 80 additions & 49 deletions crates/fleetspeak/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,59 @@ mod io;
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Communication channel with the Fleetspeak process.
pub struct Comms {
/// Returns a [comms] instance given by the parent Fleetspeak process.
///
/// [comms]: CommsUnstarted
///
/// # Safety
///
/// This function must be invoked where the environment is guaranteed not to
/// have been tampered with (e.g. at the beginning of the `main` function),
/// so that it contains values really set by Fleetspeak (and not e.g. file
/// descriptors of Rust-allocated resources "leaked" by [`as_raw_fd`][1]).
///
/// This is to adhere to [I/O safety][2] requirements.
///
/// Synchronization is done on per-instance basis, so the users should
/// ensure that this is called only once in the program lifetime.
///
/// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd
/// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html
pub unsafe fn from_env() -> std::io::Result<CommsUnstarted> {
// SAFETY: Safety contract is the same as for the outer function.
let mut raw_in = unsafe {
crate::io::CommsInRaw::from_env()
}.map_err(CommsInEnvError)?;

// SAFETY: Safety contract is the same as for the outer function.
let mut raw_out = unsafe {
crate::io::CommsOutRaw::from_env()
}.map_err(CommsOutEnvError)?;

crate::io::handshake(&mut raw_in, &mut raw_out)
.map_err(HandshakeError)?;

Ok(CommsUnstarted {
raw_in: Mutex::new(raw_in),
raw_out: Mutex::new(raw_out),
})
}

/// Communication channel with the Fleetspeak process that did not send
/// startup information.
///
/// The [`startup`] method should be used to send startup information and make
/// the communication channel ready for sending and receiving messages.
///
/// [`startup`]: CommsUnstarted::startup
pub struct CommsUnstarted {
// TODO(rust-lang/rust#134645): Migrate to `std::sync::nonpoison::Mutext`
// (or `std::sync::ReentrantLock`) once stable.
raw_out: Mutex<crate::io::CommsOutRaw>,
raw_in: Mutex<crate::io::CommsInRaw>,
last_heartbeat: Mutex<Option<Instant>>,
}

impl Comms {

// Returns a [`Comms`] instance given by the parent Fleetspeak process.
///
/// # Safety
///
/// This function must be invoked where the environment is guaranteed not to
/// have been tampered with (e.g. at the beginning of the `main` function),
/// so that it contains values really set by Fleetspeak (and not e.g. file
/// descriptors of Rust-allocated resources "leaked" by [`as_raw_fd`][1]).
///
/// This is to adhere to [I/O safety][2] requirements.
///
/// Synchronization is done on per-instance basis, so the users should
/// ensure that this is called only once in the program lifetime.
///
/// [1]: https://doc.rust-lang.org/std/os/fd/trait.AsRawFd.html#tymethod.as_raw_fd
/// [2]: https://rust-lang.github.io/rfcs/3128-io-safety.html
pub unsafe fn from_env() -> std::io::Result<Comms> {
// SAFETY: Safety contract is the same as for the outer function.
let mut raw_in = unsafe {
crate::io::CommsInRaw::from_env()
}.map_err(CommsInEnvError)?;

// SAFETY: Safety contract is the same as for the outer function.
let mut raw_out = unsafe {
crate::io::CommsOutRaw::from_env()
}.map_err(CommsOutEnvError)?;

crate::io::handshake(&mut raw_in, &mut raw_out)
.map_err(HandshakeError)?;

Ok(Comms {
raw_in: Mutex::new(raw_in),
raw_out: Mutex::new(raw_out),
last_heartbeat: Mutex::new(None),
})
}
impl CommsUnstarted {

/// Sends a system message with startup information to the Fleetspeak
/// client.
Expand All @@ -76,10 +82,30 @@ impl Comms {
///
/// The `version` string should contain a self-reported version of the
/// service. This data is used primarily for statistics.
pub fn startup(&self, version: &str) -> std::io::Result<()> {
let mut raw_out = self.raw_out.lock().unwrap();
self::io::write_startup(&mut *raw_out, version)
pub fn startup(self, version: &str) -> std::io::Result<Comms> {
{
let mut raw_out = self.raw_out.lock().unwrap();
self::io::write_startup(&mut *raw_out, version)?;
}

Ok(Comms {
raw_out: self.raw_out,
raw_in: self.raw_in,
last_heartbeat: Mutex::new(None),
})
}
}

/// Communication channel with the Fleetspeak process.
pub struct Comms {
// TODO(rust-lang/rust#134645): Migrate to `std::sync::nonpoison::Mutext`
// (or `std::sync::ReentrantLock`) once stable.
raw_out: Mutex<crate::io::CommsOutRaw>,
raw_in: Mutex<crate::io::CommsInRaw>,
last_heartbeat: Mutex<Option<Instant>>,
}

impl Comms {

/// Sends a heartbeat signal to the Fleetspeak client.
///
Expand Down Expand Up @@ -135,7 +161,8 @@ impl Comms {
/// ```no_run
/// use fleetspeak::Message;
///
/// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap();
/// let comms = unsafe { fleetspeak::from_env() }.unwrap();
/// let comms = comms.startup("0.0.0").unwrap();
///
/// comms.send(Message {
/// service: String::from("example"),
Expand Down Expand Up @@ -168,7 +195,8 @@ impl Comms {
/// ```no_run
/// use std::time::Duration;
///
/// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap();
/// let comms = unsafe { fleetspeak::from_env() }.unwrap();
/// let comms = comms.startup("0.0.0").unwrap();
///
/// for message in comms.receiver()
/// .map(Result::unwrap)
Expand Down Expand Up @@ -199,7 +227,8 @@ impl Comms {
/// # Examples
///
/// ```no_run
/// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap();
/// let comms = unsafe { fleetspeak::from_env() }.unwrap();
/// let comms = comms.startup("0.0.0").unwrap();
///
/// let message = comms.try_receive().unwrap()
/// .expect("no message");
Expand Down Expand Up @@ -237,7 +266,8 @@ impl Comms {
/// ```no_run
/// use std::time::Duration;
///
/// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap();
/// let comms = unsafe { fleetspeak::from_env() }.unwrap();
/// let comms = comms.startup("0.0.0").unwrap();
///
/// let message = comms.try_receive_with_heartbeat(Duration::from_secs(1)).unwrap()
/// .expect("no message");
Expand Down Expand Up @@ -314,7 +344,8 @@ impl<'comms> Receiver<'comms> {
/// ```no_run
/// use std::time::Duration;
///
/// let comms = unsafe { fleetspeak::Comms::from_env() }.unwrap();
/// let comms = unsafe { fleetspeak::from_env() }.unwrap();
/// let comms = comms.startup("0.0.0").unwrap();
///
/// for message in comms.receiver()
/// .with_heartbeat(std::time::Duration::from_secs(1))
Expand Down
Loading