Example #1
0
File: zmq4.go Project: 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
}
Example #2
0
/*
Receive a message part from a socket interpreted as an event.

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

For a description of event_type, see: http://api.zeromq.org/3-2:zmq-socket-monitor#toc2

For an example, see: func (*Socket) Monitor
*/
func (soc *Socket) RecvEvent(flags Flag) (event_type Event, addr string, value int, err error) {
	var msg C.zmq_msg_t
	if i, e := C.zmq_msg_init(&msg); i != 0 {
		err = errget(e)
		return
	}
	defer C.zmq_msg_close(&msg)

	size, e := C.zmq_msg_recv(&msg, soc.soc, C.int(flags))
	if size < 0 {
		err = errget(e)
		return
	}

	var t C.zmq_event_t
	if size < C.int(unsafe.Sizeof(t)) {
		err = errors.New("Not an event")
		return
	}

	et := C.int(0)
	val := C.int(0)
	addrs := C.get_event(&msg, &et, &val)
	defer C.free(unsafe.Pointer(addrs))

	event_type = Event(et)
	addr = C.GoString(addrs)
	value = int(val)

	return
}
Example #3
0
/*
Receive a message part from a socket.

For a description of flags, see: http://api.zeromq.org/3-2:zmq-msg-recv#toc2
*/
func (soc *Socket) RecvBytes(flags Flag) ([]byte, error) {
	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.memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&msg), C.size_t(size))
	return data, nil
}
Example #4
0
File: zmq4.go Project: gallir/zmq4
/*
Receive a message part from a socket interpreted as an event.

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

For a description of event_type, see: http://api.zeromq.org/4-1:zmq-socket-monitor#toc3

For an example, see: func (*Socket) Monitor
*/
func (soc *Socket) RecvEvent(flags Flag) (event_type Event, addr string, value int, err error) {
	if !soc.opened {
		return EVENT_ALL, "", 0, ErrorSocketClosed
	}
	var msg C.zmq_msg_t
	if i, e := C.zmq_msg_init(&msg); i != 0 {
		err = errget(e)
		return
	}
	defer C.zmq_msg_close(&msg)
	size, e := C.zmq_msg_recv(&msg, soc.soc, C.int(flags))
	if size < 0 {
		err = errget(e)
		return
	}
	et := C.int(0)
	val := C.int(0)

	if minor == 0 {
		C.zmq4_get_event40(&msg, &et, &val)
	} else {
		C.zmq4_get_event41(&msg, &et, &val)
	}
	more, e := soc.GetRcvmore()
	if e != nil {
		err = errget(e)
		return
	}
	if !more {
		err = ErrorMoreExpected
		return
	}
	addr, e = soc.Recv(flags)
	if e != nil {
		err = errget(e)
		return
	}

	event_type = Event(et)
	value = int(val)

	return
}
Example #5
0
/*
Receive a message part from a socket interpreted as an event.

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

For a description of event_type, see: http://api.zeromq.org/4-0:zmq-socket-monitor#toc3

For an example, see: func (*Socket) Monitor
*/
func (soc *Socket) RecvEvent(flags Flag) (event_type Event, addr string, value int, err error) {

	var msg C.zmq_msg_t
	if i, e := C.zmq_msg_init(&msg); i != 0 {
		err = errget(e)
		return
	}
	defer C.zmq_msg_close(&msg)
	size, e := C.zmq_msg_recv(&msg, soc.soc, C.int(flags))
	if size < 0 {
		err = errget(e)
		return
	}
	et := C.int(0)
	val := C.int(0)

	if _, minor, _ := Version(); minor == 0 {
		C.get_event40(&msg, &et, &val)
	} else {
		C.get_event41(&msg, &et, &val)
	}
	more, e := soc.GetRcvmore()
	if e != nil {
		err = errget(e)
		return
	}
	if !more {
		err = errors.New("More expected")
		return
	}
	addr, e = soc.Recv(flags)
	if e != nil {
		err = errget(e)
		return
	}

	event_type = Event(et)
	value = int(val)

	return
}
Example #6
0
// Receives a single part along with a boolean flag (more) indicating whether more parts of the same message follow
// (true), or this is the last part of the message (false). As with Send/SendPart, this is fairly low-level and Recv
// should generally be used instead.
func (s *Socket) RecvPart() (part []byte, more bool, err error) {
	var msg C.zmq_msg_t
	C.zmq_msg_init(&msg)
	for {
		err = nil
		r := C.zmq_msg_recv(&msg, s.sock, 0)
		if r == -1 {
			err = zmqerr()
		}
		if err != ErrInterrupted {
			break
		}
	}
	if err != nil {
		C.zmq_msg_close(&msg)
		return
	}
	part = fromMsg(&msg)
	// Check for more parts
	more = (s.getInt(C.ZMQ_RCVMORE) != 0)
	return
}
Example #7
0
// Receive a message from the socket.
// int zmq_recv (void *s, zmq_msg_t *msg, int flags);
func (s *zmqSocket) Recv(flags SendRecvOption) (data []byte, err error) {
	// Allocate and initialise a new zmq_msg_t
	var m C.zmq_msg_t
	if C.zmq_msg_init(&m) != 0 {
		err = errno()
		return
	}
	defer C.zmq_msg_close(&m)
	// Receive into message
	if C.zmq_msg_recv(&m, s.s, C.int(flags)) == -1 {
		err = errno()
		return
	}
	// Copy message data into a byte array
	// FIXME Ideally this wouldn't require a copy.
	size := C.zmq_msg_size(&m)
	if size > 0 {
		data = make([]byte, int(size))
		C.memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&m), size)
	} else {
		data = nil
	}
	return
}