stream.go 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * This file is part of the libvirt-go project
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Copyright (c) 2013 Alex Zorin
 * Copyright (C) 2016 Red Hat, Inc.
 *
 */

27 28 29
package libvirt

/*
30
#cgo pkg-config: libvirt
31 32 33
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
34
#include "stream_cfuncs.h"
35
#include "stream_compat.h"
36 37 38 39 40 41 42
*/
import "C"
import (
	"io"
	"unsafe"
)

43
type StreamFlags int
44

45
const (
46
	STREAM_NONBLOCK = StreamFlags(C.VIR_STREAM_NONBLOCK)
47 48
)

49
type StreamEventType int
50 51

const (
52 53 54 55
	STREAM_EVENT_READABLE = StreamEventType(C.VIR_STREAM_EVENT_READABLE)
	STREAM_EVENT_WRITABLE = StreamEventType(C.VIR_STREAM_EVENT_WRITABLE)
	STREAM_EVENT_ERROR    = StreamEventType(C.VIR_STREAM_EVENT_ERROR)
	STREAM_EVENT_HANGUP   = StreamEventType(C.VIR_STREAM_EVENT_HANGUP)
56 57
)

58 59 60 61 62 63
type StreamRecvFlagsValues int

const (
	STREAM_RECV_STOP_AT_HOLE = StreamRecvFlagsValues(C.VIR_STREAM_RECV_STOP_AT_HOLE)
)

64
type Stream struct {
65 66 67
	ptr C.virStreamPtr
}

68
func (v *Stream) Abort() error {
69 70
	result := C.virStreamAbort(v.ptr)
	if result == -1 {
Cédric Bosdonnat's avatar
Cédric Bosdonnat committed
71
		return GetLastError()
72 73 74 75 76
	}

	return nil
}

77
func (v *Stream) Finish() error {
78 79
	result := C.virStreamFinish(v.ptr)
	if result == -1 {
Cédric Bosdonnat's avatar
Cédric Bosdonnat committed
80
		return GetLastError()
81 82 83 84 85
	}

	return nil
}

86
func (v *Stream) Free() error {
87 88
	ret := C.virStreamFree(v.ptr)
	if ret == -1 {
Cédric Bosdonnat's avatar
Cédric Bosdonnat committed
89
		return GetLastError()
90 91 92 93
	}
	return nil
}

94 95 96 97 98 99 100 101
func (c *Stream) Ref() error {
	ret := C.virStreamRef(c.ptr)
	if ret == -1 {
		return GetLastError()
	}
	return nil
}

102
func (v *Stream) Recv(p []byte) (int, error) {
103 104
	n := C.virStreamRecv(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)))
	if n < 0 {
Cédric Bosdonnat's avatar
Cédric Bosdonnat committed
105
		return 0, GetLastError()
106 107 108 109 110 111 112 113
	}
	if n == 0 {
		return 0, io.EOF
	}

	return int(n), nil
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
func (v *Stream) RecvFlags(p []byte, flags StreamRecvFlagsValues) (int, error) {
	if C.LIBVIR_VERSION_NUMBER < 3004000 {
		return 0, GetNotImplementedError("virStreamRecvFlags")
	}

	n := C.virStreamRecvFlagsCompat(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)), C.uint(flags))
	if n < 0 {
		return 0, GetLastError()
	}
	if n == 0 {
		return 0, io.EOF
	}

	return int(n), nil
}

func (v *Stream) RecvHole(flags uint) (int64, error) {
	if C.LIBVIR_VERSION_NUMBER < 3004000 {
		return 0, GetNotImplementedError("virStreamSparseRecvHole")
	}

	var len C.longlong
	ret := C.virStreamRecvHoleCompat(v.ptr, &len, C.uint(flags))
	if ret < 0 {
		return 0, GetLastError()
	}

	return int64(len), nil
}

144
func (v *Stream) Send(p []byte) (int, error) {
145 146
	n := C.virStreamSend(v.ptr, (*C.char)(unsafe.Pointer(&p[0])), C.size_t(len(p)))
	if n < 0 {
Cédric Bosdonnat's avatar
Cédric Bosdonnat committed
147
		return 0, GetLastError()
148 149 150 151 152 153 154
	}
	if n == 0 {
		return 0, io.EOF
	}

	return int(n), nil
}
155

156 157 158 159 160 161 162 163 164 165 166 167 168
func (v *Stream) SendHole(len int64, flags uint32) error {
	if C.LIBVIR_VERSION_NUMBER < 3004000 {
		return GetNotImplementedError("virStreamSendHole")
	}

	ret := C.virStreamSendHoleCompat(v.ptr, C.longlong(len), C.uint(flags))
	if ret < 0 {
		return GetLastError()
	}

	return nil
}

169
type StreamSinkFunc func(*Stream, []byte) (int, error)
170
type StreamSinkHoleFunc func(*Stream, int64) error
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

//export streamSinkCallback
func streamSinkCallback(stream C.virStreamPtr, cdata *C.char, nbytes C.size_t, callbackID int) int {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamSinkFunc)
	if !ok {
		panic("Incorrect stream sink func callback")
	}

	data := make([]byte, int(nbytes))
	for i := 0; i < int(nbytes); i++ {
		cdatabyte := (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cdata)) + (unsafe.Sizeof(*cdata) * uintptr(i))))
		data[i] = (byte)(*cdatabyte)
	}

	retnbytes, err := callback(&Stream{ptr: stream}, data)
	if err != nil {
		return -1
	}

	return retnbytes
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
//export streamSinkHoleCallback
func streamSinkHoleCallback(stream C.virStreamPtr, length C.longlong, callbackID int) int {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamSinkHoleFunc)
	if !ok {
		panic("Incorrect stream sink hole func callback")
	}

	err := callback(&Stream{ptr: stream}, int64(length))
	if err != nil {
		return -1
	}

	return 0
}

212 213 214 215 216 217 218 219 220 221 222 223 224
func (v *Stream) RecvAll(handler StreamSinkFunc) error {

	callbackID := registerCallbackId(handler)

	ret := C.virStreamRecvAll_cgo(v.ptr, (C.int)(callbackID))
	freeCallbackId(callbackID)
	if ret == -1 {
		return GetLastError()
	}

	return nil
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
func (v *Stream) SparseRecvAll(handler StreamSinkFunc, holeHandler StreamSinkHoleFunc) error {
	if C.LIBVIR_VERSION_NUMBER < 3004000 {
		return GetNotImplementedError("virStreamSparseSendAll")
	}

	callbackID := registerCallbackId(handler)
	holeCallbackID := registerCallbackId(holeHandler)

	ret := C.virStreamSparseRecvAll_cgo(v.ptr, (C.int)(callbackID), (C.int)(holeCallbackID))
	freeCallbackId(callbackID)
	freeCallbackId(holeCallbackID)
	if ret == -1 {
		return GetLastError()
	}

	return nil
}

243
type StreamSourceFunc func(*Stream, int) ([]byte, error)
244 245
type StreamSourceHoleFunc func(*Stream) (bool, int64, error)
type StreamSourceSkipFunc func(*Stream, int64) error
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

//export streamSourceCallback
func streamSourceCallback(stream C.virStreamPtr, cdata *C.char, nbytes C.size_t, callbackID int) int {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamSourceFunc)
	if !ok {
		panic("Incorrect stream sink func callback")
	}

	data, err := callback(&Stream{ptr: stream}, (int)(nbytes))
	if err != nil {
		return -1
	}

	nretbytes := int(nbytes)
	if len(data) < nretbytes {
		nretbytes = len(data)
	}

	for i := 0; i < nretbytes; i++ {
		cdatabyte := (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cdata)) + (unsafe.Sizeof(*cdata) * uintptr(i))))
		*cdatabyte = (C.char)(data[i])
	}

	return nretbytes
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
//export streamSourceHoleCallback
func streamSourceHoleCallback(stream C.virStreamPtr, cinData *C.int, clength *C.longlong, callbackID int) int {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamSourceHoleFunc)
	if !ok {
		panic("Incorrect stream sink hole func callback")
	}

	inData, length, err := callback(&Stream{ptr: stream})
	if err != nil {
		return -1
	}

	if inData {
		*cinData = 1
	} else {
		*cinData = 0
	}
	*clength = C.longlong(length)

	return 0
}

//export streamSourceSkipCallback
func streamSourceSkipCallback(stream C.virStreamPtr, length C.longlong, callbackID int) int {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamSourceSkipFunc)
	if !ok {
		panic("Incorrect stream sink skip func callback")
	}

	err := callback(&Stream{ptr: stream}, int64(length))
	if err != nil {
		return -1
	}

	return 0
}

315 316 317 318 319 320 321 322
func (v *Stream) SendAll(handler StreamSourceFunc) error {

	callbackID := registerCallbackId(handler)

	ret := C.virStreamSendAll_cgo(v.ptr, (C.int)(callbackID))
	freeCallbackId(callbackID)
	if ret == -1 {
		return GetLastError()
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	}

	return nil
}

func (v *Stream) SparseSendAll(handler StreamSourceFunc, holeHandler StreamSourceHoleFunc, skipHandler StreamSourceSkipFunc) error {
	if C.LIBVIR_VERSION_NUMBER < 3004000 {
		return GetNotImplementedError("virStreamSparseSendAll")
	}

	callbackID := registerCallbackId(handler)
	holeCallbackID := registerCallbackId(holeHandler)
	skipCallbackID := registerCallbackId(skipHandler)

	ret := C.virStreamSparseSendAll_cgo(v.ptr, (C.int)(callbackID), (C.int)(holeCallbackID), (C.int)(skipCallbackID))
	freeCallbackId(callbackID)
	freeCallbackId(holeCallbackID)
	freeCallbackId(skipCallbackID)
	if ret == -1 {
		return GetLastError()
343 344 345 346
	}

	return nil
}
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

type StreamEventCallback func(*Stream, StreamEventType)

func (v *Stream) EventAddCallback(events StreamEventType, callback StreamEventCallback) error {
	callbackID := registerCallbackId(callback)

	ret := C.virStreamEventAddCallback_cgo(v.ptr, (C.int)(events), (C.int)(callbackID))
	if ret == -1 {
		return GetLastError()
	}

	return nil
}

//export streamEventCallback
func streamEventCallback(st C.virStreamPtr, events int, callbackID int) {
	callbackFunc := getCallbackId(callbackID)

	callback, ok := callbackFunc.(StreamEventCallback)
	if !ok {
		panic("Incorrect stream event func callback")
	}

	callback(&Stream{ptr: st}, StreamEventType(events))
}

func (v *Stream) EventUpdateCallback(events StreamEventType) error {
	ret := C.virStreamEventUpdateCallback(v.ptr, (C.int)(events))
	if ret == -1 {
		return GetLastError()
	}

	return nil
}

func (v *Stream) EventRemoveCallback() error {
	ret := C.virStreamEventRemoveCallback(v.ptr)
	if ret == -1 {
		return GetLastError()
	}

	return nil
}