package softbus /* #include #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) }