예제 #1
0
파일: zmq.go 프로젝트: mgranberry/gozmq
// 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
	err = nil
	var m C.zmq_msg_t
	if C.zmq_msg_init(&m) != 0 {
		println("init")
		err = errno()
		return
	}
	defer C.zmq_msg_close(&m)
	// Receive into message
	if C.zmq_recvmsg(s.s, &m, C.int(flags)) != 0 {
		println("recvmsg")
		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
}
예제 #2
0
파일: zmq_3_x.go 프로젝트: somia/gozmq
// 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_recvmsg(s.s, &m, C.int(flags)); rc == -1 {
		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
}
예제 #3
0
func (s *Socket) RecvEvent(flags SendRecvOption) (ty Event, addr string, ex int64, 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_recvmsg(s.s, &m, C.int(flags)); rc == -1 {
		err = casterr(err)
		return
	}
	var ev *C.zmq_event_t
	size := C.zmq_msg_size(&m)
	if uintptr(size) != unsafe.Sizeof(*ev) {
		err = errors.New("Invalid event message received")
		return
	}
	err = nil

	ev = (*C.zmq_event_t)(C.zmq_msg_data(&m))
	ty = Event(ev.event)
	addr = C.GoString(C.ev_get_addr(ev))
	ex = int64(C.ev_get_extra(ev))
	return
}