├── README.md ├── AUTHORS ├── README ├── COPYING ├── binding.cpp ├── CMakeLists.txt └── nn.hpp /README.md: -------------------------------------------------------------------------------- 1 | README -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | What follows is complete list of copyright holders to cppnanomsg codebase: 2 | 3 | Martin Sustrik 4 | Steve Atkins 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | cppnanomsg: C++ binding for nanomsg library 2 | ------------------------------------------- 3 | 4 | To build: 5 | 6 | 1. git clone git@github.com:nanomsg/cppnanomsg.git 7 | 2. cd cppnanomsg 8 | 4. mkdir build 9 | 5. cd build 10 | 6. cmake .. 11 | 7. make 12 | 8. make test 13 | 9. sudo make install 14 | 15 | Alternatively, given that the binding consists of a single header file, 16 | rather than installing it, you can avoid dependency and make nn.hpp part 17 | of your project. 18 | 19 | 20 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy 3 | of this software and associated documentation files (the "Software"), 4 | to deal in the Software without restriction, including without limitation 5 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 6 | and/or sell copies of the Software, and to permit persons to whom 7 | the Software is furnished to do so, subject to the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be included 10 | in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 18 | IN THE SOFTWARE. 19 | 20 | "nanomsg" is a trademark of 250bpm s.r.o. 21 | 22 | -------------------------------------------------------------------------------- /binding.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013 250bpm s.r.o. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), 6 | to deal in the Software without restriction, including without limitation 7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | and/or sell copies of the Software, and to permit persons to whom 9 | the Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | IN THE SOFTWARE. 21 | */ 22 | 23 | #include "nn.hpp" 24 | 25 | #include 26 | 27 | #include 28 | 29 | int main () 30 | { 31 | nn::socket s1 (AF_SP, NN_PAIR); 32 | s1.bind ("inproc://a"); 33 | nn::socket s2 (AF_SP, NN_PAIR); 34 | s2.connect ("inproc://a"); 35 | 36 | s2.send ("ABC", 3, 0); 37 | char buf [3]; 38 | int rc = s1.recv (buf, sizeof (buf), 0); 39 | assert (rc == 3); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2013 250bpm s.r.o. 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), 6 | # to deal in the Software without restriction, including without limitation 7 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | # and/or sell copies of the Software, and to permit persons to whom 9 | # the Software is furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | # IN THE SOFTWARE. 21 | # 22 | 23 | cmake_minimum_required (VERSION 2.8) 24 | project (cppnanomsg) 25 | 26 | # The test. 27 | 28 | enable_testing () 29 | set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) 30 | add_executable (binding binding.cpp) 31 | target_link_libraries (binding nanomsg) 32 | add_test (test ${CMAKE_BINARY_DIR}/binding) 33 | 34 | # There's only a single header file to install. 35 | 36 | install (FILES nn.hpp DESTINATION include/nanomsg) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /nn.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013 250bpm s.r.o. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), 6 | to deal in the Software without restriction, including without limitation 7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | and/or sell copies of the Software, and to permit persons to whom 9 | the Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | IN THE SOFTWARE. 21 | */ 22 | 23 | #ifndef NN_HPP_INCLUDED 24 | #define NN_HPP_INCLUDED 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #if defined __GNUC__ 34 | #define nn_slow(x) __builtin_expect ((x), 0) 35 | #else 36 | #define nn_slow(x) (x) 37 | #endif 38 | 39 | namespace nn 40 | { 41 | 42 | class exception : public std::exception 43 | { 44 | public: 45 | 46 | exception () : err (nn_errno ()) {} 47 | 48 | virtual const char *what () const throw () 49 | { 50 | return nn_strerror (err); 51 | } 52 | 53 | int num () const 54 | { 55 | return err; 56 | } 57 | 58 | private: 59 | 60 | int err; 61 | }; 62 | 63 | inline const char *symbol (int i, int *value) 64 | { 65 | return nn_symbol (i, value); 66 | } 67 | 68 | inline void *allocmsg (size_t size, int type) 69 | { 70 | void *msg = nn_allocmsg (size, type); 71 | if (nn_slow (!msg)) 72 | throw nn::exception (); 73 | return msg; 74 | } 75 | 76 | inline int freemsg (void *msg) 77 | { 78 | int rc = nn_freemsg (msg); 79 | if (nn_slow (rc != 0)) 80 | throw nn::exception (); 81 | return rc; 82 | } 83 | 84 | class socket 85 | { 86 | public: 87 | 88 | inline socket (int domain, int protocol) 89 | { 90 | s = nn_socket (domain, protocol); 91 | if (nn_slow (s < 0)) 92 | throw nn::exception (); 93 | } 94 | 95 | inline ~socket () 96 | { 97 | int rc = nn_close (s); 98 | assert (rc == 0); 99 | } 100 | 101 | inline void setsockopt (int level, int option, const void *optval, 102 | size_t optvallen) 103 | { 104 | int rc = nn_setsockopt (s, level, option, optval, optvallen); 105 | if (nn_slow (rc != 0)) 106 | throw nn::exception (); 107 | } 108 | 109 | inline void getsockopt (int level, int option, void *optval, 110 | size_t *optvallen) 111 | { 112 | int rc = nn_getsockopt (s, level, option, optval, optvallen); 113 | if (nn_slow (rc != 0)) 114 | throw nn::exception (); 115 | } 116 | 117 | inline int bind (const char *addr) 118 | { 119 | int rc = nn_bind (s, addr); 120 | if (nn_slow (rc < 0)) 121 | throw nn::exception (); 122 | return rc; 123 | } 124 | 125 | inline int connect (const char *addr) 126 | { 127 | int rc = nn_connect (s, addr); 128 | if (nn_slow (rc < 0)) 129 | throw nn::exception (); 130 | return rc; 131 | } 132 | 133 | inline void shutdown (int how) 134 | { 135 | int rc = nn_shutdown (s, how); 136 | if (nn_slow (rc != 0)) 137 | throw nn::exception (); 138 | } 139 | 140 | inline int send (const void *buf, size_t len, int flags) 141 | { 142 | int rc = nn_send (s, buf, len, flags); 143 | if (nn_slow (rc < 0)) { 144 | if (nn_slow (nn_errno () != EAGAIN)) 145 | throw nn::exception (); 146 | return -1; 147 | } 148 | return rc; 149 | } 150 | 151 | inline int recv (void *buf, size_t len, int flags) 152 | { 153 | int rc = nn_recv (s, buf, len, flags); 154 | if (nn_slow (rc < 0)) { 155 | if (nn_slow (nn_errno () != EAGAIN)) 156 | throw nn::exception (); 157 | return -1; 158 | } 159 | return rc; 160 | } 161 | 162 | inline int sendmsg (const struct nn_msghdr *msghdr, int flags) 163 | { 164 | int rc = nn_sendmsg (s, msghdr, flags); 165 | if (nn_slow (rc < 0)) { 166 | if (nn_slow (nn_errno () != EAGAIN)) 167 | throw nn::exception (); 168 | return -1; 169 | } 170 | return rc; 171 | } 172 | 173 | inline int recvmsg (struct nn_msghdr *msghdr, int flags) 174 | { 175 | int rc = nn_recvmsg (s, msghdr, flags); 176 | if (nn_slow (rc < 0)) { 177 | if (nn_slow (nn_errno () != EAGAIN)) 178 | throw nn::exception (); 179 | return -1; 180 | } 181 | return rc; 182 | } 183 | 184 | private: 185 | 186 | int s; 187 | 188 | /* Prevent making copies of the socket by accident. */ 189 | socket (const socket&); 190 | void operator = (const socket&); 191 | }; 192 | 193 | inline void term () 194 | { 195 | nn_term (); 196 | } 197 | 198 | } 199 | 200 | #undef nn_slow 201 | 202 | #endif 203 | 204 | 205 | --------------------------------------------------------------------------------