Move session handle abstraction into janus-plugin

This commit is contained in:
Marshall Quander 2017-10-04 15:06:40 -07:00
Родитель 7c39fcaea2
Коммит 15b0a88d79
2 изменённых файлов: 18 добавлений и 46 удалений

6
Cargo.lock сгенерированный
Просмотреть файл

@ -48,12 +48,12 @@ dependencies = [
[[package]]
name = "jansson-sys"
version = "0.1.0"
source = "git+https://github.com/mquander/janus-plugin-rs#d52787b315ddaad4a60166e93ce6a044ae5529ba"
source = "git+https://github.com/mquander/janus-plugin-rs#549a6927aafdc51b641d6b024f475f11fd66fbbf"
[[package]]
name = "janus-plugin"
version = "0.1.0"
source = "git+https://github.com/mquander/janus-plugin-rs#d52787b315ddaad4a60166e93ce6a044ae5529ba"
source = "git+https://github.com/mquander/janus-plugin-rs#549a6927aafdc51b641d6b024f475f11fd66fbbf"
dependencies = [
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -67,7 +67,7 @@ dependencies = [
[[package]]
name = "janus-plugin-sys"
version = "0.1.0"
source = "git+https://github.com/mquander/janus-plugin-rs#d52787b315ddaad4a60166e93ce6a044ae5529ba"
source = "git+https://github.com/mquander/janus-plugin-rs#549a6927aafdc51b641d6b024f475f11fd66fbbf"
dependencies = [
"glib-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jansson-sys 0.1.0 (git+https://github.com/mquander/janus-plugin-rs)",

Просмотреть файл

@ -8,7 +8,6 @@ extern crate jansson_sys as jansson;
use std::error::Error;
use std::ffi::{CStr, CString};
use std::ops::Deref;
use std::os::raw::{c_char, c_int};
use std::ptr;
use std::sync::{Arc, Mutex, RwLock, Weak};
@ -16,6 +15,7 @@ use std::sync::mpsc;
use std::thread;
use janus::{LogLevel, Plugin, PluginCallbacks, PluginMetadata,
PluginResultInfo, PluginResultType, PluginHandle};
use janus::session::SessionHandle;
use jansson::json_t as Json;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@ -25,16 +25,6 @@ enum SessionKind {
Listener,
}
#[derive(Debug)]
struct Message {
pub session: Weak<Session>,
pub transaction: *mut c_char,
pub message: *mut Json,
pub jsep: *mut Json
}
unsafe impl Send for Message {}
#[derive(Debug)]
struct SessionState {
pub kind: SessionKind,
@ -50,6 +40,18 @@ impl SessionState {
}
}
type Session = SessionHandle<Mutex<SessionState>>;
#[derive(Debug)]
struct Message {
pub session: Weak<Session>,
pub transaction: *mut c_char,
pub message: *mut Json,
pub jsep: *mut Json
}
unsafe impl Send for Message {}
const METADATA: PluginMetadata = PluginMetadata {
version: 1,
version_str: cstr!("0.0.1"),
@ -71,36 +73,6 @@ fn gateway_callbacks() -> &'static PluginCallbacks {
unsafe { CALLBACKS.expect("Callbacks not initialized -- did plugin init() succeed?") }
}
#[derive(Debug)]
struct SessionHandle<T> {
pub handle: *mut PluginHandle,
state: T,
}
impl<T> SessionHandle<T> {
pub fn establish(handle: *mut PluginHandle, state: T) -> Box<Arc<Self>> {
let result = Box::new(Arc::new(Self { handle, state: state }));
unsafe { (*handle).plugin_handle = result.as_ref() as *const _ as *mut _ };
result
}
pub fn from_ptr<'a>(handle: *mut PluginHandle) -> &'a Arc<Self> {
unsafe { &*((*handle).plugin_handle as *mut Arc<Self>) }
}
}
impl<T> Deref for SessionHandle<T> {
type Target = T;
fn deref(&self) -> &T {
&self.state
}
}
unsafe impl<T> Sync for SessionHandle<T> {}
unsafe impl<T> Send for SessionHandle<T> {}
type Session = SessionHandle<Mutex<SessionState>>;
#[derive(Debug)]
struct State {
pub sessions: RwLock<Vec<Box<Arc<Session>>>>
@ -238,10 +210,10 @@ fn handle_contents(session: &Session, _transaction: *mut c_char, message: &Json)
let kind = CStr::from_ptr(jansson::json_string_value(kind_json));
if kind == CStr::from_ptr(cstr!("publisher")) {
janus::log(LogLevel::Verb, &format!("Configuring session {:?} as publisher.", session));
session.state.lock().unwrap().set_kind(SessionKind::Publisher)
session.lock().unwrap().set_kind(SessionKind::Publisher)
} else if kind == CStr::from_ptr(cstr!("listener")) {
janus::log(LogLevel::Verb, &format!("Configuring session {:?} as listener.", session));
session.state.lock().unwrap().set_kind(SessionKind::Listener)
session.lock().unwrap().set_kind(SessionKind::Listener)
} else {
Err(From::from("Unknown session kind specified (neither publisher nor listener.)"))
}