Monero
Loading...
Searching...
No Matches
zmq.h
Go to the documentation of this file.
1// Copyright (c) 2019-2022, 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#pragma once
30
31#include <memory>
32#include <string>
33#include <system_error>
34#include <zmq.h>
35
36#include "common/expect.h"
37#include "span.h"
38
40#define MONERO_ZMQ_CHECK(...) \
41 do \
42 { \
43 if (( __VA_ARGS__ ) < 0) \
44 return {::net::zmq::get_error_code()}; \
45 } while (0)
46
48#define MONERO_LOG_ZMQ_ERROR(...) \
49 do \
50 { \
51 MERROR( __VA_ARGS__ << ": " << ::net::zmq::get_error_code().message()); \
52 } while (0)
53
55#define MONERO_ZMQ_THROW(msg) \
56 MONERO_THROW( ::net::zmq::get_error_code(), msg )
57
58namespace epee
59{
60 class byte_slice;
61}
62
63namespace net
64{
65namespace zmq
66{
67 constexpr std::size_t max_message_size = 10 * 1024 * 1024; // 10 MiB
68
70 const std::error_category& error_category() noexcept;
71
73 inline std::error_code make_error_code(int code) noexcept
74 {
75 return std::error_code{code, error_category()};
76 }
77
79 inline std::error_code get_error_code() noexcept
80 {
81 return make_error_code(zmq_errno());
82 }
83
86 {
87 static void call(void* ptr) noexcept;
88 public:
89 void operator()(void* ptr) const noexcept
90 {
91 if (ptr)
92 call(ptr);
93 }
94 };
95
97 struct close
98 {
99 void operator()(void* ptr) const noexcept
100 {
101 if (ptr)
102 zmq_close(ptr);
103 }
104 };
105
107 using context = std::unique_ptr<void, terminate>;
108
110 using socket = std::unique_ptr<void, close>;
111
118 template<typename F, typename... T>
119 expect<void> retry_op(F op, T&&... args) noexcept(noexcept(op(args...)))
120 {
121 for (;;)
122 {
123 if (0 <= op(args...))
124 return success();
125
126 const int error = zmq_errno();
127 if (error != EINTR)
128 return make_error_code(error);
129 }
130 }
131
146 expect<std::string> receive(void* socket, int flags = 0);
147
163 expect<void> send(epee::span<const std::uint8_t> payload, void* socket, int flags = 0) noexcept;
164
182 expect<void> send(epee::byte_slice&& payload, void* socket, int flags = 0) noexcept;
183} // zmq
184} // net
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
Definition expect.h:134
Calls zmq_term.
Definition zmq.h:86
static void call(void *ptr) noexcept
Definition zmq.cpp:83
void operator()(void *ptr) const noexcept
Definition zmq.h:89
bool success
Definition cold-transaction.cpp:57
static const char * code
Definition generate_translations_header.c:25
#define inline
Definition inline_c.h:34
static int flags
Definition mdb_load.c:31
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition zmq.cpp:41
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition zmq.h:107
const std::error_category & error_category() noexcept
Definition zmq.cpp:42
std::error_code get_error_code() noexcept
Definition zmq.h:79
constexpr std::size_t max_message_size
Definition zmq.h:67
expect< void > send(const epee::span< const std::uint8_t > payload, void *const socket, const int flags) noexcept
Definition zmq.cpp:176
std::error_code make_error_code(int code) noexcept
Definition zmq.h:73
expect< void > retry_op(F op, T &&... args) noexcept(noexcept(op(args...)))
Definition zmq.h:119
expect< std::string > receive(void *const socket, const int flags)
Definition zmq.cpp:169
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition zmq.h:110
Definition net_utils_base.h:59
error
General net errors.
Definition error.h:39
Definition enums.h:68
#define F(w, k)
Definition sha512-blocks.c:61
Calls zmq_close.
Definition zmq.h:98
void operator()(void *ptr) const noexcept
Definition zmq.h:99
#define T(x)