use std::sync::Arc;
use crate::error::Result;
use crate::socket::Socket;
#[derive(Clone)]
pub struct Context
{
inner: Arc<Inner>,
}
impl Context
{
pub fn new(socket: &Socket) -> Result<Context>
{
let mut ctx = nng_sys::NNG_CTX_INITIALIZER;
let rv = unsafe {
nng_sys::nng_ctx_open(&mut ctx as _, socket.handle())
};
rv2res!(rv, Context { inner: Arc::new(Inner { ctx }) })
}
pub fn id(&self) -> i32
{
let id = unsafe { nng_sys::nng_ctx_id(self.inner.ctx) };
assert!(id > 0, "Invalid context ID returned from valid context");
id
}
pub(crate) fn handle(&self) -> nng_sys::nng_ctx
{
self.inner.ctx
}
}
expose_options!{
Context :: inner.ctx -> nng_sys::nng_ctx;
GETOPT_BOOL = nng_sys::nng_ctx_getopt_bool;
GETOPT_INT = nng_sys::nng_ctx_getopt_int;
GETOPT_MS = nng_sys::nng_ctx_getopt_ms;
GETOPT_SIZE = nng_sys::nng_ctx_getopt_size;
GETOPT_SOCKADDR = crate::fake_opt;
GETOPT_STRING = crate::fake_opt;
SETOPT = nng_sys::nng_ctx_setopt;
SETOPT_BOOL = nng_sys::nng_ctx_setopt_bool;
SETOPT_INT = nng_sys::nng_ctx_setopt_int;
SETOPT_MS = nng_sys::nng_ctx_setopt_ms;
SETOPT_SIZE = nng_sys::nng_ctx_setopt_size;
SETOPT_STRING = crate::fake_opt;
Gets -> [protocol::reqrep::ResendTime, protocol::survey::SurveyTime];
Sets -> [protocol::reqrep::ResendTime, protocol::survey::SurveyTime];
}
struct Inner
{
ctx: nng_sys::nng_ctx,
}
impl Drop for Inner
{
fn drop(&mut self)
{
let rv = unsafe { nng_sys::nng_ctx_close(self.ctx) };
assert!(
rv == 0 || rv == nng_sys::NNG_ECLOSED,
"Unexpected error code while closing context ({})", rv
);
}
}