feat: server sdk (#47)
* New webrtc build scripts (Still not integrated within the SDK) * New livekit-api crate (Integrate the server sdk protocol of livekit) * Moved livekit-utils to livekit-protocol
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// TODO(theomonnom): Async methods
|
||||
#[macro_export]
|
||||
macro_rules! enum_dispatch {
|
||||
// This arm is used to avoid nested loops with the arguments
|
||||
// The arguments are transformed to $combined_args tt
|
||||
(@match [$($variant:ident),+]: $fnc:ident, $self:ident, $combined_args:tt) => {
|
||||
match $self {
|
||||
$(
|
||||
Self::$variant(inner) => inner.$fnc$combined_args,
|
||||
)+
|
||||
}
|
||||
};
|
||||
|
||||
// Create the function and extract self fron the $args tt (little hack)
|
||||
(@fnc [$($variant:ident),+]: $vis:vis fn $fnc:ident($self:ident: $sty:ty $(, $arg:ident: $t:ty)*) -> $ret:ty) => {
|
||||
#[inline]
|
||||
$vis fn $fnc($self: $sty, $($arg: $t),*) -> $ret {
|
||||
enum_dispatch!(@match [$($variant),+]: $fnc, $self, ($($arg,)*))
|
||||
}
|
||||
};
|
||||
|
||||
($variants:tt; $($vis:vis fn $fnc:ident$args:tt -> $ret:ty;)+) => {
|
||||
$(
|
||||
enum_dispatch!(@fnc $variants: $vis fn $fnc$args -> $ret);
|
||||
)+
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
pub mod observer;
|
||||
pub mod enum_dispatch;
|
||||
|
||||
pub mod livekit {
|
||||
include!(concat!(env!("OUT_DIR"), "/livekit.rs"));
|
||||
}
|
||||
|
||||
pub use livekit::*;
|
||||
@@ -0,0 +1,70 @@
|
||||
use futures_util::sink::Sink;
|
||||
use futures_util::task::{Context, Poll};
|
||||
use parking_lot::Mutex;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
senders: Arc<Mutex<Vec<mpsc::UnboundedSender<T>>>>,
|
||||
}
|
||||
|
||||
impl<T> Default for Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
senders: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
pub fn register(&self) -> mpsc::UnboundedReceiver<T> {
|
||||
let (tx, rx) = mpsc::unbounded_channel();
|
||||
self.senders.lock().push(tx);
|
||||
rx
|
||||
}
|
||||
|
||||
pub fn dispatch(&self, msg: &T) {
|
||||
self.senders
|
||||
.lock()
|
||||
.retain(|sender| sender.send(msg.clone()).is_ok());
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.senders.lock().clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sink<T> for Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
|
||||
self.dispatch(&item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user