README 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. nanomsgxx
  2. =========
  3. nanomsgxx is a binding of the nanomsg library for C++11.
  4. *The library is still under development and changes may be brought to the API.*
  5. Building and installing
  6. -----------------------
  7. ### Building and installing with waf
  8. nanomsgxx's build is driven by waf, you can get more information about
  9. what waf is and how it works [here](https://waf.io/book/).
  10. waf is packaged with nanomsgxx, all you should need is a python interpreter and
  11. running these commands:
  12. ```
  13. ./waf configure
  14. ./waf build
  15. ./waf install
  16. ```
  17. The library and headers will be installed on your system and you'll be able to
  18. link your program against **libnanomsgxx**.
  19. ### Building and installing with CMake
  20. nanomsgxx also supports CMake builds on various architectures (tested with Windows 10 + VisualStudio2017 and Ubuntu16.04 + g++/clang/c++).
  21. - install [CMake](https://cmake.org/) and a *CMake generator*, e.g. [GNU make](https://www.gnu.org/software/make/). See [here](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) for a list of available CMake generators and how to use them.
  22. #### On Linux
  23. ```bash
  24. mkdir -p build && cd $_
  25. # available build types: Debug, Release
  26. # install location may be adapted with -DCMAKE_INSTALL_PREFIX=path/to/install/nanomsgxx
  27. cmake -DCMAKE_BUILD_TYPE=Release ..
  28. # build example with GNU make
  29. make
  30. # run tests
  31. make test
  32. # install to default location (warning: uninstalling is not that easy)
  33. sudo make install
  34. sudo ldconfig
  35. ```
  36. #### On Windows using Visual Studio 2017
  37. Since VisualStudio 2017 CMake is supported (see this [tutorial](https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/)).
  38. ```cmd
  39. mkdir build
  40. cd build
  41. cmake -G "Visual Studio 15 2017" ..
  42. ```
  43. This will generate a VisualStudio 2017 `nanomsgxx.sln` file in folder `build/`. Building project `ALL_BUILD` will build the whole solution. Building project `INSTALL` (with VS2017 started as administrator) will install the project.
  44. Getting started
  45. ---------------
  46. nanomsgxx aims to provide a very thin abstraction on top of the nanomsg C API,
  47. while taking advantage of C++11's features to make the code easier to read and
  48. write.
  49. **Quick example**
  50. ```c++
  51. #include <iostream>
  52. #include <system_error>
  53. #include <nnxx/message.h>
  54. #include <nnxx/pair.h>
  55. #include <nnxx/socket.h>
  56. int main() {
  57. try {
  58. nnxx::socket s1 { nnxx::SP, nnxx::PAIR };
  59. nnxx::socket s2 { nnxx::SP, nnxx::PAIR };
  60. const char *addr = "inproc://example";
  61. s1.bind(addr);
  62. s2.connect(addr);
  63. s1.send("Hello World!");
  64. nnxx::message msg = s2.recv();
  65. std::cout << msg << std::endl;
  66. return 0;
  67. }
  68. catch (const std::system_error &e) {
  69. std::cerr << e.what() << std::endl;
  70. return 1;
  71. }
  72. }
  73. ```
  74. **What did we write?**
  75. You've probably recognized most of these calls if you're familiar with nanomsg's
  76. API. nanomsgxx uses the *nnxx* namespace, here we have...
  77. - declared two socket objects in the *SP* domain using the *PAIR* protcol
  78. and connected them together
  79. - sent *"Hello World!"* from the first socket to the second
  80. - used the *nnxx::socket::recv* method to retrieve the message on the
  81. receiver side
  82. - printed the received message to stdout
  83. **A few highlights**
  84. - as you can expect from a C++ abstraction there's no need to manually tell when
  85. to release resources, this is handled automatically in the destructors
  86. - the *nnxx::message* type automatically manages buffers for zero-copy, making
  87. high performance code easy to write.
  88. - error cases are reported throught exceptions that are subclasses of
  89. *std::system_error*
  90. **The next step**
  91. If you're getting excited about using nanomsgxx in your next C++ project then
  92. give the documentation a look and learn more about how it abstracts the pain
  93. away from building messages, polling, timeouts and more...
  94. The library provides many useful abstractions that make developing with nanomsg
  95. easy and safe.
  96. Resources
  97. ---------
  98. Documentation: http://achille-roussel.github.io/nanomsgxx
  99. nanomsg website: http://nanomsg.org/index.html
  100. nanomsg sources: https://github.com/nanomsg/nanomsg