Skip to main content

process_messages

Function process_messages 

Source
pub async fn process_messages<C, F, Fut>(
    client: &mut C,
    handler: F,
) -> Result<(), WireError>
where C: WireClient, F: FnMut(WireMessage) -> Fut, Fut: Future<Output = Result<Option<WireResponse>, WireError>>,
Expand description

Process wire messages in a loop, handling events and requests.

The loop exits when:

  • the underlying transport returns WireError::StreamClosed;
  • the handler returns an Err;
  • the client encounters an unrecoverable I/O error.

Parse errors and unknown message types are logged and skipped — the loop keeps running.

§Errors

Returns WireError if reading from the client fails, the handler returns an error, or sending a response fails.

§Example

use kimi_wire::dispatch::{process_messages, WireResponse};
use kimi_wire::message::WireMessage;

process_messages(client, |msg| async move {
    match msg {
        WireMessage::Request(req) => {
            // handle request...
            Ok(Some(WireResponse { id: req.id, result: serde_json::json!(null) }))
        }
        _ => Ok(None),
    }
}).await