pub trait AsyncHandler {
// Required method
fn poll_handle(
self: Pin<&mut Self>,
cx: &mut FutureContext<'_>,
rtmp_context: &mut RtmpContext,
) -> Poll<IOResult<()>>;
}
Expand description
The interface for handling RTMP connection steps with Future
.
This trait unifies surfaces of handler APIs:
RtmpContext
is required.- Terminating with unit (
()
) is required.
The first requirement makes RtmpContext
reusable for upper APIs.
And the second requirement makes handlers return Ok(())
when successfully terminates because currently they are run on main
.
use std::{
io::Result as IOResult,
pin::Pin,
sync::Arc,
task::{
Context as FutureContext,
Poll
}
};
use futures::future::poll_fn;
use tokio::io::{
AsyncRead,
AsyncWrite
};
use sheave_core::handlers::{
AsyncHandler,
RtmpContext
};
struct SomethingHandler<RW: AsyncRead + AsyncWrite + Unpin>(Arc<RW>);
impl<RW: AsyncRead + AsyncWrite + Unpin> AsyncHandler for SomethingHandler<RW> {
fn poll_handle(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext) -> Poll<IOResult<()>> {
// Something to handle
Poll::Ready(Ok(()))
}
}
#[tokio::main]
async fn main() -> IOResult<()> {
// Consider this is Tokio's `JoinHandle` which is run on `main`.
poll_fn(
|cx| {
use std::{
pin::pin,
sync::Arc
};
use sheave_core::handlers::{
AsyncHandler,
VecStream,
StreamWrapper
};
let stream = Arc::new(StreamWrapper::new(VecStream::default()));
pin!(SomethingHandler(stream)).poll_handle(cx, &mut RtmpContext::default())
}
).await
}