SysTutorials

Linux Manual Pages › Section 3

zframe(3)

zframe – working with single message frames

Synopsis

#define ZFRAME_MORE 1     //
#define ZFRAME_REUSE 2    //
#define ZFRAME_DONTWAIT 4 //
//  Create a new frame. If size is not null, allocates the frame data
//  to the specified size. If additionally, data is not null, copies
//  size octets from the specified data into the frame body.
CZMQ_EXPORT zframe_t *
zframe_new(const void *data, size_t size);
//  Destroy a frame
CZMQ_EXPORT void
zframe_destroy(zframe_t **self_p);
//  Create an empty (zero-sized) frame
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zframe_t *
zframe_new_empty();
//  Create a frame with a specified string content.
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zframe_t *
zframe_from(const char *string);
//  Receive frame from socket, returns zframe_t object or NULL if the recv
//  was interrupted. Does a blocking recv, if you want to not block then use
//  zpoller or zloop.
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zframe_t *
zframe_recv(void *source);
//  Send a frame to a socket, destroy frame after sending.
//  Return -1 on error, 0 on success.
CZMQ_EXPORT int
zframe_send(zframe_t **self_p, void *dest, int flags);
//  Return number of bytes in frame data
CZMQ_EXPORT size_t
zframe_size(zframe_t *self);
//  Return address of frame data
CZMQ_EXPORT byte *
zframe_data(zframe_t *self);
//  Create a new frame that duplicates an existing frame. If frame is null,
//  or memory was exhausted, returns null.
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zframe_t *
zframe_dup(zframe_t *self);
//  Return frame data encoded as printable hex string, useful for 0MQ UUIDs.
//  Caller must free string when finished with it.
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT char *
zframe_strhex(zframe_t *self);
//  Return frame data copied into freshly allocated string
//  Caller must free string when finished with it.
//  The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT char *
zframe_strdup(zframe_t *self);
//  Return TRUE if frame body is equal to string, excluding terminator
CZMQ_EXPORT bool
zframe_streq(zframe_t *self, const char *string);
//  Return frame MORE indicator (1 or 0), set when reading frame from socket
//  or by the zframe_set_more() method
CZMQ_EXPORT int
zframe_more(zframe_t *self);
//  Set frame MORE indicator (1 or 0). Note this is NOT used when sending
//  frame to socket, you have to specify flag explicitly.
CZMQ_EXPORT void
zframe_set_more(zframe_t *self, int more);
//  Return TRUE if two frames have identical size and data
//  If either frame is NULL, equality is always false.
CZMQ_EXPORT bool
zframe_eq(zframe_t *self, zframe_t *other);
//  Set new contents for frame
CZMQ_EXPORT void
zframe_reset(zframe_t *self, const void *data, size_t size);
//  Send message to zsys log sink (may be stdout, or system facility as
//  configured by zsys_set_logstream). Prefix shows before frame, if not null.
CZMQ_EXPORT void
zframe_print(zframe_t *self, const char *prefix);
//  Probe the supplied object, and report if it looks like a zframe_t.
CZMQ_EXPORT bool
zframe_is(void *self);
//  Self test of this class
CZMQ_EXPORT void
zframe_test(bool verbose);

Description

The zframe class provides methods to send and receive single message frames across 0MQ sockets. A frame corresponds to one zmq_msg_t. When you read a frame from a socket, the zframe_more() method indicates if the frame is part of an unfinished multipart message. The zframe_send method normally destroys the frame, but with the ZFRAME_REUSE flag, you can send the same frame many times. Frames are binary, and this class has no special support for text data.

Please add @discuss section in ../src/zframe.c.

Example

From zframe_test method.

//  Create two PAIR sockets and connect over inproc
zsock_t *output = zsock_new_pair("@inproc://zframe.test");
assert(output);
zsock_t *input = zsock_new_pair(">inproc://zframe.test");
assert(input);
//  Send five different frames, test ZFRAME_MORE
int frame_nbr;
for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
    frame = zframe_new("Hello", 5);
    assert(frame);
    rc = zframe_send(&frame, output, ZFRAME_MORE);
    assert(rc == 0);
}
//  Send same frame five times, test ZFRAME_REUSE
frame = zframe_new("Hello", 5);
assert(frame);
for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
    rc = zframe_send(&frame, output, ZFRAME_MORE + ZFRAME_REUSE);
    assert(rc == 0);
}
assert(frame);
zframe_t *copy = zframe_dup(frame);
assert(zframe_eq(frame, copy));
zframe_destroy(&frame);
assert(!zframe_eq(frame, copy));
assert(zframe_size(copy) == 5);
zframe_destroy(&copy);
assert(!zframe_eq(frame, copy));
//  Test zframe_new_empty
frame = zframe_new_empty();
assert(frame);
assert(zframe_size(frame) == 0);
zframe_destroy(&frame);
//  Send END frame
frame = zframe_new("NOT", 3);
assert(frame);
zframe_reset(frame, "END", 3);
char *string = zframe_strhex(frame);
assert(streq(string, "454E44"));
free(string);
string = zframe_strdup(frame);
assert(streq(string, "END"));
free(string);
rc = zframe_send(&frame, output, 0);
assert(rc == 0);
//  Read and count until we receive END
frame_nbr = 0;
for (frame_nbr = 0;; frame_nbr++) {
    zframe_t *frame = zframe_recv(input);
    if (zframe_streq(frame, "END")) {
        zframe_destroy(&frame);
        break;
    }
    assert(zframe_more(frame));
    zframe_set_more(frame, 0);
    assert(zframe_more(frame) == 0);
    zframe_destroy(&frame);
}
assert(frame_nbr == 10);
zsock_destroy(&input);
zsock_destroy(&output);

Authors

The czmq manual was written by the authors in the AUTHORS file.

Resources

Main web site: m[blue]m[]

Report bugs to the email <m[blue]zeromq-dev [at] lists.zeromq.orgm[][1]>

Copyright

Copyright (c) 1991-2012 iMatix Corporation — http://www.imatix.com Copyright other contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for 0MQ: http://czmq.zeromq.org This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. LICENSE included with the czmq distribution.

Notes

1.
zeromq-dev [at] lists.zeromq.org
mailto:zeromq-dev [at] lists.zeromq.org