zmq_msg_t& message::raw_new_msg() { parts_type tmp(_parts.size() + 1); for(size_t i = 0; i < _parts.size(); ++i) { zmq_msg_t& dest = tmp[i].msg; if( 0 != zmq_msg_init(&dest) ) { throw zmq_internal_exception(); } zmq_msg_t& src = _parts[i].msg; if( 0 != zmq_msg_move(&dest, &src) ) { throw zmq_internal_exception(); } } std::swap(tmp, _parts); zmq_msg_t& msg = _parts.back().msg; if( 0 != zmq_msg_init(&msg) ) { throw zmq_internal_exception(); } return msg; }
// Move operators will take ownership of message parts without copying void message::move(void* part, size_t& size, release_function const& release) { parts_type tmp(_parts.size() + 1); for(size_t i = 0; i < _parts.size(); ++i) { zmq_msg_t& dest = tmp[i].msg; if( 0 != zmq_msg_init(&dest) ) { throw zmq_internal_exception(); } zmq_msg_t& src = _parts[i].msg; if( 0 != zmq_msg_move(&dest, &src) ) { throw zmq_internal_exception(); } } std::swap(tmp, _parts); callback_releaser* hint = new callback_releaser(); hint->func = release; zmq_msg_t& msg = _parts.back().msg; if (0 != zmq_msg_init_data(&msg, part, size, &message::release_callback, hint)) { throw zmq_internal_exception(); } }
void message::add(void const* part, size_t const& size) { parts_type tmp(_parts.size() + 1); for(size_t i = 0; i < _parts.size(); ++i) { zmq_msg_t& dest = tmp[i].msg; if( 0 != zmq_msg_init(&dest) ) { throw zmq_internal_exception(); } zmq_msg_t& src = _parts[i].msg; if( 0 != zmq_msg_move(&dest, &src) ) { throw zmq_internal_exception(); } } std::swap(tmp, _parts); zmq_msg_t& msg = _parts.back().msg; if( 0 != zmq_msg_init_size(&msg, size) ) { throw zmq_internal_exception(); } void* msg_data = zmq_msg_data(&msg); memcpy(msg_data, part, size); }
int zmq::xrep_t::xrecv (zmq_msg_t *msg_, int flags_) { // If there is a prefetched message, return it. if (prefetched) { zmq_msg_move (msg_, &prefetched_msg); more_in = msg_->flags & ZMQ_MSG_MORE; prefetched = false; return 0; } // Deallocate old content of the message. zmq_msg_close (msg_); // If we are in the middle of reading a message, just grab next part of it. if (more_in) { zmq_assert (inpipes [current_in].active); bool fetched = inpipes [current_in].reader->read (msg_); zmq_assert (fetched); more_in = msg_->flags & ZMQ_MSG_MORE; if (!more_in) { current_in++; if (current_in >= inpipes.size ()) current_in = 0; } return 0; } // Round-robin over the pipes to get the next message. for (int count = inpipes.size (); count != 0; count--) { // Try to fetch new message. if (inpipes [current_in].active) prefetched = inpipes [current_in].reader->read (&prefetched_msg); // If we have a message, create a prefix and return it to the caller. if (prefetched) { int rc = zmq_msg_init_size (msg_, inpipes [current_in].identity.size ()); zmq_assert (rc == 0); memcpy (zmq_msg_data (msg_), inpipes [current_in].identity.data (), zmq_msg_size (msg_)); msg_->flags |= ZMQ_MSG_MORE; return 0; } // If me don't have a message, mark the pipe as passive and // move to next pipe. inpipes [current_in].active = false; current_in++; if (current_in >= inpipes.size ()) current_in = 0; } // No message is available. Initialise the output parameter // to be a 0-byte message. zmq_msg_init (msg_); errno = EAGAIN; return -1; }
ZMessage& operator=(ZMessage&& m) { if (this != &m) { zmq_msg_move(this, &m); } return *this; }
static int luazmq_msg_move(lua_State *L){ zmessage *dst, *src; int err; if(lua_gettop(L) == 1){ src = luazmq_getmessage_at(L, 1); dst = luazmq_newudata(L, zmessage, LUAZMQ_MESSAGE); err = zmq_msg_init(&dst->msg); if(-1 == err) return luazmq_fail(L, NULL); } else{ dst = luazmq_getmessage_at(L, 1); src = luazmq_getmessage_at(L, 2); lua_pushvalue(L, 1); // result } err = zmq_msg_move(&dst->msg, &src->msg); if(-1 == err) return luazmq_fail(L, NULL); return 1; }
static int luazmq_msg_set_size(lua_State *L){ zmessage *zmsg = luazmq_getmessage(L); size_t nsize = luaL_checkinteger(L, 2); size_t osize = zmq_msg_size(&zmsg->msg); int err; zmq_msg_t msg; if(nsize == osize) return luazmq_pass(L); err = zmq_msg_init_size(&msg, nsize); if(-1 == err)return luazmq_fail(L, NULL); memcpy(zmq_msg_data(&msg), zmq_msg_data(&zmsg->msg), (nsize>osize)?osize:nsize); err = zmq_msg_move(&zmsg->msg, &msg); if(-1 == err){ zmq_msg_close(&msg); return luazmq_fail(L, NULL); } zmq_msg_close(&msg); // @FIXME do not close message return luazmq_pass(L); }
int zmq::xsub_t::xrecv (zmq_msg_t *msg_, int flags_) { // If there's already a message prepared by a previous call to zmq_poll, // return it straight ahead. if (has_message) { zmq_msg_move (msg_, &message); has_message = false; more = msg_->flags & ZMQ_MSG_MORE; return 0; } // TODO: This can result in infinite loop in the case of continuous // stream of non-matching messages which breaks the non-blocking recv // semantics. while (true) { // Get a message using fair queueing algorithm. int rc = fq.recv (msg_, flags_); // If there's no message available, return immediately. // The same when error occurs. if (rc != 0) return -1; // Check whether the message matches at least one subscription. // Non-initial parts of the message are passed if (more || match (msg_)) { more = msg_->flags & ZMQ_MSG_MORE; return 0; } // Message doesn't match. Pop any remaining parts of the message // from the pipe. while (msg_->flags & ZMQ_MSG_MORE) { rc = fq.recv (msg_, ZMQ_NOBLOCK); zmq_assert (rc == 0); } } }
static int luazmq_msg_set_data(lua_State *L){ zmessage *zmsg = luazmq_getmessage(L); int start_pos = (lua_gettop(L) >= 3)?luaL_optint(L,2,1):1; size_t size; const char *data = luaL_checklstring(L, (lua_gettop(L) >= 3)?3:2, &size); int err; luaL_argcheck(L, start_pos >= 0, 2, "can not be negative or zero"); start_pos = start_pos - 1; if((start_pos + size) > zmq_msg_size(&zmsg->msg)){ zmq_msg_t msg; err = zmq_msg_init_size(&msg, start_pos + size); if(-1 == err)return luazmq_fail(L, NULL); memcpy(zmq_msg_data(&msg), zmq_msg_data(&zmsg->msg), zmq_msg_size(&zmsg->msg)); err = zmq_msg_move(&zmsg->msg, &msg); if(-1 == err){ zmq_msg_close(&msg); return luazmq_fail(L, NULL); } zmq_msg_close(&msg); // @FIXME do not close message } memcpy( (char*)zmq_msg_data(&zmsg->msg) + start_pos, data, size); return luazmq_pass(L); }
bool reply_message::construct(libfault::zmq_msg_vector& msg) { clear(); if(msg.size() != 3) return false; // first block is the reply status if (zmq_msg_size(msg.front()) != sizeof(reply_status)) return false; status = *reinterpret_cast<reply_status*>(zmq_msg_data(msg.front())); msg.pop_front_and_free(); // second block is the property bag char* propertybuf = (char*)zmq_msg_data(msg.front()); size_t propertybuflen = zmq_msg_size(msg.front()); graphlab::iarchive iarc(propertybuf, propertybuflen); iarc >> properties; msg.pop_front_and_free(); // third block is the serialization body zmq_msg_init(&bodybuf); zmq_msg_move(&bodybuf, msg.front()); body = (char*)zmq_msg_data(&bodybuf); bodylen = zmq_msg_size(&bodybuf); zmqbodyused = true; msg.pop_front_and_free(); // no free this time since we are keeping a pointer return true; }
int move(ZmqPortableBytes& pb_) { return zmq_msg_move(&msg, pb_.get_msg_ptr()); }
ZMessage(ZMessage&& m) : ZMessage() { zmq_msg_move(this, &m); }
inline void move (message_t *msg_) { int rc = zmq_msg_move (&msg, &(msg_->msg)); if (rc != 0) throw error_t (); }
inline void move (message_t const *msg_) { int rc = zmq_msg_move (&msg, const_cast<zmq_msg_t*>(&(msg_->msg))); if (rc != 0) throw error_t (); }
ZMQ_EXPORT int WINAPI mql4zmq_msg_move (zmq_msg_t *dest, zmq_msg_t *src) { return zmq_msg_move(dest, src); }
inline void move (message_t *msg_) { int rc = zmq_msg_move (this, (zmq_msg_t*) msg_); if (rc != 0) throw error_t (); }
frame& operator=(frame const& other) { frame tmp(other); zmq_msg_move(&raw_msg_, &tmp.raw_msg_); return *this; }
frame& operator=(frame&& other) noexcept { zmq_msg_move(&raw_msg_, &other.raw_msg_); return *this; }
frame(frame&& other) noexcept : frame() { zmq_msg_move(&raw_msg_, &other.raw_msg_); }