1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::sync::atomic::{AtomicPtr, Ordering};

pub struct Connection {
    ptr: AtomicPtr<*const ffi::otc_connection>,
}

impl Connection {
    /*string_getter!(
        /// Returns the unique identifier for this connection.
        => (get_id, otc_connection_get_id)
    );

    string_getter!(
        /// Returns the session ID associated with this connection.
        => (get_session_id, otc_connection_get_session_id)
    );*/

    /// Returns the timestamp corresponding with the creation of the OpenTok
    /// session.
    pub fn get_creation_time(&self) -> i64 {
        unsafe {
            ffi::otc_connection_get_creation_time(self.ptr.load(Ordering::Relaxed) as *const _)
        }
    }
}

impl Clone for Connection {
    fn clone(&self) -> Self {
        (self.ptr.load(Ordering::Relaxed) as *const ffi::otc_connection).into()
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        let ptr = self.ptr.load(Ordering::Relaxed);

        if ptr.is_null() {
            return;
        }

        unsafe {
            ffi::otc_connection_delete(ptr as *mut _);
        }

        self.ptr.store(std::ptr::null_mut(), Ordering::Relaxed);
    }
}

impl From<*const ffi::otc_connection> for Connection {
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    fn from(ptr: *const ffi::otc_connection) -> Connection {
        let ptr = unsafe { ffi::otc_connection_copy(ptr) };
        Connection {
            ptr: AtomicPtr::new(ptr as *mut _),
        }
    }
}