use tokio::sync::mpsc;
use crate::config::CreditMode;
use crate::error::LinkError;
use crate::link::credit::LinkCredit;
use crate::link::delivery::PartialDelivery;
use crate::link::settlement::UnsettledMap;
use crate::proto::{LinkAttached, LinkEvent, Reply};
use crate::types::definitions::ReceiverSettleMode;
pub const HARD_MAX_MESSAGE_SIZE: u64 = 256 * 1024 * 1024;
#[derive(Debug)]
pub struct ReceiverLink {
pub handle: u32,
pub remote_handle: Option<u32>,
pub name: String,
pub attached: bool,
pub events: mpsc::Sender<LinkEvent>,
pub pending_attach: Option<Reply<LinkAttached, LinkError>>,
pub credit: LinkCredit,
pub unsettled: UnsettledMap,
pub partial: Option<PartialDelivery>,
pub settle_mode: ReceiverSettleMode,
pub max_message_size: Option<u64>,
}
impl ReceiverLink {
pub fn new(
handle: u32,
name: String,
events: mpsc::Sender<LinkEvent>,
pending_attach: Reply<LinkAttached, LinkError>,
settle_mode: ReceiverSettleMode,
credit_mode: CreditMode,
max_message_size: Option<u64>,
) -> Self {
ReceiverLink {
handle,
remote_handle: None,
name,
attached: false,
events,
pending_attach: Some(pending_attach),
credit: LinkCredit::new(0, credit_mode),
unsettled: UnsettledMap::new(),
partial: None,
settle_mode,
max_message_size,
}
}
pub fn size_cap(&self) -> u64 {
self.max_message_size
.map(|m| m.min(HARD_MAX_MESSAGE_SIZE))
.unwrap_or(HARD_MAX_MESSAGE_SIZE)
}
pub fn initial_credit(&self) -> u32 {
match self.credit.mode() {
CreditMode::Auto { initial, .. } => initial,
CreditMode::Manual => 0,
}
}
}