| 322 | } |
| 323 | |
| 324 | pub fn parse_checked_message( |
| 325 | &self, |
| 326 | msg: ButtplugCheckedClientMessageV4, |
| 327 | ) -> BoxFuture<'static, Result<ButtplugServerMessageV4, message::ErrorV0>> { |
| 328 | trace!( |
| 329 | "Buttplug Server {} received message to client parse: {:?}", |
| 330 | self.server_name, msg |
| 331 | ); |
| 332 | let id = msg.id(); |
| 333 | |
| 334 | // Check connection state for message validity |
| 335 | { |
| 336 | let state = self.state.read().expect("State lock poisoned"); |
| 337 | let error = match &*state { |
| 338 | ConnectionState::PingedOut => { |
| 339 | // Connection lost due to ping timeout |
| 340 | Some(message::ErrorV0::from(ButtplugError::from( |
| 341 | ButtplugPingError::PingedOut, |
| 342 | ))) |
| 343 | } |
| 344 | ConnectionState::Disconnected => { |
| 345 | // Already disconnected, no reconnection allowed |
| 346 | Some(message::ErrorV0::from(ButtplugError::from( |
| 347 | ButtplugHandshakeError::ReconnectDenied, |
| 348 | ))) |
| 349 | } |
| 350 | ConnectionState::AwaitingHandshake => { |
| 351 | // Only RSI messages allowed before handshake |
| 352 | if !matches!(msg, ButtplugCheckedClientMessageV4::RequestServerInfo(_)) { |
| 353 | Some(message::ErrorV0::from(ButtplugError::from( |
| 354 | ButtplugHandshakeError::RequestServerInfoExpected, |
| 355 | ))) |
| 356 | } else { |
| 357 | None |
| 358 | } |
| 359 | } |
| 360 | ConnectionState::Connected { .. } => { |
| 361 | // Connected, all messages allowed |
| 362 | None |
| 363 | } |
| 364 | }; |
| 365 | if let Some(mut return_error) = error { |
| 366 | return_error.set_id(msg.id()); |
| 367 | return future::ready(Err(return_error)).boxed(); |
| 368 | } |
| 369 | } |
| 370 | // Produce whatever future is needed to reply to the message, this may be a |
| 371 | // device command future, or something the server handles. All futures will |
| 372 | // return Result<ButtplugServerMessage, ButtplugError>, and we'll handle |
| 373 | // tagging the result with the message id in the future we put out as the |
| 374 | // return value from this method. |
| 375 | let out_fut = if ButtplugDeviceManagerMessageUnion::try_from(msg.clone()).is_ok() |
| 376 | || ButtplugDeviceCommandMessageUnionV4::try_from(msg.clone()).is_ok() |
| 377 | { |
| 378 | self.device_manager.parse_message(msg.clone()) |
| 379 | } else { |
| 380 | match msg { |
| 381 | ButtplugCheckedClientMessageV4::RequestServerInfo(rsi_msg) => { |