Ejemplo n.º 1
0
int main()
{
    using namespace CpperoMQ;

    // Prepare our context and sockets.
    Context context;
    RouterSocket frontend(context.createRouterSocket());
    DealerSocket backend(context.createDealerSocket());

    frontend.bind("tcp://*:5559");
    backend.bind("tcp://*:5560");

    auto frontendPollItem = isReceiveReady(frontend, [&frontend, &backend]()
    {
        bool more = true;

        while (more)
        {
            // Process all parts of the message.
            IncomingMessage inMsg;
            inMsg.receive(frontend, more);

            OutgoingMessage outMsg(inMsg.size(), inMsg.data());
            outMsg.send(backend, more);
        }
    });

    auto backendPollItem = isReceiveReady(backend, [&frontend, &backend]()
    {
        bool more = true;

        while (more)
        {
            // Process all parts of the message.
            IncomingMessage inMsg;
            inMsg.receive(backend, more);

            OutgoingMessage outMsg(inMsg.size(), inMsg.data());
            outMsg.send(frontend, more);
        }
    });

    Poller poller;
    while (true)
    {
        poller.poll(frontendPollItem, backendPollItem);
    }

    return 0;
}
Ejemplo n.º 2
0
void Poller::onData(uv_poll_t* handle, int status, int events) {
  Nan::HandleScope scope;
  Poller* obj = static_cast<Poller*>(handle->data);
  v8::Local<v8::Value> argv[2];
  if (0 != status) {
    // fprintf(stdout, "OnData Error status=%s events=%d\n", uv_strerror(status), events);
    argv[0] = v8::Exception::Error(Nan::New<v8::String>(uv_strerror(status)).ToLocalChecked());
    argv[1] = Nan::Undefined();
  } else {
    // fprintf(stdout, "OnData status=%d events=%d\n", status, events);
    argv[0] = Nan::Null();
    argv[1] = Nan::New<v8::Integer>(events);
  }
  // remove triggered events from the poll
  int newEvents = obj->events & ~events;
  obj->poll(newEvents);

  obj->callback.Call(2, argv);
}