コード例 #1
0
ファイル: zmq4.go プロジェクト: gallir/zmq4
/*
Receive a message part with metadata.

This requires ZeroMQ version 4.1.0. Lower versions will return the message part without metadata.

The returned metadata map contains only those properties that exist on the message.

For a description of flags, see: http://api.zeromq.org/4-1:zmq-msg-recv#toc2

For a description of metadata, see: http://api.zeromq.org/4-1:zmq-msg-gets#toc3
*/
func (soc *Socket) RecvBytesWithMetadata(flags Flag, properties ...string) (msg []byte, metadata map[string]string, err error) {
	if !soc.opened {
		return []byte{}, map[string]string{}, ErrorSocketClosed
	}

	metadata = make(map[string]string)

	var m C.zmq_msg_t
	if i, err := C.zmq_msg_init(&m); i != 0 {
		return []byte{}, metadata, errget(err)
	}
	defer C.zmq_msg_close(&m)

	size, err := C.zmq_msg_recv(&m, soc.soc, C.int(flags))
	if size < 0 {
		return []byte{}, metadata, errget(err)
	}

	data := make([]byte, int(size))
	if size > 0 {
		C.zmq4_memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&m), C.size_t(size))
	}

	if minor > 0 {
		for _, p := range properties {
			ps := C.CString(p)
			s, err := C.zmq_msg_gets(&m, ps)
			if err == nil {
				metadata[p] = C.GoString(s)
			}
			C.free(unsafe.Pointer(ps))
		}
	}
	return data, metadata, nil
}
コード例 #2
0
ファイル: zmq4.go プロジェクト: gallir/zmq4
/*
Receive a message part from a socket.

For a description of flags, see: http://api.zeromq.org/4-1:zmq-msg-recv#toc2
*/
func (soc *Socket) RecvBytes(flags Flag) ([]byte, error) {
	if !soc.opened {
		return []byte{}, ErrorSocketClosed
	}
	var msg C.zmq_msg_t
	if i, err := C.zmq_msg_init(&msg); i != 0 {
		return []byte{}, errget(err)
	}
	defer C.zmq_msg_close(&msg)

	size, err := C.zmq_msg_recv(&msg, soc.soc, C.int(flags))
	if size < 0 {
		return []byte{}, errget(err)
	}
	if size == 0 {
		return []byte{}, nil
	}
	data := make([]byte, int(size))
	C.zmq4_memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&msg), C.size_t(size))
	return data, nil
}