use crate::core::io::{BroadcastScope, ChannelId, ChannelInfo, ConnectionId, OutgoingFrame};
use crate::core::utils::Sealed;
use crate::prelude::*;
pub trait CallbackApiInternal<V: MaybeVersioned>: Sealed {
unsafe fn send_internal(&self, frame: OutgoingFrame<V>) -> Result<()>;
fn process_frame(&self, frame: &Frame<V>) -> Result<Frame<V>>;
}
pub trait CallbackApi<V: MaybeVersioned>: CallbackApiInternal<V> {
fn info(&self) -> &ChannelInfo;
#[inline(always)]
fn channel_id(&self) -> ChannelId {
self.info().id()
}
#[inline(always)]
fn connection_id(&self) -> ConnectionId {
self.info().connection_id()
}
fn send(&self, frame: &Frame<V>) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe { self.send_internal(OutgoingFrame::scoped(frame, BroadcastScope::All)) }
}
fn respond(&self, frame: &Frame<V>) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe {
self.send_internal(OutgoingFrame::scoped(
frame,
BroadcastScope::ExactChannel(self.channel_id()),
))
}
}
fn broadcast(&self, frame: &Frame<V>) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe {
self.send_internal(OutgoingFrame::scoped(
frame,
BroadcastScope::ExceptChannel(self.channel_id()),
))
}
}
fn broadcast_within(&self, frame: &Frame<V>) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe {
self.send_internal(OutgoingFrame::scoped(
frame,
BroadcastScope::ExceptChannelWithin(self.channel_id()),
))
}
}
fn broadcast_except(&self, frame: &Frame<V>) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe {
self.send_internal(OutgoingFrame::scoped(
frame,
BroadcastScope::ExceptConnection(self.connection_id()),
))
}
}
fn forward(&self, frame: &Frame<V>, connection_id: ConnectionId) -> Result<()> {
let frame = self.process_frame(frame)?;
unsafe {
self.send_internal(OutgoingFrame::scoped(
frame,
BroadcastScope::ExactConnection(connection_id),
))
}
}
}