23 #ifndef NN_HPP_INCLUDED
24 #define NN_HPP_INCLUDED
26 #include <nanomsg/nn.h>
34 #define nn_slow(x) __builtin_expect ((x), 0)
36 #define nn_slow(x) (x)
42 class exception :
public std::exception
46 exception () : err (nn_errno ()) {}
48 virtual const char *what ()
const throw ()
50 return nn_strerror (err);
63 inline const char *symbol (
int i,
int *value)
65 return nn_symbol (i, value);
68 inline void *allocmsg (
size_t size,
int type)
70 void *msg = nn_allocmsg (size, type);
72 throw nn::exception ();
76 inline int freemsg (
void *msg)
78 int rc = nn_freemsg (msg);
79 if (nn_slow (rc != 0))
80 throw nn::exception ();
88 inline socket (
int domain,
int protocol)
90 s = nn_socket (domain, protocol);
92 throw nn::exception ();
97 int rc = nn_close (s);
102 inline void setsockopt (
int level,
int option,
const void *optval,
105 int rc = nn_setsockopt (s, level, option, optval, optvallen);
106 if (nn_slow (rc != 0))
107 throw nn::exception ();
110 inline void getsockopt (
int level,
int option,
void *optval,
113 int rc = nn_getsockopt (s, level, option, optval, optvallen);
114 if (nn_slow (rc != 0))
115 throw nn::exception ();
118 inline int bind (
const char *addr)
120 int rc = nn_bind (s, addr);
121 if (nn_slow (rc < 0))
122 throw nn::exception ();
126 inline int connect (
const char *addr)
128 int rc = nn_connect (s, addr);
129 if (nn_slow (rc < 0))
130 throw nn::exception ();
134 inline void shutdown (
int how)
136 int rc = nn_shutdown (s, how);
137 if (nn_slow (rc != 0))
138 throw nn::exception ();
141 inline int send (
const void *buf,
size_t len,
int flags)
const
143 int rc = nn_send (s, buf, len, flags);
144 if (nn_slow (rc < 0)) {
145 if (nn_slow (nn_errno () != EAGAIN && nn_errno () != ETIMEDOUT))
146 throw nn::exception ();
152 inline int recv (
void *buf,
size_t len,
int flags)
154 int rc = nn_recv (s, buf, len, flags);
155 if (nn_slow (rc < 0)) {
156 if (nn_slow (nn_errno () != EAGAIN && nn_errno () != ETIMEDOUT))
157 throw nn::exception ();
163 inline int sendmsg (
const struct nn_msghdr *msghdr,
int flags)
const
165 int rc = nn_sendmsg (s, msghdr, flags);
166 if (nn_slow (rc < 0)) {
167 if (nn_slow (nn_errno () != EAGAIN && nn_errno () != ETIMEDOUT))
168 throw nn::exception ();
174 inline int recvmsg (
struct nn_msghdr *msghdr,
int flags)
176 int rc = nn_recvmsg (s, msghdr, flags);
177 if (nn_slow (rc < 0)) {
178 if (nn_slow (nn_errno () != EAGAIN && nn_errno () != ETIMEDOUT))
179 throw nn::exception ();
190 socket (
const socket&);
191 void operator = (
const socket&);