Esempio n. 1
0
/*
Receive a message part from a socket.

For a description of flags, see: http://api.zeromq.org/2-2:zmq-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)

	var size C.int
	var err error

	var i C.int
	i, err = C.zmq_recv(soc.soc, &msg, C.int(flags))
	if i == 0 {
		size = C.int(C.zmq_msg_size(&msg))
	} else {
		size = -1
	}

	if size < 0 {
		return []byte{}, errget(err)
	}
	if size == 0 {
		return []byte{}, nil
	}
	data := make([]byte, int(size))
	C.my_memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&msg), C.size_t(size))
	return data, nil
}
Esempio n. 2
0
// Receive a message from the socket.
// int zmq_recv (void *s, zmq_msg_t *msg, int flags);
func (s *Socket) Recv(flags SendRecvOption) (data []byte, err error) {
	// Allocate and initialise a new zmq_msg_t
	var m C.zmq_msg_t
	var rc C.int
	if rc, err = C.zmq_msg_init(&m); rc != 0 {
		err = casterr(err)
		return
	}
	defer C.zmq_msg_close(&m)
	// Receive into message
	if rc, err = C.zmq_recv(s.s, &m, C.int(flags)); rc != 0 {
		err = casterr(err)
		return
	}
	err = nil
	// 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
}
Esempio n. 3
0
File: zmq.go Progetto: miffa/gozero
func (p lzmqSocket) Receive(msg Message, flags int) os.Error {
	if msg == nil {
		return os.EINVAL
	}
	lzmqMsgHolder, castable := msg.(lzmqMessageHolder)
	if !castable {
		return os.EINVAL
	}
	lzmqMsg := lzmqMsgHolder.getLzmqMessage()
	err := lzmqMsg.empty()
	if err != nil {
		return err
	}
	ret := p.Provider().OkIf(C.zmq_recv(unsafe.Pointer(p), lzmqMsg.ptr(), C.int(flags)) == 0)
	// fmt.Println("recv", msg)
	return ret
}
Esempio n. 4
0
//func Poll(items []*PollItem, timeout int64) os.Error {
//	return handle(C.zmq_poll(
//}
func Recv(socket ZSocket, message ZMessage, flags int) os.Error {
	return handle(C.zmq_recv(socket.Ptr, message.Ptr, C.int(flags)))
}