Exemple #1
0
  /*
   * return number of bytes received, negative if error, 0 for received nothing,
   *  which should be treated as error
   */
  int32_t RecvMsg(zmq::socket_t &sock, boost::shared_array<uint8_t> &data,
      bool *_more){
    zmq::message_t msgt;
    int nbytes;
    try{
      nbytes = sock.recv(&msgt);
    }catch(zmq::error_t &e){
      LOG(ERROR) << "RecvMsg error = " << e.what();
      return -1;
    }

    if(nbytes == 0) return 0;

    size_t len = msgt.size();    
    uint8_t *dataptr;
    try{
      dataptr = new uint8_t[len];
    }catch(std::bad_alloc &e){
      return -1;
    }
    
    memcpy(dataptr, msgt.data(), len);
    data.reset(dataptr);

    if(_more != NULL){
      int more_recv;
      size_t s = sizeof(int);
      sock.getsockopt(ZMQ_RCVMORE, &more_recv, &s);
      *_more = (more_recv == 1) ? true : false;
    }
    return len;
  }
Exemple #2
0
   bool recv(zmq::socket_t & socket) {
      clear();
      while(1) {
         zmq::message_t message(0);
         try {
            if (!socket.recv(&message, 0)) {
               return false;
            }
         } catch (zmq::error_t error) {
            //std::cout << "E: " << error.what() << std::endl;
            return false;
         }
         char *data = reinterpret_cast<char*>(message.data());
         if (message.size() == 17 && data[0] == 0) {
            push_back(encode_uuid(msg_to_string(message)));
         } else {
            push_back(msg_to_string(message));
         }
		 boost::int64_t more;
         size_t more_size = sizeof(more);
         socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
         if (!more) {
            break;
         }
      }
      return true;
   }
Exemple #3
0
    Message receive(zmq::socket_t& socket)
    {
      std::vector<std::string> address;
      std::string frame = receiveString(socket);
      // Do we have an address?
      while (frame.compare("") != 0)
      {
        address.push_back(frame);
        frame = receiveString(socket);
      }
      // address is a stack, so reverse it to get the right way around
      std::reverse(address.begin(), address.end());

      // We've just read the delimiter, so now get subject
      auto subjectString = receiveString(socket);
      //the underlying representation is (explicitly) an int so fairly safe
      Subject subject = (Subject)detail::unserialise<std::uint32_t>(subjectString);
      std::vector<std::string> data;
      while (true)
      {
        int isMore = 0;
        size_t moreSize = sizeof(isMore);
        socket.getsockopt(ZMQ_RCVMORE, &isMore, &moreSize);
        if (!isMore)
          break;
        data.push_back(receiveString(socket));
      }
      return Message(address, subject, data);
    }
Exemple #4
0
void receiveAllStr(zmq::socket_t& socket, string& s, int flags = 0) {
	int64_t more = 1;
	size_t moreSize = sizeof(int64_t);
	while (more == 1) {
		receiveStr(socket, s, flags);
		socket.getsockopt(ZMQ_RCVMORE, &more, &moreSize);
	}
}
 bool
 has_more(zmq::socket_t& sock)
 {
   int64_t more = 0;
   size_t more_size = sizeof(more);
   sock.getsockopt(ZMQ_RCVMORE, &more, &more_size);
   return (more != 0);
 }
bool sockopt_rcvmore(zmq::socket_t & socket)
{
    int64_t rcvmore = 0;
    size_t type_size = sizeof(int64_t);
    socket.getsockopt(ZMQ_RCVMORE, &rcvmore, &type_size);
    // std::cout << rcvmore << std::endl;
    return rcvmore ? true : false;
}
Exemple #7
0
bool Server::getRequest (zmq::socket_t &socket, pb::RequestEnvelope &envelope) {
  long long more;
  size_t sz = sizeof (more);
  zmq::message_t request;
  
  socket.recv (&request);
  socket.getsockopt (ZMQ_RCVMORE, &more, &sz);
  bool isAnotherRequest = more != 0;
  envelope.ParseFromArray (request.data(), request.size());
  
  return isAnotherRequest;
}
Exemple #8
0
Fichier : zmq.cpp Projet : plq/zmq
void recv_multipart(zmq::socket_t &socket) {
    int64_t more=-1;
    size_t more_size = sizeof (more);
    while(more) {
        zmq::message_t response;
        socket.recv(&response);

        std::string response_str((const char *)response.data(),
                                                             response.size());
        cout << response_str << endl;

        socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
    }
}
// relay data from workers to frontend
void process_backend(zmq::socket_t& frontend, zmq::socket_t& backend) {
  int64_t more;
  size_t moresz;
  moresz = sizeof(more);

  while(true) {
    zmq::message_t worker_msg;
    backend.recv(&worker_msg);
    backend.getsockopt(ZMQ_RCVMORE, &more, &moresz);
    frontend.send(worker_msg, more ? ZMQ_SNDMORE : 0);
    if(!more) {
      break;
    }
  }
}
Exemple #10
0
 bool MultipartMessage::receiveFrom(zmq::socket_t& s, int flags) {
     int64_t has_more = 0 ;
     size_t ilen = sizeof(int64_t);
     parts.clear();
     while (true) {
         Message m ;
         bool res = m.receiveFrom(s, flags);
         if (!res) {
             parts.clear();
             return res ;
         }
         parts.push_back(m);
         s.getsockopt(ZMQ_RCVMORE, &has_more, &ilen);
         if (!has_more)
             break ;
     }
     return true ;
 }
Exemple #11
0
// job allocation, relay request to workers
void process_frontend(zmq::socket_t& frontend, zmq::socket_t& backend, int& number_of_workers) {
  int64_t more;
  size_t moresz;
  moresz = sizeof(more);

  // decrement available workers
  --number_of_workers;

  // process incoming using multipart aware code
  while(true) {
    zmq::message_t client_msg;
    frontend.recv(&client_msg);
    frontend.getsockopt(ZMQ_RCVMORE, &more, &moresz);
    backend.send(client_msg, more ? ZMQ_SNDMORE : 0);
    if(!more) {
      break;
    }
  }
}
//  Receives all message parts from socket, prints neatly
//
static void
s_dump (zmq::socket_t & socket)
{
    std::cout << "----------------------------------------" << std::endl;

    while (1) {
        //  Process all parts of the message
        zmq::message_t message;
        socket.recv(&message);

        //  Dump the message as text or binary
        int size = message.size();
        std::string data(static_cast<char*>(message.data()), size);

        bool is_text = true;

        int char_nbr;
        unsigned char byte;
        for (char_nbr = 0; char_nbr < size; char_nbr++) {
            byte = data [char_nbr];
            if (byte < 32 || byte > 127)
                is_text = false;
        }
        std::cout << "[" << std::setfill('0') << std::setw(3) << size << "]";
        for (char_nbr = 0; char_nbr < size; char_nbr++) {
            if (is_text)
                std::cout << (char)data [char_nbr];
            else
                std::cout << std::setfill('0') << std::setw(2)
                   << std::hex << (unsigned int) data [char_nbr];
        }
        std::cout << std::endl;

        int more = 0;           //  Multipart detection
        size_t more_size = sizeof (more);
        socket.getsockopt (ZMQ_RCVMORE, &more, &more_size);
        if (!more)
            break;              //  Last message part
    }
}
Exemple #13
0
//  Receives all message parts from socket, prints neatly
//
static void
s_dump (zmq::socket_t & socket)
{
    std::cout << "----------------------------------------" << std::endl;

    while (1) {
        //  Process all parts of the message

		zmq::message_t message;
        socket.recv(&message);

        //  Dump the message as text or binary
        std::string data(static_cast<char*>(message.data()));
        int size = message.size();

        bool is_text = true;

        int char_nbr;
        for (char_nbr = 0; char_nbr < size; char_nbr++)
            if (data [char_nbr] < 32 || data [char_nbr] > 127)
                is_text = 0;

        printf ("[%03d] ", size);

        for (char_nbr = 0; char_nbr < size; char_nbr++) {
            if (is_text)
                printf ("%c", data [char_nbr]);
            else
                printf ("%02X", (unsigned char) data [char_nbr]);
        }
        printf ("\n");

        int64_t more;           //  Multipart detection
        size_t more_size = sizeof (more);
        socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);

        if (!more)
            break;      //  Last message part
    }
}
Exemple #14
0
bool TransferMessage(::zmq::socket_t &receiver, ::zmq::socket_t &sender)
{
    message_t msg;
    int32 more;
    size_t moresz;

    while (true) {
        if (!receiver.recv(&msg, 0)) {
            return false;
        }

        moresz = sizeof(more);
        receiver.getsockopt(ZMQ_RCVMORE, &more, &moresz);

        if (!sender.send(msg, more ? ZMQ_SNDMORE : 0)) {
            return false;
        }

        if (!more) break;
    }

    return true;
}
Exemple #15
0
  // 0 for received nothing
  int32_t RecvMsgAsync(zmq::socket_t &sock, boost::shared_array<uint8_t> &data,
      bool *_more){
    zmq::message_t msgt;
    int nbytes;
    try{
      nbytes = sock.recv(&msgt, ZMQ_DONTWAIT);
    }catch(zmq::error_t &e){
      return -1;
    }

    if(nbytes == 0){
      if(zmq_errno() == EAGAIN)
        return 0;
      else
        return -1;
    }

    size_t len = msgt.size();
    uint8_t *dataptr;
    try{
      dataptr = new uint8_t[len];
    }catch(std::bad_alloc &e){
      return -1;
    }
    memcpy(dataptr, msgt.data(), len);
    data.reset(dataptr);

    if(_more != NULL){
      int more_recv;
      size_t s = sizeof(int);
      sock.getsockopt(ZMQ_RCVMORE, &more_recv, &s);
      *_more = (more_recv == 1) ? true : false;
    }

    return len;
  }
Exemple #16
0
bool ReceiveMessage(::zmq::socket_t &socket, MessageContainer &container, int flags)
{
    container.Clear();

    bool in_identity = true;

    while (true) {
        message_t *msg = new message_t();
        if (!socket.recv(msg, flags)) {
            delete msg;
            return false;
        }

        if (msg->size() == 0) {
            in_identity = false;
            delete msg;
            continue;
        }

        if (in_identity) {
            container.AddIdentity(msg);
        }
        else {
            container.AddMessage(msg);
        }

        int32 more;
        size_t moresz;
        moresz = sizeof(more);
        socket.getsockopt(ZMQ_RCVMORE, &more, &moresz);

        if (!more) break;
    }

    return true;
}