use crate::*;
use log::trace;
use runng_sys::*;
pub trait Ctx {
fn ctx(&self) -> nng_ctx;
}
#[derive(Debug)]
pub struct NngCtx {
ctx: nng_ctx,
}
impl NngCtx {
pub fn new(socket: NngSocket) -> Result<Self> {
let mut ctx = nng_ctx::default();
let res = unsafe { nng_ctx_open(&mut ctx, socket.nng_socket()) };
nng_int_to_result(res)?;
let ctx = Self { ctx };
Ok(ctx)
}
pub fn id(&self) -> i32 {
unsafe { nng_ctx_id(self.ctx) }
}
}
impl Ctx for NngCtx {
fn ctx(&self) -> nng_ctx {
self.ctx
}
}
impl Drop for NngCtx {
fn drop(&mut self) {
unsafe {
let id = self.id();
if id > 0 {
trace!("NngCtx.drop {:x}", id);
nng_ctx_close(self.ctx);
}
}
}
}