| | |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | 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 |
| | |
| | | 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"; |
| | | |
| | | ////////////////////////////////////////////////////////////////////// |
| | | |
| | |
| | | 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 |
| | | } |
| | |
| | | */ |
| | | 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*); |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @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*); |
| | | /** |
| | | * 释放存储接收信息的buf |
| | | */ |
| | | typedef void(*tfn_dgram_socket_free) (void*); |
| | | |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
New file |
| | |
| | | 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) |
| | | } |