pub struct ClientMessageSorter { /* private fields */ }Expand description
Message sorting and pairing for remote client connectors.
In order to create reliable connections to remote systems, we need a way to maintain message coherence. We expect that whenever a client sends the server a request message, the server will always send back a response message.
For the [in-process][crate::connector::ButtplugInProcessClientConnector] case, where the client and
server are in the same process, we can simply use execution flow to match the client message and
server response. However, when going over IPC or network, we have to wait to hear back from the
server. To match the outgoing client request message with the incoming server response message
in the remote case, we use the id field of ButtplugMessage. The client’s request message
will have a server response with a matching index. Any message that comes from the server
without an originating client message ([DeviceAdded][crate::core::messages::DeviceAdded],
[Log][crate::core::messages::Log], etc…) will have an id of 0 and is considered an event,
meaning something happened on the server that was not directly tied to a client request.
The ClientConnectionMessageSorter does two things to facilitate this matching:
- Creates and keeps track of the current message
id, as a u32 - Manages a HashMap of indexes to resolvable futures.
Whenever a remote connector sends a ButtplugMessage, it first puts it through its
ClientMessageSorter to fill in the message id. Similarly, when a ButtplugMessage is
received, it comes through the sorter, with one of 3 outcomes:
- If there is a future with matching
idwaiting on a response, it resolves that future using the incoming message - If the message
idis 0, the message is emitted as an event. - If the message
idis not zero but there is no future waiting, the message is dropped and an error is emitted.
Implementations§
Source§impl ClientMessageSorter
impl ClientMessageSorter
Sourcepub fn register_future(&self, msg_fut: &mut ButtplugClientMessageFuturePair)
pub fn register_future(&self, msg_fut: &mut ButtplugClientMessageFuturePair)
Registers a future to be resolved when we receive a response.
Given a message and its related future, set the message’s id, and match that id with the
future to be resolved when we get a response back.
Sourcepub fn maybe_resolve_result(&self, msg: &ButtplugServerMessageV3) -> bool
pub fn maybe_resolve_result(&self, msg: &ButtplugServerMessageV3) -> bool
Given a response message from the server, resolve related future if we have one.
Returns true if the response message was resolved to a future via matching id, otherwise
returns false. False returns mean the message should be considered as an event.