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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
extern crate libc;

use std::mem::transmute;
use std::ffi;

use libc::{ c_int, c_void, size_t, c_short, c_long, c_ushort };
use super::{ Error, Message, SocketType };
use ::std;
use ::zmq_ffi;

fn bytes_to_string(bytes: Vec<u8>) -> String {
    unsafe { ffi::CStr::from_ptr(transmute(bytes.as_ptr())).to_str().unwrap().to_string() }
}

fn str_to_cstr_bytes(s: &str) -> Vec<u8> {
    let cstr = ffi::CString::new(s).unwrap();
    //cstr.into_bytes_with_nul()            // currently unstable
    cstr.as_bytes_with_nul().to_vec()
}

macro_rules! getsockopt_template(
    // function name to declare, option name, query/return type
    ($name: ident, $opt: expr, $t: ty) => {
        pub fn $name(&self) -> Result<$t, Error> {
            let mut optval: $t = std::default::Default::default();
            let mut optval_len: size_t = std::mem::size_of::<$t>() as size_t;
            let optval_ptr = &mut optval as *mut $t;

            let rc = unsafe { zmq_ffi::zmq_getsockopt(self.socket, $opt as c_int, transmute(optval_ptr), &mut optval_len) };
            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok(optval)
            }
        }
    };
    // function name to declare, option name, query type, query count, map queried value to return value, return type
    ($name: ident, $opt: expr, $t: ty, $n: expr, $rmap: expr, $r: ty) => {
        pub fn $name(&self) -> Result<$r, Error> {
            let mut optval: Vec<$t> = Vec::with_capacity($n);

            let mut optval_len: size_t = (optval.capacity() * std::mem::size_of::<$t>()) as size_t;
            let optval_ptr = optval.as_mut_ptr();

            let rc = unsafe {
                zmq_ffi::zmq_getsockopt(self.socket, $opt as c_int,
                    transmute(optval_ptr), &mut optval_len)
            };

            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                unsafe { optval.set_len(optval_len); }
                Ok($rmap(optval))
            }
        }
    };
    // function name to declare, option name, query type, map queried value to return value, return type
    ($name: ident, $opt: expr, $t: ty, $rmap: expr, $r: ty) => {
        pub fn $name(&self) -> Result<$r, Error> {
            let mut optval: $t = std::default::Default::default();
            let mut optval_len: size_t = std::mem::size_of::<$t>() as size_t;
            let optval_ptr = &mut optval as *mut $t;

            let rc = unsafe { zmq_ffi::zmq_getsockopt(self.socket, $opt as c_int, transmute(optval_ptr), &mut optval_len) };
            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok($rmap(optval))
            }
        }
    };
    // function name to declare, option name, query type, query count
    ($name: ident, $opt: expr, $t: ty, $n: expr) => {
        pub fn $name(&self) -> Result<Vec<$t>, Error> {
            let mut optval: Vec<$t> = Vec::with_capacity($n);

            let mut optval_len: size_t = (optval.capacity() * std::mem::size_of::<$t>()) as size_t;
            let optval_ptr = optval.as_mut_ptr();

            let rc = unsafe {
                zmq_ffi::zmq_getsockopt(self.socket, $opt as c_int,
                    transmute(optval_ptr), &mut optval_len)
            };

            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                unsafe { optval.set_len(optval_len); }
                Ok(optval)
            }
        }
    };
);

macro_rules! setsockopt_nullptr_template(
    ($name: ident, $opt: expr) => {
        pub fn $name(&self) -> Result<(), Error> {
            let optval_len: size_t = 0;
            let optval_ptr: *const u8 = std::ptr::null();

            let rc = unsafe {
                zmq_ffi::zmq_setsockopt(self.socket, $opt as c_int,
                    transmute(optval_ptr), optval_len)
            };

            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok(())
            }
        }
    };
);

macro_rules! setsockopt_template(
    // function name to declare, option name, optval type
    ($name: ident, $opt: expr, $t: ty) => {
        pub fn $name(&self, optval: &$t) -> Result<(), Error> {
            let optval_len: size_t = std::mem::size_of::<$t>() as size_t;
            let optval_ptr = optval as *const $t;

            let rc = unsafe { zmq_ffi::zmq_setsockopt(self.socket, $opt as c_int, transmute(optval_ptr), optval_len) };
            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok(())
            }
        }
    };

    // function name to declare, option name
    ($name: ident, $opt: expr) => {
        pub fn $name(&self, optval: &[u8]) -> Result<(), Error> {
            let optval_len: size_t = optval.len() as size_t;
            let optval_ptr: *const u8 = optval.as_ptr();

            let rc = unsafe {
                zmq_ffi::zmq_setsockopt(self.socket, $opt as c_int,
                    transmute(optval_ptr), optval_len)
            };

            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok(())
            }
        }
    };

    ($name: ident, $opt: expr, $t: ty, $map: expr) => {
        pub fn $name(&self, optval: $t) -> Result<(), Error> {
            let optval: Vec<u8> = $map(optval);
            let optval_len: size_t = optval.len() as size_t;

            let optval_ptr: *const u8 = optval.as_ptr();

            let rc = unsafe {
                zmq_ffi::zmq_setsockopt(self.socket, $opt as c_int,
                    transmute(optval_ptr), optval_len)
            };

            if rc == -1 {
                Err(Error::from_last_err())
            } else {
                Ok(())
            }
        }
    };
);

#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
enum SocketOption {
    AFFINITY = 4,
    IDENTITY = 5,
    SUBSCRIBE = 6,
    UNSUBSCRIBE = 7,
    RATE = 8,
    RECOVERY_IVL = 9,
    SNDBUF = 11,
    RCVBUF = 12,
    RCVMORE = 13,
    FD = 14,
    EVENTS = 15,
    TYPE = 16,
    LINGER = 17,
    RECONNECT_IVL = 18,
    BACKLOG = 19,
    RECONNECT_IVL_MAX = 21,
    MAXMSGSIZE = 22,
    SNDHWM = 23,
    RCVHWM = 24,
    MULTICAST_HOPS = 25,
    RCVTIMEO = 27,
    SNDTIMEO = 28,
    LAST_ENDPOINT = 32,
    ROUTER_MANDATORY = 33,
    TCP_KEEPALIVE = 34,
    TCP_KEEPALIVE_CNT = 35,
    TCP_KEEPALIVE_IDLE = 36,
    TCP_KEEPALIVE_INTVL = 37,
    IMMEDIATE = 39,
    XPUB_VERBOSE = 40,
    ROUTER_RAW = 41,
    IPV6 = 42,
    MECHANISM = 43,
    PLAIN_SERVER = 44,
    PLAIN_USERNAME = 45,
    PLAIN_PASSWORD = 46,
    CURVE_SERVER = 47,
    CURVE_PUBLICKEY = 48,
    CURVE_SECRETKEY = 49,
    CURVE_SERVERKEY = 50,
    PROBE_ROUTER = 51,
    REQ_CORRELATE = 52,
    REQ_RELAXED = 53,
    CONFLATE = 54,
    ZAP_DOMAIN = 55,
    ROUTER_HANDOVER = 56,
    TOS = 57,
    CONNECT_RID = 61,
    GSSAPI_SERVER = 62,
    GSSAPI_PRINCIPAL = 63,
    GSSAPI_SERVICE_PRINCIPAL = 64,
    GSSAPI_PLAINTEXT = 65,
    HANDSHAKE_IVL = 66,
    SOCKS_PROXY = 68,
    XPUB_NODROP = 69,
}

pub type SocketEvent = c_ushort;

pub const CONNECTED: SocketEvent = 0x0001;
pub const CONNECT_DELAYED: SocketEvent = 0x0002;
pub const CONNECT_RETRIED: SocketEvent = 0x0004;
pub const LISTENING: SocketEvent = 0x0008;
pub const BIND_FAILED: SocketEvent = 0x0010;
pub const ACCEPTED: SocketEvent = 0x0020;
pub const ACCEPT_FAILED: SocketEvent = 0x0040;
pub const CLOSED: SocketEvent = 0x0080;
pub const CLOSE_FAILED: SocketEvent = 0x0100;
pub const DISCONNECTED: SocketEvent = 0x0200;
pub const MONITOR_STOPPED: SocketEvent = 0x0400;
pub const ALL: SocketEvent = 0xFFFF;

pub type SocketFlag = c_int;

pub const DONTWAIT: SocketFlag = 1;
pub const SNDMORE: SocketFlag = 2;

pub struct Socket {
    socket: *mut c_void,
    closed: bool,
}

unsafe impl Send for Socket {}

impl Socket {
    pub fn from_raw(socket: *mut c_void) -> Socket {
        Socket { socket: socket, closed: false }
    }

    /// Close 0MQ socket
    ///
    /// Binding of `int zmq_close (void *s);`
    ///
    /// It's not mandatory to call this function since socket can be closed automatically on dropping
    /// The function will destroy this socket.
    /// Any outstanding messages physically received from the network
    /// but not yet received by the application with recv() shall be discarded.
    /// The behaviour for discarding messages sent by the application with send()
    /// but not yet physically transferred to the network depends on the value of
    /// the ZMQ_LINGER socket option for the specified socket.
    pub fn close(&mut self) -> Result<(), Error> {
        self.closed = true;
        self.close_underly()
    }

    fn close_underly(&mut self) -> Result<(), Error> {
        loop {
            let rc = unsafe { zmq_ffi::zmq_close(self.socket) };
            if rc != 0 {
                let e = Error::from_last_err();
                if e.get_errno() == ::errno::EINTR {
                    continue;
                } else {
                    return Err(e);
                }

            } else {
                break;
            }
        }
        Ok(())
    }

    ///  Accept incoming connections on a socket
    ///
    /// Binding of `int zmq_bind (void *socket, const char *endpoint);`
    ///
    /// The function binds the socket to a local endpoint and then accepts incoming connections on that endpoint.
    pub fn bind(&mut self, endpoint: &str) -> Result<(), Error> {
        let endpoint_cstr = ffi::CString::new(endpoint).unwrap();
        let rc = unsafe { zmq_ffi::zmq_bind(self.socket, endpoint_cstr.as_ptr()) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Create outgoing connection from socket
    ///
    /// Binding of `int zmq_connect (void *socket, const char *endpoint);`
    ///
    /// The function connects the socket to an endpoint and then accepts incoming connections on that endpoint.
    pub fn connect(&mut self, endpoint: &str) -> Result<(), Error> {
        let endpoint_cstr = ffi::CString::new(endpoint).unwrap();
        let rc = unsafe { zmq_ffi::zmq_connect(self.socket, endpoint_cstr.as_ptr()) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Stop accepting connections on a socket
    ///
    /// Binding of `int zmq_unbind (void *socket, const char *endpoint);`
    ///
    /// The function will unbind a socket specified by the socket argument from the endpoint specified by the endpoint argument.
    pub fn unbind(&mut self, endpoint: &str) -> Result<(), Error> {
        let endpoint_cstr = ffi::CString::new(endpoint).unwrap();
        let rc = unsafe { zmq_ffi::zmq_unbind(self.socket, endpoint_cstr.as_ptr()) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Disconnect a socket
    ///
    /// Binding of `int zmq_disconnect (void *socket, const char *endpoint);`
    ///
    /// The function will disconnect socket from the endpoint specified by the endpoint argument.
    /// Any outstanding messages physically received from the network but not yet received by the application with recv() will be discarded.
    /// The behaviour for discarding messages sent by the application with send() but
    /// not yet physically transferred to the network depends on the value of the ZMQ_LINGER socket option for the socket.
    pub fn disconnect(&mut self, endpoint: &str) -> Result<(), Error> {
        let endpoint_cstr = ffi::CString::new(endpoint).unwrap();
        let rc = unsafe { zmq_ffi::zmq_disconnect(self.socket, endpoint_cstr.as_ptr()) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Send a message part on a socket
    ///
    /// Binding of `int zmq_msg_send (zmq_msg_t *msg, void *socket, int flags);`
    pub fn send_msg(&mut self, mut msg: Message, flags: SocketFlag) -> Result<i32, Error> {
        let rc = unsafe { zmq_ffi::zmq_msg_send(&mut msg.msg, self.socket, flags) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(rc)
        }
    }

    /// Receive a message part from a socket
    ///
    /// Binding of `int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);`
    pub fn recv_into_msg(&mut self, msg: &mut Message, flags: SocketFlag) -> Result<i32, Error> {
        let rc = unsafe { zmq_ffi::zmq_msg_recv(&mut msg.msg, self.socket, flags) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(rc)
        }
    }

    /// Receive a message part from a socket
    ///
    /// Binding of `int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);`
    pub fn recv_msg(&mut self, flags: SocketFlag) -> Result<Message, Error> {
        let mut msg = try!(Message::new());
        match self.recv_into_msg(&mut msg, flags) {
            Ok(_) => Ok(msg),
            Err(e) => Err(e),
        }
    }

    /// Send bytes on a socket
    ///
    /// Data will be copied into a Message object in order to be sent.
    pub fn send_bytes(&mut self, data: &[u8], flags: SocketFlag) -> Result<i32, Error> {
        let msg = try!(Message::from_slice(data));
        self.send_msg(msg, flags)
    }

    /// Send a constant-memory message part on a socket
    ///
    /// Binding of `ZMQ_EXPORT int zmq_send_const (void *s, const void *buf, size_t len, int flags);`
    ///
    /// The message buffer is assumed to be constant-memory(static) and will therefore not be copied or deallocated in any way
    pub fn send_const_bytes(&mut self, data: &'static [u8], flags: SocketFlag) -> Result<i32, Error> {
        let rc = unsafe { zmq_ffi::zmq_send_const(self.socket, transmute(data.as_ptr()), data.len(), flags) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(rc)
        }
    }

    /// Send a UTF-8 string on socket
    pub fn send_str(&mut self, data: &str, flags: SocketFlag) -> Result<i32, Error> {
        self.send_bytes(data.as_bytes(), flags)
    }

    /// Receive bytes from a socket
    pub fn recv_bytes(&mut self, flags: SocketFlag) -> Result<Vec<u8>, Error> {
        match self.recv_msg(flags) {
            Ok(msg) => Ok(msg.to_vec()),
            Err(e) => Err(e),
        }
    }

    /// Receive bytes into a mutable slice
    /// # Caution
    /// *Any bytes exceeding the length of buffer will be truncated.*
    pub fn recv_bytes_into_slice(&mut self, buffer: &mut [u8], flags: SocketFlag) -> Result<i32, Error> {
        let rc = unsafe { zmq_ffi::zmq_recv(self.socket, transmute(buffer.as_mut_ptr()), buffer.len(), flags) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(rc)
        }
    }

    /// Receive a UTF-8 string from socket
    pub fn recv_string(&mut self, flags: SocketFlag) -> Result<Result<String, Vec<u8>>, Error> {
        match self.recv_bytes(flags) {
            Ok(msg) => {
                Ok({
                    let s = String::from_utf8(msg);
                    if s.is_ok() {
                        Ok(s.unwrap())
                    } else {
                        Err(s.unwrap_err().into_bytes())
                    }
                })
            }
            Err(e) => Err(e),
        }
    }

    /// Monitor socket events
    ///
    /// Binding of `int zmq_socket_monitor (void *socket, char *endpoint, int events);`
    ///
    /// The method lets an application thread track socket events (like connects) on a ZeroMQ socket
    pub fn socket_monitor(&mut self, endpoint: &str, events: &Vec<SocketEvent>) -> Result<(), Error> {
        let mut event_mask: i32 = 0;
        for event in events {
            event_mask |= Clone::clone(event) as i32;
        }

        let endpoint_cstr = ffi::CString::new(endpoint).unwrap();
        let rc = unsafe { zmq_ffi::zmq_socket_monitor(self.socket, endpoint_cstr.as_ptr(), event_mask) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Start built-in 0MQ proxy
    ///
    /// Binding of `int zmq_proxy (const void *frontend, const void *backend, const void *capture);`
    ///
    /// The function starts the built-in ØMQ proxy in the current application thread.
    pub fn run_proxy(frontend: &mut Socket, backend: &mut Socket) -> Result<(), Error> {
        let rc = unsafe { zmq_ffi::zmq_proxy(frontend.socket, backend.socket, std::ptr::null_mut()) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Start built-in 0MQ proxy
    ///
    /// Binding of `int zmq_proxy (const void *frontend, const void *backend, const void *capture);` or
    /// `int zmq_proxy_steerable (const void *frontend, const void *backend, const void *capture, const void *control);`
    ///
    /// The function starts the built-in ØMQ proxy in the current application thread.
    /// The proxy will send all messages, received on both frontend and backend, to the capture socket.
    /// The capture socket should be a ZMQ_PUB, ZMQ_DEALER, ZMQ_PUSH, or ZMQ_PAIR socket.
    /// If the control socket is not None, the proxy supports control flow.
    /// If PAUSE is received on this socket, the proxy suspends its activities.
    /// If RESUME is received, it goes on. If TERMINATE is received, it terminates smoothly.
    /// At start, the proxy runs normally as if run_proxy was used.
    pub fn run_proxy_ex(frontend: &mut Socket, backend: &mut Socket,
    capture: Option<&mut Socket>, control: Option<&mut Socket>) -> Result<(), Error> {
        let capture_ptr = if capture.is_none() { std::ptr::null_mut() } else { capture.unwrap().socket };

        let rc = {
            if control.is_none() {
                unsafe { zmq_ffi::zmq_proxy(frontend.socket, backend.socket, capture_ptr) }
            } else {
                unsafe { zmq_ffi::zmq_proxy_steerable(frontend.socket, backend.socket, capture_ptr, control.unwrap().socket) }
            }
        };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(())
        }
    }

    /// Create a poll item from current socket
    ///
    /// # Safty
    /// There is no lifetime guarantee that poll item does not live out socket
    pub fn as_poll_item(&self) -> PollItem {
        PollItem::from_socket(&self)
    }

    ///  input/output multiplexing
    ///
    /// Binding of `int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout);`
    pub fn poll(items: &mut [PollItem], nitems: i32, timeout: i32) -> Result<i32, Error> {
        let rc = unsafe { zmq_ffi::zmq_poll(transmute(items.as_mut_ptr()), nitems as c_int, timeout as c_long) };
        if rc == -1 {
            Err(Error::from_last_err())
        } else {
            Ok(rc)
        }
    }

    //-------------------------------- get options ----------------------------------- //

    getsockopt_template!(get_affinity, SocketOption::AFFINITY, u64);
    getsockopt_template!(get_backlog, SocketOption::BACKLOG, i32);
    getsockopt_template!(get_curve_publickey, SocketOption::CURVE_PUBLICKEY, u8, 32);
    getsockopt_template!(get_curve_printable_publickey, SocketOption::CURVE_PUBLICKEY, u8, 41,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_curve_secretkey, SocketOption::CURVE_SECRETKEY, u8, 32);
    getsockopt_template!(get_curve_printable_secretkey, SocketOption::CURVE_SECRETKEY, u8, 41,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_curve_serverkey, SocketOption::CURVE_SERVERKEY, u8, 32);
    getsockopt_template!(get_curve_printable_serverkey, SocketOption::CURVE_SERVERKEY, u8, 41,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_events, SocketOption::EVENTS, PollEvent);
    getsockopt_template!(get_fd, SocketOption::FD, SocketFd);
    getsockopt_template!(is_gssapi_plaintext, SocketOption::GSSAPI_PLAINTEXT, i32, |r| { r > 0 }, bool);
    getsockopt_template!(get_gssapi_principal, SocketOption::GSSAPI_PRINCIPAL, u8, 256,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(is_gssapi_server, SocketOption::GSSAPI_SERVER, i32, |r| { r > 0 }, bool);
    getsockopt_template!(get_gssapi_service_principal, SocketOption::GSSAPI_SERVICE_PRINCIPAL, u8, 256,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_handshake_ivl, SocketOption::HANDSHAKE_IVL, i32);
    getsockopt_template!(get_identity, SocketOption::IDENTITY, u8, 256);
    getsockopt_template!(is_immediate, SocketOption::IMMEDIATE, i32, |r| { r > 0 }, bool);
    //getsockopt_template!(get_ipv4only, SocketOption::IPV4ONLY, i32);      // deprecated
    getsockopt_template!(is_ipv6_enabled, SocketOption::IPV6, i32, |r| { r > 0 }, bool);
    /// Get last endpoint bound for TCP and IPC transports
    /// if last endpoint has more than 2048 bytes, method call will be failed.
    getsockopt_template!(get_last_endpoint, SocketOption::LAST_ENDPOINT, u8, 2048,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_linger, SocketOption::LINGER, i32);
    getsockopt_template!(get_max_msg_size, SocketOption::MAXMSGSIZE, i64);
    getsockopt_template!(get_mechanism, SocketOption::MECHANISM, i32);
    getsockopt_template!(get_multicast_hops, SocketOption::MULTICAST_HOPS, i32);
    getsockopt_template!(get_plain_password, SocketOption::PLAIN_PASSWORD, u8, 256,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(is_plain_server, SocketOption::PLAIN_SERVER, i32, |r| { r > 0 }, bool);
    getsockopt_template!(get_plain_username, SocketOption::PLAIN_USERNAME, u8, 256,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);
    getsockopt_template!(get_rate, SocketOption::RATE, i32);
    getsockopt_template!(get_rcvbuf, SocketOption::RCVBUF, i32);
    getsockopt_template!(get_rcvhwm, SocketOption::RCVHWM, i32);
    getsockopt_template!(can_rcvmore, SocketOption::RCVMORE, i32, |r| { r > 0 }, bool);
    getsockopt_template!(get_rcvtimeo, SocketOption::RCVTIMEO, i32);
    getsockopt_template!(get_reconnect_ivl, SocketOption::RECONNECT_IVL, i32);
    getsockopt_template!(get_reconnect_ivl_max, SocketOption::RECONNECT_IVL_MAX, i32);
    getsockopt_template!(get_recovery_ivl, SocketOption::RECOVERY_IVL, i32);
    getsockopt_template!(get_sndbuf, SocketOption::SNDBUF, i32);
    getsockopt_template!(get_sndhwm, SocketOption::SNDHWM, i32);
    getsockopt_template!(get_sndtimeo, SocketOption::SNDTIMEO, i32);
    getsockopt_template!(get_tcp_keepalive, SocketOption::TCP_KEEPALIVE, i32);
    getsockopt_template!(get_tcp_keepalive_cnt, SocketOption::TCP_KEEPALIVE_CNT, i32);
    getsockopt_template!(get_tcp_keepalive_idle, SocketOption::TCP_KEEPALIVE_IDLE, i32);
    getsockopt_template!(get_tcp_keepalive_intvl, SocketOption::TCP_KEEPALIVE_INTVL, i32);
    getsockopt_template!(get_tos, SocketOption::TOS, i32);
    getsockopt_template!(get_type, SocketOption::TYPE, SocketType);
    getsockopt_template!(get_zap_domain, SocketOption::ZAP_DOMAIN, u8, 256,
        |r: Vec<u8>| {
            bytes_to_string(r)
        }, String);

    //-------------------------------- set options ----------------------------------- //
    setsockopt_template!(set_affinity, SocketOption::AFFINITY, u64);
    setsockopt_template!(set_backlog, SocketOption::BACKLOG, i32);
    setsockopt_template!(set_connect_rid, SocketOption::CONNECT_RID);
    setsockopt_template!(set_conflate, SocketOption::CONFLATE, i32);
    setsockopt_template!(set_curve_publickey, SocketOption::CURVE_PUBLICKEY);
    setsockopt_template!(set_curve_plaintext_publickey, SocketOption::CURVE_PUBLICKEY, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_template!(set_curve_secretkey, SocketOption::CURVE_SECRETKEY);
    setsockopt_template!(set_curve_plaintext_secretkey, SocketOption::CURVE_SECRETKEY, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_template!(set_curve_server, SocketOption::CURVE_SERVER, i32);
    setsockopt_template!(set_curve_serverkey, SocketOption::CURVE_SERVERKEY);
    setsockopt_template!(set_curve_plaintext_serverkey, SocketOption::CURVE_SERVERKEY, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_template!(set_gssapi_plaintext, SocketOption::GSSAPI_PLAINTEXT, i32);
    setsockopt_template!(set_gssapi_principal, SocketOption::GSSAPI_PRINCIPAL, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_template!(set_gssapi_server, SocketOption::GSSAPI_SERVER, i32);
    setsockopt_template!(set_gssapi_service_principal, SocketOption::GSSAPI_SERVICE_PRINCIPAL, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_template!(set_handshake_ivl, SocketOption::HANDSHAKE_IVL, i32);
    setsockopt_template!(set_identity, SocketOption::IDENTITY);
    setsockopt_template!(set_immediate, SocketOption::IMMEDIATE, i32);
    setsockopt_template!(set_ipv6, SocketOption::IPV6, i32);
    setsockopt_template!(set_linger, SocketOption::LINGER, i32);
    setsockopt_template!(set_max_msg_size, SocketOption::MAXMSGSIZE, i64);
    setsockopt_template!(set_multicast_hops, SocketOption::MULTICAST_HOPS, i32);
    setsockopt_template!(set_plain_password, SocketOption::PLAIN_PASSWORD, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_nullptr_template!(set_plain_password_empty, SocketOption::PLAIN_PASSWORD);
    setsockopt_template!(set_plain_server, SocketOption::PLAIN_SERVER, i32);
    setsockopt_template!(set_plain_username, SocketOption::PLAIN_USERNAME, &str,
        |s| { str_to_cstr_bytes(s) });
    setsockopt_nullptr_template!(set_plain_username_empty, SocketOption::PLAIN_USERNAME);
    setsockopt_template!(set_probe_router, SocketOption::PROBE_ROUTER, i32);
    setsockopt_template!(set_rate, SocketOption::RATE, i32);
    setsockopt_template!(set_rcvbuf, SocketOption::RCVBUF, i32);
    setsockopt_template!(set_rcvhwm, SocketOption::RCVHWM, i32);
    setsockopt_template!(set_rcvtimeo, SocketOption::RCVTIMEO, i32);
    setsockopt_template!(set_reconnect_ivl, SocketOption::RECONNECT_IVL, i32);
    setsockopt_template!(set_reconnect_ivl_max, SocketOption::RECONNECT_IVL_MAX, i32);
    setsockopt_template!(set_recovery_ivl, SocketOption::RECOVERY_IVL, i32);
    setsockopt_template!(set_req_correlate, SocketOption::REQ_CORRELATE, i32);
    setsockopt_template!(set_req_relaxed, SocketOption::REQ_RELAXED, i32);
    setsockopt_template!(set_router_handover, SocketOption::ROUTER_HANDOVER, i32);
    setsockopt_template!(set_router_mandatory, SocketOption::ROUTER_MANDATORY, i32);
    setsockopt_template!(set_router_raw, SocketOption::ROUTER_RAW, i32);
    setsockopt_template!(set_sndbuf, SocketOption::SNDBUF, i32);
    setsockopt_template!(set_sndhwm, SocketOption::SNDHWM, i32);
    setsockopt_template!(set_sndtimeo, SocketOption::SNDTIMEO, i32);
    setsockopt_template!(set_subscribe, SocketOption::SUBSCRIBE);
    setsockopt_template!(set_tcp_keepalive, SocketOption::TCP_KEEPALIVE, i32);
    setsockopt_template!(set_tcp_keepalive_cnt, SocketOption::TCP_KEEPALIVE_CNT, i32);
    setsockopt_template!(set_tcp_keepalive_idle, SocketOption::TCP_KEEPALIVE_IDLE, i32);
    setsockopt_template!(set_tcp_keepalive_intvl, SocketOption::TCP_KEEPALIVE_INTVL, i32);
    setsockopt_template!(set_tos, SocketOption::TOS, i32);
    setsockopt_template!(set_unsubscribe, SocketOption::UNSUBSCRIBE);
    setsockopt_template!(set_xpub_verbose, SocketOption::XPUB_VERBOSE, i32);
    setsockopt_template!(set_zqp_domain, SocketOption::ZAP_DOMAIN, &str,
        |s| { str_to_cstr_bytes(s) });
}

impl Drop for Socket {
    fn drop(&mut self) {
        if !self.closed {
            self.close_underly().unwrap();
        }
    }
}

pub type PollEvent = c_int;

pub const POLLIN: PollEvent = 1;
pub const POLLOUT: PollEvent = 2;
pub const POLLERR: PollEvent = 4;

#[cfg(target_os = "windows")]
pub type SocketFd = ::libc::intptr_t;
#[cfg(not(target_os = "windows"))]
pub type SocketFd = c_int;

#[repr(C)]
pub struct PollItem {
    pub socket: *mut c_void,
    pub fd: SocketFd,
    pub events: c_short,
    pub revents: c_short,
}

impl PollItem {
    pub fn from_socket(socket: &Socket) -> PollItem {
        PollItem {
            socket: socket.socket,
            fd: 0,
            events: 0,
            revents: 0,
        }
    }

    pub fn from_fd(fd: SocketFd) -> PollItem {
        PollItem {
            socket: std::ptr::null_mut(),
            fd: fd,
            events: 0,
            revents: 0,
        }
    }

    pub fn set_socket(&mut self, socket: &Socket) -> &mut PollItem {
        self.socket = socket.socket;
        self.fd = 0;
        self
    }

    pub fn set_fd(&mut self, fd: SocketFd) -> &mut PollItem {
        self.socket = std::ptr::null_mut();
        self.fd = fd;
        self
    }

    pub fn clear_events(&mut self) -> &mut PollItem {
        self.events = 0;
        self
    }

    pub fn reg_event(&mut self, ev: PollEvent) -> &mut PollItem {
        self.events |= ev as c_short;
        self
    }

    pub fn unreg_event(&mut self, ev: PollEvent) -> &mut PollItem {
        self.events &= !(ev as c_short);
        self
    }

    /// Clear all returned events
    pub fn clear_revents(&mut self) -> &mut PollItem {
        self.revents = 0;
        self
    }

    /// Does this PollItem have the specified PollEvent returned.
    pub fn has_revent(&self, ev: PollEvent) -> bool {
        (self.revents & (ev as c_short)) > 0
    }
}