| 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 | |
| 39 | //! If the expression is less than 0, return the current ZMQ error code. |
| 40 | #define MONERO_ZMQ_CHECK(...) \ |
| 41 | do \ |
| 42 | { \ |
| 43 | if (( __VA_ARGS__ ) < 0) \ |
| 44 | return {::net::zmq::get_error_code()}; \ |
| 45 | } while (0) |
| 46 | |
| 47 | //! Print a message followed by the current ZMQ error message. |
| 48 | #define MONERO_LOG_ZMQ_ERROR(...) \ |
| 49 | do \ |
| 50 | { \ |
| 51 | MERROR( __VA_ARGS__ << ": " << ::net::zmq::get_error_code().message()); \ |
| 52 | } while (0) |
| 53 | |
| 54 | //! Throw an exception with a custom `msg`, current ZMQ error code, filename, and line number. |
| 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 | //! \return Category for ZMQ errors. |
| 68 | const std::error_category& error_category() noexcept; |
| 69 | |
| 70 | //! \return `code` (usally from zmq_errno()`) using `net::zmq::error_category()`. |
| 71 | inline std::error_code make_error_code(int code) noexcept |
| 72 | { |
| 73 | return std::error_code{code, error_category()}; |
| 74 | } |
| 75 | |
| 76 | //! \return Error from `zmq_errno()` using `net::zmq::error_category()`. |
| 77 | inline std::error_code get_error_code() noexcept |
| 78 | { |
| 79 | return make_error_code(zmq_errno()); |
| 80 | } |
| 81 | |
| 82 | //! Calls `zmq_term` |
| 83 | class terminate |
| 84 | { |
| 85 | static void call(void* ptr) noexcept; |
| 86 | public: |
| 87 | void operator()(void* ptr) const noexcept |
| 88 | { |
| 89 | if (ptr) |
| 90 | call(ptr); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | //! Calls `zmq_close` |
| 95 | struct close |
| 96 | { |
| 97 | void operator()(void* ptr) const noexcept |
| 98 | { |
| 99 | if (ptr) |
| 100 | zmq_close(ptr); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | //! Unique ZMQ context handle, calls `zmq_term` on destruction. |
| 105 | using context = std::unique_ptr<void, terminate>; |
| 106 | |
| 107 | //! Unique ZMQ socket handle, calls `zmq_close` on destruction. |
| 108 | using socket = std::unique_ptr<void, close>; |
| 109 | |
| 110 | /*! Retry a ZMQ function on `EINTR` errors. `F` must return an int with |
| 111 | values less than 0 on error. |
| 112 | |
| 113 | \param op The ZMQ function to execute + retry |
| 114 | \param args Forwarded to `op`. Must be resuable in case of retry. |
| 115 | \return All errors except for `EINTR`. */ |
| 116 | template<typename F, typename... T> |
| 117 | expect<void> retry_op(F op, T&&... args) noexcept(noexcept(op(args...))) |
| 118 | { |
| 119 | for (;;) |
| 120 | { |
| 121 | if (0 <= op(args...)) |
| 122 | return success(); |
| 123 | |
| 124 | const int error = zmq_errno(); |
| 125 | if (error != EINTR) |
| 126 | return make_error_code(error); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /*! Read all parts of the next message on `socket`. Blocks until the entire |
| 131 | next message (all parts) are read, or until `zmq_term` is called on the |
| 132 | `zmq_context` associated with `socket`. If the context is terminated, |
| 133 | `make_error_code(ETERM)` is returned. |
| 134 | |
| 135 | \note This will automatically retry on `EINTR`, so exiting on |
| 136 | interrupts requires context termination. |
| 137 | \note If non-blocking behavior is requested on `socket` or by `flags`, |
| 138 | then `net::zmq::make_error_code(EAGAIN)` will be returned if this |
| 139 | would block. |
| 140 | |
| 141 | \param socket Handle created with `zmq_socket`. |
| 142 | \param flags See `zmq_msg_read` for possible flags. |
| 143 | \return Message payload read from `socket` or ZMQ error. */ |
| 144 | expect<std::string> receive(void* socket, int flags = 0); |
| 145 | |
| 146 | /*! Sends `payload` on `socket`. Blocks until the entire message is queued |
| 147 | for sending, or until `zmq_term` is called on the `zmq_context` |
| 148 | associated with `socket`. If the context is terminated, |
| 149 | `make_error_code(ETERM)` is returned. |
| 150 | |
| 151 | \note This will automatically retry on `EINTR`, so exiting on |
| 152 | interrupts requires context termination. |
| 153 | \note If non-blocking behavior is requested on `socket` or by `flags`, |
| 154 | then `net::zmq::make_error_code(EAGAIN)` will be returned if this |
| 155 | would block. |
| 156 | |
| 157 | \param payload sent as one message on `socket`. |
| 158 | \param socket Handle created with `zmq_socket`. |
| 159 | \param flags See `zmq_send` for possible flags. |
| 160 | \return `success()` if sent, otherwise ZMQ error. */ |
| 161 | expect<void> send(epee::span<const std::uint8_t> payload, void* socket, int flags = 0) noexcept; |
| 162 | |
| 163 | /*! Sends `payload` on `socket`. Blocks until the entire message is queued |
| 164 | for sending, or until `zmq_term` is called on the `zmq_context` |
| 165 | associated with `socket`. If the context is terminated, |
| 166 | `make_error_code(ETERM)` is returned. |
| 167 | |
| 168 | \note This will automatically retry on `EINTR`, so exiting on |
| 169 | interrupts requires context termination. |
| 170 | \note If non-blocking behavior is requested on `socket` or by `flags`, |
| 171 | then `net::zmq::make_error_code(EAGAIN)` will be returned if this |
| 172 | would block. |
| 173 | |
| 174 | \param payload sent as one message on `socket`. |
| 175 | \param socket Handle created with `zmq_socket`. |
| 176 | \param flags See `zmq_msg_send` for possible flags. |
| 177 | |
| 178 | \post `payload.emtpy()` - ownership is transferred to zmq. |
| 179 | \return `success()` if sent, otherwise ZMQ error. */ |
| 180 | expect<void> send(epee::byte_slice&& payload, void* socket, int flags = 0) noexcept; |
| 181 | } // zmq |
| 182 | } // net |