Пример #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 */
}
Пример #2
0
func (msg *xsMessage) Close() error {

	r := C.xs_msg_close(&msg.msg)
	if r == 0 {
		return nil
	}

	return errno()
}
Пример #3
0
func (sck *xsSocket) SendMsg(msg Message, flags SocketOption) error {

	/* do a zero-copy */
	cmsg, err := msg.ZeroCopy()
	if err != nil {
		return err
	}

	r := C.xs_sendmsg(sck.sck, cmsg, C.int(flags))
	if r == -1 {
		C.xs_msg_close(cmsg)
		return errno()
	}

	return nil
}