本文整理汇总了C++中zmq_msg_close函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_msg_close函数的具体用法?C++ zmq_msg_close怎么用?C++ zmq_msg_close使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_msg_close函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: zmq_send
int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
{
zmq_msg_t msg;
int rc = zmq_msg_init_size (&msg, len_);
if (rc != 0)
return -1;
memcpy (zmq_msg_data (&msg), buf_, len_);
rc = zmq_sendmsg (s_, &msg, flags_);
if (unlikely (rc < 0)) {
int err = errno;
int rc2 = zmq_msg_close (&msg);
errno_assert (rc2 == 0);
errno = err;
return -1;
}
// Note the optimisation here. We don't close the msg object as it is
// empty anyway. This may change when implementation of zmq_msg_t changes.
return rc;
}
示例2: zmq_msg_close
int zmq::fq_t::recv (zmq_msg_t *msg_, int flags_)
{
// Deallocate old content of the message.
zmq_msg_close (msg_);
// Round-robin over the pipes to get next message.
for (int count = active; count != 0; count--) {
bool fetched = pipes [current]->read (msg_);
current++;
if (current >= active)
current = 0;
if (fetched)
return 0;
}
// No message is available. Initialise the output parameter
// to be a 0-byte message.
zmq_msg_init (msg_);
errno = EAGAIN;
return -1;
}
示例3: pollitem_on_input
static int pollitem_on_input(void *socket, void *ehub, void *data) {
zmq_msg_t msg;
int r, n;
char *message;
r = zmq_msg_init(&msg);
if (r==-1) return zmq_errno();
n = zmq_msg_recv(&msg, socket, 0);
if (n==-1) return zmq_errno();
message = malloc(n + 1);
if (!message) return ENOMEM;
memcpy(message, zmq_msg_data(&msg), n);
r = zmq_msg_close(&msg);
if (r==-1) return zmq_errno();
ehub_on_input(ehub, socket, message, data);
free(message);
}
示例4: zmq_recviov
// Receive a multi-part message
//
// Receives up to *count_ parts of a multi-part message.
// Sets *count_ to the actual number of parts read.
// ZMQ_RCVMORE is set to indicate if a complete multi-part message was read.
// Returns number of message parts read, or -1 on error.
//
// Note: even if -1 is returned, some parts of the message
// may have been read. Therefore the client must consult
// *count_ to retrieve message parts successfully read,
// even if -1 is returned.
//
// The iov_base* buffers of each iovec *a_ filled in by this
// function may be freed using free().
//
// Implementation note: We assume zmq::msg_t buffer allocated
// by zmq::recvmsg can be freed by free().
// We assume it is safe to steal these buffers by simply
// not closing the zmq::msg_t.
//
int zmq_recviov (void *s_, iovec *a_, size_t *count_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
size_t count = (int) *count_;
int nread = 0;
bool recvmore = true;
for (size_t i = 0; recvmore && i < count; ++i) {
// Cheat! We never close any msg
// because we want to steal the buffer.
zmq_msg_t msg;
int rc = zmq_msg_init (&msg);
errno_assert (rc == 0);
int nbytes = s_recvmsg (s, &msg, flags_);
if (unlikely (nbytes < 0)) {
int err = errno;
rc = zmq_msg_close (&msg);
errno_assert (rc == 0);
errno = err;
nread = -1;
break;
}
++*count_;
++nread;
// Cheat: acquire zmq_msg buffer.
a_[i].iov_base = static_cast<char *> (zmq_msg_data (&msg));
a_[i].iov_len = zmq_msg_size (&msg);
// Assume zmq_socket ZMQ_RVCMORE is properly set.
recvmore = ((zmq::msg_t*) (void *) &msg)->flags () & zmq::msg_t::more;
}
return nread;
}
示例5: R_zmq_msg_recv
SEXP R_zmq_msg_recv(SEXP R_socket, SEXP R_flags){
SEXP R_rmsg = R_NilValue;
int C_rmsg_length;
int C_ret = -1, C_errno, C_flags = INTEGER(R_flags)[0];
void *C_socket = R_ExternalPtrAddr(R_socket);
zmq_msg_t msg;
if(C_socket != NULL){
C_ret = zmq_msg_init(&msg);
if(C_ret == -1){
C_errno = zmq_errno();
REprintf("R_zmq_msg_init errno: %d strerror: %s\n",
C_errno, zmq_strerror(C_errno));
}
C_ret = zmq_msg_recv(&msg, C_socket, C_flags);
if(C_ret == -1){
C_errno = zmq_errno();
REprintf("R_zmq_msg_recv errno: %d strerror: %s\n",
C_errno, zmq_strerror(C_errno));
}
C_rmsg_length = zmq_msg_size(&msg);
PROTECT(R_rmsg = allocVector(RAWSXP, C_rmsg_length));
memcpy(RAW(R_rmsg), zmq_msg_data(&msg), C_rmsg_length);
C_ret = zmq_msg_close(&msg);
if(C_ret == -1){
C_errno = zmq_errno();
REprintf("R_zmq_msg_close errno: %d strerror: %s\n",
C_errno, zmq_strerror(C_errno));
}
UNPROTECT(1);
return(R_rmsg);
} else{
REprintf("R_zmq_send: C_socket is not available.\n");
}
return(R_rmsg);
} /* End of R_zmq_msg_recv(). */
示例6: while
void zmq::session_t::clean_pipes ()
{
// Get rid of half-processed messages in the out pipe. Flush any
// unflushed messages upstream.
if (out_pipe) {
out_pipe->rollback ();
out_pipe->flush ();
}
// Remove any half-read message from the in pipe.
if (in_pipe) {
while (incomplete_in) {
zmq_msg_t msg;
zmq_msg_init (&msg);
if (!read (&msg)) {
zmq_assert (!incomplete_in);
break;
}
zmq_msg_close (&msg);
}
}
}
示例7: main
int main (int argc, char const *argv[]) {
void* context = zmq_ctx_new();
//create a SUB socket
void* subscriber = zmq_socket(context, ZMQ_SUB);
const char* filter;
if(argc > 1) {
filter = argv[1];
} else {
filter = "Company1|";
}
printf("Collecting stock information from the server.\n");
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
// must set a subscription for SUB socket
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter));
int i = 0;
for(i = 0; i < 10; i++) {
zmq_msg_t reply;
zmq_msg_init(&reply);
// receive the message, previous message is deallocated
zmq_msg_recv(&reply, subscriber, 0);
int length = zmq_msg_size(&reply);
char* value = malloc(length + 1);
memcpy(value, zmq_msg_data(&reply), length);
zmq_msg_close(&reply);
printf("%s\n", value);
free(value);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
示例8: receiveSimpleResponse
/**
* Simple responses are composed of:
* - A header frame, with character 4 expected to be 0, else an error is assumed
* - If the 4th header character is not 0, a second frame containing an error message shall be received
* @param errorString Left unchanged if no error occurs, set to an error description string if any error occurs
* @return 0 on success. -1 for communication errors, 1 with errorMessage set in case of error-indicating response
*/
static inline int receiveSimpleResponse(void* socket, std::string& errorString) {
zmq_msg_t msg;
zmq_msg_init(&msg);
int rc = zmq_msg_recv(&msg, socket, 0);
if (rc == -1) {
return -1;
}
//Check if there is any error frame (there *should* be one, if the fourth byte is != 0)
if (((char*) zmq_msg_data(&msg))[3] != 0) {
if (!zmq_sockopt_get_rcvmore(socket)) {
errorString = "No error message received from server -- Exact error cause is unknown";
return -1;
}
//We have an error frame from the server. Return it.
if(receiveStringFrame(socket, errorString) == -1) {
return -1;
}
return 1;
}
zmq_msg_close(&msg);
return 0;
}
示例9: zmq_msg_close
bool zmq::zmq_encoder_t::message_ready ()
{
// Destroy content of the old message.
zmq_msg_close(&in_progress);
// Read new message from the dispatcher. If there is none, return false.
// Note that new state is set only if write is successful. That way
// unsuccessful write will cause retry on the next state machine
// invocation.
if (!source || !source->read (&in_progress)) {
zmq_msg_init (&in_progress);
return false;
}
// Get the message size. If the prefix is not to be sent, adjust the
// size accordingly.
size_t size = zmq_msg_size (&in_progress);
if (trim) {
zmq_assert (size);
size_t prefix_size =
(*(unsigned char*) zmq_msg_data (&in_progress)) + 1;
zmq_assert (prefix_size <= size);
size -= prefix_size;
}
// For messages less than 255 bytes long, write one byte of message size.
// For longer messages write 0xff escape character followed by 8-byte
// message size.
if (size < 255) {
tmpbuf [0] = (unsigned char) size;
next_step (tmpbuf, 1, &zmq_encoder_t::size_ready, true);
}
else {
tmpbuf [0] = 0xff;
put_uint64 (tmpbuf + 1, size);
next_step (tmpbuf, 9, &zmq_encoder_t::size_ready, true);
}
return true;
}
示例10: socket_write_exit
static int socket_write_exit(void *socket, int code)
{
zmq_msg_t msg;
if (zmq_msg_init_size(&msg, 5) == -1) {
TRACE_ERRNO("zmq_msg_init_size() failed");
return -1;
}
char *data = zmq_msg_data(&msg);
data[0] = msg_type_exit;
*(uint32_t*)&data[1] = htonl((uint32_t)code);
if (zmq_msg_send(&msg, socket, 0) == -1) {
TRACE_ERRNO("zmq_msg_send() failed");
if (zmq_msg_close(&msg) == -1)
TRACE_ERRNO("zmq_msg_close() failed");
return -1;
}
TRACE("sent exit message, code=%i", code);
return 0;
}
示例11: send_response
static void send_response(void *sock, int internal_status, int exit_status)
{
void *buf;
int len;
CqRep rep = CQ_REP__INIT;
zmq_msg_t msg;
zmq_msg_init(&msg);
rep.exit_status = exit_status;
rep.internal_status = internal_status;
len = cq_rep__get_packed_size(&rep);
buf = malloc(len);
cq_rep__pack(&rep, buf);
zmq_msg_init_data(&msg, buf, len, free_buf, NULL);
zmq_msg_send(&msg, sock, 0);
zmq_msg_close(&msg);
}
示例12: LOG_DEBUG
void LastValueCache::replay_cache(void *entry)
{
std::vector<zmq_msg_t *> *cache_record = &_cache[entry];
if (cache_record->empty())
{
LOG_DEBUG("No cached record");
return;
}
LOG_DEBUG("Replaying cache for entry %p (length: %d)", cache_record, entry, cache_record->size());
std::vector<zmq_msg_t *>::iterator it;
for (std::vector<zmq_msg_t *>::iterator it = cache_record->begin();
it != cache_record->end();
it++)
{
zmq_msg_t message;
zmq_msg_init(&message);
zmq_msg_copy(&message, *it);
zmq_sendmsg(_publisher, &message, (it + 1 != cache_record->end()) ? ZMQ_SNDMORE : 0);
zmq_msg_close(&message);
}
}
示例13: main
int main ()
{
void *ctx = zmq_init (1);
assert (ctx);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
int rc = zmq_connect (dealer, "tcp://localhost:2211");
assert (rc == 0);
while (1) {
// Send signal to parent
zmq_msg_t msg;
zmq_msg_init (&msg);
zmq_sendmsg (dealer, &msg, 0);
zmq_msg_close (&msg);
// Do some work
sleep (1);
}
return 0;
}
示例14: zmq_msg_copy
int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_)
{
zmq_msg_close (dest_);
// VSMs and delimiters require no special handling.
if (src_->content != (zmq::msg_content_t*) ZMQ_DELIMITER &&
src_->content != (zmq::msg_content_t*) ZMQ_VSM) {
// One reference is added to shared messages. Non-shared messages
// are turned into shared messages and reference count is set to 2.
zmq::msg_content_t *content = (zmq::msg_content_t*) src_->content;
if (src_->shared)
content->refcnt.add (1);
else {
src_->shared = true;
content->refcnt.set (2);
}
}
*dest_ = *src_;
return 0;
}
示例15: do_check
void do_check(void* sb, void* sc, unsigned int msgsz)
{
setup_test_environment();
int rc;
int sum =0;
for (int i = 0; i < 10; i++)
{
zmq_msg_t msg;
zmq_msg_init_size(&msg, msgsz);
void * data = zmq_msg_data(&msg);
memcpy(data,&i, sizeof(int));
rc = zmq_msg_send(&msg,sc,i==9 ? 0 :ZMQ_SNDMORE);
assert (rc == (int)msgsz);
zmq_msg_close(&msg);
sum += i;
}
struct iovec ibuffer[32] ;
memset(&ibuffer[0], 0, sizeof(ibuffer));
size_t count = 10;
rc = zmq_recviov(sb,&ibuffer[0],&count,0);
assert (rc == 10);
int rsum=0;
for(;count;--count)
{
int v;
memcpy(&v,ibuffer[count-1].iov_base,sizeof(int));
rsum += v;
assert(ibuffer[count-1].iov_len == msgsz);
// free up the memory
free(ibuffer[count-1].iov_base);
}
assert ( sum == rsum );
}