Frame.opCall

$(ANCHOR Frame.opCall_data) Initializes a $(ZMQ) message frame from a supplied buffer.

If free is not specified, data must refer to a slice of memory which has been allocated by the garbage collector (typically using operator new). Ownership will then be transferred temporarily to $(ZMQ), and then transferred back to the GC when $(ZMQ) is done using the buffer.

For memory which is not garbage collected, the argument free must be a pointer to a function that will release the memory, which will be called when $(ZMQ) no longer needs the buffer. free must point to a function with the following signature:

extern(C) void f(void* d, void* h) nothrow;

When it is called, d will be equal to data.ptr, while h will be equal to hint (which is optional and null by default).

free is passed directly to the underlying $(ZMQ) C function, which is why it needs to be extern(C).

Warning: Some care must be taken when using this function, as there is no telling when, whether, or in which thread $(ZMQ) relinquishes ownership of the buffer. Client code should therefore avoid retaining any references to it, including slices that contain, overlap with or are contained in data. For the "non-GC" version, client code should in general not retain slices or pointers to any memory which will be released when free is called.

  1. Frame opCall()
  2. Frame opCall(size_t size)
  3. Frame opCall(ubyte[] data)
    struct Frame
    @safe static @system
    opCall
    (
    ubyte[] data
    )
  4. Frame opCall(ubyte[] data, FreeData free, void* hint)

Throws

ZmqException if $(ZMQ) reports an error.

Corresponds to

$(ZMQREF zmq_msg_init_data())

Examples

// Garbage-collected memory
auto buf = new ubyte[123];
auto msg = Frame(buf);
assert(msg.size == buf.length);
assert(msg.data.ptr == buf.ptr);
// Manually managed memory
import core.stdc.stdlib: malloc, free;
static extern(C) void myFree(void* data, void* hint) nothrow { free(data); }
auto buf = (cast(ubyte*) malloc(10))[0 .. 10];
auto msg = Frame(buf, &myFree);
assert(msg.size == buf.length);
assert(msg.data.ptr == buf.ptr);

See Also

Meta