use std::{io, sync::Arc};
use tokio_util::codec::{Decoder, Encoder};
use crate::service::ServiceAsyncControl;
use crate::{
context::{ProtocolContext, ProtocolContextMutRef, ServiceContext, SessionContext},
service::{ServiceError, ServiceEvent},
substream::SubstreamReadPart,
};
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait ServiceHandle: Send {
async fn handle_error(&mut self, _control: &mut ServiceContext, _error: ServiceError) {}
async fn handle_event(&mut self, _control: &mut ServiceContext, _event: ServiceEvent) {}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait ServiceProtocol: Send {
async fn init(&mut self, context: &mut ProtocolContext);
async fn connected(&mut self, _context: ProtocolContextMutRef<'_>, _version: &str) {}
async fn disconnected(&mut self, _context: ProtocolContextMutRef<'_>) {}
async fn received(&mut self, _context: ProtocolContextMutRef<'_>, _data: bytes::Bytes) {}
async fn notify(&mut self, _context: &mut ProtocolContext, _token: u64) {}
#[inline]
async fn poll(&mut self, _context: &mut ProtocolContext) -> Option<()> {
None
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait SessionProtocol: Send {
async fn connected(&mut self, _context: ProtocolContextMutRef<'_>, _version: &str) {}
async fn disconnected(&mut self, _context: ProtocolContextMutRef<'_>) {}
async fn received(&mut self, _context: ProtocolContextMutRef<'_>, _data: bytes::Bytes) {}
async fn notify(&mut self, _context: ProtocolContextMutRef<'_>, _token: u64) {}
#[inline]
async fn poll(&mut self, _context: ProtocolContextMutRef<'_>) -> Option<()> {
None
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ServiceHandle for Box<dyn ServiceHandle + Send + 'static> {
async fn handle_error(&mut self, control: &mut ServiceContext, error: ServiceError) {
(&mut **self).handle_error(control, error).await
}
async fn handle_event(&mut self, control: &mut ServiceContext, event: ServiceEvent) {
(&mut **self).handle_event(control, event).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ServiceHandle for Box<dyn ServiceHandle + Send + Sync + 'static> {
async fn handle_error(&mut self, control: &mut ServiceContext, error: ServiceError) {
(&mut **self).handle_error(control, error).await
}
async fn handle_event(&mut self, control: &mut ServiceContext, event: ServiceEvent) {
(&mut **self).handle_event(control, event).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ServiceHandle for () {}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ServiceProtocol for Box<dyn ServiceProtocol + Send + 'static + Unpin> {
async fn init(&mut self, context: &mut ProtocolContext) {
(&mut **self).init(context).await
}
async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
(&mut **self).connected(context, version).await
}
async fn disconnected(&mut self, context: ProtocolContextMutRef<'_>) {
(&mut **self).disconnected(context).await
}
async fn received(&mut self, context: ProtocolContextMutRef<'_>, data: bytes::Bytes) {
(&mut **self).received(context, data).await
}
async fn notify(&mut self, context: &mut ProtocolContext, token: u64) {
(&mut **self).notify(context, token).await
}
#[inline]
async fn poll(&mut self, context: &mut ProtocolContext) -> Option<()> {
(&mut **self).poll(context).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl ServiceProtocol for Box<dyn ServiceProtocol + Send + Sync + 'static + Unpin> {
async fn init(&mut self, context: &mut ProtocolContext) {
(&mut **self).init(context).await
}
async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
(&mut **self).connected(context, version).await
}
async fn disconnected(&mut self, context: ProtocolContextMutRef<'_>) {
(&mut **self).disconnected(context).await
}
async fn received(&mut self, context: ProtocolContextMutRef<'_>, data: bytes::Bytes) {
(&mut **self).received(context, data).await
}
async fn notify(&mut self, context: &mut ProtocolContext, token: u64) {
(&mut **self).notify(context, token).await
}
#[inline]
async fn poll(&mut self, context: &mut ProtocolContext) -> Option<()> {
(&mut **self).poll(context).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl SessionProtocol for Box<dyn SessionProtocol + Send + 'static + Unpin> {
async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
(&mut **self).connected(context, version).await
}
async fn disconnected(&mut self, context: ProtocolContextMutRef<'_>) {
(&mut **self).disconnected(context).await
}
async fn received(&mut self, context: ProtocolContextMutRef<'_>, data: bytes::Bytes) {
(&mut **self).received(context, data).await
}
async fn notify(&mut self, context: ProtocolContextMutRef<'_>, token: u64) {
(&mut **self).notify(context, token).await
}
#[inline]
async fn poll(&mut self, context: ProtocolContextMutRef<'_>) -> Option<()> {
(&mut **self).poll(context).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl SessionProtocol for Box<dyn SessionProtocol + Send + Sync + 'static + Unpin> {
async fn connected(&mut self, context: ProtocolContextMutRef<'_>, version: &str) {
(&mut **self).connected(context, version).await
}
async fn disconnected(&mut self, context: ProtocolContextMutRef<'_>) {
(&mut **self).disconnected(context).await
}
async fn received(&mut self, context: ProtocolContextMutRef<'_>, data: bytes::Bytes) {
(&mut **self).received(context, data).await
}
async fn notify(&mut self, context: ProtocolContextMutRef<'_>, token: u64) {
(&mut **self).notify(context, token).await
}
#[inline]
async fn poll(&mut self, context: ProtocolContextMutRef<'_>) -> Option<()> {
(&mut **self).poll(context).await
}
}
#[cfg_attr(not(feature = "unstable"), doc(hidden))]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
pub trait ProtocolSpawn {
fn spawn(
&self,
context: Arc<SessionContext>,
control: &ServiceAsyncControl,
read_part: SubstreamReadPart,
);
}
pub trait Codec:
Decoder<Item = bytes::BytesMut, Error = io::Error> + Encoder<bytes::Bytes, Error = io::Error>
{
}
impl<T> Codec for T where
T: Decoder<Item = bytes::BytesMut, Error = io::Error>
+ Encoder<bytes::Bytes, Error = io::Error>
{
}
impl Decoder for Box<dyn Codec + Send + 'static> {
type Item = bytes::BytesMut;
type Error = io::Error;
fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
Decoder::decode(&mut **self, src)
}
}
impl Encoder<bytes::Bytes> for Box<dyn Codec + Send + 'static> {
type Error = io::Error;
fn encode(&mut self, item: bytes::Bytes, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> {
Encoder::encode(&mut **self, item, dst)
}
}