Giter VIP home page Giter VIP logo

zmqcpp's Introduction

ZMQ C++ wrapper (with no added magic)

Welcome to the super-thin C++ wrapper for zmq version 1.1.0.

Features

  • Very simple to understand and use.
  • Design based on python bindings.
  • Resource management (zmq messages, sockets, context, etc) taken care of.
  • Messages easily constructed by streaming in types.

Usage

To connect to another zmq socket and send a message:

#include "zmqcpp.h"

using namespace zmqcpp;

Context ctx;
Socket socket(ctx, xreq);
socket.setsockopt(linger, 0);
socket.connect("tcp://127.0.0.1:4050");

Message msg;
msg << "first part" << 123 << "third part";

if (!socket.send(msg))
  std::cout << "Send error:" << socket.last_error() << "\n";

To bind a zmq socket and receive a message:

#include "zmqcpp.h"

using namespace zmqcpp;

Context ctx;
Socket socket(ctx, xrep);
socket.bind("tpc://*:4050");

Message msg;
if (!socket.recv(msg)) {
  std::cout << "Recv error:" << socket.last_error() << "\n";
}
else {
  std::string first;
  int32_t second;
  std::string third;

  msg >> first >> second >> third;

...

}

To poll a zmq socket:

#include "zmqcpp.h"

using namespace zmqcpp;

Context ctx;
Socket socket(ctx, xrep);
socket.bind("tpc://*:4050");

Poller poller;
poller.add(socket, pollin);

Message msg;

while (true) {
poller.poll(1000); // 1 second timeout
  if (poller.has_polled(socket)) {
    socket.recv(msg);
    // do something with msg here
  }
}

To receive a callback when a zmq socket is polled:

#include "zmqcpp.h"

using namespace zmqcpp;

void socket_callback(const Socket& socket) {
  Message msg;
  socket.recv(msg);
  // doc something with msg here
}

...

Context ctx;
Socket socket(ctx, xrep);
socket.bind("tpc://*:4050");

Poller poller;
poller.add(socket, pollin, socket_callback);

while (true) {
  poller.poll(1000); // 1 second timeout
  // callback function called here if polled
}

For more examples, including copying messages and constructing messages from custom data types, then check out the unit tests.

Build Instructions using CMake

Simply run CMake to generate the required Makefile or project and build the unit test application test_zmqcpp.

zmqcpp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zmqcpp's Issues

brew install --HEAD zmqcpp fails

==> Cloning git://github.com/alanw/zmqcpp.git
Cloning into '/Library/Caches/Homebrew/zmqcpp--git'...
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 15 (delta 0), reused 8 (delta 0)
Receiving objects: 100% (15/15), 16.81 KiB | 0 bytes/s, done.
Checking connectivity... done.
==> cmake . -DCMAKE_INSTALL_PREFIX='/usr/local/Cellar/zmqcpp/HEAD' -DCMAKE_BUILD_TYPE=None -DCMAKE_FIND_FRAMEWORK=LAST -Wno-dev
==> make install


make[2]: *** [ThirdParty/src/googlemock-stamp/googlemock-build] Error 1
make[1]: *** [test/CMakeFiles/googlemock.dir/all] Error 2
make: *** [all] Error 2

looks like the error is in googlemock:

$ cat /Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock-stamp/googlemock-build-err.log

In file included from /Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/src/gmock-all.cc:40:
In file included from /Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/gmock.h:58:
In file included from /Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/gmock-actions.h:46:
/Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/internal/gmock-internal-utils.h:453:27: error: use of undeclared identifier 'RelationToSourceReference'
    return type(array, N, RelationToSourceReference());
                          ^
/Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/internal/gmock-internal-utils.h:460:27: error: use of undeclared identifier 'RelationToSourceCopy'
    return type(array, N, RelationToSourceCopy());
                          ^
/Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/internal/gmock-internal-utils.h:477:47: error: use of undeclared identifier 'RelationToSourceReference'
    return type(get<0>(array), get<1>(array), RelationToSourceReference());
                                              ^
/Library/Caches/Homebrew/zmqcpp--git/ThirdParty/src/googlemock/include/gmock/internal/gmock-internal-utils.h:480:47: error: use of undeclared identifier 'RelationToSourceCopy'
    return type(get<0>(array), get<1>(array), RelationToSourceCopy());
                                              ^
4 errors generated.
make[5]: *** [CMakeFiles/gmock.dir/src/gmock-all.cc.o] Error 1
make[4]: *** [CMakeFiles/gmock.dir/all] Error 2
make[3]: *** [all] Error 2

can zmqcpp be pinned to a buildable version of googlemock?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.