From 0bde715af72b7b3d55ad3aac816d7cd153a60b42 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 03 八月 2020 13:29:21 +0800
Subject: [PATCH] add timeout api

---
 softbusDgram.go |  158 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 158 insertions(+), 0 deletions(-)

diff --git a/softbusDgram.go b/softbusDgram.go
index f390075..68d0a29 100644
--- a/softbusDgram.go
+++ b/softbusDgram.go
@@ -71,6 +71,26 @@
 	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 {
@@ -92,6 +112,48 @@
 	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 {
@@ -104,6 +166,46 @@
 	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)
@@ -135,6 +237,32 @@
 	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)
+}
+
 // Pub bus
 func (d *DgramSocket) Pub(topic, msg string, port int) int {
 	if libsoftbus == nil {
@@ -150,6 +278,36 @@
 	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 {

--
Gitblit v1.8.0