[−][src]Struct buttplug::client::ButtplugClient
Struct used by applications to communicate with a Buttplug Server.
Buttplug Clients provide an API layer on top of the Buttplug Protocol that handles boring things like message creation and pairing, protocol ordering, etc... This allows developers to concentrate on controlling hardware with the API.
Clients serve a few different purposes:
- Managing connections to servers, thru ButtplugClientConnectors
- Emitting events received from the Server
- Holding state related to the server (i.e. what devices are currently connected, etc...)
When a client is first created, it will be able to create an internal loop as a Future, and return it via the [ButtplugClient::get_loop] call. This loop needs to be awaited before awaiting other client calls (like ButtplugClient::connect), otherwise the system will panic.
Fields
client_name: String
The client name. Depending on the connection type and server being used, this name is sometimes shown on the server logs or GUI.
server_name: Option<String>
The server name. Once connected, this contains the name of the server, so we can know what we're connected to.
Methods
impl ButtplugClient
[src]
pub fn run<F, T>(name: &str, func: F) -> impl Future where
F: FnOnce(ButtplugClient) -> T,
T: Future,
[src]
F: FnOnce(ButtplugClient) -> T,
T: Future,
Runs the client event loop.
Given a client name and a function that takes the client and returns an future (since we can't have async closures yet), this function
- creates a ButtplugClient instance
- passes it to the
func
argument to create the application Future - returns a Future with a [join] for the client event loop future and the client application future.
Parameters
name
: Name of the client, see ButtplugClient::client_namefunc
: Function that takes the client instance, and returns a future for what the application will be doing with the client instance.
Examples
use buttplug::client::{ButtplugClient, connector::ButtplugEmbeddedClientConnector}; futures::executor::block_on(async { ButtplugClient::run("Test Client", |mut client| { async move { client .connect(ButtplugEmbeddedClientConnector::new("Test Server", 0)) .await; println!("Are we connected? {}", client.connected()); } }).await; });
pub async fn connect<'_>(
&'_ mut self,
connector: impl ButtplugClientConnector + 'static
) -> Option<ButtplugClientError>
[src]
&'_ mut self,
connector: impl ButtplugClientConnector + 'static
) -> Option<ButtplugClientError>
Connects and runs handshake flow with [crate::server::server::ButtplugServer], either local or remote.
Tries to connect to a server via the given ButtplugClientConnector struct. If connection is successful, also runs the handshake flow and retrieves a list of currently connected devices. These devices will be emitted as ButtplugClientEvent::DeviceAdded events next time ButtplugClient::wait_for_event is run.
Parameters
connector
: A connector of some type that will handle the connection to the server. The core library ships with an "embedded" connector (connector::ButtplugEmbeddedClientConnector) that will run a server in-process with the client, or there are add-on libraries like buttplug-ws-connector that will handle other communication methods like websockets, TCP/UDP, etc...
Returns
An Option
which is:
- None if connection succeeded
- Some containing a ButtplugClientError on connection failure.
pub fn connected(&self) -> bool
[src]
Returns true if client is currently connected to server.
pub fn disconnect(&mut self) -> Option<ButtplugClientError>
[src]
Disconnects from server, if connected.
pub async fn start_scanning<'_>(&'_ mut self) -> Option<ButtplugClientError>
[src]
Tells server to start scanning for devices.
pub async fn wait_for_event<'_>(&'_ mut self) -> Vec<ButtplugClientEvent>
[src]
Produces a future that will wait for a set of events from the internal loop. Returns once any number of events is received.
This should be called whenever the client isn't doing anything otherwise, so we can respond to unexpected updates from the server, such as devices connections/disconnections, log messages, etc... This is basically what event handlers in C# and JS would deal with, but we're in Rust so this requires us to be slightly more explicit.
Trait Implementations
impl Send for ButtplugClient
[src]
impl Sync for ButtplugClient
[src]
impl Clone for ButtplugClient
[src]
fn clone(&self) -> ButtplugClient
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Auto Trait Implementations
impl Unpin for ButtplugClient
impl !UnwindSafe for ButtplugClient
impl !RefUnwindSafe for ButtplugClient
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,