-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsspi_cgo.go
52 lines (41 loc) · 903 Bytes
/
msspi_cgo.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package msspi
/*
#include <memory.h> // memcpy
int cgo_msspi_read( void * goPointer, void * buf, int len );
int cgo_msspi_write( void * goPointer, void * buf, int len );
*/
import "C"
import (
"unsafe"
"go-pointer"
)
//export cgo_msspi_read
func cgo_msspi_read(goPointer, buffer unsafe.Pointer, length C.int) C.int {
c := pointer.Restore(goPointer).(*Handler)
if c == nil {
return 0
}
b := make([]byte, length)
n, err := (*c.conn).Read(b)
c.rerr = err
if n > 0 {
C.memcpy(buffer, unsafe.Pointer(&b[0]), C.size_t(n))
b = C.GoBytes(buffer, C.int(n))
return C.int(n)
}
return 0
}
//export cgo_msspi_write
func cgo_msspi_write(goPointer, buffer unsafe.Pointer, length C.int) C.int {
c := pointer.Restore(goPointer).(*Handler)
if c == nil {
return 0
}
b := C.GoBytes(buffer, length)
n, err := (*c.conn).Write(b)
c.werr = err
if n > 0 {
return C.int(n)
}
return 0
}