Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 22b5d5ba6e
Fetching contributors…

Cannot retrieve contributors at this time

executable file 340 lines (277 sloc) 9.671 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/* =========================================================================
zframe - working with single message frames

-------------------------------------------------------------------------
Copyright (c) 1991-2011 iMatix Corporation <www.imatix.com>
Copyright other contributors as noted in the AUTHORS file.

This file is part of zapi, the C binding for 0MQ: http://zapi.zeromq.org.

This is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABIL-
ITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=========================================================================
*/

/*
@header
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.
@discuss
@end
*/

#include "../include/zapi_prelude.h"
#include "../include/zctx.h"
#include "../include/zframe.h"

// Structure of our class

struct _zframe_t {
    zmq_msg_t zmsg; // zmq_msg_t blob for frame
    int more; // More flag, from last read
};


// --------------------------------------------------------------------------
// Constructor; if size is >0, allocates frame with that size, and if data
// is not null, copies data into frame.

zframe_t *
zframe_new (const void *data, size_t size)
{
    zframe_t
        *self;

    self = (zframe_t *) zmalloc (sizeof (zframe_t));
    if (size) {
        zmq_msg_init_size (&self->zmsg, size);
        if (data)
            memcpy (zmq_msg_data (&self->zmsg), data, size);
    }
    else
        zmq_msg_init (&self->zmsg);

    return self;
}


// --------------------------------------------------------------------------
// Destructor

void
zframe_destroy (zframe_t **self_p)
{
    assert (self_p);
    if (*self_p) {
        zframe_t *self = *self_p;
        zmq_msg_close (&self->zmsg);
        free (self);
        *self_p = NULL;
    }
}


// --------------------------------------------------------------------------
// 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
// the zloop class or zmq_poll to check for socket input before receiving.

zframe_t *
zframe_recv (void *socket)
{
    assert (socket);
    zframe_t *self = zframe_new (NULL, 0);
    if (zmq_recv (socket, &self->zmsg, 0)) {
        zframe_destroy (&self);
        return NULL; // Interrupted or terminated
    }
    // Check multipart framing
    int64_t more;
    size_t more_size = sizeof (more);
    zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
    self->more = (int) more;
    return self;
}


// --------------------------------------------------------------------------
// Send frame to socket, destroy after sending unless ZFRAME_REUSE is set.

void
zframe_send (zframe_t **self_p, void *socket, int flags)
{
    assert (socket);
    assert (self_p);
    if (*self_p) {
        zframe_t *self = *self_p;
        if (flags & ZFRAME_REUSE) {
            zmq_msg_t copy;
            zmq_msg_init (&copy);
            zmq_msg_copy (&copy, &self->zmsg);
            zmq_send (socket, &copy, (flags & ZFRAME_MORE)? ZMQ_SNDMORE: 0);
            zmq_msg_close (&copy);
        }
        else {
            zmq_send (socket, &self->zmsg, (flags & ZFRAME_MORE)? ZMQ_SNDMORE: 0);
            zframe_destroy (self_p);
        }
    }
}


// --------------------------------------------------------------------------
// Return size of frame.

size_t
zframe_size (zframe_t *self)
{
    assert (self);
    return zmq_msg_size (&self->zmsg);
}


// --------------------------------------------------------------------------
// Return pointer to frame data.

void *
zframe_data (zframe_t *self)
{
    assert (self);
    return zmq_msg_data (&self->zmsg);
}


// --------------------------------------------------------------------------
// Create a new frame that duplicates an existing frame

zframe_t *
zframe_dup (zframe_t *self)
{
    assert (self);
    return zframe_new (
        zframe_data (self), zframe_size (self));
}


// --------------------------------------------------------------------------
// Return frame data encoded as printable hex string, useful for 0MQ UUIDs.
// Caller must free string when finished with it.

char *
zframe_strhex (zframe_t *self)
{
    static char
        hex_char [] = "0123456789ABCDEF";

    size_t size = zframe_size (self);
    byte *data = zframe_data (self);
    char *hex_str = malloc (size * 2 + 1);

    int byte_nbr;
    for (byte_nbr = 0; byte_nbr < size; byte_nbr++) {
        hex_str [byte_nbr * 2 + 0] = hex_char [data [byte_nbr] >> 4];
        hex_str [byte_nbr * 2 + 1] = hex_char [data [byte_nbr] & 15];
    }
    hex_str [size * 2] = 0;
    return hex_str;
}


// --------------------------------------------------------------------------
// Return frame data copied into freshly allocated string
// Caller must free string when finished with it.

char *
zframe_strdup (zframe_t *self)
{
    size_t size = zframe_size (self);
    char *string = malloc (size + 1);
    memcpy (string, zframe_data (self), size);
    string [size] = 0;
    return string;
}


// --------------------------------------------------------------------------
// Return TRUE if frame body is equal to string, excluding terminator

Bool
zframe_streq (zframe_t *self, char *string)
{
    assert (self);
    if (zframe_size (self) == strlen (string)
    && memcmp (zframe_data (self), string, strlen (string)) == 0)
        return TRUE;
    else
        return FALSE;
}


// --------------------------------------------------------------------------
// Return frame MORE indicator (1 or 0), set when reading frame from socket

int
zframe_more (zframe_t *self)
{
    assert (self);
    return self->more;
}


// --------------------------------------------------------------------------
// Print contents of frame to stderr, prefix is ignored if null.

void
zframe_print (zframe_t *self, char *prefix)
{
    assert (self);
    if (prefix)
        fprintf (stderr, "%s", prefix);
    byte *data = zframe_data (self);
    size_t size = zframe_size (self);

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

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


// --------------------------------------------------------------------------
// Set new contents for frame

void
zframe_reset (zframe_t *self, const void *data, size_t size)
{
    assert (self);
    assert (data);
    zmq_msg_close (&self->zmsg);
    zmq_msg_init_size (&self->zmsg, size);
    memcpy (zmq_msg_data (&self->zmsg), data, size);
}


// --------------------------------------------------------------------------
// Selftest

int
zframe_test (Bool verbose)
{
    printf (" * zframe: ");

    // @selftest
    zctx_t *ctx = zctx_new ();

    void *output = zctx_socket_new (ctx, ZMQ_PAIR);
    zmq_bind (output, "inproc://zframe.test");
    void *input = zctx_socket_new (ctx, ZMQ_PAIR);
    zmq_connect (input, "inproc://zframe.test");

    // Send five different frames, test ZFRAME_MORE
    int frame_nbr;
    for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
        zframe_t *frame = zframe_new ("Hello", 5);
        zframe_send (&frame, output, ZFRAME_MORE);
    }
    // Send same frame five times, test ZFRAME_REUSE
    zframe_t *frame = zframe_new ("Hello", 5);
    for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
        zframe_send (&frame, output, ZFRAME_MORE + ZFRAME_REUSE);
    }
    assert (frame);
    zframe_t *copy = zframe_dup (frame);
    zframe_destroy (&frame);
    assert (zframe_size (copy) == 5);
    zframe_destroy (&copy);

    // Send END frame
    frame = zframe_new ("NOT", 3);
    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);
    zframe_send (&frame, output, 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_destroy (&frame);
    }
    assert (frame_nbr == 10);

    zctx_destroy (&ctx);
    // @end
    printf ("OK\n");
    return 0;
}
Something went wrong with that request. Please try again.