Monero
Toggle main menu visibility
Loading...
Searching...
No Matches
src
net
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
58
namespace
epee
59
{
60
class
byte_slice;
61
}
62
63
namespace
net
64
{
65
namespace
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
85
class
terminate
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
epee::span
Non-owning sequence of data. Does not deep copy.
Definition
span.h:55
expect
Definition
expect.h:134
net::zmq::terminate
Calls zmq_term.
Definition
zmq.h:86
net::zmq::terminate::call
static void call(void *ptr) noexcept
Definition
zmq.cpp:83
net::zmq::terminate::operator()
void operator()(void *ptr) const noexcept
Definition
zmq.h:89
success
bool success
Definition
cold-transaction.cpp:57
expect.h
code
static const char * code
Definition
generate_translations_header.c:25
inline
#define inline
Definition
inline_c.h:34
flags
static int flags
Definition
mdb_load.c:31
epee
TODO: (mj-xmr) This will be reduced in an another PR.
Definition
byte_slice.h:40
net::zmq
Definition
zmq.cpp:41
net::zmq::context
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition
zmq.h:107
net::zmq::error_category
const std::error_category & error_category() noexcept
Definition
zmq.cpp:42
net::zmq::get_error_code
std::error_code get_error_code() noexcept
Definition
zmq.h:79
net::zmq::max_message_size
constexpr std::size_t max_message_size
Definition
zmq.h:67
net::zmq::send
expect< void > send(const epee::span< const std::uint8_t > payload, void *const socket, const int flags) noexcept
Definition
zmq.cpp:176
net::zmq::make_error_code
std::error_code make_error_code(int code) noexcept
Definition
zmq.h:73
net::zmq::retry_op
expect< void > retry_op(F op, T &&... args) noexcept(noexcept(op(args...)))
Definition
zmq.h:119
net::zmq::receive
expect< std::string > receive(void *const socket, const int flags)
Definition
zmq.cpp:169
net::zmq::socket
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition
zmq.h:110
net
Definition
net_utils_base.h:59
net::error
error
General net errors.
Definition
error.h:39
std
Definition
enums.h:68
F
#define F(w, k)
Definition
sha512-blocks.c:61
span.h
net::zmq::close
Calls zmq_close.
Definition
zmq.h:98
net::zmq::close::operator()
void operator()(void *ptr) const noexcept
Definition
zmq.h:99
T
#define T(x)
zmq.h
Generated on
for Monero by
1.17.0