Beispiel #1
0
/* int xs_recv (void *sck,void *buf,size_t len,int flags) */
func (sck *xsSocket) Recv(flags SocketOption) ([]byte, error, uint64) {

	var msg C.xs_msg_t
	r := C.xs_msg_init(&msg)
	if r != 0 {
		return nil, errno(), 0
	}

	defer C.xs_msg_close(&msg)

	r = C.xs_recvmsg(sck.sck, &msg, C.int(flags))
	if r == -1 {
		return nil, errno(), 0
	}
	size := C.xs_msg_size(&msg)
	var more uint64
	more_size := C.size_t(unsafe.Sizeof(more))
	C.xs_getmsgopt(&msg, C.XS_MORE, unsafe.Pointer(&more), &more_size)

	if size > 0 {
		data := make([]byte, int(size))
		C.memcpy(unsafe.Pointer(&data[0]), C.xs_msg_data(&msg), size)
		return data, nil, more
	}

	return nil, nil, more /* nothing to recv */
}
Beispiel #2
0
func NewMessage(data []byte) (Message, error) {

	var msg xsMessage
	//r := C.xs_msg_init_data (&msg.msg,unsafe.Pointer(&data[0]),C.size_t(len(data)),nil,nil)
	size := C.size_t(len(data))
	r := C.xs_msg_init_size(&msg.msg, size)
	if r == -1 {
		return nil, errno()
	}
	C.memcpy(unsafe.Pointer(C.xs_msg_data(&msg.msg)), unsafe.Pointer(&data[0]), size)

	return &msg, nil
}