1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
// Buttplug Rust Source Code File - See https://buttplug.io for more info. // // Copyright 2016-2019 Nonpolynomial Labs LLC. All rights reserved. // // Licensed under the BSD 3-Clause license. See LICENSE file in the project root // for full license information. //! Communications API for accessing Buttplug Servers pub mod connectors; pub mod device; pub mod internal; use connectors::{ ButtplugClientConnectionFuture, ButtplugClientConnector, ButtplugClientConnectorError, }; use device::ButtplugClientDevice; use internal::{client_event_loop, ButtplugClientMessage}; use crate::{ core::{ errors::{ ButtplugDeviceError, ButtplugError, ButtplugHandshakeError, ButtplugMessageError, ButtplugUnknownError, }, messages::{ self, ButtplugMessageUnion, DeviceMessageInfo, LogLevel, RequestDeviceList, RequestServerInfo, StartScanning, }, }, util::future::{ButtplugFuture, ButtplugMessageFuture}, }; use async_std::{ prelude::FutureExt, sync::{channel, Receiver, Sender}, }; use futures::{Future, StreamExt}; use std::{error::Error, fmt}; type ButtplugClientResult<T = ()> = Result<T, ButtplugClientError>; /// Represents all of the different types of errors a ButtplugClient can return. /// /// Clients can return two types of errors: /// /// - [ButtplugClientConnectorError], which means there was a problem with the /// connection between the client and the server, like a network connection /// issue. /// - [ButtplugError], which is an error specific to the Buttplug Protocol. #[derive(Debug, Clone)] pub enum ButtplugClientError { /// Connector error ButtplugClientConnectorError(ButtplugClientConnectorError), /// Protocol error ButtplugError(ButtplugError), } impl fmt::Display for ButtplugClientError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ButtplugClientError::ButtplugError(ref e) => e.fmt(f), ButtplugClientError::ButtplugClientConnectorError(ref e) => e.fmt(f), } } } impl Error for ButtplugClientError { fn source(&self) -> Option<&(dyn Error + 'static)> { None } } impl From<ButtplugClientConnectorError> for ButtplugClientError { fn from(error: ButtplugClientConnectorError) -> Self { ButtplugClientError::ButtplugClientConnectorError(error) } } impl From<ButtplugMessageError> for ButtplugClientError { fn from(error: ButtplugMessageError) -> Self { ButtplugClientError::ButtplugError(ButtplugError::ButtplugMessageError(error)) } } impl From<ButtplugDeviceError> for ButtplugClientError { fn from(error: ButtplugDeviceError) -> Self { ButtplugClientError::ButtplugError(ButtplugError::ButtplugDeviceError(error)) } } /// Enum representing different events that can be emitted by a client. /// /// These events are created by the server and sent to the client, and represent /// unrequested actions that the client will need to respond to, or that /// applications using the client may be interested in. pub enum ButtplugClientEvent { /// Emitted when a scanning session (started via a StartScanning call on /// [ButtplugClient]) has finished. ScanningFinished, /// Emitted when a device has been added to the server. Includes a /// [ButtplugClientDevice] object representing the device. DeviceAdded(ButtplugClientDevice), /// Emitted when a device has been removed from the server. Includes a /// [ButtplugClientDevice] object representing the device. DeviceRemoved(DeviceMessageInfo), /// Emitted when log messages are sent from the server. Log(LogLevel, String), /// Emitted when a client has not pinged the server in a sufficient amount /// of time. PingTimeout, /// Emitted when a client connector detects that the server has /// disconnected. ServerDisconnect, } /// 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 [ButtplugClientConnector]s /// - Emitting events received from the Server /// - Holding state related to the server (i.e. what devices are currently /// connected, etc...) /// /// Clients are created by the [ButtplugClient::run()] method, which also /// handles spinning up the event loop and connecting the client to the server. /// Closures passed to the run() method can access and use the Client object. pub struct ButtplugClient { /// The client name. Depending on the connection type and server being used, /// this name is sometimes shown on the server logs or GUI. pub client_name: String, /// The server name. Once connected, this contains the name of the server, /// so we can know what we're connected to. pub server_name: Option<String>, // Sender to relay messages to the internal client loop message_sender: Sender<ButtplugClientMessage>, // Receives event notifications from the ButtplugClientLoop event_receiver: Receiver<ButtplugClientEvent>, // True if the connector is currently connected, and handshake was // successful. connected: bool, // Storage for events received when checking for events during // non-wait_for_event calls. events: Vec<ButtplugClientEvent>, } unsafe impl Sync for ButtplugClient {} unsafe impl Send for ButtplugClient {} impl ButtplugClient { /// Runs the client event loop. /// /// Given a client name, a connector, 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, and connects it to the server via the /// connector instance that was passed in. /// - passes it to the `func` argument to create the application [Future] /// - returns a [Future] that joins the client event loop future and /// the client application future. /// /// # Parameters /// /// - `name`: Name of the client, see [ButtplugClient::client_name] /// - `connector`: Connector instance for handling connection and communication /// with the Buttplug Server /// - `func`: Function that takes the client instance, and returns a future /// for what the application will be doing with the client instance. /// /// # Returns /// /// Ok(()) if connection is successful and closure executes correctly, /// Err(ButtplugClientError) if connection with the server fails. /// /// # Examples /// /// ``` /// #[cfg(feature = "server")] /// use buttplug::client::{ButtplugClient, connectors::ButtplugEmbeddedClientConnector}; /// /// #[cfg(feature = "server")] /// futures::executor::block_on(async { /// ButtplugClient::run("Test Client", ButtplugEmbeddedClientConnector::new("Test Server", 0), |mut client| { /// async move { /// println!("Are we connected? {}", client.connected()); /// } /// }).await; /// }); /// ``` pub fn run<F, T>( name: &str, connector: impl ButtplugClientConnector + 'static, func: F, ) -> impl Future<Output = ButtplugClientResult> where F: FnOnce(ButtplugClient) -> T, T: Future<Output = ()>, { debug!("Run called!"); let (event_sender, event_receiver) = channel(256); let (message_sender, message_receiver) = channel(256); let mut client = ButtplugClient { client_name: name.to_string(), server_name: None, event_receiver, message_sender, connected: true, events: vec![], }; let app_future = async move { client.connect(connector).await?; func(client).await; Ok(()) }; async move { let internal_loop_future = client_event_loop(event_sender, message_receiver); app_future.race(internal_loop_future).await } } /// Connects and runs handshake flow with /// [crate::server::server::ButtplugServer], either local or remote. /// /// Called by run() while spinning up the event loop. 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(self) async fn connect( &mut self, connector: impl ButtplugClientConnector + 'static, ) -> ButtplugClientResult { debug!("Running client connection."); // Send the connector to the internal loop for management. Once we throw // the connector over, the internal loop will handle connecting and any // further communications with the server, if connection is successful. let fut = ButtplugClientConnectionFuture::default(); let msg = ButtplugClientMessage::Connect(Box::new(connector), fut.get_state_clone()); self.send_internal_message(msg).await?; debug!("Waiting on internal loop to connect"); if let Err(e) = fut.await { return Err(ButtplugClientError::from(e)); } info!("Client connected to server, running handshake."); self.handshake().await } // Runs the handshake flow with the server. // // Sends over RequestServerInfo, gets back ServerInfo, sets up ping timer if // needed. async fn handshake(&mut self) -> ButtplugClientResult { info!("Running handshake with server."); match self .send_message(&RequestServerInfo::new(&self.client_name, 1).into()) .await { Ok(msg) => { debug!("Got ServerInfo return."); if let ButtplugMessageUnion::ServerInfo(server_info) = msg { info!("Connected to {}", server_info.server_name); self.server_name = Option::Some(server_info.server_name); // TODO Handle ping time in the internal event loop // Get currently connected devices. The event loop will // handle sending the message and getting the return, and // will send the client updates as events. let msg = self .send_message(&RequestDeviceList::default().into()) .await?; if let ButtplugMessageUnion::DeviceList(m) = msg { self.send_internal_message(ButtplugClientMessage::HandleDeviceList(m)) .await?; } Ok(()) } else { self.disconnect().await?; Err(ButtplugClientError::ButtplugError( ButtplugError::ButtplugHandshakeError(ButtplugHandshakeError { message: "Did not receive expected ServerInfo or Error messages." .to_string(), }), )) } } // TODO Error message case may need to be implemented here when // we aren't only using embedded connectors. Err(e) => Err(e), } } /// Status of the client connection. /// /// # Returns /// Returns true if client is currently connected to server, false otherwise. pub fn connected(&self) -> bool { self.connected } /// Disconnects from server, if connected. /// /// # Returns /// /// Ok(()) if disconnection is successful, Err(ButtplugClientError) if /// disconnection fails. It can be assumed that even on failure, the client /// will be disconnected. pub async fn disconnect(&mut self) -> ButtplugClientResult { // Send the connector to the internal loop for management. Once we throw // the connector over, the internal loop will handle connecting and any // further communications with the server, if connection is successful. let fut = ButtplugClientConnectionFuture::default(); let msg = ButtplugClientMessage::Disconnect(fut.get_state_clone()); self.send_internal_message(msg).await?; self.connected = false; Ok(()) } /// Tells server to start scanning for devices. /// /// # Returns /// /// Ok(()) if request is successful, Err([ButtplugClientError]) if request /// fails due to issues with DeviceManagers on the server, disconnection, /// etc. pub async fn start_scanning(&mut self) -> ButtplugClientResult { self.send_message_expect_ok(&StartScanning::default().into()).await } // Don't expose outside of crate, just handy to use for internal tests. pub (crate) async fn test(&mut self) -> ButtplugClientResult { let test_string = "client test"; self.send_message(&messages::Test::new(test_string).into()) .await .and_then(|msg| { if let ButtplugMessageUnion::Test(m) = msg { if m.test_string == test_string { Ok(()) } else { Err(ButtplugClientError::ButtplugError(ButtplugUnknownError::new("Test strings did not match").into())) } } else { Err(ButtplugClientError::ButtplugError(ButtplugUnknownError::new("Test did not return Test message").into())) } }) .map_err(|err| err) } // Send message to the internal event loop. Mostly for handling boilerplate // around possible send errors. async fn send_internal_message( &mut self, msg: ButtplugClientMessage, ) -> Result<(), ButtplugClientConnectorError> { // Since we're using async_std channels, if we send a message and the // event loop has shut down, we may never know (and therefore possibly // block infinitely) if we don't check the status of an event loop // receiver to see if it's returned None. Always run connection/event // checks before sending messages to the event loop. self.check_for_events().await?; // If we're running the event loop, we should have a message_sender. // Being connected to the server doesn't matter here yet because we use // this function in order to connect also. self.message_sender.send(msg).await; Ok(()) } // Sends a ButtplugMessage from client to server. Expects to receive a // ButtplugMessage back from the server. async fn send_message( &mut self, msg: &ButtplugMessageUnion, ) -> Result<ButtplugMessageUnion, ButtplugClientError> { // Create a future to pair with the message being resolved. let fut = ButtplugMessageFuture::default(); let internal_msg = ButtplugClientMessage::Message((msg.clone(), fut.get_state_clone())); // Send message to internal loop and wait for return. self.send_internal_message(internal_msg).await?; Ok(fut.await) } // Sends a ButtplugMessage from client to server. Expects to receive an [Ok] // type ButtplugMessage back from the server. async fn send_message_expect_ok(&mut self, msg: &ButtplugMessageUnion) -> ButtplugClientResult { match self.send_message(msg).await? { ButtplugMessageUnion::Ok(_) => Ok(()), _ => Err(ButtplugMessageError::new("Got non-Ok message back").into()), } } async fn check_for_events(&mut self) -> Result<(), ButtplugClientConnectorError> { if !self.connected { return Err(ButtplugClientConnectorError::new("Client not connected.")); } while !self.event_receiver.is_empty() { match self.event_receiver.next().await { Some(msg) => self.events.push(msg), None => { self.connected = false; // If we got None, this means the internal loop stopped and our // sender was dropped. We should consider this a disconnect. self.events.push(ButtplugClientEvent::ServerDisconnect); return Err(ButtplugClientConnectorError::new("Client not connected.")); } } } Ok(()) } /// Produces a future that will wait for a set of events from the /// internal loop. Returns every time an event 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. /// /// # Returns /// /// Ok([ButtplugClientEvent]) if event is received successfully, /// Err([ButtplugClientConnectorError]) if waiting fails due to server/client /// disconnection. pub async fn wait_for_event( &mut self, ) -> Result<ButtplugClientEvent, ButtplugClientConnectorError> { debug!("Client waiting for event."); if !self.connected { return Err(ButtplugClientConnectorError::new("Client not connected.")); } Ok({ if !self.events.is_empty() { self.events.pop().unwrap() } else { match self.event_receiver.next().await { Some(msg) => msg, None => { // If we got None, this means the internal loop stopped and our // sender was dropped. We should consider this a disconnect. self.connected = false; ButtplugClientEvent::ServerDisconnect } } } }) } /// Retreives a list of devices. This requires communication with the Event /// Loop, which is why it is an asynchronous function. /// /// # Returns /// /// Ok(Vec<[ButtplugClientDevice]>) if successful, /// Err([ButtplugClientConnectorError]) if the server has disconnected. pub async fn devices( &mut self, ) -> Result<Vec<ButtplugClientDevice>, ButtplugClientConnectorError> { info!("Request devices from inner loop!"); let fut = ButtplugFuture::<Vec<ButtplugClientDevice>>::default(); let msg = ButtplugClientMessage::RequestDeviceList(fut.get_state_clone()); info!("Sending device request to inner loop!"); self.send_internal_message(msg).await?; info!("Waiting for device list return!"); Ok(fut.await) } } #[cfg(all(test, feature = "server"))] mod test { use super::ButtplugClient; use crate::{ client::connectors::{ ButtplugClientConnector, ButtplugClientConnectorError, ButtplugEmbeddedClientConnector, }, core::messages::ButtplugMessageUnion, util::future::ButtplugMessageStateShared, }; use async_std::{ future::Future, sync::{channel, Receiver}, task, }; use async_trait::async_trait; use env_logger; async fn connect_test_client<F, T>(func: F) where F: FnOnce(ButtplugClient) -> T, T: Future<Output = ()>, { let _ = env_logger::builder().is_test(true).try_init(); assert!(ButtplugClient::run( "Test Client", ButtplugEmbeddedClientConnector::new("Test Server", 0), func ) .await .is_ok()); } #[derive(Default)] struct ButtplugFailingConnector {} #[async_trait] impl ButtplugClientConnector for ButtplugFailingConnector { async fn connect(&mut self) -> Result<(), ButtplugClientConnectorError> { Err(ButtplugClientConnectorError::new("Always fails")) } async fn disconnect(&mut self) -> Result<(), ButtplugClientConnectorError> { Err(ButtplugClientConnectorError::new("Always fails")) } async fn send(&mut self, _msg: &ButtplugMessageUnion, _state: &ButtplugMessageStateShared) { } fn get_event_receiver(&mut self) -> Receiver<ButtplugMessageUnion> { // This will panic if we've already taken the receiver. let (_send, recv) = channel(256); recv } } #[test] fn test_failing_connection() { let _ = env_logger::builder().is_test(true).try_init(); task::block_on(async { assert!(ButtplugClient::run( "Test Client", ButtplugFailingConnector::default(), |_| { async {} } ) .await .is_err()); }); } #[test] fn test_disconnect_status() { task::block_on(async { connect_test_client(|mut client| { async move { assert!(client.disconnect().await.is_ok()); assert!(!client.connected()); } }) .await; }); } #[test] fn test_double_disconnect() { task::block_on(async { connect_test_client(|mut client| { async move { assert!(client.disconnect().await.is_ok()); assert!(client.disconnect().await.is_err()); } }) .await; }); } #[test] fn test_connect_init() { task::block_on(async { connect_test_client(|client| { async move { assert_eq!(client.server_name.as_ref().unwrap(), "Test Server"); } }) .await; }); } #[test] fn test_test_msg() { task::block_on(async { connect_test_client(|mut client| async move { assert!(client.test().await.is_ok()); }) .await; }); } // Test ignored until we have a test device manager. #[test] #[ignore] fn test_start_scanning() { task::block_on(async { connect_test_client(|mut client| { async move { assert!(client.start_scanning().await.is_ok()); } }) .await; }); } // #[test] // fn test_scanning_finished() { // task::block_on(async { // let mut client = connect_test_client().await; // assert_eq!(client.server_name.as_ref().unwrap(), "Test Server"); // assert!(client.start_scanning().await.is_none()); // }); // } // Failure on server version error is unit tested in server. }