Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit 5144cb8

Browse files
authored
Add again zmq.hpp before tagging release 9.2.5 to be consistent with 9.2.5 version from SVN Sourceforge (#348)
1 parent 3223bc1 commit 5144cb8

File tree

1 file changed

+305
-0
lines changed

1 file changed

+305
-0
lines changed

cppapi/client/zmq.hpp

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
/*
2+
Copyright (c) 2007-2011 iMatix Corporation
3+
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4+
5+
This file is part of 0MQ.
6+
7+
0MQ is free software; you can redistribute it and/or modify it under
8+
the terms of the GNU Lesser General Public License as published by
9+
the Free Software Foundation; either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
0MQ is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef __ZMQ_HPP_INCLUDED__
22+
#define __ZMQ_HPP_INCLUDED__
23+
24+
#include <zmq.h>
25+
26+
#include <cassert>
27+
#include <cstring>
28+
#include <exception>
29+
30+
namespace zmq
31+
{
32+
33+
typedef zmq_free_fn free_fn;
34+
typedef zmq_pollitem_t pollitem_t;
35+
36+
class error_t : public std::exception
37+
{
38+
public:
39+
40+
error_t () : errnum (zmq_errno ()) {}
41+
42+
virtual const char *what () const throw ()
43+
{
44+
return zmq_strerror (errnum);
45+
}
46+
47+
int num () const
48+
{
49+
return errnum;
50+
}
51+
52+
private:
53+
54+
int errnum;
55+
};
56+
57+
inline int poll (zmq_pollitem_t *items_, int nitems_, long timeout_ = -1)
58+
{
59+
int rc = zmq_poll (items_, nitems_, timeout_);
60+
if (rc < 0)
61+
throw error_t ();
62+
return rc;
63+
}
64+
65+
inline void version (int *major_, int *minor_, int *patch_)
66+
{
67+
zmq_version (major_, minor_, patch_);
68+
}
69+
70+
class message_t
71+
{
72+
friend class socket_t;
73+
74+
public:
75+
76+
inline message_t ()
77+
{
78+
int rc = zmq_msg_init (&msg);
79+
if (rc != 0)
80+
throw error_t ();
81+
}
82+
83+
inline message_t (size_t size_)
84+
{
85+
int rc = zmq_msg_init_size (&msg, size_);
86+
if (rc != 0)
87+
throw error_t ();
88+
}
89+
90+
inline message_t (void *data_, size_t size_, free_fn *ffn_,
91+
void *hint_ = NULL)
92+
{
93+
int rc = zmq_msg_init_data (&msg, data_, size_, ffn_, hint_);
94+
if (rc != 0)
95+
throw error_t ();
96+
}
97+
98+
inline ~message_t ()
99+
{
100+
int rc = zmq_msg_close (&msg);
101+
assert (rc == 0);
102+
}
103+
104+
inline void rebuild ()
105+
{
106+
int rc = zmq_msg_close (&msg);
107+
if (rc != 0)
108+
throw error_t ();
109+
rc = zmq_msg_init (&msg);
110+
if (rc != 0)
111+
throw error_t ();
112+
}
113+
114+
inline void rebuild (size_t size_)
115+
{
116+
int rc = zmq_msg_close (&msg);
117+
if (rc != 0)
118+
throw error_t ();
119+
rc = zmq_msg_init_size (&msg, size_);
120+
if (rc != 0)
121+
throw error_t ();
122+
}
123+
124+
inline void rebuild (void *data_, size_t size_, free_fn *ffn_,
125+
void *hint_ = NULL)
126+
{
127+
int rc = zmq_msg_close (&msg);
128+
if (rc != 0)
129+
throw error_t ();
130+
rc = zmq_msg_init_data (&msg, data_, size_, ffn_, hint_);
131+
if (rc != 0)
132+
throw error_t ();
133+
}
134+
135+
inline void move (message_t *msg_)
136+
{
137+
int rc = zmq_msg_move (&msg, &(msg_->msg));
138+
if (rc != 0)
139+
throw error_t ();
140+
}
141+
142+
inline void copy (message_t *msg_)
143+
{
144+
int rc = zmq_msg_copy (&msg, &(msg_->msg));
145+
if (rc != 0)
146+
throw error_t ();
147+
}
148+
149+
inline void *data ()
150+
{
151+
return zmq_msg_data (&msg);
152+
}
153+
154+
inline size_t size ()
155+
{
156+
return zmq_msg_size (&msg);
157+
}
158+
159+
private:
160+
161+
// The underlying message
162+
zmq_msg_t msg;
163+
164+
// Disable implicit message copying, so that users won't use shared
165+
// messages (less efficient) without being aware of the fact.
166+
message_t (const message_t&);
167+
void operator = (const message_t&);
168+
};
169+
170+
class context_t
171+
{
172+
friend class socket_t;
173+
174+
public:
175+
176+
inline context_t (int io_threads_)
177+
{
178+
ptr = zmq_init (io_threads_);
179+
if (ptr == NULL)
180+
throw error_t ();
181+
}
182+
183+
inline ~context_t ()
184+
{
185+
int rc = zmq_term (ptr);
186+
assert (rc == 0);
187+
}
188+
189+
// Be careful with this, it's probably only useful for
190+
// using the C api together with an existing C++ api.
191+
// Normally you should never need to use this.
192+
inline operator void* ()
193+
{
194+
return ptr;
195+
}
196+
197+
private:
198+
199+
void *ptr;
200+
201+
context_t (const context_t&);
202+
void operator = (const context_t&);
203+
};
204+
205+
class socket_t
206+
{
207+
public:
208+
209+
inline socket_t (context_t &context_, int type_)
210+
{
211+
ptr = zmq_socket (context_.ptr, type_);
212+
if (ptr == NULL)
213+
throw error_t ();
214+
}
215+
216+
inline ~socket_t ()
217+
{
218+
close();
219+
}
220+
221+
inline operator void* ()
222+
{
223+
return ptr;
224+
}
225+
226+
inline void close()
227+
{
228+
if(ptr == NULL)
229+
// already closed
230+
return ;
231+
int rc = zmq_close (ptr);
232+
if (rc != 0)
233+
throw error_t ();
234+
ptr = 0 ;
235+
}
236+
237+
inline void setsockopt (int option_, const void *optval_,
238+
size_t optvallen_)
239+
{
240+
int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_);
241+
if (rc != 0)
242+
throw error_t ();
243+
}
244+
245+
inline void getsockopt (int option_, void *optval_,
246+
size_t *optvallen_)
247+
{
248+
int rc = zmq_getsockopt (ptr, option_, optval_, optvallen_);
249+
if (rc != 0)
250+
throw error_t ();
251+
}
252+
253+
inline void bind (const char *addr_)
254+
{
255+
int rc = zmq_bind (ptr, addr_);
256+
if (rc != 0)
257+
throw error_t ();
258+
}
259+
260+
inline void connect (const char *addr_)
261+
{
262+
int rc = zmq_connect (ptr, addr_);
263+
if (rc != 0)
264+
throw error_t ();
265+
}
266+
267+
inline void disconnect (const char *addr_)
268+
{
269+
int rc = zmq_disconnect (ptr, addr_);
270+
if (rc != 0)
271+
throw error_t ();
272+
}
273+
274+
inline bool send (message_t &msg_, int flags_ = 0)
275+
{
276+
int nbytes = zmq_sendmsg (ptr, &(msg_.msg), flags_);
277+
if (nbytes >= 0)
278+
return true;
279+
if (zmq_errno () == EAGAIN)
280+
return false;
281+
throw error_t ();
282+
}
283+
284+
inline bool recv (message_t *msg_, int flags_ = 0)
285+
{
286+
int nbytes = zmq_recvmsg (ptr, &(msg_->msg), flags_);
287+
if (nbytes >= 0)
288+
return true;
289+
if (zmq_errno () == EAGAIN)
290+
return false;
291+
throw error_t ();
292+
}
293+
294+
private:
295+
296+
void *ptr;
297+
298+
socket_t (const socket_t&);
299+
void operator = (const socket_t&);
300+
};
301+
302+
}
303+
304+
#endif
305+

0 commit comments

Comments
 (0)