bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
nn.h
1/*
2 Copyright (c) 2013 250bpm s.r.o.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom
9 the Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21*/
22
23#ifndef NN_HPP_INCLUDED
24#define NN_HPP_INCLUDED
25
26#include <nanomsg/nn.h>
27
28#include <cassert>
29#include <cstring>
30#include <algorithm>
31#include <exception>
32
33#if defined __GNUC__
34#define nn_slow(x) __builtin_expect ((x), 0)
35#else
36#define nn_slow(x) (x)
37#endif
38
39namespace nn
40{
41
42 class exception : public std::exception
43 {
44 public:
45
46 exception () : err (nn_errno ()) {}
47
48 virtual const char *what () const throw ()
49 {
50 return nn_strerror (err);
51 }
52
53 int num () const
54 {
55 return err;
56 }
57
58 private:
59
60 int err;
61 };
62
63 inline const char *symbol (int i, int *value)
64 {
65 return nn_symbol (i, value);
66 }
67
68 inline void *allocmsg (size_t size, int type)
69 {
70 void *msg = nn_allocmsg (size, type);
71 if (nn_slow (!msg))
72 throw nn::exception ();
73 return msg;
74 }
75
76 inline int freemsg (void *msg)
77 {
78 int rc = nn_freemsg (msg);
79 if (nn_slow (rc != 0))
80 throw nn::exception ();
81 return rc;
82 }
83
84 class socket
85 {
86 public:
87
88 inline socket (int domain, int protocol)
89 {
90 s = nn_socket (domain, protocol);
91 if (nn_slow (s < 0))
92 throw nn::exception ();
93 }
94
95 inline ~socket ()
96 {
97 int rc = nn_close (s);
98 (void) rc;
99 assert (rc == 0);
100 }
101
102 inline void setsockopt (int level, int option, const void *optval,
103 size_t optvallen)
104 {
105 int rc = nn_setsockopt (s, level, option, optval, optvallen);
106 if (nn_slow (rc != 0))
107 throw nn::exception ();
108 }
109
110 inline void getsockopt (int level, int option, void *optval,
111 size_t *optvallen)
112 {
113 int rc = nn_getsockopt (s, level, option, optval, optvallen);
114 if (nn_slow (rc != 0))
115 throw nn::exception ();
116 }
117
118 inline int bind (const char *addr)
119 {
120 int rc = nn_bind (s, addr);
121 if (nn_slow (rc < 0))
122 throw nn::exception ();
123 return rc;
124 }
125
126 inline int connect (const char *addr)
127 {
128 int rc = nn_connect (s, addr);
129 if (nn_slow (rc < 0))
130 throw nn::exception ();
131 return rc;
132 }
133
134 inline void shutdown (int how)
135 {
136 int rc = nn_shutdown (s, how);
137 if (nn_slow (rc != 0))
138 throw nn::exception ();
139 }
140
141 inline int send (const void *buf, size_t len, int flags) const
142 {
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 ();
147 return -1;
148 }
149 return rc;
150 }
151
152 inline int recv (void *buf, size_t len, int flags)
153 {
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 ();
158 return -1;
159 }
160 return rc;
161 }
162
163 inline int sendmsg (const struct nn_msghdr *msghdr, int flags) const
164 {
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 ();
169 return -1;
170 }
171 return rc;
172 }
173
174 inline int recvmsg (struct nn_msghdr *msghdr, int flags)
175 {
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 ();
180 return -1;
181 }
182 return rc;
183 }
184
185 private:
186
187 int s{0};
188
189 /* Prevent making copies of the socket by accident. */
190 socket (const socket&);
191 void operator = (const socket&);
192 };
193
194 inline void term ()
195 {
196 nn_term ();
197 }
198
199}
200
201#undef nn_slow
202
203#endif
204
205