Expand description

Provides libs for connecting to and interacting with a FIDL protocol.

If you have a fidl protocol like this:

type Error = strict enum : int32 {
    PERMANENT = 1;
    TRANSIENT = 2;
};

@discoverable
protocol ProtocolFactory {
    CreateProtocol(resource struct {
        protocol server_end:Protocol;
    }) -> () error Error;
};

protocol Protocol {
    DoAction() -> () error Error;
};

Then you could implement ConnectedProtocol as follows:

struct ProtocolConnectedProtocol;
impl ConnectedProtocol for ProtocolConnectedProtocol {
    type Protocol = ProtocolProxy;
    type ConnectError = anyhow::Error;
    type Message = ();
    type SendError = anyhow::Error;

    fn get_protocol<'a>(
        &'a mut self,
    ) -> BoxFuture<'a, Result<Self::Protocol, Self::ConnectError>> {
        async move {
            let (protocol_proxy, server_end) =
                fidl::endpoints::create_proxy().context("creating server endpoints failed")?;
            let protocol_factory = connect_to_protocol::<ProtocolFactoryMarker>()
                .context("Failed to connect to test.protocol.ProtocolFactory")?;

            protocol_factory
                .create_protocol(server_end)
                .await?
                .map_err(|e| format_err!("Failed to create protocol: {:?}", e))?;

            Ok(protocol_proxy)
        }
        .boxed()
    }

    fn send_message<'a>(
        &'a mut self,
        protocol: &'a Self::Protocol,
        _msg: (),
    ) -> BoxFuture<'a, Result<(), Self::SendError>> {
        async move {
            protocol.do_action().await?.map_err(|e| format_err!("Failed to do action: {:?}", e))?;
            Ok(())
        }
        .boxed()
    }
}

Then all you would have to do to connect to the service is:

let connector = ProtocolConnector::new(ProtocolConnectedProtocol);
let (sender, future) = connector.serve_and_log_errors();
let future = Task::spawn(future);
// Use sender to send messages to the protocol

Structs§

  • ProtocolConnector contains the logic to use a ConnectedProtocol to connect to and forward messages to a protocol.
  • A ProtocolSender wraps around an mpsc::Sender object that is used to send messages to a running ProtocolConnector instance.

Enums§

  • Errors encountered while connecting to or sending messages to the ConnectedProtocol implementation.
  • Returned by ProtocolSender::send to notify the caller about the state of the underlying mpsc::channel. None of these status codes should be considered an error state, they are purely informational.

Traits§

  • A trait for implementing connecting to and sending messages to a FIDL protocol.