use super::{ConnectionContext, ProtocolHooks};
use crate::{codec::EofError, push::PushHandle};
pub(crate) struct ConnectionProtocol<F, E> {
hooks: ProtocolHooks<F, E>,
context: ConnectionContext,
}
impl<F, E> ConnectionProtocol<F, E> {
pub(crate) fn new(hooks: ProtocolHooks<F, E>) -> Self {
Self {
hooks,
context: ConnectionContext::default(),
}
}
pub(crate) const fn with_context(
hooks: ProtocolHooks<F, E>,
context: ConnectionContext,
) -> Self {
Self { hooks, context }
}
pub(crate) const fn set_context(&mut self, context: ConnectionContext) {
self.context = context;
}
pub(crate) fn on_connection_setup(&mut self, handle: PushHandle<F>) {
self.hooks.on_connection_setup(handle, &mut self.context);
}
pub(crate) fn before_send(&mut self, frame: &mut F) {
self.hooks.before_send(frame, &mut self.context);
}
pub(crate) fn on_eof(&mut self, error: &EofError, partial_data: &[u8]) {
self.hooks.on_eof(error, partial_data, &mut self.context);
}
pub(crate) fn on_command_end(&mut self) { self.hooks.on_command_end(&mut self.context); }
pub(crate) fn handle_error(&mut self, error: E) {
self.hooks.handle_error(error, &mut self.context);
}
pub(crate) fn stream_end_frame(&mut self) -> Option<F> {
self.hooks.stream_end_frame(&mut self.context)
}
}