package softbus
|
|
/*
|
#cgo LDFLAGS: -ldl
|
#include <stdlib.h>
|
#include "libcsoftbus.h"
|
|
void *get_array(const int size){
|
int *arr = (int*)malloc(sizeof(int)*size);
|
return arr;
|
}
|
|
void set_array(void *arr, const int index, const int value){
|
int *iarr = (int*)arr;
|
iarr[index] = value;
|
}
|
*/
|
import "C"
|
import (
|
"errors"
|
"unsafe"
|
)
|
|
var libsoftbus C.hcsoftbus
|
|
// Init Dynamic library
|
func Init(so string) error {
|
cso := C.CString(so)
|
defer C.free(unsafe.Pointer(cso))
|
|
handle := C.c_softbus_handle(cso)
|
if handle == nil {
|
// fmt.Errorf("Go SoftBus Init From %s Error\n", so)
|
return errors.New("Init SoftBus Error")
|
}
|
|
libsoftbus = handle
|
return nil
|
}
|
|
// Release handle of c softbus
|
func Release() {
|
if libsoftbus != nil {
|
C.c_softbus_release(libsoftbus)
|
}
|
}
|
|
// ShmInit block size
|
func ShmInit(size int) error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
C.wrap_fn_shm_init(libsoftbus, C.int(size))
|
return nil
|
}
|
|
// ShmAllocKey alloc key
|
func ShmAllocKey() int {
|
if libsoftbus == nil {
|
return -1
|
}
|
r := C.wrap_fn_shm_alloc_key(libsoftbus)
|
return int(r)
|
}
|
|
// ShmDestroy destroy shm block, every softbus proc MUST call it
|
func ShmDestroy() {
|
if libsoftbus != nil {
|
C.wrap_fn_shm_destroy(libsoftbus)
|
}
|
}
|
|
// ShmRmDeadQueue remove dead queue but pass live queue keys
|
func ShmRmDeadQueue(keys []int) error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
if len(keys) == 0 {
|
C.wrap_fn_shm_rm_dead_queue(libsoftbus, nil, 0)
|
return nil
|
}
|
|
l := len(keys)
|
ca := C.get_array(C.int(l))
|
defer C.free(ca)
|
for k, v := range keys {
|
C.set_array(ca, C.int(k), C.int(v))
|
}
|
|
C.wrap_fn_shm_rm_dead_queue(libsoftbus, ca, C.int(l))
|
return nil
|
}
|
|
// Queue struct wrap shmqueue pointer
|
type Queue struct {
|
shmqueue unsafe.Pointer
|
key int
|
}
|
|
// Create shm queue
|
func Create(qSize int) *Queue {
|
if libsoftbus == nil {
|
return nil
|
}
|
|
var key C.int
|
csb := C.wrap_fn_queue_create(libsoftbus, &key, C.int(qSize))
|
if csb != nil {
|
return &Queue{
|
shmqueue: csb,
|
key: int(key),
|
}
|
}
|
return nil
|
}
|
|
// Attach shm queue with key
|
func Attach(key int) *Queue {
|
if libsoftbus == nil {
|
return nil
|
}
|
|
csb := C.wrap_fn_queue_attach(libsoftbus, C.int(key))
|
if csb != nil {
|
return &Queue{
|
shmqueue: csb,
|
key: key,
|
}
|
}
|
|
return nil
|
}
|
|
// Drop shm queue
|
func (q *Queue) Drop() error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
|
C.wrap_fn_queue_drop(libsoftbus, q.shmqueue)
|
return nil
|
}
|
|
// Size shm queue
|
func (q *Queue) Size() int {
|
if libsoftbus == nil {
|
return 0
|
}
|
|
cs := C.wrap_fn_queue_size(libsoftbus, q.shmqueue)
|
return int(cs)
|
}
|
|
// Full shm queue
|
func (q *Queue) Full() bool {
|
if libsoftbus == nil {
|
return true
|
}
|
|
f := C.wrap_fn_queue_full(libsoftbus, q.shmqueue)
|
if f != 0 {
|
return true
|
}
|
return false
|
}
|
|
// Empty shm queue
|
func (q *Queue) Empty() bool {
|
if libsoftbus == nil {
|
return false
|
}
|
|
f := C.wrap_fn_queue_full(libsoftbus, q.shmqueue)
|
if f != 0 {
|
return true
|
}
|
return false
|
|
}
|
|
// Push shm queue
|
func (q *Queue) Push(data []byte) error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
|
r := C.wrap_fn_queue_push(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)))
|
if r == 0 {
|
return nil
|
}
|
return errors.New("Queue Push Data Error")
|
}
|
|
// PushNoWait shm queue
|
func (q *Queue) PushNoWait(data []byte) error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
|
r := C.wrap_fn_queue_push_nowait(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)))
|
if r == 0 {
|
return nil
|
}
|
return errors.New("Queue PushNoWait Data Error")
|
}
|
|
// PushTimeout shm queue push with timeout
|
func (q *Queue) PushTimeout(data []byte, sec, nsec int) error {
|
if libsoftbus == nil {
|
return errors.New("C SoftBus Handle Is Nil")
|
}
|
|
r := C.wrap_fn_queue_push_timeout(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(sec), C.int(nsec))
|
if r == 0 {
|
return nil
|
}
|
return errors.New("Queue PushTimeout Data Error")
|
}
|
|
// Pop from shm queue
|
func (q *Queue) Pop() ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, errors.New("C SoftBus Handle Is Nil")
|
}
|
|
var data unsafe.Pointer
|
var size C.int
|
r := C.wrap_fn_queue_pop(libsoftbus, q.shmqueue, &data, &size)
|
if r == 0 {
|
gd := C.GoBytes(data, size)
|
C.wrap_fn_queue_free_poped(libsoftbus, data)
|
return gd, nil
|
}
|
return nil, errors.New("Queue Pop Data Error")
|
}
|
|
// PopNoWait from shm queue
|
func (q *Queue) PopNoWait() ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, errors.New("C SoftBus Handle Is Nil")
|
}
|
|
var data unsafe.Pointer
|
var size C.int
|
r := C.wrap_fn_queue_pop_nowait(libsoftbus, q.shmqueue, &data, &size)
|
if r == 0 {
|
gd := C.GoBytes(data, size)
|
C.wrap_fn_queue_free_poped(libsoftbus, data)
|
return gd, nil
|
}
|
return nil, errors.New("Queue Pop Data Error")
|
}
|
|
// PopTimeout from shm queue
|
func (q *Queue) PopTimeout(sec, nsec int) ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, errors.New("C SoftBus Handle Is Nil")
|
}
|
|
var data unsafe.Pointer
|
var size C.int
|
r := C.wrap_fn_queue_pop_timeout(libsoftbus, q.shmqueue, &data, &size, C.int(sec), C.int(nsec))
|
if r == 0 {
|
gd := C.GoBytes(data, size)
|
C.wrap_fn_queue_free_poped(libsoftbus, data)
|
return gd, nil
|
}
|
return nil, errors.New("Queue Pop Data Error")
|
}
|