1package zmq4
2
3/*
4#include <zmq.h>
5*/
6import "C"
7
8import (
9	"syscall"
10)
11
12// An Errno is an unsigned number describing an error condition as returned by a call to ZeroMQ.
13// It implements the error interface.
14// The number is either a standard system error, or an error defined by the C library of ZeroMQ.
15type Errno uintptr
16
17const (
18	// Error conditions defined by the C library of ZeroMQ.
19
20	// On Windows platform some of the standard POSIX errnos are not defined.
21	EADDRINUSE      = Errno(C.EADDRINUSE)
22	EADDRNOTAVAIL   = Errno(C.EADDRNOTAVAIL)
23	EAFNOSUPPORT    = Errno(C.EAFNOSUPPORT)
24	ECONNABORTED    = Errno(C.ECONNABORTED)
25	ECONNREFUSED    = Errno(C.ECONNREFUSED)
26	ECONNRESET      = Errno(C.ECONNRESET)
27	EHOSTUNREACH    = Errno(C.EHOSTUNREACH)
28	EINPROGRESS     = Errno(C.EINPROGRESS)
29	EMSGSIZE        = Errno(C.EMSGSIZE)
30	ENETDOWN        = Errno(C.ENETDOWN)
31	ENETRESET       = Errno(C.ENETRESET)
32	ENETUNREACH     = Errno(C.ENETUNREACH)
33	ENOBUFS         = Errno(C.ENOBUFS)
34	ENOTCONN        = Errno(C.ENOTCONN)
35	ENOTSOCK        = Errno(C.ENOTSOCK)
36	ENOTSUP         = Errno(C.ENOTSUP)
37	EPROTONOSUPPORT = Errno(C.EPROTONOSUPPORT)
38	ETIMEDOUT       = Errno(C.ETIMEDOUT)
39
40	// Native 0MQ error codes.
41	EFSM           = Errno(C.EFSM)
42	EMTHREAD       = Errno(C.EMTHREAD)
43	ENOCOMPATPROTO = Errno(C.ENOCOMPATPROTO)
44	ETERM          = Errno(C.ETERM)
45)
46
47func errget(err error) error {
48	eno, ok := err.(syscall.Errno)
49	if ok {
50		return Errno(eno)
51	}
52	return err
53}
54
55// Return Errno as string.
56func (errno Errno) Error() string {
57	if errno >= C.ZMQ_HAUSNUMERO {
58		return C.GoString(C.zmq_strerror(C.int(errno)))
59	}
60	return syscall.Errno(errno).Error()
61}
62
63/*
64Convert error to Errno.
65
66Example usage:
67
68    switch AsErrno(err) {
69
70    case zmq.Errno(syscall.EINTR):
71        // standard system error
72
73        // call was interrupted
74
75    case zmq.ETERM:
76        // error defined by ZeroMQ
77
78        // context was terminated
79
80    }
81
82See also: examples/interrupt.go
83*/
84func AsErrno(err error) Errno {
85	if eno, ok := err.(Errno); ok {
86		return eno
87	}
88	if eno, ok := err.(syscall.Errno); ok {
89		return Errno(eno)
90	}
91	return Errno(0)
92}
93
94func (ctx *Context) retry(err error) bool {
95	if !ctx.retryEINTR || err == nil {
96		return false
97	}
98	eno, ok := err.(syscall.Errno)
99	if !ok {
100		return false
101	}
102	return eno == syscall.EINTR
103}
104