transfer.rs

Overview

The transfer.rs file provides core functionality for serializing and sending network messages over a connection, as well as handling and representing errors that may arise during the transport process. It defines a custom error enumeration TransportError encapsulating various transport-related failures, and it exposes an asynchronous function transfer that transmits serialized messages over a network connection while optionally reporting transmission metrics.


Entities and Functions

TransportError Enum

TransportError is an error type representing various failure modes that can occur during network transport operations. It leverages the thiserror::Error derive macro to provide human-readable error messages and integrates with underlying errors from the wtransport crate.

Variants

Methods


transfer Function

pub async fn transfer(
    connection: &impl NetConnection,
    net_message: &NetMessage,
    metrics: &Option<NetMetrics>,
) -> Result<usize, TransportError>

Purpose

Serializes a given network message and asynchronously sends it over the provided network connection. It optionally records metrics about the serialization and sending process.

Parameters

Return Value

Returns a Result<usize, TransportError>:

Implementation Details

  1. Serialization: Uses bincode::serialize to convert the net_message into a byte vector. Serialization errors are wrapped in TransportError::BincodeSerialization.

  2. Timing: Captures the current time immediately before sending to measure the duration of the serialization process for metrics.

  3. Sending: Sends serialized data asynchronously via the send method of the NetConnection. Errors during sending are logged with a detailed error message and converted into the TransportError::Send variant.

  4. Metrics Reporting: If a metrics collector is provided, it reports the elapsed time since serialization started.

Usage Example

let bytes_sent = transfer(&connection, &message, &Some(metrics)).await?;
println!("Sent {} bytes", bytes_sent);

Implementation and Algorithm Details


Interactions with Other Components


Mermaid Class Diagram

classDiagram
class TransportError {
<<enum>>
+Connection(ConnectionError)
+StreamOpening(StreamOpeningError)
+StreamWrite(StreamWriteError)
+Send(String)
+BincodeSerialization(String)
+kind_str() string
}
class transfer {
<<async fn>>
+transfer(connection: NetConnection, net_message: NetMessage, metrics: Option<NetMetrics>) Result<usize, TransportError>
}
TransportError <|-- ConnectionError
TransportError <|-- StreamOpeningError
TransportError <|-- StreamWriteError
transfer ..> TransportError : returns
transfer ..> NetConnection : uses
transfer ..> NetMessage : uses
transfer ..> NetMetrics : optionally uses

This diagram shows the TransportError enum with its variants and method, the transfer asynchronous function, and their relationships with other types used in this file.