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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::io;

use mio::{unix::EventedFd, Evented, Poll, PollOpt, Ready, Token};

use crate::Result;

/// Wrapper on top of a ZMQ socket.
///
/// The socket needs to be wrapped to allow various trait implementations.
pub(crate) struct SocketWrapper {
    pub(crate) socket: zmq::Socket,
}

impl SocketWrapper {
    pub fn new(socket: zmq::Socket) -> Self {
        Self { socket }
    }
}

impl Evented for SocketWrapper {
    fn register(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        EventedFd(&self.socket.get_fd()?).register(poll, token, interest, opts)
    }
    fn reregister(
        &self,
        poll: &Poll,
        token: Token,
        interest: Ready,
        opts: PollOpt,
    ) -> io::Result<()> {
        EventedFd(&self.socket.get_fd()?).reregister(poll, token, interest, opts)
    }
    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        EventedFd(&self.socket.get_fd()?).deregister(poll)
    }
}

/// Trait for various ZMQ socket wrappers.
pub trait AsZmqSocket {
    /// Return a reference to the inner ZMQ socket.
    fn get_socket(&self) -> &zmq::Socket;
}

/// Trait which defines configuration functions for ZMQ sockets.
///
/// See ZMQ documentation for more info: [http://api.zeromq.org/4-2:zmq-setsockopt](http://api.zeromq.org/4-2:zmq-setsockopt)
///
/// Some socket options need to be set before bind/connect via the [`SocketBuilder`](struct.SocketBuilder.html) methods
pub trait SocketExt {
    /// Accessor for the `ZMQ_LINGER` option.
    fn get_linger(&self) -> Result<i32>;
    /// Setter for the `ZMQ_LINGER` option.
    fn set_linger(&self, value: i32) -> Result<()>;

    /// Accessor for the `ZMQ_SNDHWM` option.
    fn get_sndhwm(&self) -> Result<i32>;
    /// Setter for the `ZMQ_SNDHWM` option.
    fn set_sndhwm(&self, value: i32) -> Result<()>;

    /// Accessor for the `ZMQ_RCVHWM` option.
    fn get_rcvhwm(&self) -> Result<i32>;
    /// Setter for the `ZMQ_RCVHWM` option.
    fn set_rcvhwm(&self, value: i32) -> Result<()>;

    /// Accessor for the `ZMQ_PROBE_ROUTER` option.
    fn is_probe_router(&self) -> Result<bool>;
    /// Setter for the `ZMQ_PROBE_ROUTER` option.
    fn set_probe_router(&self, value: bool) -> Result<()>;

    /// Accessor for the `ZMQ_IPV6` option.
    fn is_ipv6(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_IMMEDIATE` option.
    fn is_immediate(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_PLAIN_SERVER` option.
    fn is_plain_server(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_CONFLATE` option.
    fn is_conflate(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_CURVE_SERVER` option.
    fn is_curve_server(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_GSSAPI_SERVER` option.
    fn is_gssapi_server(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_GSSAPI_PLAINTEXT` option.
    fn is_gssapi_plaintext(&self) -> Result<bool>;

    /// Accessor for the `ZMQ_MAXMSGSIZE` option.
    fn get_maxmsgsize(&self) -> Result<i64>;

    /// Accessor for the `ZMQ_AFFINITY` option.
    fn get_affinity(&self) -> Result<u64>;

    /// Accessor for the `ZMQ_RATE` option.
    fn get_rate(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_RECOVERY_IVL` option.
    fn get_recovery_ivl(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_SNDBUF` option.
    fn get_sndbuf(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_RCVBUF` option.
    fn get_rcvbuf(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_TOS` option.
    fn get_tos(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_RECONNECT_IVL` option.
    fn get_reconnect_ivl(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_RECONNECT_IVL_MAX` option.
    fn get_reconnect_ivl_max(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_BACKLOG` option.
    fn get_backlog(&self) -> Result<i32>;

    /// Accessor for the `ZMQ_IDENTITY` option.
    fn get_identity(&self) -> Result<Vec<u8>>;
}

macro_rules! getter {
    ($name: ident, $retval: ty) => {
        #[inline]
        fn $name(&self) -> $crate::Result<$retval> {
            self.get_socket().$name().map_err(|e| e.into())
        }
    }
}
macro_rules! setter {
    ($name: ident, $type: ty) => {
        #[inline]
        fn $name(&self, value: $type) -> $crate::Result<()> {
            self.get_socket().$name(value).map_err(|e| e.into())
        }
    }
}

impl<T: AsZmqSocket> SocketExt for T {
    getter!(is_ipv6, bool);

    getter!(is_immediate, bool);

    getter!(is_plain_server, bool);

    getter!(is_conflate, bool);

    getter!(is_probe_router, bool);
    setter!(set_probe_router, bool);

    getter!(is_curve_server, bool);

    getter!(is_gssapi_server, bool);

    getter!(is_gssapi_plaintext, bool);

    getter!(get_maxmsgsize, i64);

    getter!(get_sndhwm, i32);
    setter!(set_sndhwm, i32);

    getter!(get_rcvhwm, i32);
    setter!(set_rcvhwm, i32);

    getter!(get_affinity, u64);

    getter!(get_rate, i32);

    getter!(get_recovery_ivl, i32);

    getter!(get_sndbuf, i32);

    getter!(get_rcvbuf, i32);

    getter!(get_tos, i32);

    getter!(get_linger, i32);
    setter!(set_linger, i32);

    getter!(get_reconnect_ivl, i32);

    getter!(get_reconnect_ivl_max, i32);

    getter!(get_backlog, i32);

    getter!(get_identity, Vec<u8>);
}