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
use libc::c_int;
use nanomsg_sys;
use result::{Result,last_nano_error};

/// An endpoint created for a specific socket. Each endpoint is identified
/// by a unique return value that can be further passed to a shutdown
/// function. The shutdown is done through the endpoint itself and not the Socket
pub struct Endpoint {
    value: c_int,
    socket: c_int
}

impl Endpoint {
    pub fn new(value: c_int, socket: c_int) -> Endpoint {
        Endpoint { value: value, socket: socket }
    }

    /// Removes an endpoint from the socket that created it (via `bind` or `connect`).
    /// The call will return immediately, however, 
    /// the library will try to deliver any outstanding outbound messages to the endpoint 
    /// for the time specified by `Socket::set_linger`.
    pub fn shutdown(&mut self) -> Result<()> {
        let ret = unsafe { nanomsg_sys::nn_shutdown(self.socket, self.value) };

        if ret == -1 {
            Err(last_nano_error())
        } else {
            Ok(())
        }
    }
}