package softbus
|
|
/*
|
#include <stdlib.h>
|
#include "libcsoftbus.h"
|
extern void *get_array(const int size);
|
extern void set_array(void *arr, const int index, const int value);
|
*/
|
import "C"
|
import (
|
"fmt"
|
"unsafe"
|
)
|
|
// DgramSocket dgram
|
type DgramSocket struct {
|
dgram unsafe.Pointer
|
}
|
|
// OpenDgramSocket dgram
|
func OpenDgramSocket() *DgramSocket {
|
if libsoftbus == nil {
|
return nil
|
}
|
|
d := C.wrap_fn_dgram_socket_open(libsoftbus)
|
if d == nil {
|
return nil
|
}
|
|
return &DgramSocket{
|
dgram: d,
|
}
|
}
|
|
// Close close dgram socket
|
func (d *DgramSocket) Close() int {
|
if libsoftbus != nil && d.dgram != nil {
|
r := C.wrap_fn_dgram_socket_close(libsoftbus, d.dgram)
|
return int(r)
|
}
|
|
return RETVAL
|
}
|
|
// Bind bind
|
func (d *DgramSocket) Bind(port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_bind(libsoftbus, d.dgram, C.int(port))
|
return int(r)
|
}
|
|
// ForceBind bind
|
func (d *DgramSocket) ForceBind(port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_force_bind(libsoftbus, d.dgram, C.int(port))
|
return int(r)
|
}
|
|
// SendTo port
|
func (d *DgramSocket) SendTo(data []byte, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_sendto(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port))
|
return int(r)
|
}
|
|
// SendToTimeout port
|
func (d *DgramSocket) SendToTimeout(data []byte, port int, sec, usec int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_sendto_timeout(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port), C.int(sec), C.int(usec))
|
return int(r)
|
}
|
|
// SendToNoWait port
|
func (d *DgramSocket) SendToNoWait(data []byte, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_sendto_nowait(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port))
|
return int(r)
|
}
|
|
// RecvFrom data and port
|
func (d *DgramSocket) RecvFrom() ([]byte, int, error) {
|
if libsoftbus == nil {
|
return nil, -1, fmt.Errorf("RecvFrom Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
var rp C.int
|
|
r := C.wrap_fn_dgram_socket_recvfrom(libsoftbus, d.dgram, &rb, &rs, &rp)
|
if r != 0 {
|
return nil, int(rp), fmt.Errorf("RecvFrom Func Failed %d", int(r))
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, int(rp), nil
|
}
|
|
// RecvFromTimeout data and port
|
func (d *DgramSocket) RecvFromTimeout(sec, usec int) ([]byte, int, error) {
|
if libsoftbus == nil {
|
return nil, -1, fmt.Errorf("RecvFromTimeout Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
var rp C.int
|
|
r := C.wrap_fn_dgram_socket_recvfrom_timeout(libsoftbus, d.dgram, &rb, &rs, &rp, C.int(sec), C.int(usec))
|
if r != 0 {
|
return nil, int(rp), fmt.Errorf("RecvFromTimeout Func Failed %d", int(r))
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, int(rp), nil
|
}
|
|
// RecvFromNoWait data and port
|
func (d *DgramSocket) RecvFromNoWait() ([]byte, int, error) {
|
if libsoftbus == nil {
|
return nil, -1, fmt.Errorf("RecvFromNoWait Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
var rp C.int
|
|
r := C.wrap_fn_dgram_socket_recvfrom_nowait(libsoftbus, d.dgram, &rb, &rs, &rp)
|
if r != 0 {
|
return nil, int(rp), fmt.Errorf("RecvFromNoWait Func Failed %d", int(r))
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, int(rp), nil
|
}
|
|
// SendAndRecv sync
|
func (d *DgramSocket) SendAndRecv(sdata []byte, port int) ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, fmt.Errorf("SendAndRecv Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
|
r := C.wrap_fn_dgram_socket_sendandrecv(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs)
|
if r != 0 {
|
return nil, fmt.Errorf("SendAndRecv Send To %d And Recv From It Failed", port)
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, nil
|
}
|
|
// SendAndRecvTimeout sync
|
func (d *DgramSocket) SendAndRecvTimeout(sdata []byte, port int, sec, usec int) ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, fmt.Errorf("SendAndRecvTimeout Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
|
r := C.wrap_fn_dgram_socket_sendandrecv_timeout(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs, C.int(sec), C.int(usec))
|
if r != 0 {
|
return nil, fmt.Errorf("SendAndRecvTimeout Send To %d And Recv From It Failed", port)
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, nil
|
}
|
|
// SendAndRecvNoWait sync
|
func (d *DgramSocket) SendAndRecvNoWait(sdata []byte, port int) ([]byte, error) {
|
if libsoftbus == nil {
|
return nil, fmt.Errorf("SendAndRecvNoWait Func Test libsoftbus Is Nil")
|
}
|
|
var rb unsafe.Pointer
|
var rs C.int
|
|
r := C.wrap_fn_dgram_socket_sendandrecv_nowait(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs)
|
if r != 0 {
|
return nil, fmt.Errorf("SendAndRecvNoWait Send To %d And Recv From It Failed", port)
|
}
|
|
data := C.GoBytes(rb, rs)
|
C.wrap_fn_dgram_socket_free(libsoftbus, rb)
|
|
return data, nil
|
}
|
|
// StartBus bus
|
func (d *DgramSocket) StartBus() int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_socket_start_bus(libsoftbus, d.dgram)
|
return int(r)
|
}
|
|
// Sub sub bus
|
func (d *DgramSocket) Sub(topic string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_sub(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
|
return int(r)
|
}
|
|
// SubTimeout sub bus
|
func (d *DgramSocket) SubTimeout(topic string, port int, sec, usec int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_sub_timeout(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port), C.int(sec), C.int(usec))
|
return int(r)
|
}
|
|
// SubNoWait sub bus
|
func (d *DgramSocket) SubNoWait(topic string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_sub_nowait(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
|
return int(r)
|
}
|
|
// Desub remove sub
|
func (d *DgramSocket) Desub(topic string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_desub(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
|
return int(r)
|
}
|
|
// DesubTimeout timeout
|
func (d *DgramSocket) DesubTimeout(topic string, port int, sec, usec int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_desub_timeout(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port), C.int(sec), C.int(usec))
|
return int(r)
|
}
|
|
// DesubNoWait remove sub
|
func (d *DgramSocket) DesubNoWait(topic string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
|
r := C.wrap_fn_dgram_socket_desub_nowait(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
|
return int(r)
|
}
|
|
// Pub bus
|
func (d *DgramSocket) Pub(topic, msg string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
cm := C.CString(msg)
|
defer C.free(unsafe.Pointer(cm))
|
|
r := C.wrap_fn_dgram_socket_pub(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port))
|
return int(r)
|
}
|
|
// PubTimeout bus
|
func (d *DgramSocket) PubTimeout(topic, msg string, port int, sec, usec int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
cm := C.CString(msg)
|
defer C.free(unsafe.Pointer(cm))
|
|
r := C.wrap_fn_dgram_socket_pub_timeout(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port), C.int(sec), C.int(usec))
|
return int(r)
|
}
|
|
// PubNoWait bus
|
func (d *DgramSocket) PubNoWait(topic, msg string, port int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
ct := C.CString(topic)
|
defer C.free(unsafe.Pointer(ct))
|
cm := C.CString(msg)
|
defer C.free(unsafe.Pointer(cm))
|
|
r := C.wrap_fn_dgram_socket_pub_nowait(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port))
|
return int(r)
|
}
|
|
// Port socket
|
func (d *DgramSocket) Port() int {
|
if libsoftbus == nil {
|
return -1
|
}
|
|
r := C.wrap_fn_dgram_socket_port(libsoftbus, d.dgram)
|
return int(r)
|
}
|
|
// RemoveKey rm key
|
func RemoveKey(key int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
r := C.wrap_fn_dgram_remove_key(libsoftbus, C.int(key))
|
return int(r)
|
}
|
|
// RemoveKeys rm key
|
func RemoveKeys(keys []int) int {
|
if libsoftbus == nil {
|
return RETVAL
|
}
|
|
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))
|
}
|
|
r := C.wrap_fn_dgram_remove_keys(libsoftbus, ca, C.int(l))
|
return int(r)
|
}
|