Electroneum
Loading...
Searching...
No Matches
zmq.h
Go to the documentation of this file.
1// Copyright (c) 2019, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include <memory>
30#include <string>
31#include <system_error>
32#include <zmq.h>
33
34#include "common/expect.h"
35#include "span.h"
36
38#define ELECTRONEUM_ZMQ_CHECK(...) \
39 do \
40 { \
41 if (( __VA_ARGS__ ) < 0) \
42 return {::net::zmq::get_error_code()}; \
43 } while (0)
44
46#define ELECTRONEUM_LOG_ZMQ_ERROR(...) \
47 do \
48 { \
49 MERROR( __VA_ARGS__ << ": " << ::net::zmq::get_error_code().message()); \
50 } while (0)
51
53#define ELECTRONEUM_ZMQ_THROW(msg) \
54 ELECTRONEUM_THROW( ::net::zmq::get_error_code(), msg )
55
56namespace net
57{
58 namespace zmq
59 {
61 const std::error_category& error_category() noexcept;
62
64 inline std::error_code make_error_code(int code) noexcept
65 {
66 return std::error_code{code, error_category()};
67 }
68
70 inline std::error_code get_error_code() noexcept
71 {
72 return make_error_code(zmq_errno());
73 }
74
77 {
78 static void call(void* ptr) noexcept;
79 public:
80 void operator()(void* ptr) const noexcept
81 {
82 if (ptr)
83 call(ptr);
84 }
85 };
86
88 struct close
89 {
90 void operator()(void* ptr) const noexcept
91 {
92 if (ptr)
93 zmq_close(ptr);
94 }
95 };
96
98 using context = std::unique_ptr<void, terminate>;
99
101 using socket = std::unique_ptr<void, close>;
102
115 expect<std::string> receive(void* socket, int flags = 0);
116
130 expect<void> send(epee::span<const std::uint8_t> payload, void* socket, int flags = 0) noexcept;
131 } // zmq
132} // net
Non-owning sequence of data. Does not deep copy.
Definition span.h:57
Calls zmq_term.
Definition zmq.h:77
void operator()(void *ptr) const noexcept
Definition zmq.h:80
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition zmq.h:98
const std::error_category & error_category() noexcept
Definition zmq.cpp:40
std::error_code get_error_code() noexcept
Definition zmq.h:70
expect< void > send(const epee::span< const std::uint8_t > payload, void *const socket, const int flags) noexcept
Definition zmq.cpp:182
std::error_code make_error_code(int code) noexcept
Definition zmq.h:64
expect< std::string > receive(void *const socket, const int flags)
Definition zmq.cpp:175
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition zmq.h:101
Calls zmq_close.
Definition zmq.h:89
void operator()(void *ptr) const noexcept
Definition zmq.h:90