pub struct HydraClient<R> { /* private fields */ }Expand description
HydraClient - Cross-lang-friendly client connects to the HydraServer as a producer or consumer,
performs handshake, and sends/receives encrypted packet (AES-GCM). It maintains an internal memory buffer (12 Mb) for zero-copy crypto and buffering.
use hydra_sync::client::{HydraClient, Producer, Consumer};
#[tokio::main]
async fn main() {
let addr = "127.0.0.1:8000";
let session_id = [0xFFu8; 64];
let session_key = [0xAAu8; 32];
let mut producer = HydraClient::<Producer>::connect(addr, session_id, session_key).await.unwrap();
producer.broadcast(b"I luv you >.<").await.unwrap(); // sends to all consumer
let mut consumer = HydraClient::<Consumer>::connect(addr, session_id, session_key).await.unwrap();
consumer.recv().await.unwrap(); // recv whatever next frame on ring buf
}Implementations§
Source§impl HydraClient<Producer>
impl HydraClient<Producer>
Sourcepub async fn connect<A: ToSocketAddrs>(
addr: A,
session_id: [u8; 64],
session_key: [u8; 32],
) -> Result<Self>
pub async fn connect<A: ToSocketAddrs>( addr: A, session_id: [u8; 64], session_key: [u8; 32], ) -> Result<Self>
Connects to the server, performs handshake, sends encrypted JoinHeader, decrypts/reads the StatusCode response,
and returns a HydraClient<Producer> instance if successful.
Sourcepub async fn broadcast(&mut self, data: &[u8]) -> Result<()>
pub async fn broadcast(&mut self, data: &[u8]) -> Result<()>
Broadcasts the given data as fixed encrypted packets (AES-GCM256) to all connected consumers (Zero-copy)
and the HydraServer does not decrypt it..
The caller must ensure data.len() is an exact multiple of the server’s payload capacity (server_read_write_len i.e. raw bytes per packet, AES-GCM (28 bytes) overhead is handled internally), or this will return an error.
This may block until the packets are written to the server ring buffer, this behavior is configured by
ChannelOverflowStrategy.
Source§impl HydraClient<Consumer>
impl HydraClient<Consumer>
Sourcepub async fn connect<A: ToSocketAddrs>(
addr: A,
session_id: [u8; 64],
session_key: [u8; 32],
) -> Result<Self>
pub async fn connect<A: ToSocketAddrs>( addr: A, session_id: [u8; 64], session_key: [u8; 32], ) -> Result<Self>
Connects to the server, performs handshake, sends encrypted JoinHeader, decrypts/reads the StatusCode response,
and returns a HydraClient<Consumer> instance if successful.
Sourcepub async fn recv(&mut self) -> Result<&[u8]>
pub async fn recv(&mut self) -> Result<&[u8]>
Receives the next fixed encrypted packet from the producer, decrypts it, and returns the plaintext data as a byte slice.
The returned slice is valid until the next call to recv or broadcast, which reuse the internal scratch buffer.
This may block until the next packet is available on server consumer’s queue.
Source§impl<R> HydraClient<R>
impl<R> HydraClient<R>
Sourcepub fn get_server_read_write_length(&self) -> u64
pub fn get_server_read_write_length(&self) -> u64
Returns the number of raw payload bytes per packet that server considers for read/write operations.
NOTE: This is the plaintext data length of a single packet in bytes, the
AES-GCMnonce and tag are handled internally.Broadcastalready checks againstserver_read_write_lento ensure match the API correctly. So you might wanna check your packet size againstserver_read_write_len, no crypto arithmetic needed on your side.