├── LICENSE ├── README.md └── include └── CpperoMQ ├── All.hpp ├── Common.hpp ├── Context.hpp ├── DealerSocket.hpp ├── Error.hpp ├── ExtendedPublishSocket.hpp ├── ExtendedSubscribeSocket.hpp ├── IncomingMessage.hpp ├── Message.hpp ├── Mixins ├── ConflatingSocket.hpp ├── IdentifyingSocket.hpp ├── ReceivingSocket.hpp ├── RequestingSocket.hpp ├── RouterProbingSocket.hpp ├── RoutingSocket.hpp ├── SendingSocket.hpp ├── SocketTypeWrapper.hpp └── SubscribingSocket.hpp ├── OutgoingMessage.hpp ├── PollItem.hpp ├── Poller.hpp ├── Proxy.hpp ├── PublishSocket.hpp ├── PullSocket.hpp ├── PushSocket.hpp ├── Receivable.hpp ├── ReplySocket.hpp ├── RequestSocket.hpp ├── RouterSocket.hpp ├── Sendable.hpp ├── Socket.hpp ├── SubscribeSocket.hpp └── Version.hpp /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jason Shipman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to 7 | deal in the Software without restriction, including without limitation the 8 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | sell copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CpperoMQ 2 | CpperoMQ is a C++11 binding for the [0MQ (libzmq)][1] C library. 3 | 4 | ## Features 5 | CpperoMQ offers several advantages over using the [libzmq][1] C library directly and some of the other currently available C++ 0MQ bindings: 6 | 7 | 1. Highly composable multipart messaging 8 | 2. Type safety 9 | 3. Performance close to using libzmq directly (via a good optimizer) 10 | 11 | ## Using the Library 12 | 13 | To see zguide examples implemented with CpperoMQ as well as some additional examples, see the [CpperoMQ-examples][9] repository. 14 | 15 | Just like 0MQ, the building blocks in CpperoMQ are contexts, sockets, and messages. 16 | 17 | All CpperoMQ classes exist under the ```CpperoMQ``` namespace, so the following saves a few keystrokes for the remainder of this section: 18 | 19 | ```cpp 20 | using namespace CpperoMQ; 21 | ``` 22 | 23 | A context encapsulates 0MQ's internal state. A CpperoMQ `Context` can be initialized as follows: 24 | 25 | ```cpp 26 | Context context; 27 | ``` 28 | 29 | A `Context` can be customized at construction and via mutators. The most important responsibility of the Context is to create `Sockets`: 30 | 31 | ```cpp 32 | RequestSocket requestSocket(context.createRequestSocket()); 33 | ``` 34 | 35 | `Socket` objects are strongly typed and move-constructed from temporaries provided by the `Context`: 36 | 37 | ```cpp 38 | auto dealer(context.createDealerSocket()); 39 | auto extPub(context.createExtendedPublishSocket()); 40 | auto extSub(context.createExtendedSubscribeSocket()); 41 | auto pub(context.createPublishSocket()); 42 | auto pull(context.createPullSocket()); 43 | auto push(context.createPushSocket()); 44 | auto reply(context.createReplySocket()); 45 | auto request(context.createRequestSocket()); 46 | auto router(context.createRouterSocket()); 47 | auto sub(context.createSubscribeSocket()); 48 | ``` 49 | 50 | Depending on the socket type, a `Socket` can send `OutgoingMessage` objects and/or receive `IncomingMessage` objects. An `OutgoingMessage` is initialized with data at construction only. Instances can be sent but not received: 51 | 52 | ```cpp 53 | OutgoingMessage message("Hello"); 54 | pub.send(message); 55 | // pub.receive(message); /* Compiler error! */ 56 | ``` 57 | 58 | An `IncomingMessage` is default-constructed only. Instances can be received but not sent: 59 | 60 | ```cpp 61 | IncomingMessage message; 62 | sub.receive(message); 63 | // sub.send(message); /* Compiler error! */ 64 | ``` 65 | 66 | The primary goal of CpperoMQ is to provide users with extremely **composable multipart messaging**. The `send` and `receive` methods on a `Socket` take any positive number of parameters, as long as each parameter implements the appropriate `Sendable` or `Receivable` interface. All parameters in a single call to `send` or `receive` are treated as the individual message parts (a.k.a. frames) of a multipart message. 67 | 68 | Long story short, users send a multipart message like this: 69 | 70 | ```cpp 71 | // 'send' treats all of its parameters as a single, multipart message. 72 | // No need to build up a list of message parts before-hand. 73 | pub.send( OutgoingMessage("This") 74 | , OutgoingMessage("is") 75 | , OutgoingMessage("a") 76 | , OutgoingMessage("multipart") 77 | , OutgoingMessage("message") ); 78 | ``` 79 | 80 | Users can then receive the above multipart message as its individual parts like this: 81 | 82 | ```cpp 83 | // 'receive' treats all of its parameters as the parts of a single, 84 | // multipart message. 85 | IncomingMessage inMsg1, inMsg2, inMsg3, inMsg4, inMsg5; 86 | sub.receive(inMsg1, inMsg2, inMsg3, inMsg4, inMsg5); 87 | ``` 88 | 89 | In many cases, it is undesirable to have to know up-front how many message parts are expected when receiving on a socket. It is more convenient to send or receive complex objects directly on a socket. Enter the `Sendable` and `Receivable` interfaces: 90 | 91 | ```cpp 92 | class Employee : public Sendable, public Receivable 93 | { 94 | public: 95 | // ... 96 | private: 97 | int mId; 98 | int mAge; 99 | std::string mName; 100 | }; 101 | 102 | class Department : public Sendable, public Receivable 103 | { 104 | public: 105 | // ... 106 | private: 107 | int mCode; 108 | std::string mName; 109 | }; 110 | ``` 111 | 112 | Now the user can freely send and receive any combinations of CpperoMQ's `OutgoingMessage` and `IncomingMessage` as well as the above user-defined `Employee` and `Department`. 113 | 114 | For a somewhat contrived example, imagine publishing a department update message where we broadcast that an employee now belongs to a different department: 115 | 116 | ```cpp 117 | // From the publishing executable... 118 | pub.send( Department(/* ... */) // old department 119 | , Department(/* ... */) // new department 120 | , Employee(/* ... */) ); // employee 121 | ``` 122 | 123 | ```cpp 124 | // From the subscribing executable... 125 | Department oldDept; 126 | Department newDept; 127 | Employee employee; 128 | 129 | sub.receive(oldDept, newDept, employee); 130 | ``` 131 | 132 | The above is sent as a single, multipart message that is guaranteed by 0MQ to be either received in full or not received at all by a connected receiving socket. Note that at any point in the list of parameters of `send` we could have incorporated some `OutgoingMessage` objects too. Likewise, for `receive` we could have included some `IncomingMessage` objects. 133 | 134 | Take a look at the implementation of the `Sendable` interface below for `Department`: 135 | 136 | ```cpp 137 | virtual bool send(const CpperoMQ::Socket& socket, const bool moreToSend) const override 138 | { 139 | using namespace CpperoMQ; 140 | 141 | OutgoingMessage idMsgPart(serializeNumber(mCode)); 142 | if (!idMsgPart.send(socket, true)) 143 | return false; 144 | 145 | OutgoingMessage nameMsgPart(mName.c_str()); 146 | if (!nameMsgPart.send(socket, moreToSend)) 147 | return false; 148 | 149 | return true; 150 | } 151 | ``` 152 | 153 | Implementing this interface is what enables us to compose multipart messages containing `Department` objects. Note that the caller and callee are flipped - instead of calling `send` on the socket, we call `send` on the `OutgoingMessage` objects, passing the `Socket` and a boolean as a parameter. The boolean indicates whether or not there are more parts to send for the multipart message. 154 | 155 | When implementing the `Sendable` interface, the requirement is to pass true for the boolean parameter for all internal send calls up to the final one. The final send must pass the `moreToSend` parameter. **Composing sendable, multipart messages containing complex objects in CpperoMQ _requires_ that this pattern is correctly followed.** 156 | 157 | The implementation of the `Receivable` interface is similar: 158 | 159 | ```cpp 160 | virtual bool receive(CpperoMQ::Socket& socket, bool& moreToReceive) override 161 | { 162 | using namespace CpperoMQ; 163 | 164 | IncomingMessage idMsgPart; 165 | if (!idMsgPart.receive(socket, moreToReceive) || !moreToReceive) 166 | return false; 167 | 168 | IncomingMessage nameMsgPart; 169 | if (!nameMsgPart.receive(socket, moreToReceive)) 170 | return false; 171 | 172 | mCode = deserializeNumber(idMsgPart); 173 | mName.assign(nameMsgPart.charData(), nameMsgPart.size()); 174 | 175 | return true; 176 | } 177 | ``` 178 | 179 | When implementing the `Receivable` interface, the requirement is to pass the moreToReceive boolean reference parameter to all internal receive calls. After a receive is performed, this parameter's value indicates whether or not there exist more parts to receive for the multipart message. Its value must be checked after all internal receive calls up to the last one. **Composing receivable, multipart messages containing complex objects in CpperoMQ _requires_ that this pattern is correctly followed.** 180 | 181 | We can go a step further with the above example by encapsulating the individual objects involved in a Department Update message: 182 | 183 | ```cpp 184 | class DepartmentUpdate : public CpperoMQ::Sendable 185 | , public CpperoMQ::Receivable 186 | { 187 | public: 188 | // ... 189 | 190 | virtual bool send(const CpperoMQ::Socket& socket, const bool moreToSend) const override 191 | { 192 | using namespace CpperoMQ; 193 | 194 | if (!mOldDept.send(socket, true)) 195 | return false; 196 | 197 | if (!mNewDept.send(socket, true)) 198 | return false; 199 | 200 | if (!mEmployee.send(socket, moreToSend)) 201 | return false; 202 | 203 | return true; 204 | } 205 | 206 | virtual bool receive(CpperoMQ::Socket& socket, bool& moreToReceive) override 207 | { 208 | using namespace CpperoMQ; 209 | 210 | if (!mOldDept.receive(socket, moreToReceive) || !moreToReceive) 211 | return false; 212 | 213 | if (!mNewDept.receive(socket, moreToReceive) || !moreToReceive) 214 | return false; 215 | 216 | if (!mEmployee.receive(socket, moreToReceive)) 217 | return false; 218 | 219 | return true; 220 | } 221 | 222 | private: 223 | Department mOldDept; 224 | Department mNewDept; 225 | Employee mEmployee; 226 | }; 227 | ``` 228 | 229 | Now we can send `DepartmentUpdate` objects directly on `Sockets`. 230 | 231 | **To see zguide examples implemented with CpperoMQ as well as some additional examples, see the [CpperoMQ-examples][9] repository.** 232 | 233 | **Disclaimer:** Most of the above code did not check for errors. Real code should check the boolean result of each relevant library function. CpperoMQ can throw a CpperoMQ::Error exception, so that should be caught too. 234 | 235 | ## Drawbacks 236 | CpperoMQ currently does not implement the following: 237 | 238 | 1. CURVE security 239 | 2. PAIR sockets 240 | 3. Some uncommon socket options (workaround by passing socket handle directly to [libzmq][1]) 241 | 242 | Feel free to contribute implementations for the above items. The author may also add these as needed. The workaround for 1 and 3 is to pass the socket handle to [libzmq][1]. 2 requires direct use of [libzmq][1]. 243 | 244 | ## Alternatives 245 | The below 0MQ C++ bindings are alternatives to CpperoMQ: 246 | 247 | 1. [cppzmq][6] 248 | 2. [zmqpp][7] 249 | 3. [azmq][8] 250 | 251 | ## Requirements 252 | CpperoMQ requires the following: 253 | 254 | 1. A compiler with [C++11 support][2] 255 | 2. [libzmq][1] 4.1 or greater. 256 | 3. libzmq's [zmq.hpp header][3] is available on the include path 257 | 4. Binaries compiled with CpperoMQ must be linked with [libzmq][1]. 258 | 259 | The specific C++11 features used are listed below. Note that the source code is more likely to change than this README, so the list may be outdated. 260 | 261 | ``` 262 | auto 263 | Defaulted functions 264 | Delegating constructors 265 | Deleted functions 266 | final 267 | function 268 | noexcept (if not MSVC) 269 | nullptr 270 | override 271 | Rvalue references 272 | static_assert 273 | Trailing return types 274 | Variadic templates 275 | ``` 276 | 277 | ## Compiler Support 278 | CpperoMQ has been tested on the following operating systems and compilers: 279 | 280 | | Operating System | Compiler | 281 | | ---------------- | ---------- | 282 | | OS X 10.10 | Xcode 6.4 | 283 | | Windows 8 | VS2013 | 284 | 285 | The above are the environments and compilers available to the author. Please update this table directly or notify the author upon confirming additional compiler support. 286 | 287 | ## Installation 288 | CpperoMQ is a C++11 header-only library, making installation a painless two-step process: 289 | 290 | 1. Download CpperoMQ. 291 | 2. From the project root's 'include' directory, copy the CpperoMQ directory into a project's (or the system) include path. 292 | 293 | ## Contributing 294 | Contributions to this binding via pull requests or bug reports are always welcome! See the [0MQ contribution policy][4] page for details. 295 | 296 | ## Licensing 297 | The CpperoMQ library is licensed under the MIT license. The included [LICENSE](LICENSE) file and the comment header of each source file contain the full text of the [MIT license][5]. 298 | 299 | [1]: https://github.com/zeromq/libzmq 300 | [2]: http://en.cppreference.com/w/cpp/compiler_support 301 | [3]: https://github.com/zeromq/libzmq/blob/master/include/zmq.h 302 | [4]: http://zeromq.org/docs:contributing 303 | [5]: http://opensource.org/licenses/MIT 304 | [6]: https://github.com/zeromq/cppzmq 305 | [7]: https://github.com/zeromq/zmqpp 306 | [8]: https://github.com/zeromq/azmq 307 | [9]: https://github.com/jship/CpperoMQ-examples 308 | -------------------------------------------------------------------------------- /include/CpperoMQ/All.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | -------------------------------------------------------------------------------- /include/CpperoMQ/Common.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #ifndef NDEBUG 26 | #define CPPEROMQ_ASSERT(expression) assert(expression) 27 | #else 28 | #define CPPEROMQ_ASSERT(expression) (void)(expression) 29 | #endif 30 | 31 | #ifndef _MSC_VER 32 | #define NOEXCEPT noexcept 33 | #else 34 | #define NOEXCEPT 35 | #endif 36 | 37 | #include 38 | #include 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/Context.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | namespace CpperoMQ 37 | { 38 | 39 | class Context 40 | { 41 | public: 42 | Context(const int ioThreadCount = 1, const int maxSocketCount = 1023); 43 | ~Context(); 44 | Context(const Context & rhs) = delete; 45 | Context(Context&& other); 46 | Context & operator=(const Context & rhs) = delete; 47 | Context& operator=(Context&& other); 48 | 49 | auto createDealerSocket() -> DealerSocket; 50 | auto createExtendedPublishSocket() -> ExtendedPublishSocket; 51 | auto createExtendedSubscribeSocket() -> ExtendedSubscribeSocket; 52 | auto createPublishSocket() -> PublishSocket; 53 | auto createPullSocket() -> PullSocket; 54 | auto createPushSocket() -> PushSocket; 55 | auto createReplySocket() -> ReplySocket; 56 | auto createRequestSocket() -> RequestSocket; 57 | auto createRouterSocket() -> RouterSocket; 58 | auto createSubscribeSocket() -> SubscribeSocket; 59 | 60 | auto getIoThreadCount() const -> int; 61 | auto getMaxSocketCount() const -> int; 62 | auto getMaxConfigurableSocketCount() const -> int; 63 | 64 | auto isIPv6Enabled() const -> bool; 65 | 66 | #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 67 | auto isBlocky() const -> bool; 68 | #endif 69 | 70 | auto setIPv6Enabled(bool enabled) -> void; 71 | 72 | #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 73 | auto setBlocky(bool blocky) -> void; 74 | #endif 75 | 76 | private: 77 | auto getContextSetting(const int settingName) const -> int; 78 | auto setContextSetting(int settingName, int settingValue) -> void; 79 | 80 | void* mContext; 81 | }; 82 | 83 | inline 84 | Context::Context(const int ioThreadCount, const int maxSocketCount) 85 | : mContext(zmq_ctx_new()) 86 | { 87 | if (nullptr == mContext) 88 | { 89 | throw Error(); 90 | } 91 | 92 | setContextSetting(ZMQ_IO_THREADS, ioThreadCount); 93 | setContextSetting(ZMQ_MAX_SOCKETS, maxSocketCount); 94 | } 95 | 96 | inline 97 | Context::~Context() 98 | { 99 | if (mContext) 100 | { 101 | int result = zmq_ctx_term(mContext); 102 | CPPEROMQ_ASSERT(0 == result); 103 | mContext = nullptr; 104 | } 105 | } 106 | 107 | inline 108 | Context::Context(Context&& other) 109 | : mContext(other.mContext) 110 | { 111 | other.mContext = nullptr; 112 | } 113 | 114 | inline 115 | Context& Context::operator=(Context&& other) 116 | { 117 | using std::swap; 118 | swap(mContext, other.mContext); 119 | return (*this); 120 | } 121 | 122 | inline 123 | auto Context::createDealerSocket() -> DealerSocket 124 | { 125 | DealerSocket socket(mContext); 126 | return socket; 127 | } 128 | 129 | inline 130 | auto Context::createExtendedPublishSocket() -> ExtendedPublishSocket 131 | { 132 | ExtendedPublishSocket socket(mContext); 133 | return socket; 134 | } 135 | 136 | inline 137 | auto Context::createExtendedSubscribeSocket() -> ExtendedSubscribeSocket 138 | { 139 | ExtendedSubscribeSocket socket(mContext); 140 | return socket; 141 | } 142 | 143 | inline 144 | auto Context::createPublishSocket() -> PublishSocket 145 | { 146 | PublishSocket socket(mContext); 147 | return socket; 148 | } 149 | 150 | inline 151 | auto Context::createPullSocket() -> PullSocket 152 | { 153 | PullSocket socket(mContext); 154 | return socket; 155 | } 156 | 157 | inline 158 | auto Context::createPushSocket() -> PushSocket 159 | { 160 | PushSocket socket(mContext); 161 | return socket; 162 | } 163 | 164 | inline 165 | auto Context::createReplySocket() -> ReplySocket 166 | { 167 | ReplySocket socket(mContext); 168 | return socket; 169 | } 170 | 171 | inline 172 | auto Context::createRequestSocket() -> RequestSocket 173 | { 174 | RequestSocket socket(mContext); 175 | return socket; 176 | } 177 | 178 | inline 179 | auto Context::createRouterSocket() -> RouterSocket 180 | { 181 | RouterSocket socket(mContext); 182 | return socket; 183 | } 184 | 185 | inline 186 | auto Context::createSubscribeSocket() -> SubscribeSocket 187 | { 188 | SubscribeSocket socket(mContext); 189 | return socket; 190 | } 191 | 192 | inline 193 | auto Context::getIoThreadCount() const -> int 194 | { 195 | return (getContextSetting(ZMQ_IO_THREADS)); 196 | } 197 | 198 | inline 199 | auto Context::getMaxSocketCount() const -> int 200 | { 201 | return (getContextSetting(ZMQ_MAX_SOCKETS)); 202 | } 203 | 204 | inline 205 | auto Context::getMaxConfigurableSocketCount() const -> int 206 | { 207 | return (getContextSetting(ZMQ_SOCKET_LIMIT)); 208 | } 209 | 210 | inline 211 | auto Context::isIPv6Enabled() const -> bool 212 | { 213 | return (1 == getContextSetting(ZMQ_IPV6)); 214 | } 215 | 216 | #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 217 | inline 218 | auto Context::isBlocky() const -> bool 219 | { 220 | return (1 == getContextSetting(ZMQ_BLOCKY)); 221 | } 222 | #endif 223 | 224 | inline 225 | auto Context::setIPv6Enabled(bool enabled) -> void 226 | { 227 | setContextSetting(ZMQ_IPV6, enabled ? 1 : 0); 228 | } 229 | 230 | #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) 231 | inline 232 | auto Context::setBlocky(bool blocky) -> void 233 | { 234 | setContextSetting(ZMQ_BLOCKY, blocky ? 1 : 0); 235 | } 236 | #endif 237 | 238 | inline 239 | auto Context::getContextSetting(const int settingName) const -> int 240 | { 241 | int result = zmq_ctx_get(mContext, settingName); 242 | 243 | if (result < 0) 244 | { 245 | throw Error(); 246 | } 247 | 248 | return result; 249 | } 250 | 251 | inline 252 | auto Context::setContextSetting(int settingName, int settingValue) -> void 253 | { 254 | if (0 != zmq_ctx_set(mContext, settingName, settingValue)) 255 | { 256 | throw Error(); 257 | } 258 | } 259 | 260 | } 261 | -------------------------------------------------------------------------------- /include/CpperoMQ/DealerSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace CpperoMQ 34 | { 35 | 36 | typedef Mixins::SocketTypeWrapper > > > > > DealerSocket; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /include/CpperoMQ/Error.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | 28 | namespace CpperoMQ 29 | { 30 | 31 | class Error final : public std::exception 32 | { 33 | public: 34 | Error(); 35 | 36 | virtual auto what() const NOEXCEPT -> const char* override; 37 | auto number() const -> int; 38 | 39 | private: 40 | int mErrorNumber; 41 | }; 42 | 43 | inline 44 | Error::Error() 45 | : mErrorNumber(zmq_errno()) 46 | { 47 | } 48 | 49 | inline 50 | auto Error::what() const NOEXCEPT -> const char* 51 | { 52 | return zmq_strerror(mErrorNumber); 53 | } 54 | 55 | inline 56 | auto Error::number() const -> int 57 | { 58 | return mErrorNumber; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /include/CpperoMQ/ExtendedPublishSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | namespace CpperoMQ 32 | { 33 | 34 | // Note that XPUB-specific socket options are currently unsupported. 35 | 36 | typedef Mixins::SocketTypeWrapper > > > ExtendedPublishSocket; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /include/CpperoMQ/ExtendedSubscribeSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | namespace CpperoMQ 33 | { 34 | 35 | typedef Mixins::SocketTypeWrapper > > > > ExtendedSubscribeSocket; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /include/CpperoMQ/IncomingMessage.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace CpperoMQ 30 | { 31 | 32 | class IncomingMessage final : public Message, public Receivable 33 | { 34 | public: 35 | IncomingMessage(); 36 | virtual ~IncomingMessage() = default; 37 | IncomingMessage(const IncomingMessage& other) = delete; 38 | IncomingMessage(IncomingMessage&& other); 39 | IncomingMessage& operator=(const IncomingMessage& other) = delete; 40 | IncomingMessage& operator=(IncomingMessage&& other); 41 | 42 | auto size() const -> size_t; 43 | auto data() const -> const void*; 44 | auto charData() const -> const char*; 45 | 46 | virtual auto receive(Socket& socket, bool& moreToReceive) -> bool override; 47 | }; 48 | 49 | inline 50 | IncomingMessage::IncomingMessage() 51 | : Message() 52 | { 53 | } 54 | 55 | inline 56 | IncomingMessage::IncomingMessage(IncomingMessage&& other) 57 | : Message(std::move(other)) 58 | { 59 | } 60 | 61 | inline 62 | IncomingMessage& IncomingMessage::operator=(IncomingMessage&& other) 63 | { 64 | Message::operator=(std::move(other)); 65 | return (*this); 66 | } 67 | 68 | inline 69 | auto IncomingMessage::size() const -> size_t 70 | { 71 | const zmq_msg_t* const msgPtr = getInternalMessage(); 72 | CPPEROMQ_ASSERT(nullptr != msgPtr); 73 | return (zmq_msg_size(const_cast(msgPtr))); 74 | } 75 | 76 | inline 77 | auto IncomingMessage::data() const -> const void* 78 | { 79 | const zmq_msg_t* const msgPtr = getInternalMessage(); 80 | CPPEROMQ_ASSERT(nullptr != msgPtr); 81 | return (zmq_msg_data(const_cast(msgPtr))); 82 | } 83 | 84 | inline 85 | auto IncomingMessage::charData() const -> const char* 86 | { 87 | return (static_cast(data())); 88 | } 89 | 90 | inline 91 | auto IncomingMessage::receive(Socket& socket, bool& moreToReceive) -> bool 92 | { 93 | zmq_msg_t* msgPtr = getInternalMessage(); 94 | 95 | CPPEROMQ_ASSERT(nullptr != msgPtr); 96 | CPPEROMQ_ASSERT(nullptr != socket.mSocket); 97 | 98 | if (0 != zmq_msg_close(msgPtr)) 99 | { 100 | throw Error(); 101 | } 102 | 103 | if (0 != zmq_msg_init(msgPtr)) 104 | { 105 | throw Error(); 106 | } 107 | 108 | moreToReceive = false; 109 | 110 | const int flags = 0; 111 | if (zmq_msg_recv(msgPtr, socket.mSocket, flags) >= 0) 112 | { 113 | moreToReceive = (0 != zmq_msg_more(msgPtr)); 114 | return true; 115 | } 116 | 117 | if (zmq_errno() == EAGAIN) 118 | { 119 | return false; 120 | } 121 | 122 | throw Error(); 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /include/CpperoMQ/Message.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | namespace CpperoMQ 28 | { 29 | 30 | class Message 31 | { 32 | public: 33 | virtual ~Message(); 34 | Message(const Message& other) = delete; 35 | Message(Message&& other); 36 | Message& operator=(const Message& other) = delete; 37 | Message& operator=(Message&& other); 38 | 39 | friend auto swap(Message& lhs, Message& rhs) -> void; 40 | 41 | protected: 42 | Message(); 43 | Message(const size_t size, const void* sourceData); 44 | 45 | auto getInternalMessage() const -> const zmq_msg_t* const; 46 | auto getInternalMessage() -> zmq_msg_t*; 47 | 48 | auto shallowCopy(Message& dest) const -> void; 49 | 50 | private: 51 | zmq_msg_t mMsg; 52 | }; 53 | 54 | inline 55 | Message::~Message() 56 | { 57 | int result = zmq_msg_close(&mMsg); 58 | CPPEROMQ_ASSERT(0 == result); 59 | } 60 | 61 | inline 62 | Message::Message(Message&& other) 63 | : Message() 64 | { 65 | swap(*this, other); 66 | } 67 | 68 | inline 69 | Message& Message::operator=(Message&& other) 70 | { 71 | swap(*this, other); 72 | return (*this); 73 | } 74 | 75 | inline 76 | Message::Message() 77 | { 78 | if (0 != zmq_msg_init(&mMsg)) 79 | { 80 | throw Error(); 81 | } 82 | } 83 | 84 | inline 85 | Message::Message(const size_t size, const void* sourceData) 86 | { 87 | CPPEROMQ_ASSERT(nullptr != sourceData); 88 | 89 | if (0 != zmq_msg_init_size(&mMsg, size)) 90 | { 91 | throw Error(); 92 | } 93 | 94 | void* msgData = zmq_msg_data(&mMsg); 95 | CPPEROMQ_ASSERT(nullptr != msgData); 96 | memcpy(msgData, sourceData, size); 97 | } 98 | 99 | inline 100 | auto Message::getInternalMessage() const -> const zmq_msg_t* const 101 | { 102 | return (&mMsg); 103 | } 104 | 105 | inline 106 | auto Message::getInternalMessage() -> zmq_msg_t* 107 | { 108 | return (&mMsg); 109 | } 110 | 111 | inline 112 | auto Message::shallowCopy(Message& dest) const -> void 113 | { 114 | if (0 != zmq_msg_copy(&dest.mMsg, const_cast(&mMsg))) 115 | { 116 | throw Error(); 117 | } 118 | } 119 | 120 | inline 121 | auto swap(Message& lhs, Message& rhs) -> void 122 | { 123 | using std::swap; 124 | swap(lhs.mMsg, rhs.mMsg); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/ConflatingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | namespace Mixins 28 | { 29 | 30 | template 31 | class ConflatingSocket : public S 32 | { 33 | public: 34 | ConflatingSocket() = delete; 35 | virtual ~ConflatingSocket() = default; 36 | ConflatingSocket(const ConflatingSocket& other) = delete; 37 | ConflatingSocket(ConflatingSocket&& other); 38 | ConflatingSocket& operator=(ConflatingSocket& other) = delete; 39 | ConflatingSocket& operator=(ConflatingSocket&& other); 40 | 41 | auto setConflate(const bool conflate) -> void; 42 | 43 | protected: 44 | ConflatingSocket(void* context, int type); 45 | }; 46 | 47 | template 48 | inline 49 | ConflatingSocket::ConflatingSocket(ConflatingSocket&& other) 50 | : S(std::move(other)) 51 | { 52 | } 53 | 54 | template 55 | inline 56 | ConflatingSocket& ConflatingSocket::operator=(ConflatingSocket&& other) 57 | { 58 | S::operator=(std::move(other)); 59 | return (*this); 60 | } 61 | 62 | template 63 | inline 64 | auto ConflatingSocket::setConflate(bool conflate) -> void 65 | { 66 | S::template setSocketOption(ZMQ_CONFLATE, (conflate) ? 1 : 0); 67 | } 68 | 69 | 70 | template 71 | inline 72 | ConflatingSocket::ConflatingSocket(void* context, int type) 73 | : S(context, type) 74 | { 75 | } 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/IdentifyingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | #include 25 | 26 | namespace CpperoMQ 27 | { 28 | namespace Mixins 29 | { 30 | 31 | template 32 | class IdentifyingSocket : public S 33 | { 34 | public: 35 | IdentifyingSocket() = delete; 36 | virtual ~IdentifyingSocket() = default; 37 | IdentifyingSocket(const IdentifyingSocket& other) = delete; 38 | IdentifyingSocket(IdentifyingSocket&& other); 39 | IdentifyingSocket& operator=(IdentifyingSocket& other) = delete; 40 | IdentifyingSocket& operator=(IdentifyingSocket&& other); 41 | 42 | auto setIdentity(const char* buffer = "") -> void; 43 | auto setIdentity(size_t length, const char* buffer) -> void; 44 | 45 | auto getIdentity(size_t length, const char* buffer) -> void; 46 | 47 | protected: 48 | IdentifyingSocket(void* context, int type); 49 | }; 50 | 51 | template 52 | inline 53 | IdentifyingSocket::IdentifyingSocket(IdentifyingSocket&& other) 54 | : S(std::move(other)) 55 | { 56 | } 57 | 58 | template 59 | inline 60 | IdentifyingSocket& IdentifyingSocket::operator=(IdentifyingSocket&& other) 61 | { 62 | S::operator=(std::move(other)); 63 | return (*this); 64 | } 65 | 66 | template 67 | inline 68 | auto IdentifyingSocket::setIdentity(const char* buffer) -> void 69 | { 70 | CPPEROMQ_ASSERT(buffer != nullptr); 71 | setIdentity(std::strlen(buffer), buffer); 72 | } 73 | 74 | template 75 | inline 76 | auto IdentifyingSocket::setIdentity(size_t length, const char* buffer) -> void 77 | { 78 | CPPEROMQ_ASSERT(buffer != nullptr); 79 | S::setSocketOption(ZMQ_IDENTITY, buffer, length); 80 | } 81 | 82 | template 83 | inline 84 | auto IdentifyingSocket::getIdentity( size_t length 85 | , const char* buffer ) -> void 86 | { 87 | CPPEROMQ_ASSERT(buffer != nullptr); 88 | return (S::getSocketOption(ZMQ_IDENTITY, buffer, length)); 89 | } 90 | 91 | template 92 | inline 93 | IdentifyingSocket::IdentifyingSocket(void* context, int type) 94 | : S(context, type) 95 | { 96 | } 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/ReceivingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | namespace CpperoMQ 28 | { 29 | namespace Mixins 30 | { 31 | 32 | template 33 | class ReceivingSocket : public S 34 | { 35 | public: 36 | ReceivingSocket() = delete; 37 | virtual ~ReceivingSocket() = default; 38 | ReceivingSocket(const ReceivingSocket& other) = delete; 39 | ReceivingSocket(ReceivingSocket&& other); 40 | ReceivingSocket& operator=(ReceivingSocket& other) = delete; 41 | ReceivingSocket& operator=(ReceivingSocket&& other); 42 | 43 | template 44 | auto receive( Receivable& receivable 45 | , ReceivableTypes&... receivables ) -> bool; 46 | 47 | auto getMaxInboundMessageSize() const -> int; 48 | auto getReceiveBufferSize() const -> int; 49 | auto getReceiveHighWaterMark() const -> int; 50 | auto getReceiveTimeout() const -> int; 51 | 52 | auto setMaxInboundMessageSize(const int size) -> void; 53 | auto setReceiveBufferSize(const int size) -> void; 54 | auto setReceiveHighWaterMark(const int hwm) -> void; 55 | auto setReceiveTimeout(const int timeout) -> void; 56 | 57 | protected: 58 | ReceivingSocket(void* context, int type); 59 | 60 | private: 61 | // Terminating function for variadic member template. 62 | auto receive() -> bool { return true; } 63 | }; 64 | 65 | template 66 | inline 67 | ReceivingSocket::ReceivingSocket(ReceivingSocket&& other) 68 | : S(std::move(other)) 69 | { 70 | } 71 | 72 | template 73 | inline 74 | ReceivingSocket& ReceivingSocket::operator=(ReceivingSocket&& other) 75 | { 76 | S::operator=(std::move(other)); 77 | return (*this); 78 | } 79 | 80 | template 81 | template 82 | inline 83 | auto ReceivingSocket::receive( Receivable& receivable 84 | , ReceivableTypes&... receivables ) -> bool 85 | { 86 | bool moreToReceive = false; 87 | if (!receivable.receive(*this, moreToReceive)) 88 | { 89 | return false; 90 | } 91 | 92 | const size_t receivablesCount = sizeof...(receivables); 93 | 94 | if (!moreToReceive && (receivablesCount > 0)) 95 | { 96 | return false; 97 | } 98 | 99 | if (moreToReceive && (receivablesCount == 0)) 100 | { 101 | return false; 102 | } 103 | 104 | return (receive(receivables...)); 105 | } 106 | 107 | template 108 | inline 109 | auto ReceivingSocket::getMaxInboundMessageSize() const -> int 110 | { 111 | return (S::template getSocketOption(ZMQ_MAXMSGSIZE)); 112 | } 113 | 114 | template 115 | inline 116 | auto ReceivingSocket::getReceiveBufferSize() const -> int 117 | { 118 | return (S::template getSocketOption(ZMQ_RCVBUF)); 119 | } 120 | 121 | template 122 | inline 123 | auto ReceivingSocket::getReceiveHighWaterMark() const -> int 124 | { 125 | return (S::template getSocketOption(ZMQ_RCVHWM)); 126 | } 127 | 128 | template 129 | inline 130 | auto ReceivingSocket::getReceiveTimeout() const -> int 131 | { 132 | return (S::template getSocketOption(ZMQ_RCVTIMEO)); 133 | } 134 | 135 | template 136 | inline 137 | auto ReceivingSocket::setMaxInboundMessageSize(const int size) -> void 138 | { 139 | S::template setSocketOption(ZMQ_MAXMSGSIZE, size); 140 | } 141 | 142 | template 143 | inline 144 | auto ReceivingSocket::setReceiveBufferSize(const int size) -> void 145 | { 146 | S::template setSocketOption(ZMQ_RCVBUF, size); 147 | } 148 | 149 | template 150 | inline 151 | auto ReceivingSocket::setReceiveHighWaterMark(const int hwm) -> void 152 | { 153 | S::template setSocketOption(ZMQ_RCVHWM, hwm); 154 | } 155 | 156 | template 157 | inline 158 | auto ReceivingSocket::setReceiveTimeout(const int timeout) -> void 159 | { 160 | S::template setSocketOption(ZMQ_RCVTIMEO, timeout); 161 | } 162 | 163 | template 164 | inline 165 | ReceivingSocket::ReceivingSocket(void* context, int type) 166 | : S(context, type) 167 | { 168 | } 169 | 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/RequestingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | namespace Mixins 28 | { 29 | 30 | template 31 | class RequestingSocket : public S 32 | { 33 | public: 34 | RequestingSocket() = delete; 35 | virtual ~RequestingSocket() = default; 36 | RequestingSocket(const RequestingSocket& other) = delete; 37 | RequestingSocket(RequestingSocket&& other); 38 | RequestingSocket& operator=(RequestingSocket& other) = delete; 39 | RequestingSocket& operator=(RequestingSocket&& other); 40 | 41 | auto setRequestCorrelation(bool correlate) -> void; 42 | auto setRelaxed(bool relax) -> void; 43 | 44 | protected: 45 | RequestingSocket(void* context, int type); 46 | }; 47 | 48 | template 49 | inline 50 | RequestingSocket::RequestingSocket(RequestingSocket&& other) 51 | : S(std::move(other)) 52 | { 53 | } 54 | 55 | template 56 | inline 57 | RequestingSocket& RequestingSocket::operator=(RequestingSocket&& other) 58 | { 59 | S::operator=(std::move(other)); 60 | return (*this); 61 | } 62 | 63 | template 64 | inline 65 | auto RequestingSocket::setRequestCorrelation(bool correlate) -> void 66 | { 67 | S::template setSocketOption(ZMQ_REQ_CORRELATE, (correlate) ? 1 : 0); 68 | } 69 | 70 | template 71 | inline 72 | auto RequestingSocket::setRelaxed(bool relax) -> void 73 | { 74 | S::template setSocketOption(ZMQ_REQ_RELAXED, (relax) ? 1 : 0); 75 | } 76 | 77 | template 78 | inline 79 | RequestingSocket::RequestingSocket(void* context, int type) 80 | : S(context, type) 81 | { 82 | } 83 | 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/RouterProbingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | namespace Mixins 28 | { 29 | 30 | template 31 | class RouterProbingSocket : public S 32 | { 33 | public: 34 | RouterProbingSocket() = delete; 35 | virtual ~RouterProbingSocket() = default; 36 | RouterProbingSocket(const RouterProbingSocket& other) = delete; 37 | RouterProbingSocket(RouterProbingSocket&& other); 38 | RouterProbingSocket& operator=(RouterProbingSocket& other) = delete; 39 | RouterProbingSocket& operator=(RouterProbingSocket&& other); 40 | 41 | // Must ONLY be called on sockets that talk to RouterSockets. 42 | auto setRouterProbing(bool routerProbing) -> void; 43 | 44 | protected: 45 | RouterProbingSocket(void* context, int type); 46 | }; 47 | 48 | template 49 | inline 50 | RouterProbingSocket::RouterProbingSocket(RouterProbingSocket&& other) 51 | : S(std::move(other)) 52 | { 53 | } 54 | 55 | template 56 | inline 57 | RouterProbingSocket& RouterProbingSocket::operator=(RouterProbingSocket&& other) 58 | { 59 | S::operator=(std::move(other)); 60 | return (*this); 61 | } 62 | 63 | template 64 | inline 65 | auto RouterProbingSocket::setRouterProbing(bool routerProbing) -> void 66 | { 67 | S::template setSocketOption(ZMQ_PROBE_ROUTER, (routerProbing) ? 1 : 0); 68 | } 69 | 70 | template 71 | inline 72 | RouterProbingSocket::RouterProbingSocket(void* context, int type) 73 | : S(context, type) 74 | { 75 | } 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/RoutingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | namespace Mixins 28 | { 29 | 30 | template 31 | class RoutingSocket : public S 32 | { 33 | public: 34 | RoutingSocket() = delete; 35 | virtual ~RoutingSocket() = default; 36 | RoutingSocket(const RoutingSocket& other) = delete; 37 | RoutingSocket(RoutingSocket&& other); 38 | RoutingSocket& operator=(RoutingSocket& other) = delete; 39 | RoutingSocket& operator=(RoutingSocket&& other); 40 | 41 | auto setHandover(bool handover) -> void; 42 | auto setMandatoryRouting(bool mandatory) -> void; 43 | auto setRawMode(bool rawMode) -> void; 44 | 45 | protected: 46 | RoutingSocket(void* context, int type); 47 | }; 48 | 49 | template 50 | inline 51 | RoutingSocket::RoutingSocket(RoutingSocket&& other) 52 | : S(std::move(other)) 53 | { 54 | } 55 | 56 | template 57 | inline 58 | RoutingSocket& RoutingSocket::operator=(RoutingSocket&& other) 59 | { 60 | S::operator=(std::move(other)); 61 | return (*this); 62 | } 63 | 64 | template 65 | inline 66 | auto RoutingSocket::setHandover(bool handover) -> void 67 | { 68 | S::template setSocketOption(ZMQ_ROUTER_HANDOVER, (handover) ? 1 : 0); 69 | } 70 | 71 | template 72 | inline 73 | auto RoutingSocket::setMandatoryRouting(bool mandatory) -> void 74 | { 75 | S::template setSocketOption(ZMQ_ROUTER_MANDATORY, (mandatory) ? 1 : 0); 76 | } 77 | 78 | template 79 | inline 80 | auto RoutingSocket::setRawMode(bool rawMode) -> void 81 | { 82 | S::template setSocketOption(ZMQ_ROUTER_RAW, (rawMode) ? 1 : 0); 83 | } 84 | 85 | template 86 | inline 87 | RoutingSocket::RoutingSocket(void* context, int type) 88 | : S(context, type) 89 | { 90 | } 91 | 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/SendingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | namespace CpperoMQ 28 | { 29 | namespace Mixins 30 | { 31 | 32 | template 33 | class SendingSocket : public S 34 | { 35 | public: 36 | SendingSocket() = delete; 37 | virtual ~SendingSocket() = default; 38 | SendingSocket(const SendingSocket& other) = delete; 39 | SendingSocket(SendingSocket&& other); 40 | SendingSocket& operator=(SendingSocket& other) = delete; 41 | SendingSocket& operator=(SendingSocket&& other); 42 | 43 | template 44 | auto send(const Sendable& sendable, SendableTypes&&... sendables) const -> bool; 45 | 46 | auto getLingerPeriod() const -> int; 47 | auto getMulticastHops() const -> int; 48 | auto getSendBufferSize() const -> int; 49 | auto getSendHighWaterMark() const -> int; 50 | auto getSendTimeout() const -> int; 51 | 52 | auto setLingerPeriod(const int milliseconds) -> void; 53 | auto setMulticastHops(const int hops) -> void; 54 | auto setSendBufferSize(const int size) -> void; 55 | auto setSendHighWaterMark(const int hwm) -> void; 56 | auto setSendTimeout(const int timeout) -> void; 57 | 58 | protected: 59 | SendingSocket(void* context, int type); 60 | 61 | private: 62 | // Terminating function for variadic member template. 63 | auto send() const -> bool { return true; } 64 | }; 65 | 66 | template 67 | inline 68 | SendingSocket::SendingSocket(SendingSocket&& other) 69 | : S(std::move(other)) 70 | { 71 | } 72 | 73 | template 74 | inline 75 | SendingSocket& SendingSocket::operator=(SendingSocket&& other) 76 | { 77 | S::operator=(std::move(other)); 78 | return (*this); 79 | } 80 | 81 | template 82 | template 83 | inline 84 | auto SendingSocket::send( const Sendable& sendable 85 | , SendableTypes&&... sendables ) const -> bool 86 | { 87 | if (!sendable.send(*this, (sizeof...(sendables) > 0))) 88 | { 89 | return false; 90 | } 91 | 92 | return (send(sendables...)); 93 | } 94 | 95 | template 96 | inline 97 | auto SendingSocket::getLingerPeriod() const -> int 98 | { 99 | return (S::template getSocketOption(ZMQ_LINGER)); 100 | } 101 | 102 | template 103 | inline 104 | auto SendingSocket::getMulticastHops() const -> int 105 | { 106 | return (S::template getSocketOption(ZMQ_MULTICAST_HOPS)); 107 | } 108 | 109 | template 110 | inline 111 | auto SendingSocket::getSendBufferSize() const -> int 112 | { 113 | return (S::template getSocketOption(ZMQ_SNDBUF)); 114 | } 115 | 116 | template 117 | inline 118 | auto SendingSocket::getSendHighWaterMark() const -> int 119 | { 120 | return (S::template getSocketOption(ZMQ_SNDHWM)); 121 | } 122 | 123 | template 124 | inline 125 | auto SendingSocket::getSendTimeout() const -> int 126 | { 127 | return (S::template getSocketOption(ZMQ_SNDTIMEO)); 128 | } 129 | 130 | template 131 | inline 132 | auto SendingSocket::setLingerPeriod(const int milliseconds) -> void 133 | { 134 | S::template setSocketOption(ZMQ_LINGER, milliseconds); 135 | } 136 | 137 | template 138 | inline 139 | auto SendingSocket::setMulticastHops(const int hops) -> void 140 | { 141 | S::template setSocketOption(ZMQ_MULTICAST_HOPS, hops); 142 | } 143 | 144 | template 145 | inline 146 | auto SendingSocket::setSendBufferSize(const int size) -> void 147 | { 148 | S::template setSocketOption(ZMQ_SNDBUF, size); 149 | } 150 | 151 | template 152 | inline 153 | auto SendingSocket::setSendHighWaterMark(const int hwm) -> void 154 | { 155 | S::template setSocketOption(ZMQ_SNDHWM, hwm); 156 | } 157 | 158 | template 159 | inline 160 | auto SendingSocket::setSendTimeout(const int timeout) -> void 161 | { 162 | S::template setSocketOption(ZMQ_SNDTIMEO, timeout); 163 | } 164 | 165 | template 166 | inline 167 | SendingSocket::SendingSocket(void* context, int type) 168 | : S(context, type) 169 | { 170 | } 171 | 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/SocketTypeWrapper.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | 28 | class Context; 29 | 30 | namespace Mixins 31 | { 32 | 33 | template 34 | class SocketTypeWrapper final : public S 35 | { 36 | friend class CpperoMQ::Context; 37 | 38 | public: 39 | SocketTypeWrapper() = delete; 40 | virtual ~SocketTypeWrapper() = default; 41 | SocketTypeWrapper(const SocketTypeWrapper& other) = delete; 42 | SocketTypeWrapper(SocketTypeWrapper&& other); 43 | SocketTypeWrapper& operator=(SocketTypeWrapper& other) = delete; 44 | SocketTypeWrapper& operator=(SocketTypeWrapper&& other); 45 | 46 | private: 47 | SocketTypeWrapper(void* context); 48 | }; 49 | 50 | template 51 | inline 52 | SocketTypeWrapper::SocketTypeWrapper(SocketTypeWrapper&& other) 53 | : S(std::move(other)) 54 | { 55 | } 56 | 57 | template 58 | inline 59 | SocketTypeWrapper& SocketTypeWrapper::operator=(SocketTypeWrapper&& other) 60 | { 61 | S::operator=(std::move(other)); 62 | return (*this); 63 | } 64 | 65 | template 66 | inline 67 | SocketTypeWrapper::SocketTypeWrapper(void* context) 68 | : S(context, TypeId) 69 | { 70 | } 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /include/CpperoMQ/Mixins/SubscribingSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | namespace Mixins 28 | { 29 | 30 | template 31 | class SubscribingSocket : public S 32 | { 33 | public: 34 | SubscribingSocket() = delete; 35 | virtual ~SubscribingSocket() = default; 36 | SubscribingSocket(const SubscribingSocket& other) = delete; 37 | SubscribingSocket(SubscribingSocket&& other); 38 | SubscribingSocket& operator=(SubscribingSocket& other) = delete; 39 | SubscribingSocket& operator=(SubscribingSocket&& other); 40 | 41 | auto subscribe(const char* buffer = "") -> void; 42 | auto subscribe(size_t length, const char* buffer) -> void; 43 | 44 | auto unsubscribe(const char* buffer) -> void; 45 | auto unsubscribe(size_t length, const char* buffer) -> void; 46 | 47 | protected: 48 | SubscribingSocket(void* context, int type); 49 | }; 50 | 51 | template 52 | inline 53 | SubscribingSocket::SubscribingSocket(SubscribingSocket&& other) 54 | : S(std::move(other)) 55 | { 56 | } 57 | 58 | template 59 | inline 60 | SubscribingSocket& SubscribingSocket::operator=(SubscribingSocket&& other) 61 | { 62 | S::operator=(std::move(other)); 63 | return (*this); 64 | } 65 | 66 | template 67 | inline 68 | auto SubscribingSocket::subscribe(const char* buffer) -> void 69 | { 70 | CPPEROMQ_ASSERT(buffer != nullptr); 71 | subscribe(std::strlen(buffer), buffer); 72 | } 73 | 74 | template 75 | inline 76 | auto SubscribingSocket::subscribe(size_t length, const char* buffer) -> void 77 | { 78 | CPPEROMQ_ASSERT(buffer != nullptr); 79 | S::setSocketOption(ZMQ_SUBSCRIBE, buffer, length); 80 | } 81 | 82 | template 83 | inline 84 | auto SubscribingSocket::unsubscribe(const char* buffer) -> void 85 | { 86 | CPPEROMQ_ASSERT(buffer != nullptr); 87 | unsubscribe(std::strlen(buffer), buffer); 88 | } 89 | 90 | template 91 | inline 92 | auto SubscribingSocket::unsubscribe( size_t length 93 | , const char* buffer ) -> void 94 | { 95 | CPPEROMQ_ASSERT(buffer != nullptr); 96 | S::setSocketOption(ZMQ_UNSUBSCRIBE, buffer, length); 97 | } 98 | 99 | template 100 | inline 101 | SubscribingSocket::SubscribingSocket(void* context, int type) 102 | : S(context, type) 103 | { 104 | } 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /include/CpperoMQ/OutgoingMessage.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace CpperoMQ 30 | { 31 | 32 | class OutgoingMessage final : public Message, public Sendable 33 | { 34 | public: 35 | OutgoingMessage(const size_t size, const void* sourceData); 36 | OutgoingMessage(const size_t size, const char* sourceData); 37 | OutgoingMessage(const char* sourceData); 38 | OutgoingMessage(); // for empty frames 39 | virtual ~OutgoingMessage() = default; 40 | OutgoingMessage(const OutgoingMessage& other) = delete; 41 | OutgoingMessage(OutgoingMessage&& other); 42 | OutgoingMessage& operator=(const OutgoingMessage& other) = delete; 43 | OutgoingMessage& operator=(OutgoingMessage&& other); 44 | 45 | virtual auto send(const Socket& socket, const bool moreToSend) const -> bool override; 46 | }; 47 | 48 | inline 49 | OutgoingMessage::OutgoingMessage(const size_t size, const void* sourceData) 50 | : Message(size, sourceData) 51 | { 52 | } 53 | 54 | inline 55 | OutgoingMessage::OutgoingMessage(const size_t size, const char* sourceData) 56 | : Message(size, sourceData) 57 | { 58 | } 59 | 60 | inline 61 | OutgoingMessage::OutgoingMessage(const char* sourceData) 62 | : Message(std::strlen(sourceData), sourceData) 63 | { 64 | } 65 | 66 | inline 67 | OutgoingMessage::OutgoingMessage() 68 | : Message() 69 | { 70 | } 71 | 72 | inline 73 | OutgoingMessage::OutgoingMessage(OutgoingMessage&& other) 74 | : Message(std::move(other)) 75 | { 76 | } 77 | 78 | inline 79 | OutgoingMessage& OutgoingMessage::operator=(OutgoingMessage&& other) 80 | { 81 | Message::operator=(std::move(other)); 82 | return (*this); 83 | } 84 | 85 | inline 86 | auto OutgoingMessage::send(const Socket& socket, const bool moreToSend) const -> bool 87 | { 88 | zmq_msg_t* msgPtr = const_cast(getInternalMessage()); 89 | 90 | CPPEROMQ_ASSERT(nullptr != msgPtr); 91 | CPPEROMQ_ASSERT(nullptr != socket.mSocket); 92 | 93 | OutgoingMessage message; 94 | shallowCopy(message); 95 | msgPtr = message.getInternalMessage(); 96 | 97 | CPPEROMQ_ASSERT(nullptr != msgPtr); 98 | 99 | const int flags = (moreToSend) ? ZMQ_SNDMORE : 0; 100 | if (zmq_msg_send(msgPtr, socket.mSocket, flags) >= 0) 101 | { 102 | return true; 103 | } 104 | 105 | if (zmq_errno() == EAGAIN) 106 | { 107 | return false; 108 | } 109 | 110 | throw Error(); 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /include/CpperoMQ/PollItem.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | 37 | namespace CpperoMQ 38 | { 39 | 40 | class PollItem 41 | { 42 | public: 43 | using Callback = std::function; 44 | 45 | virtual ~PollItem() = default; 46 | PollItem(const PollItem& other) = delete; 47 | PollItem(PollItem&& other); 48 | PollItem& operator=(const PollItem& other) = delete; 49 | PollItem& operator=(PollItem&& other) = delete; 50 | 51 | auto getEvents() const -> int; 52 | 53 | auto getRawSocket() const -> const void*; 54 | auto getRawSocket() -> void*; 55 | 56 | auto getCallback() -> Callback; 57 | 58 | protected: 59 | PollItem(int events, Socket& socket, Callback callable = Callback()); 60 | 61 | private: 62 | PollItem(int events, Socket* socket, Callback callable = Callback()); 63 | 64 | int mEvents; 65 | Socket* mSocketPtr; 66 | Callback mCallable; 67 | }; 68 | 69 | inline 70 | PollItem::PollItem(PollItem&& other) 71 | : PollItem(0, nullptr, Callback()) 72 | { 73 | using std::swap; 74 | swap(mEvents, other.mEvents); 75 | swap(mSocketPtr, other.mSocketPtr); 76 | swap(mCallable, other.mCallable); 77 | } 78 | 79 | inline 80 | PollItem::PollItem(int events, Socket& socket, Callback callable) 81 | : PollItem(events, &socket, callable) 82 | { 83 | } 84 | 85 | inline 86 | PollItem::PollItem(int events, Socket* socket, Callback callable) 87 | : mEvents(events) 88 | , mSocketPtr(socket) 89 | , mCallable(callable) 90 | { 91 | } 92 | 93 | inline 94 | auto PollItem::getEvents() const -> int 95 | { 96 | return mEvents; 97 | } 98 | 99 | inline 100 | auto PollItem::getRawSocket() const -> const void* 101 | { 102 | return (mSocketPtr) ? static_cast(*mSocketPtr) : nullptr; 103 | } 104 | 105 | inline 106 | auto PollItem::getRawSocket() -> void* 107 | { 108 | return (mSocketPtr) ? static_cast(*mSocketPtr) : nullptr; 109 | } 110 | 111 | inline 112 | auto PollItem::getCallback() -> PollItem::Callback 113 | { 114 | return mCallable; 115 | } 116 | 117 | template 118 | class IsReceiveReady : public PollItem 119 | { 120 | // This is ugly, but mixins make it tough to use std::is_base_of. 121 | static_assert( std::is_same::value || 122 | std::is_same::value || 123 | std::is_same::value || 124 | std::is_same::value || 125 | std::is_same::value || 126 | std::is_same::value || 127 | std::is_same::value 128 | , "Template parameter 'S' must inherit ReceivingSocket mixin." ); 129 | 130 | public: 131 | IsReceiveReady(S& socket, Callback callable = Callback()); 132 | virtual ~IsReceiveReady() = default; 133 | IsReceiveReady(const IsReceiveReady& other) = delete; 134 | IsReceiveReady(IsReceiveReady&& other); 135 | IsReceiveReady& operator=(const IsReceiveReady& other) = delete; 136 | IsReceiveReady& operator=(IsReceiveReady&& other) = delete; 137 | }; 138 | 139 | template 140 | inline 141 | IsReceiveReady::IsReceiveReady(S& socket, Callback callable) 142 | : PollItem(ZMQ_POLLIN, socket, callable) 143 | { 144 | } 145 | 146 | template 147 | inline 148 | IsReceiveReady::IsReceiveReady(IsReceiveReady&& other) 149 | : PollItem(std::move(other)) 150 | { 151 | } 152 | 153 | template 154 | class IsSendReady : public PollItem 155 | { 156 | // This is ugly, but mixins make it tough to use std::is_base_of. 157 | static_assert( std::is_same::value || 158 | std::is_same::value || 159 | std::is_same::value || 160 | std::is_same::value || 161 | std::is_same::value || 162 | std::is_same::value || 163 | std::is_same::value 164 | , "Template parameter 'S' must inherit SendingSocket mixin." ); 165 | 166 | public: 167 | IsSendReady(S& socket, Callback callable = Callback()); 168 | virtual ~IsSendReady() = default; 169 | IsSendReady(const IsSendReady& other) = delete; 170 | IsSendReady(IsSendReady&& other); 171 | IsSendReady& operator=(const IsSendReady& other) = delete; 172 | IsSendReady& operator=(IsSendReady&& other) = delete; 173 | }; 174 | 175 | template 176 | inline 177 | IsSendReady::IsSendReady(S& socket, Callback callable) 178 | : PollItem(ZMQ_POLLOUT, socket, callable) 179 | { 180 | } 181 | 182 | template 183 | inline 184 | IsSendReady::IsSendReady(IsSendReady&& other) 185 | : PollItem(std::move(other)) 186 | { 187 | } 188 | 189 | template 190 | class IsSendOrReceiveReady : public PollItem 191 | { 192 | // This is ugly, but mixins make it tough to use std::is_base_of. 193 | static_assert( std::is_same::value || 194 | std::is_same::value || 195 | std::is_same::value || 196 | std::is_same::value 197 | , "Template parameter 'S' must inherit ReceivingSocket and SendingSocket mixins." ); 198 | 199 | public: 200 | IsSendOrReceiveReady(S& socket, Callback callable = Callback()); 201 | virtual ~IsSendOrReceiveReady() = default; 202 | IsSendOrReceiveReady(const IsSendOrReceiveReady& other) = delete; 203 | IsSendOrReceiveReady(IsSendOrReceiveReady&& other); 204 | IsSendOrReceiveReady& operator=(const IsSendOrReceiveReady& other) = delete; 205 | IsSendOrReceiveReady& operator=(IsSendOrReceiveReady&& other) = delete; 206 | }; 207 | 208 | template 209 | inline 210 | IsSendOrReceiveReady::IsSendOrReceiveReady(S& socket, Callback callable) 211 | : PollItem(ZMQ_POLLIN | ZMQ_POLLOUT, socket, callable) 212 | { 213 | } 214 | 215 | template 216 | inline 217 | IsSendOrReceiveReady::IsSendOrReceiveReady(IsSendOrReceiveReady&& other) 218 | : PollItem(std::move(other)) 219 | { 220 | } 221 | 222 | template 223 | inline 224 | auto isReceiveReady(S& socket, PollItem::Callback callable = PollItem::Callback()) -> IsReceiveReady 225 | { 226 | IsReceiveReady receiveReady(socket, callable); 227 | return (receiveReady); 228 | } 229 | 230 | template 231 | inline 232 | auto isSendReady(S& socket, PollItem::Callback callable = PollItem::Callback()) -> IsSendReady 233 | { 234 | IsSendReady sendReady(socket, callable); 235 | return (sendReady); 236 | } 237 | 238 | template 239 | inline 240 | auto isSendOrReceiveReady(S& socket, PollItem::Callback callable = PollItem::Callback()) -> IsSendOrReceiveReady 241 | { 242 | IsSendOrReceiveReady sendOrReceiveReady(socket, callable); 243 | return (sendOrReceiveReady); 244 | } 245 | 246 | } 247 | -------------------------------------------------------------------------------- /include/CpperoMQ/Poller.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | #include 28 | 29 | namespace CpperoMQ 30 | { 31 | 32 | class Poller 33 | { 34 | public: 35 | Poller(const long timeout = -1); 36 | 37 | auto getTimeout() const -> long; 38 | auto setTimeout(const long timeout) -> void; 39 | 40 | template 41 | auto poll(PollItem& pollItem, PollItemTypes&... pollItems) -> void; 42 | 43 | private: 44 | template 45 | auto poll( std::array& pollItemArray 46 | , std::array& callbackArray 47 | , PollItem& pollItem 48 | , PollItemTypes&... pollItems ) -> void; 49 | 50 | template 51 | auto poll( std::array& pollItemArray 52 | , std::array& callbackArray ) -> void; 53 | 54 | long mTimeout; 55 | }; 56 | 57 | inline 58 | Poller::Poller(const long timeout) 59 | : mTimeout(timeout) 60 | { 61 | } 62 | 63 | inline 64 | auto Poller::getTimeout() const -> long 65 | { 66 | return mTimeout; 67 | } 68 | 69 | inline 70 | auto Poller::setTimeout(const long timeout) -> void 71 | { 72 | mTimeout = timeout; 73 | } 74 | 75 | template 76 | inline 77 | auto Poller::poll(PollItem& pollItem, PollItemTypes&... pollItems) -> void 78 | { 79 | std::array pollItemArray; 80 | std::array callbackArray; 81 | 82 | poll(pollItemArray, callbackArray, pollItem, pollItems...); 83 | } 84 | 85 | template 86 | inline 87 | auto Poller::poll( std::array& pollItemArray 88 | , std::array& callbackArray 89 | , PollItem& pollItem 90 | , PollItemTypes&... pollItems ) -> void 91 | { 92 | CPPEROMQ_ASSERT(nullptr != pollItem.getRawSocket()); 93 | 94 | zmq_pollitem_t internalPollItem; 95 | internalPollItem.socket = static_cast(pollItem.getRawSocket()); 96 | internalPollItem.fd = 0; 97 | internalPollItem.events = pollItem.getEvents(); 98 | internalPollItem.revents = 0; 99 | 100 | std::get(pollItemArray) = internalPollItem; 101 | std::get(callbackArray) = pollItem.getCallback(); 102 | 103 | poll(pollItemArray, callbackArray, pollItems...); 104 | } 105 | 106 | template 107 | inline 108 | auto Poller::poll( std::array& pollItemArray 109 | , std::array& callbackArray ) -> void 110 | { 111 | if (zmq_poll(pollItemArray.data(), N, mTimeout) < 0) 112 | { 113 | throw Error(); 114 | } 115 | 116 | for (size_t i = 0; i < N; ++i) 117 | { 118 | if (callbackArray[i]) 119 | { 120 | if (pollItemArray[i].revents & ZMQ_POLLIN) 121 | { 122 | callbackArray[i](); 123 | } 124 | else if (pollItemArray[i].revents & ZMQ_POLLOUT) 125 | { 126 | callbackArray[i](); 127 | } 128 | } 129 | } 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /include/CpperoMQ/Proxy.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | namespace CpperoMQ 35 | { 36 | 37 | class Proxy 38 | { 39 | public: 40 | Proxy(); 41 | ~Proxy() = default; 42 | Proxy(const Proxy& other) = delete; 43 | Proxy(Proxy&& other); 44 | Proxy& operator=(const Proxy& other) = delete; 45 | Proxy& operator=(Proxy&& other); 46 | 47 | friend auto swap(Proxy& lhs, Proxy& rhs) -> void; 48 | 49 | template 50 | auto run(S1& frontend, S2& backend) -> bool; 51 | 52 | template 53 | auto setCaptureSocket(S& socket) -> void; 54 | 55 | template 56 | auto setControlSocket(S& socket) -> void; 57 | 58 | private: 59 | void* mCaptureSocketPtr; 60 | void* mControlSocketPtr; 61 | }; 62 | 63 | inline 64 | Proxy::Proxy() 65 | : mCaptureSocketPtr(nullptr) 66 | , mControlSocketPtr(nullptr) 67 | { 68 | } 69 | 70 | inline 71 | Proxy::Proxy(Proxy&& other) 72 | : Proxy() 73 | { 74 | swap(*this, other); 75 | } 76 | 77 | inline 78 | Proxy& Proxy::operator=(Proxy&& other) 79 | { 80 | swap(*this, other); 81 | return (*this); 82 | } 83 | 84 | template 85 | inline 86 | auto Proxy::run(S1& frontend, S2& backend) -> bool 87 | { 88 | static_assert( (std::is_same::value && std::is_same::value) || 89 | (std::is_same::value && std::is_same::value) || 90 | 91 | (std::is_same::value && std::is_same::value) || 92 | (std::is_same::value && std::is_same::value) || 93 | 94 | (std::is_same::value && std::is_same::value) || 95 | (std::is_same::value && std::is_same::value) 96 | 97 | , "Template parameters 'S1' and 'S2' must be Router/Dealer, ExtendedSubscribe/ExtendedPublish, " 98 | "or Pull/Push." ); 99 | 100 | int result = zmq_proxy_steerable( 101 | static_cast(frontend), 102 | static_cast(backend), 103 | mCaptureSocketPtr, 104 | mControlSocketPtr 105 | ); 106 | 107 | return (mControlSocketPtr) ? (result == 0) : true; 108 | } 109 | 110 | template 111 | inline 112 | auto Proxy::setCaptureSocket(S& socket) -> void 113 | { 114 | static_assert( std::is_same::value || 115 | std::is_same::value || 116 | std::is_same::value 117 | , "Template parameter 'S' must be DealerSocket, " 118 | "PublishSocket, or PushSocket." ); 119 | 120 | mCaptureSocketPtr = static_cast(socket); 121 | } 122 | 123 | template 124 | inline 125 | auto Proxy::setControlSocket(S& socket) -> void 126 | { 127 | static_assert( std::is_same::value || 128 | std::is_same::value 129 | , "Template parameter 'S' must be PullSocket or " 130 | "SubscribeSocket." ); 131 | 132 | mControlSocketPtr = static_cast(socket); 133 | } 134 | 135 | inline 136 | auto swap(Proxy& lhs, Proxy& rhs) -> void 137 | { 138 | using std::swap; 139 | swap(lhs.mCaptureSocketPtr, rhs.mCaptureSocketPtr); 140 | swap(lhs.mControlSocketPtr, rhs.mControlSocketPtr); 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /include/CpperoMQ/PublishSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace CpperoMQ 31 | { 32 | 33 | typedef Mixins::SocketTypeWrapper > > PublishSocket; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/PullSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace CpperoMQ 31 | { 32 | 33 | typedef Mixins::SocketTypeWrapper > > PullSocket; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/PushSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace CpperoMQ 31 | { 32 | 33 | typedef Mixins::SocketTypeWrapper > > PushSocket; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/Receivable.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | 28 | class Receivable 29 | { 30 | friend class Socket; 31 | 32 | public: 33 | virtual ~Receivable() {} 34 | 35 | virtual auto receive(Socket& socket, bool& moreToReceive) -> bool = 0; 36 | }; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/ReplySocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | namespace CpperoMQ 32 | { 33 | 34 | typedef Mixins::SocketTypeWrapper > > > ReplySocket; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /include/CpperoMQ/RequestSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace CpperoMQ 34 | { 35 | 36 | typedef Mixins::SocketTypeWrapper > > > > > RequestSocket; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /include/CpperoMQ/RouterSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace CpperoMQ 34 | { 35 | 36 | typedef Mixins::SocketTypeWrapper > > > > > RouterSocket; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /include/CpperoMQ/Sendable.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | namespace CpperoMQ 26 | { 27 | 28 | class Sendable 29 | { 30 | friend class Socket; 31 | 32 | public: 33 | virtual ~Sendable() {} 34 | 35 | virtual auto send(const Socket& socket, const bool moreToSend) const -> bool = 0; 36 | }; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /include/CpperoMQ/Socket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | namespace CpperoMQ 31 | { 32 | 33 | class Socket 34 | { 35 | friend class IncomingMessage; 36 | friend class OutgoingMessage; 37 | 38 | public: 39 | Socket() = delete; 40 | virtual ~Socket(); 41 | Socket(const Socket& other) = delete; 42 | Socket& operator=(Socket& other) = delete; 43 | 44 | auto bind(const char* address) -> void; 45 | auto unbind(const char* address) -> void; 46 | 47 | auto connect(const char* address) -> void; 48 | auto disconnect(const char* address) -> void; 49 | 50 | auto getBacklog() const -> int; 51 | auto getHandshakeInterval() const -> int; 52 | auto getImmediate() const -> bool; 53 | auto getIoThreadAffinity() const -> uint64_t; 54 | auto getIPv6() const -> bool; 55 | auto getLastEndpoint(size_t length, char* buffer) const -> void; 56 | auto getMaxReconnectInterval() const -> int; 57 | auto getMulticastRate() const -> int; 58 | auto getMulticastRecoveryInterval() const -> int; 59 | auto getReconnectInterval() const -> int; 60 | 61 | auto setBacklog(const int backlog) -> void; 62 | auto setHandshakeInterval(const int milliseconds) -> void; 63 | auto setImmediate(const bool immediate) -> void; 64 | auto setIoThreadAffinity(const uint64_t affinity) -> void; 65 | auto setIPv6(const bool ipv6) -> void; 66 | auto setMaxReconnectInterval(const int milliseconds) -> void; 67 | auto setMulticastRate(const int kbps) -> void; 68 | auto setMulticastRecoveryInterval(const int milliseconds) -> void; 69 | auto setReconnectInterval(const int milliseconds) -> void; 70 | 71 | explicit operator void*(); 72 | 73 | protected: 74 | Socket(void* context, int type); 75 | Socket(Socket&& other); 76 | Socket& operator=(Socket&& other); 77 | 78 | template 79 | auto getSocketOption(const int option) const -> T; 80 | auto getSocketOption( const int option 81 | , void* value 82 | , size_t* valueLength ) const -> void; 83 | 84 | template 85 | auto setSocketOption(const int option, const T value) -> void; 86 | auto setSocketOption( const int option 87 | , const void* value 88 | , const size_t valueLength ) -> void; 89 | 90 | private: 91 | void* mSocket; 92 | }; 93 | 94 | inline 95 | Socket::~Socket() 96 | { 97 | if (mSocket != nullptr) 98 | { 99 | int result = zmq_close(mSocket); 100 | CPPEROMQ_ASSERT(result == 0); 101 | mSocket = 0 ; 102 | } 103 | } 104 | 105 | inline 106 | auto Socket::bind(const char* address) -> void 107 | { 108 | if (0 != zmq_bind(mSocket, address)) 109 | { 110 | throw Error(); 111 | } 112 | } 113 | 114 | inline 115 | auto Socket::unbind(const char* address) -> void 116 | { 117 | if (0 != zmq_unbind(mSocket, address)) 118 | { 119 | throw Error(); 120 | } 121 | } 122 | 123 | inline 124 | auto Socket::connect(const char* address) -> void 125 | { 126 | if (0 != zmq_connect(mSocket, address)) 127 | { 128 | throw Error(); 129 | } 130 | } 131 | 132 | inline 133 | auto Socket::disconnect(const char* address) -> void 134 | { 135 | if (0 != zmq_disconnect(mSocket, address)) 136 | { 137 | throw Error(); 138 | } 139 | } 140 | 141 | inline 142 | Socket::Socket(void* context, int type) 143 | : mSocket(nullptr) 144 | { 145 | CPPEROMQ_ASSERT(context != nullptr); 146 | 147 | mSocket = zmq_socket(context, type); 148 | if (mSocket == NULL) 149 | { 150 | throw Error(); 151 | } 152 | } 153 | 154 | inline 155 | Socket::Socket(Socket&& other) 156 | : mSocket(other.mSocket) 157 | { 158 | other.mSocket = nullptr; 159 | } 160 | 161 | inline 162 | Socket& Socket::operator=(Socket&& other) 163 | { 164 | using std::swap; 165 | swap(mSocket, other.mSocket); 166 | return (*this); 167 | } 168 | 169 | inline 170 | auto Socket::getBacklog() const -> int 171 | { 172 | return (getSocketOption(ZMQ_BACKLOG)); 173 | } 174 | 175 | inline 176 | auto Socket::getHandshakeInterval() const -> int 177 | { 178 | return (getSocketOption(ZMQ_HANDSHAKE_IVL)); 179 | } 180 | 181 | inline 182 | auto Socket::getImmediate() const -> bool 183 | { 184 | return (getSocketOption(ZMQ_IMMEDIATE)); 185 | } 186 | 187 | inline 188 | auto Socket::getIoThreadAffinity() const -> uint64_t 189 | { 190 | return (getSocketOption(ZMQ_AFFINITY)); 191 | } 192 | 193 | inline 194 | auto Socket::getIPv6() const -> bool 195 | { 196 | return (getSocketOption(ZMQ_IPV6)); 197 | } 198 | 199 | inline 200 | auto Socket::getLastEndpoint(size_t length, char* buffer) const -> void 201 | { 202 | CPPEROMQ_ASSERT(buffer != nullptr); 203 | memset(buffer, 0, length); 204 | return (getSocketOption(ZMQ_LAST_ENDPOINT, buffer, &length)); 205 | } 206 | 207 | inline 208 | auto Socket::getMaxReconnectInterval() const -> int 209 | { 210 | return (getSocketOption(ZMQ_RECONNECT_IVL_MAX)); 211 | } 212 | 213 | inline 214 | auto Socket::getMulticastRate() const -> int 215 | { 216 | return (getSocketOption(ZMQ_RATE)); 217 | } 218 | 219 | inline 220 | auto Socket::getMulticastRecoveryInterval() const -> int 221 | { 222 | return (getSocketOption(ZMQ_RECOVERY_IVL)); 223 | } 224 | 225 | inline 226 | auto Socket::getReconnectInterval() const -> int 227 | { 228 | return (getSocketOption(ZMQ_RECONNECT_IVL)); 229 | } 230 | 231 | inline 232 | auto Socket::setBacklog(const int backlog) -> void 233 | { 234 | setSocketOption(ZMQ_BACKLOG, backlog); 235 | } 236 | 237 | inline 238 | auto Socket::setHandshakeInterval(const int milliseconds) -> void 239 | { 240 | setSocketOption(ZMQ_HANDSHAKE_IVL, milliseconds); 241 | } 242 | 243 | inline 244 | auto Socket::setImmediate(const bool immediate) -> void 245 | { 246 | setSocketOption(ZMQ_IMMEDIATE, (immediate) ? 1 : 0); 247 | } 248 | 249 | inline 250 | auto Socket::setIoThreadAffinity(const uint64_t affinity) -> void 251 | { 252 | setSocketOption(ZMQ_AFFINITY, affinity); 253 | } 254 | 255 | inline 256 | auto Socket::setIPv6(const bool ipv6) -> void 257 | { 258 | setSocketOption(ZMQ_IPV6, (ipv6) ? 1 : 0); 259 | } 260 | 261 | inline 262 | auto Socket::setMulticastRate(const int kbps) -> void 263 | { 264 | setSocketOption(ZMQ_RATE, kbps); 265 | } 266 | 267 | inline 268 | auto Socket::setMulticastRecoveryInterval(const int milliseconds) -> void 269 | { 270 | setSocketOption(ZMQ_RECOVERY_IVL, milliseconds); 271 | } 272 | 273 | inline 274 | auto Socket::setMaxReconnectInterval(const int milliseconds) -> void 275 | { 276 | setSocketOption(ZMQ_RECONNECT_IVL_MAX, milliseconds); 277 | } 278 | 279 | inline 280 | auto Socket::setReconnectInterval(const int milliseconds) -> void 281 | { 282 | setSocketOption(ZMQ_RECONNECT_IVL, milliseconds); 283 | } 284 | 285 | inline 286 | Socket::operator void*() 287 | { 288 | return mSocket; 289 | } 290 | 291 | template 292 | inline 293 | auto Socket::getSocketOption(const int option) const -> T 294 | { 295 | T value; 296 | size_t valueLength = sizeof(T); 297 | getSocketOption(option, &value, &valueLength); 298 | return value; 299 | } 300 | 301 | inline 302 | auto Socket::getSocketOption( const int option 303 | , void* value 304 | , size_t* valueLength ) const -> void 305 | { 306 | if (0 != zmq_getsockopt(mSocket, option, value, valueLength)) 307 | { 308 | throw Error(); 309 | } 310 | } 311 | 312 | template 313 | inline 314 | auto Socket::setSocketOption(const int option, const T value) -> void 315 | { 316 | setSocketOption(option, &value, sizeof(value)); 317 | } 318 | 319 | inline 320 | auto Socket::setSocketOption( const int option 321 | , const void* value 322 | , const size_t valueLength ) -> void 323 | { 324 | if (0 != zmq_setsockopt(mSocket, option, value, valueLength)) 325 | { 326 | throw Error(); 327 | } 328 | } 329 | 330 | } 331 | -------------------------------------------------------------------------------- /include/CpperoMQ/SubscribeSocket.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | namespace CpperoMQ 32 | { 33 | 34 | typedef Mixins::SocketTypeWrapper > > > SubscribeSocket; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /include/CpperoMQ/Version.hpp: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2015 Jason Shipman 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to 7 | // deal in the Software without restriction, including without limitation the 8 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9 | // sell copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 | // IN THE SOFTWARE. 22 | 23 | #pragma once 24 | 25 | #include 26 | 27 | #include 28 | 29 | namespace CpperoMQ 30 | { 31 | 32 | class Version 33 | { 34 | public: 35 | Version(); 36 | 37 | auto getTuple() const -> std::tuple; 38 | auto getMajor() const -> int; 39 | auto getMinor() const -> int; 40 | auto getPatch() const -> int; 41 | 42 | private: 43 | int mMajor; 44 | int mMinor; 45 | int mPatch; 46 | }; 47 | 48 | inline 49 | Version::Version() 50 | : mMajor(0) 51 | , mMinor(0) 52 | , mPatch(0) 53 | { 54 | zmq_version(&mMajor, &mMinor, &mPatch); 55 | } 56 | 57 | inline 58 | auto Version::getTuple() const -> std::tuple 59 | { 60 | return std::make_tuple(mMajor, mMinor, mPatch); 61 | } 62 | 63 | inline 64 | auto Version::getMajor() const -> int 65 | { 66 | return mMajor; 67 | } 68 | 69 | inline 70 | auto Version::getMinor() const -> int 71 | { 72 | return mMinor; 73 | } 74 | 75 | inline 76 | auto Version::getPatch() const -> int 77 | { 78 | return mPatch; 79 | } 80 | 81 | } 82 | --------------------------------------------------------------------------------