From b70473ad3084fe968bbfaa50eb54cc248b79e02a Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 28 七月 2020 16:10:50 +0800
Subject: [PATCH] 添加dgram socket接口

---
 libcsoftbus.h      |   42 +++++
 libcsoftbus_func.h |   66 +++++++++
 softbusDgram.go    |  161 +++++++++++++++++++++++
 libcsoftbus.c      |  102 ++++++++++++++
 4 files changed, 370 insertions(+), 1 deletions(-)

diff --git a/libcsoftbus.c b/libcsoftbus.c
index 9058cfe..1c02eaf 100644
--- a/libcsoftbus.c
+++ b/libcsoftbus.c
@@ -257,3 +257,105 @@
 
     return fn_socket_port(s);
 }
+////////////////////////////////////////////
+// dgram socket mode
+////////////////////////////////////////////
+void *wrap_fn_dgram_socket_open(hcsoftbus lib){
+    if (!fn_dgram_socket_open){
+        fn_dgram_socket_open = (tfn_dgram_socket_open)dlsym(lib , l_dgram_socket_open);
+        check_with_ret(fn_dgram_socket_open, lib, NULL);
+    }
+    return fn_dgram_socket_open();
+}
+
+int wrap_fn_dgram_socket_close(hcsoftbus lib, void *s){
+    if (!fn_dgram_socket_close){
+        fn_dgram_socket_close = (tfn_dgram_socket_close)dlsym(lib, l_dgram_socket_close);
+        check_with_ret(fn_dgram_socket_close, lib, -1);
+    }
+    return fn_dgram_socket_close(s);
+}
+
+int wrap_fn_dgram_socket_bind(hcsoftbus lib, void *s, int port){
+    if (!fn_dgram_socket_bind){
+        fn_dgram_socket_bind = (tfn_dgram_socket_bind)dlsym(lib, l_dgram_socket_bind);
+        check_with_ret(fn_dgram_socket_bind, lib, -1);
+    }
+    return fn_dgram_socket_bind(s, port);
+}
+
+int wrap_fn_dgram_socket_force_bind(hcsoftbus lib, void *s, int port){
+    if (!fn_dgram_socket_force_bind){
+        fn_dgram_socket_force_bind = (tfn_dgram_socket_force_bind)dlsym(lib, l_dgram_socket_force_bind);
+        check_with_ret(fn_dgram_socket_force_bind, lib, -1);
+    }
+    return fn_dgram_socket_force_bind(s, port);
+}
+
+int wrap_fn_dgram_socket_sendto(hcsoftbus lib, void *s, const void *buf, const int size, const int port){
+    if (!fn_dgram_socket_sendto){
+        fn_dgram_socket_sendto = (tfn_dgram_socket_sendto)dlsym(lib, l_dgram_socket_sendto);
+        check_with_ret(fn_dgram_socket_sendto, lib, -1);
+    }
+    return fn_dgram_socket_sendto(s, buf, size, port);
+}
+
+int wrap_fn_dgram_socket_recvfrom(hcsoftbus lib, void *s, void **buf, int *size, int *port){
+    if (!fn_dgram_socket_recvfrom){
+        fn_dgram_socket_recvfrom = (tfn_dgram_socket_recvfrom)dlsym(lib, l_dgram_socket_recvfrom);
+        check_with_ret(fn_dgram_socket_recvfrom, lib, -1);
+    }
+    return fn_dgram_socket_recvfrom(s, buf, size, port);
+}
+
+int wrap_fn_dgram_socket_sendandrecv(hcsoftbus lib, void *s, const void *sbuf, const int ssize, const int port, void **rbuf, int *rsize){
+    if (!fn_dgram_socket_sendandrecv){
+        fn_dgram_socket_sendandrecv = (tfn_dgram_socket_sendandrecv)dlsym(lib, l_dgram_socket_sendandrecv);
+        check_with_ret(fn_dgram_socket_sendandrecv, lib, -1);
+    }
+    return fn_dgram_socket_sendandrecv(s, sbuf, ssize, port, rbuf, rsize);
+}
+
+int wrap_fn_dgram_socket_start_bus(hcsoftbus lib, void *s){
+    if (!fn_dgram_socket_start_bus){
+        fn_dgram_socket_start_bus = (tfn_dgram_socket_start_bus)dlsym(lib, l_dgram_socket_start_bus);
+        check_with_ret(fn_dgram_socket_start_bus, lib, -1);
+    }
+    return fn_dgram_socket_start_bus(s);
+}
+
+int wrap_fn_dgram_socket_sub(hcsoftbus lib, void *s, void *topic, int size, int port){
+    if (!fn_dgram_socket_sub){
+        fn_dgram_socket_sub = (tfn_dgram_socket_sub)dlsym(lib, l_dgram_socket_sub);
+        check_with_ret(fn_dgram_socket_sub, lib, -1);
+    }
+    return fn_dgram_socket_sub(s, topic, size, port);
+}
+
+int wrap_fn_dgram_socket_pub(hcsoftbus lib, void *s, void *topic, int tsize, void *content, int csize, int port){
+    if (!fn_dgram_socket_pub){
+        fn_dgram_socket_pub = (tfn_dgram_socket_pub)dlsym(lib, l_dgram_socket_pub);
+        check_with_ret(fn_dgram_socket_pub, lib, -1);
+    }
+    return fn_dgram_socket_pub(s, topic, tsize, content, csize, port);
+}
+
+int wrap_fn_dgram_socket_port(hcsoftbus lib, void *s){
+    if (!fn_dgram_socket_port){
+        fn_dgram_socket_port = (tfn_dgram_socket_port)dlsym(lib, l_dgram_socket_port);
+        check_with_ret(fn_dgram_socket_port, lib, -1);
+    }
+    return fn_dgram_socket_port(s);
+}
+
+void wrap_fn_dgram_socket_free(hcsoftbus lib, void *buf){
+    if (!fn_dgram_socket_free){
+        fn_dgram_socket_free = (tfn_dgram_socket_free)dlsym(lib, l_dgram_socket_free);
+    }
+    if (fn_dgram_socket_free){
+        fn_dgram_socket_free(buf);
+    }else{
+        free(buf);
+    }
+}
+
diff --git a/libcsoftbus.h b/libcsoftbus.h
index db38512..4e8a86c 100644
--- a/libcsoftbus.h
+++ b/libcsoftbus.h
@@ -45,6 +45,20 @@
 static tfn_socket_buf_free      fn_socket_buf_free = NULL;
 static tfn_socket_port          fn_socket_port = NULL;
 
+// dgram mode socket
+static tfn_dgram_socket_open    fn_dgram_socket_open = NULL;
+static tfn_dgram_socket_close   fn_dgram_socket_close = NULL;
+static tfn_dgram_socket_bind    fn_dgram_socket_bind = NULL;
+static tfn_dgram_socket_force_bind    fn_dgram_socket_force_bind = NULL;
+static tfn_dgram_socket_sendto  fn_dgram_socket_sendto = NULL;
+static tfn_dgram_socket_recvfrom      fn_dgram_socket_recvfrom = NULL;
+static tfn_dgram_socket_sendandrecv   fn_dgram_socket_sendandrecv = NULL;
+static tfn_dgram_socket_start_bus     fn_dgram_socket_start_bus = NULL;
+static tfn_dgram_socket_sub     fn_dgram_socket_sub = NULL;
+static tfn_dgram_socket_pub     fn_dgram_socket_pub = NULL;
+static tfn_dgram_socket_port    fn_dgram_socket_port = NULL;
+static tfn_dgram_socket_free    fn_dgram_socket_free = NULL;
+
 //////////////////////////////////////////////////////////////////////
 
 // labels
@@ -85,6 +99,19 @@
 const static char l_socket_free[] = "mod_free";
 const static char l_socket_port[] = "mod_get_socket_port";
 
+// dgram mode socket
+const static char l_dgram_socket_open[] = "dgram_mod_open_socket";
+const static char l_dgram_socket_close[] = "dgram_mod_close_socket";
+const static char l_dgram_socket_bind[] = "dgram_mod_bind";
+const static char l_dgram_socket_force_bind[] = "dgram_mod_force_bind";
+const static char l_dgram_socket_sendto[] = "dgram_mod_sendto";
+const static char l_dgram_socket_recvfrom[] = "dgram_mod_recvfrom";
+const static char l_dgram_socket_sendandrecv[] = "dgram_mod_sendandrecv";
+const static char l_dgram_socket_start_bus[] = "dgram_mod_start_bus";
+const static char l_dgram_socket_sub[] = "dgram_mod_sub";
+const static char l_dgram_socket_pub[] = "dgram_mod_pub";
+const static char l_dgram_socket_port[] = "dgram_mod_get_port";
+const static char l_dgram_socket_free[] = "dgram_mod_free";
 
 //////////////////////////////////////////////////////////////////////
 
@@ -131,7 +158,20 @@
 void wrap_fn_socket_buf_free(hcsoftbus lib, void *buf);
 int wrap_fn_socket_port(hcsoftbus lib, void *s);
 
-
+/////////////////////////////////////////////////////////
+// dgram socket mode
+void *wrap_fn_dgram_socket_open(hcsoftbus lib);
+int wrap_fn_dgram_socket_close(hcsoftbus lib, void *s);
+int wrap_fn_dgram_socket_bind(hcsoftbus lib, void *s, int port);
+int wrap_fn_dgram_socket_force_bind(hcsoftbus lib, void *s, int port);
+int wrap_fn_dgram_socket_sendto(hcsoftbus lib, void *s, const void *buf, const int size, const int port);
+int wrap_fn_dgram_socket_recvfrom(hcsoftbus lib, void *s, void **buf, int *size, int *port);
+int wrap_fn_dgram_socket_sendandrecv(hcsoftbus lib, void *s, const void *sbuf, const int ssize, const int port, void **rbuf, int *rsize);
+int wrap_fn_dgram_socket_start_bus(hcsoftbus lib, void *s);
+int wrap_fn_dgram_socket_sub(hcsoftbus lib, void *s, void *topic, int size, int port);
+int wrap_fn_dgram_socket_pub(hcsoftbus lib, void *s, void *topic, int tsize, void *content, int csize, int port);
+int wrap_fn_dgram_socket_port(hcsoftbus lib, void *s);
+void wrap_fn_dgram_socket_free(hcsoftbus lib, void *buf);
 
 #ifdef __cplusplus
 }
diff --git a/libcsoftbus_func.h b/libcsoftbus_func.h
index 7ac20f5..5bd60dc 100644
--- a/libcsoftbus_func.h
+++ b/libcsoftbus_func.h
@@ -146,6 +146,72 @@
  */
 typedef int(*tfn_socket_port) (void*);
 
+//////////////////////////////////////////////
+// dgram socket
+/**
+ * 鍒涘缓socket
+ * @return socket鍦板潃
+*/
+typedef void*(*tfn_dgram_socket_open) ();
+/**
+ * 鍏抽棴socket
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+typedef int(*tfn_dgram_socket_close) (void*);
+/**
+ * 缁戝畾绔彛鍒皊ocket, 濡傛灉涓嶇粦瀹氬垯绯荤粺鑷姩鍒嗛厤涓�涓�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+typedef int(*tfn_dgram_socket_bind) (void*, int);
+typedef tfn_dgram_socket_bind tfn_dgram_socket_force_bind;
+/**
+ * 鍙戦�佷俊鎭�
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+typedef int(*tfn_dgram_socket_sendto) (void*, const void*, const int, const int);
+/**
+ * 鎺ユ敹淇℃伅
+ * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+typedef int(*tfn_dgram_socket_recvfrom) (void*, void**, int*, int*);
+/**
+ * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+typedef int(*tfn_dgram_socket_sendandrecv) (void*, const void*, const int, const int, void**, int*);
+/**
+ * 鍚姩bus
+ *
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+typedef int(*tfn_dgram_socket_start_bus) (void*);
+/**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+typedef int(*tfn_dgram_socket_sub) (void*, void*, int, int);
+/**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @port 鎬荤嚎绔彛
+ */
+typedef int(*tfn_dgram_socket_pub) (void*, void*, int, void*, int, int);
+/**
+ * 鑾峰彇soket绔彛鍙�
+ */
+typedef int(*tfn_dgram_socket_port) (void*);
+/**
+ * 閲婃斁瀛樺偍鎺ユ敹淇℃伅鐨刡uf
+ */
+typedef void(*tfn_dgram_socket_free) (void*);
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/softbusDgram.go b/softbusDgram.go
new file mode 100644
index 0000000..f390075
--- /dev/null
+++ b/softbusDgram.go
@@ -0,0 +1,161 @@
+package softbus
+
+/*
+#include <stdlib.h>
+#include "libcsoftbus.h"
+*/
+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)
+}
+
+// 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
+}
+
+// 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
+}
+
+// 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)
+}
+
+// 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)
+}
+
+// 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)
+}

--
Gitblit v1.8.0