use std::io;
use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};
use crate::client::{ClientConfig, ClientEvent, ClientSession};
use crate::server::{ServerEvent, ServerSession};
pub(crate) fn io_err(e: crate::RtmpError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, e)
}
#[derive(Debug)]
pub(crate) struct ServerCodec {
session: ServerSession,
reply: Vec<u8>,
}
impl ServerCodec {
pub(crate) fn new(session: ServerSession) -> Self {
Self {
session,
reply: Vec::new(),
}
}
pub(crate) fn take_reply(&mut self) -> Vec<u8> {
std::mem::take(&mut self.reply)
}
}
impl Decoder for ServerCodec {
type Item = Vec<ServerEvent>;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> io::Result<Option<Vec<ServerEvent>>> {
if src.is_empty() {
return Ok(None);
}
let chunk = src.split();
let (reply, events) = self.session.handle_data(&chunk).map_err(io_err)?;
self.reply.extend_from_slice(&reply);
Ok(Some(events))
}
}
impl Encoder<Vec<u8>> for ServerCodec {
type Error = io::Error;
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> io::Result<()> {
dst.extend_from_slice(&item);
Ok(())
}
}
#[derive(Debug)]
pub(crate) struct ClientCodec {
session: ClientSession,
reply: Vec<u8>,
}
impl ClientCodec {
pub(crate) fn new(config: ClientConfig) -> Self {
Self {
session: ClientSession::new(config),
reply: Vec::new(),
}
}
pub(crate) fn session_mut(&mut self) -> &mut ClientSession {
&mut self.session
}
pub(crate) fn take_reply(&mut self) -> Vec<u8> {
std::mem::take(&mut self.reply)
}
}
impl Decoder for ClientCodec {
type Item = Vec<ClientEvent>;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> io::Result<Option<Vec<ClientEvent>>> {
if src.is_empty() {
return Ok(None);
}
let chunk = src.split();
let (reply, events) = self.session.handle_data(&chunk).map_err(io_err)?;
self.reply.extend_from_slice(&reply);
Ok(Some(events))
}
}
impl Encoder<Vec<u8>> for ClientCodec {
type Error = io::Error;
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> io::Result<()> {
dst.extend_from_slice(&item);
Ok(())
}
}