New file |
| | |
| | | package bhomebus |
| | | |
| | | /* |
| | | #cgo LDFLAGS: -ldl |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <stdio.h> |
| | | #include "libcbhomebus.h" |
| | | |
| | | void* create_net_node_t(const int size){ |
| | | net_node_t* nodes = (net_node_t*)malloc(sizeof(net_node_t) * size); |
| | | return nodes; |
| | | } |
| | | void release_net_node_t(void* arr){ |
| | | net_node_t* nodes = (net_node_t*)arr; |
| | | free(nodes); |
| | | } |
| | | void set_1_net_node(void* arr, const int index, char* host, const int port, const int key){ |
| | | net_node_t* nodes = (net_node_t*)arr; |
| | | nodes[index].host = strlen(host) > 0 ? host : NULL; |
| | | nodes[index].port = port; |
| | | nodes[index].key = key; |
| | | } |
| | | int get_1_net_mod_recv_msg(void* arr, const int index, |
| | | char** host, int* port, int* key, void** content, int* content_length){ |
| | | |
| | | if (!arr) return -1; |
| | | |
| | | net_mod_recv_msg_t* msg = (net_mod_recv_msg_t*)arr; |
| | | *host = msg[index].host; |
| | | *port = msg[index].port; |
| | | *key = msg[index].key; |
| | | *content = msg[index].content; |
| | | *content_length = msg[index].content_length; |
| | | return 0; |
| | | } |
| | | void* create_int_array(const int size){ |
| | | int* arr = (int*)malloc(sizeof(int) * size); |
| | | return arr; |
| | | } |
| | | void release_int_array(void* arr){ |
| | | int* carr = (int*)arr; |
| | | free(carr); |
| | | } |
| | | int set_1_int(void* arr, const int index, const int key){ |
| | | if (!arr) return -1; |
| | | int* carr = (int*)arr; |
| | | carr[index] = key; |
| | | return 0; |
| | | } |
| | | */ |
| | | import "C" |
| | | import ( |
| | | "errors" |
| | | "unsafe" |
| | | ) |
| | | |
| | | var libbhomebus C.hbhomebus |
| | | |
| | | // Init Dynamic library |
| | | func Init(so string) error { |
| | | cso := C.CString(so) |
| | | defer C.free(unsafe.Pointer(cso)) |
| | | |
| | | handle := C.c_bhomebus_handle(cso) |
| | | if handle == nil { |
| | | return errors.New("Init BHomeBus Error") |
| | | } |
| | | |
| | | libbhomebus = handle |
| | | return nil |
| | | } |
| | | |
| | | // Release handle of c softbus |
| | | func Release() { |
| | | if libbhomebus != nil { |
| | | C.c_bhomebus_release(libbhomebus) |
| | | } |
| | | } |
| | | |
| | | // ShmInit block size |
| | | func ShmInit(size int) error { |
| | | if libbhomebus == nil { |
| | | return errors.New("C BHomeBus Handle Is Nil") |
| | | } |
| | | C.wrap_fn_shm_init(libbhomebus, C.int(size)) |
| | | return nil |
| | | } |
| | | |
| | | // ShmAllocKey alloc key |
| | | func ShmAllocKey() int { |
| | | if libbhomebus == nil { |
| | | return -1 |
| | | } |
| | | r := C.wrap_fn_shm_alloc_key(libbhomebus) |
| | | return int(r) |
| | | } |
| | | |
| | | // ShmDestroy destroy shm block, every softbus proc MUST call it |
| | | func ShmDestroy() { |
| | | if libbhomebus != nil { |
| | | C.wrap_fn_shm_destroy(libbhomebus) |
| | | } |
| | | } |
| | | |
| | | // Socket of bhomebus |
| | | type Socket struct { |
| | | socket unsafe.Pointer |
| | | } |
| | | |
| | | // OpenSocket open socket |
| | | func OpenSocket() *Socket { |
| | | if libbhomebus == nil { |
| | | return nil |
| | | } |
| | | |
| | | sock := C.wrap_fn_socket_open(libbhomebus) |
| | | return &Socket{sock} |
| | | } |
| | | |
| | | // Close socket |
| | | func (s *Socket) Close() { |
| | | if libbhomebus == nil { |
| | | return |
| | | } |
| | | C.wrap_fn_socket_close(libbhomebus, s.socket) |
| | | } |
| | | |
| | | // Bind socket |
| | | func (s *Socket) Bind(key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_bind(libbhomebus, s.socket, C.int(key))) |
| | | } |
| | | |
| | | // ForceBind socket |
| | | func (s *Socket) ForceBind(key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_force_bind(libbhomebus, s.socket, C.int(key))) |
| | | } |
| | | |
| | | // Sendto socket |
| | | func (s *Socket) Sendto(data []byte, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_sendto(libbhomebus, s.socket, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(key))) |
| | | } |
| | | |
| | | // SendtoTimeout socket |
| | | func (s *Socket) SendtoTimeout(data []byte, key int, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_sendto_timeout(libbhomebus, s.socket, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(key), 0, C.int(milliseconds*1000000))) |
| | | } |
| | | |
| | | // SendtoNowait socket |
| | | func (s *Socket) SendtoNowait(data []byte, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_sendto_nowait(libbhomebus, s.socket, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(key))) |
| | | } |
| | | |
| | | // Recvfrom socket |
| | | func (s *Socket) Recvfrom(data *[]byte, key *int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | var buf unsafe.Pointer |
| | | var size, ckey C.int |
| | | ret := C.wrap_fn_socket_recvfrom(libbhomebus, s.socket, &buf, &size, &ckey) |
| | | defer C.wrap_fn_socket_free(libbhomebus, buf) |
| | | if ret == 0 && buf != nil { |
| | | *data = C.GoBytes(buf, size) |
| | | *key = int(ckey) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // RecvfromTimeout socket |
| | | func (s *Socket) RecvfromTimeout(data *[]byte, key *int, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | var buf unsafe.Pointer |
| | | var size, ckey C.int |
| | | |
| | | ret := C.wrap_fn_socket_recvfrom_timeout(libbhomebus, s.socket, &buf, &size, &ckey, 0, C.int(milliseconds*1000000)) |
| | | defer C.wrap_fn_socket_free(libbhomebus, buf) |
| | | if ret == 0 && buf != nil { |
| | | *data = C.GoBytes(buf, size) |
| | | *key = int(ckey) |
| | | } |
| | | return int(ret) |
| | | |
| | | } |
| | | |
| | | // RecvfromNowait socket |
| | | func (s *Socket) RecvfromNowait(data *[]byte, key *int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | var buf unsafe.Pointer |
| | | var size, ckey C.int |
| | | ret := C.wrap_fn_socket_recvfrom_nowait(libbhomebus, s.socket, &buf, &size, &ckey) |
| | | defer C.wrap_fn_socket_free(libbhomebus, buf) |
| | | if ret == 0 && buf != nil { |
| | | *data = C.GoBytes(buf, size) |
| | | *key = int(ckey) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // NetNode peer info |
| | | type NetNode struct { |
| | | IPHost string |
| | | Port int |
| | | Key int |
| | | } |
| | | |
| | | // Mesg msg from peer |
| | | type Mesg struct { |
| | | PeerNode NetNode |
| | | Data []byte |
| | | } |
| | | |
| | | func transferMesg(recvMsg unsafe.Pointer, recvMsgSize C.int) []Mesg { |
| | | var msgs []Mesg |
| | | |
| | | var host *C.char |
| | | var content unsafe.Pointer |
| | | var port, key, contentLength C.int |
| | | for i := 0; i < int(recvMsgSize); i++ { |
| | | if 0 == C.get_1_net_mod_recv_msg(recvMsg, C.int(i), &host, &port, &key, &content, &contentLength) { |
| | | msgs = append(msgs, |
| | | Mesg{ |
| | | PeerNode: NetNode{ |
| | | IPHost: C.GoString(host), |
| | | Port: int(port), |
| | | Key: int(key), |
| | | }, |
| | | Data: C.GoBytes(content, contentLength), |
| | | }) |
| | | } |
| | | |
| | | } |
| | | return msgs |
| | | } |
| | | |
| | | // Sendandrecv socket |
| | | func (s *Socket) Sendandrecv(nodes []NetNode, data []byte, msgs *[]Mesg) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | var recvMsg unsafe.Pointer |
| | | var recvMsgSize C.int |
| | | ret := C.wrap_fn_socket_sendandrecv(libbhomebus, s.socket, cnodes, C.int(len(nodes)), unsafe.Pointer(&data[0]), C.int(len(data)), &recvMsg, &recvMsgSize) |
| | | defer C.wrap_fn_socket_free_recv_msg_arr(libbhomebus, recvMsg, recvMsgSize) |
| | | if ret > 0 && recvMsg != nil && recvMsgSize > 0 { |
| | | *msgs = transferMesg(recvMsg, recvMsgSize) |
| | | } |
| | | |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | return int(ret) |
| | | } |
| | | |
| | | // SendandrecvTimeout socket |
| | | func (s *Socket) SendandrecvTimeout(nodes []NetNode, data []byte, msgs *[]Mesg, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | var recvMsg unsafe.Pointer |
| | | var recvMsgSize C.int |
| | | ret := C.wrap_fn_socket_sendandrecv_timeout(libbhomebus, s.socket, cnodes, C.int(len(nodes)), unsafe.Pointer(&data[0]), C.int(len(data)), &recvMsg, &recvMsgSize, C.int(milliseconds)) |
| | | defer C.wrap_fn_socket_free_recv_msg_arr(libbhomebus, recvMsg, recvMsgSize) |
| | | if ret > 0 && recvMsg != nil && recvMsgSize > 0 { |
| | | *msgs = transferMesg(recvMsg, recvMsgSize) |
| | | } |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // SendandrecvNowait socket |
| | | func (s *Socket) SendandrecvNowait(nodes []NetNode, data []byte, msgs *[]Mesg) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | var recvMsg unsafe.Pointer |
| | | var recvMsgSize C.int |
| | | ret := C.wrap_fn_socket_sendandrecv_nowait(libbhomebus, s.socket, cnodes, C.int(len(nodes)), unsafe.Pointer(&data[0]), C.int(len(data)), &recvMsg, &recvMsgSize) |
| | | defer C.wrap_fn_socket_free_recv_msg_arr(libbhomebus, recvMsg, recvMsgSize) |
| | | if ret > 0 && recvMsg != nil && recvMsgSize > 0 { |
| | | *msgs = transferMesg(recvMsg, recvMsgSize) |
| | | } |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // StartBus socket |
| | | func (s *Socket) StartBus() int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_start_bus(libbhomebus, s.socket)) |
| | | } |
| | | |
| | | // Pub socket |
| | | func (s *Socket) Pub(nodes []NetNode, topic string, data []byte) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | ret := C.wrap_fn_socket_pub(libbhomebus, s.socket, cnodes, C.int(len(nodes)), ctopic, C.int(len(topic)), unsafe.Pointer(&data[0]), C.int(len(data))) |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // PubTimeout socket |
| | | func (s *Socket) PubTimeout(nodes []NetNode, topic string, data []byte, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | ret := C.wrap_fn_socket_pub_timeout(libbhomebus, s.socket, cnodes, C.int(len(nodes)), ctopic, C.int(len(topic)), unsafe.Pointer(&data[0]), C.int(len(data)), C.int(milliseconds)) |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // PubNowait socket |
| | | func (s *Socket) PubNowait(nodes []NetNode, topic string, data []byte) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | cnodes := C.create_net_node_t(C.int(len(nodes))) |
| | | defer C.release_net_node_t(cnodes) |
| | | |
| | | if cnodes == nil { |
| | | return -2 |
| | | } |
| | | var ciphosts []*C.char |
| | | for i, v := range nodes { |
| | | chost := C.CString(v.IPHost) |
| | | ciphosts = append(ciphosts, chost) |
| | | C.set_1_net_node(cnodes, C.int(i), chost, C.int(v.Port), C.int(v.Key)) |
| | | } |
| | | |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | ret := C.wrap_fn_socket_pub_nowait(libbhomebus, s.socket, cnodes, C.int(len(nodes)), ctopic, C.int(len(topic)), unsafe.Pointer(&data[0]), C.int(len(data))) |
| | | for _, v := range ciphosts { |
| | | C.free(unsafe.Pointer(v)) |
| | | } |
| | | |
| | | return int(ret) |
| | | } |
| | | |
| | | // Sub socket |
| | | func (s *Socket) Sub(topic string, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_sub(libbhomebus, s.socket, unsafe.Pointer(ctopic), C.int(len(topic)), C.int(key))) |
| | | } |
| | | |
| | | // SubTimeout socket |
| | | func (s *Socket) SubTimeout(topic string, key int, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_sub_timeout(libbhomebus, s.socket, unsafe.Pointer(ctopic), C.int(len(topic)), C.int(key), 0, C.int(milliseconds*1000000))) |
| | | |
| | | } |
| | | |
| | | // SubNowait socket |
| | | func (s *Socket) SubNowait(topic string, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_sub_nowait(libbhomebus, s.socket, unsafe.Pointer(ctopic), C.int(len(topic)), C.int(key))) |
| | | |
| | | } |
| | | |
| | | // Desub socket |
| | | func (s *Socket) Desub(topic string, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_desub(libbhomebus, s.socket, unsafe.Pointer(&ctopic), C.int(len(topic)), C.int(key))) |
| | | } |
| | | |
| | | // DesubTimeout socket |
| | | func (s *Socket) DesubTimeout(topic string, key int, milliseconds int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_desub_timeout(libbhomebus, s.socket, unsafe.Pointer(&ctopic), C.int(len(topic)), C.int(key), 0, C.int(milliseconds*1000000))) |
| | | } |
| | | |
| | | // DesubNowait socket |
| | | func (s *Socket) DesubNowait(topic string, key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | ctopic := C.CString(topic) |
| | | defer C.free(unsafe.Pointer(ctopic)) |
| | | |
| | | return int(C.wrap_fn_socket_desub_nowait(libbhomebus, s.socket, unsafe.Pointer(&ctopic), C.int(len(topic)), C.int(key))) |
| | | } |
| | | |
| | | // Getkey socket |
| | | func (s *Socket) Getkey() int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_get_key(libbhomebus, s.socket)) |
| | | } |
| | | |
| | | // FreeRecvMsgArr socket |
| | | func (s *Socket) FreeRecvMsgArr(data []Mesg) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return 0 |
| | | } |
| | | |
| | | // FreeBuf socket |
| | | func (s *Socket) FreeBuf(buf unsafe.Pointer) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | return 0 |
| | | } |
| | | |
| | | // Removekey socket |
| | | func (s *Socket) Removekey(key int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_remove_key(libbhomebus, C.int(key))) |
| | | } |
| | | |
| | | // Removekeys socket |
| | | func (s *Socket) Removekeys(keys []int) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | ckey := C.create_int_array(C.int(len(keys))) |
| | | defer C.release_int_array(ckey) |
| | | if ckey == nil { |
| | | return -2 |
| | | } |
| | | |
| | | for i, v := range keys { |
| | | C.set_1_int(ckey, C.int(i), C.int(v)) |
| | | } |
| | | |
| | | return int(C.wrap_fn_socket_remove_keys(libbhomebus, ckey, C.int(len(keys)))) |
| | | } |
New file |
| | |
| | | #ifdef __cplusplus |
| | | extern "C"{ |
| | | #endif |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | | |
| | | #include <dlfcn.h> |
| | | #include <stdlib.h> |
| | | #include <stdio.h> |
| | | |
| | | #include "libcbhomebus.h" |
| | | |
| | | #define check_only(fn, lib) \ |
| | | do{ \ |
| | | if(!fn){ \ |
| | | dlclose(lib); \ |
| | | printf("run func %s error\n", #fn); \ |
| | | return; \ |
| | | } \ |
| | | }while(0) |
| | | |
| | | #define check_with_ret(fn, lib, flag) \ |
| | | do{ \ |
| | | if(!fn){ \ |
| | | dlclose(lib); \ |
| | | printf("run func %s error\n", #fn); \ |
| | | return flag; \ |
| | | } \ |
| | | }while(0) |
| | | |
| | | |
| | | hbhomebus c_bhomebus_handle(const char *so){ |
| | | hbhomebus lib = dlopen(so, RTLD_LAZY); |
| | | if(lib){ |
| | | |
| | | fn_shm_destroy = (tfn_shm_destroy)dlsym(lib, l_shm_destroy); |
| | | check_with_ret(fn_shm_destroy, lib, NULL); |
| | | }else{ |
| | | printf("softbus dlopen - %s\n", dlerror()); |
| | | } |
| | | return lib; |
| | | } |
| | | |
| | | void c_bhomebus_release(hbhomebus lib){ |
| | | if(lib) { |
| | | dlclose(lib); |
| | | } |
| | | } |
| | | |
| | | void wrap_fn_shm_init(hbhomebus lib, int size){ |
| | | if (!fn_shm_init){ |
| | | fn_shm_init = (tfn_shm_init)dlsym(lib, l_shm_init); |
| | | check_only(fn_shm_init, lib); |
| | | } |
| | | fn_shm_init(size); |
| | | } |
| | | |
| | | int wrap_fn_shm_alloc_key(hbhomebus lib){ |
| | | if (!fn_shm_alloc_key){ |
| | | fn_shm_alloc_key = (tfn_shm_alloc_key)dlsym(lib, l_shm_alloc_key); |
| | | check_with_ret(fn_shm_alloc_key, lib, -1); |
| | | } |
| | | return fn_shm_alloc_key(); |
| | | } |
| | | |
| | | void wrap_fn_shm_destroy(hbhomebus lib){ |
| | | if (!fn_shm_destroy){ |
| | | printf("shm destroy failed\n"); |
| | | return; |
| | | } |
| | | fn_shm_destroy(); |
| | | } |
| | | |
| | | //////////////////////////////////////////// |
| | | // dgram socket mode |
| | | //////////////////////////////////////////// |
| | | |
| | | void * wrap_fn_socket_open(hbhomebus lib) |
| | | { |
| | | if(!fn_socket_open){ |
| | | fn_socket_open = (tfn_net_mod_socket_open)dlsym(lib,l_net_mod_socket_open); |
| | | check_with_ret(fn_socket_open, lib, NULL); |
| | | } |
| | | return fn_socket_open(); |
| | | } |
| | | |
| | | void wrap_fn_socket_close(hbhomebus lib, void *_socket) |
| | | { |
| | | if(!fn_socket_close){ |
| | | fn_socket_close = (tfn_net_mod_socket_close)dlsym(lib,l_net_mod_socket_close); |
| | | check_only(fn_socket_close, lib); |
| | | } |
| | | return fn_socket_close(_socket); |
| | | } |
| | | |
| | | int wrap_fn_socket_bind(hbhomebus lib, void * _socket, int key) |
| | | { |
| | | if(!fn_socket_bind){ |
| | | fn_socket_bind = (tfn_net_mod_socket_bind)dlsym(lib,l_net_mod_socket_bind); |
| | | check_with_ret(fn_socket_bind, lib, -1); |
| | | } |
| | | return fn_socket_bind(_socket, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_force_bind(hbhomebus lib, void * _socket, int key) |
| | | { |
| | | if(!fn_socket_force_bind){ |
| | | fn_socket_force_bind = (tfn_net_mod_socket_force_bind)dlsym(lib,l_net_mod_socket_force_bind); |
| | | check_with_ret(fn_socket_force_bind, lib, -1); |
| | | } |
| | | return fn_socket_force_bind(_socket, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendto(hbhomebus lib, void *_socket, const void *buf, const int size, const int key) |
| | | { |
| | | if(!fn_socket_sendto){ |
| | | fn_socket_sendto = (tfn_net_mod_socket_sendto)dlsym(lib,l_net_mod_socket_sendto); |
| | | check_with_ret(fn_socket_sendto, lib, -1); |
| | | } |
| | | return fn_socket_sendto(_socket, buf, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendto_timeout(hbhomebus lib, void *_socket, const void *buf, const int size, const int key, int sec, int nsec) |
| | | { |
| | | if(!fn_socket_sendto_timeout){ |
| | | fn_socket_sendto_timeout = (tfn_net_mod_socket_sendto_timeout)dlsym(lib,l_net_mod_socket_sendto_timeout); |
| | | check_with_ret(fn_socket_sendto_timeout, lib, -1); |
| | | } |
| | | return fn_socket_sendto_timeout(_socket, buf, size, key, sec, nsec); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendto_nowait(hbhomebus lib, void *_socket, const void *buf, const int size, const int key) |
| | | { |
| | | if(!fn_socket_sendto_nowait){ |
| | | fn_socket_sendto_nowait = (tfn_net_mod_socket_sendto_nowait)dlsym(lib,l_net_mod_socket_sendto_nowait); |
| | | check_with_ret(fn_socket_sendto_nowait, lib, -1); |
| | | } |
| | | return fn_socket_sendto_nowait(_socket, buf, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_recvfrom(hbhomebus lib, void *_socket, void **buf, int *size, int *key) |
| | | { |
| | | if(!fn_socket_recvfrom){ |
| | | fn_socket_recvfrom = (tfn_net_mod_socket_recvfrom)dlsym(lib,l_net_mod_socket_recvfrom); |
| | | check_with_ret(fn_socket_recvfrom, lib, -1); |
| | | } |
| | | return fn_socket_recvfrom(_socket, buf, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_recvfrom_timeout(hbhomebus lib, void *_socket, void **buf, int *size, int *key, int sec, int nsec) |
| | | { |
| | | if(!fn_socket_recvfrom_timeout){ |
| | | fn_socket_recvfrom_timeout = (tfn_net_mod_socket_recvfrom_timeout)dlsym(lib,l_net_mod_socket_recvfrom_timeout); |
| | | check_with_ret(fn_socket_recvfrom_timeout, lib, -1); |
| | | } |
| | | return fn_socket_recvfrom_timeout(_socket, buf, size, key, sec, nsec); |
| | | } |
| | | |
| | | int wrap_fn_socket_recvfrom_nowait(hbhomebus lib, void *_socket, void **buf, int *size, int *key) |
| | | { |
| | | if(!fn_socket_recvfrom_nowait){ |
| | | fn_socket_recvfrom_nowait = (tfn_net_mod_socket_recvfrom_nowait)dlsym(lib,l_net_mod_socket_recvfrom_nowait); |
| | | check_with_ret(fn_socket_recvfrom_nowait, lib, -1); |
| | | } |
| | | return fn_socket_recvfrom_nowait(_socket, buf, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendandrecv(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size) |
| | | { |
| | | if(!fn_socket_sendandrecv){ |
| | | fn_socket_sendandrecv = (tfn_net_mod_socket_sendandrecv)dlsym(lib,l_net_mod_socket_sendandrecv); |
| | | check_with_ret(fn_socket_sendandrecv, lib, -1); |
| | | } |
| | | return fn_socket_sendandrecv(_sockt, (net_node_t*) node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendandrecv_timeout(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size, int timeout) |
| | | { |
| | | if(!fn_socket_sendandrecv_timeout){ |
| | | fn_socket_sendandrecv_timeout = (tfn_net_mod_socket_sendandrecv_timeout)dlsym(lib,l_net_mod_socket_sendandrecv_timeout); |
| | | check_with_ret(fn_socket_sendandrecv_timeout, lib, -1); |
| | | } |
| | | return fn_socket_sendandrecv_timeout(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size, timeout); |
| | | } |
| | | |
| | | int wrap_fn_socket_sendandrecv_nowait(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size) |
| | | { |
| | | if(!fn_socket_sendandrecv_nowait){ |
| | | fn_socket_sendandrecv_nowait = (tfn_net_mod_socket_sendandrecv_nowait)dlsym(lib,l_net_mod_socket_sendandrecv_nowait); |
| | | check_with_ret(fn_socket_sendandrecv_nowait, lib, -1); |
| | | } |
| | | return fn_socket_sendandrecv_nowait(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size); |
| | | } |
| | | |
| | | int wrap_fn_socket_start_bus(hbhomebus lib, void * _socket) |
| | | { |
| | | if(!fn_socket_start_bus){ |
| | | fn_socket_start_bus = (tfn_net_mod_socket_start_bus)dlsym(lib,l_net_mod_socket_start_bus); |
| | | check_with_ret(fn_socket_start_bus, lib, -1); |
| | | } |
| | | return fn_socket_start_bus(_socket); |
| | | } |
| | | |
| | | int wrap_fn_socket_pub(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size) |
| | | { |
| | | if(!fn_socket_pub){ |
| | | fn_socket_pub = (tfn_net_mod_socket_pub)dlsym(lib,l_net_mod_socket_pub); |
| | | check_with_ret(fn_socket_pub, lib, -1); |
| | | } |
| | | return fn_socket_pub(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | | |
| | | int wrap_fn_socket_pub_timeout(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout) |
| | | { |
| | | if(!fn_socket_pub_timeout){ |
| | | fn_socket_pub_timeout = (tfn_net_mod_socket_pub_timeout)dlsym(lib,l_net_mod_socket_pub_timeout); |
| | | check_with_ret(fn_socket_pub_timeout, lib, -1); |
| | | } |
| | | return fn_socket_pub_timeout(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size, timeout); |
| | | } |
| | | |
| | | int wrap_fn_socket_pub_nowait(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size) |
| | | { |
| | | if(!fn_socket_pub_nowait){ |
| | | fn_socket_pub_nowait = (tfn_net_mod_socket_pub_nowait)dlsym(lib,l_net_mod_socket_pub_nowait); |
| | | check_with_ret(fn_socket_pub_nowait, lib, -1); |
| | | } |
| | | return fn_socket_pub_nowait(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | | |
| | | int wrap_fn_socket_sub(hbhomebus lib, void * _socket, void *topic, int size, int key) |
| | | { |
| | | if(!fn_socket_sub){ |
| | | fn_socket_sub = (tfn_net_mod_socket_sub)dlsym(lib,l_net_mod_socket_sub); |
| | | check_with_ret(fn_socket_sub, lib, -1); |
| | | } |
| | | return fn_socket_sub(_socket, topic, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_sub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec) |
| | | { |
| | | if(!fn_socket_sub_timeout){ |
| | | fn_socket_sub_timeout = (tfn_net_mod_socket_sub_timeout)dlsym(lib,l_net_mod_socket_sub_timeout); |
| | | check_with_ret(fn_socket_sub_timeout, lib, -1); |
| | | } |
| | | return fn_socket_sub_timeout(_socket, topic, size, key, sec, nsec); |
| | | } |
| | | |
| | | int wrap_fn_socket_sub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key) |
| | | { |
| | | if(!fn_socket_sub_nowait){ |
| | | fn_socket_sub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait); |
| | | check_with_ret(fn_socket_sub_nowait, lib, -1); |
| | | } |
| | | return fn_socket_sub_nowait(_socket, topic, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_desub(hbhomebus lib, void * _socket, void *topic, int size, int key) |
| | | { |
| | | if(!fn_socket_desub){ |
| | | fn_socket_desub = (tfn_net_mod_socket_desub)dlsym(lib,l_net_mod_socket_desub); |
| | | check_with_ret(fn_socket_desub, lib, -1); |
| | | } |
| | | return fn_socket_desub(_socket, topic, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_desub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec) |
| | | { |
| | | if(!fn_socket_desub_timeout){ |
| | | fn_socket_desub_timeout = (tfn_net_mod_socket_desub_timeout)dlsym(lib,l_net_mod_socket_desub_timeout); |
| | | check_with_ret(fn_socket_desub_timeout, lib, -1); |
| | | } |
| | | return fn_socket_desub_timeout(_socket, topic, size, key, sec, nsec); |
| | | } |
| | | |
| | | int wrap_fn_socket_desub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key) |
| | | { |
| | | if(!fn_socket_desub_nowait){ |
| | | fn_socket_desub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait); |
| | | check_with_ret(fn_socket_desub_nowait, lib, -1); |
| | | } |
| | | return fn_socket_desub_nowait(_socket, topic, size, key); |
| | | } |
| | | |
| | | int wrap_fn_socket_get_key(hbhomebus lib, void * _socket) |
| | | { |
| | | if(!fn_socket_get_key){ |
| | | fn_socket_get_key = (tfn_net_mod_socket_get_key)dlsym(lib,l_net_mod_socket_get_key); |
| | | check_with_ret(fn_socket_get_key, lib, -1); |
| | | } |
| | | return fn_socket_get_key(_socket); |
| | | } |
| | | |
| | | void wrap_fn_socket_free_recv_msg_arr(hbhomebus lib, void * arr, int size) |
| | | { |
| | | if(!fn_socket_free_recv_msg_arr){ |
| | | fn_socket_free_recv_msg_arr = (tfn_net_mod_socket_free_recv_msg_arr)dlsym(lib,l_net_mod_socket_free_recv_msg_arr); |
| | | check_only(fn_socket_free_recv_msg_arr, lib); |
| | | } |
| | | return fn_socket_free_recv_msg_arr((net_mod_recv_msg_t*)arr, size); |
| | | } |
| | | |
| | | void wrap_fn_socket_free(hbhomebus lib, void *buf) |
| | | { |
| | | if(!fn_socket_free){ |
| | | fn_socket_free = (tfn_net_mod_socket_free)dlsym(lib,l_net_mod_socket_free); |
| | | check_only(fn_socket_free, lib); |
| | | } |
| | | return fn_socket_free(buf); |
| | | } |
| | | |
| | | int wrap_fn_socket_remove_key(hbhomebus lib, int key){ |
| | | if (!fn_socket_remove_key){ |
| | | fn_socket_remove_key = (tfn_shm_mod_socket_remove_key)dlsym(lib, l_shm_mod_socket_remove_key); |
| | | check_with_ret(fn_socket_remove_key, lib, -1); |
| | | } |
| | | return fn_socket_remove_key(key); |
| | | } |
| | | |
| | | int wrap_fn_socket_remove_keys(hbhomebus lib, void *keys, int length){ |
| | | if (!fn_socket_remove_keys){ |
| | | fn_socket_remove_keys = (tfn_shm_mod_socket_remove_keys)dlsym(lib, l_shm_mod_socket_remove_keys); |
| | | check_with_ret(fn_socket_remove_keys, lib, -1); |
| | | } |
| | | return fn_socket_remove_keys((int*)keys, length); |
| | | } |
New file |
| | |
| | | #ifndef _c_libshm_queue_so_go_wrapper_h_ |
| | | #define _c_libshm_queue_so_go_wrapper_h_ |
| | | |
| | | |
| | | #ifdef __cplusplus |
| | | extern "C"{ |
| | | #endif |
| | | |
| | | #include "libcbhomebus_func.h" |
| | | #include <stddef.h> |
| | | |
| | | // shm manipulate |
| | | static tfn_shm_init fn_shm_init = NULL; |
| | | static tfn_shm_alloc_key fn_shm_alloc_key = NULL; |
| | | static tfn_shm_destroy fn_shm_destroy = NULL; |
| | | |
| | | // net mode socket |
| | | static tfn_net_mod_socket_open fn_socket_open = NULL; |
| | | static tfn_net_mod_socket_close fn_socket_close = NULL; |
| | | static tfn_net_mod_socket_bind fn_socket_bind = NULL; |
| | | static tfn_net_mod_socket_force_bind fn_socket_force_bind = NULL; |
| | | static tfn_net_mod_socket_sendto fn_socket_sendto = NULL; |
| | | static tfn_net_mod_socket_sendto_timeout fn_socket_sendto_timeout = NULL; |
| | | static tfn_net_mod_socket_sendto_nowait fn_socket_sendto_nowait = NULL; |
| | | static tfn_net_mod_socket_recvfrom fn_socket_recvfrom = NULL; |
| | | static tfn_net_mod_socket_recvfrom_timeout fn_socket_recvfrom_timeout = NULL; |
| | | static tfn_net_mod_socket_recvfrom_nowait fn_socket_recvfrom_nowait = NULL; |
| | | static tfn_net_mod_socket_sendandrecv fn_socket_sendandrecv = NULL; |
| | | static tfn_net_mod_socket_sendandrecv_timeout fn_socket_sendandrecv_timeout = NULL; |
| | | static tfn_net_mod_socket_sendandrecv_nowait fn_socket_sendandrecv_nowait = NULL; |
| | | static tfn_net_mod_socket_start_bus fn_socket_start_bus = NULL; |
| | | static tfn_net_mod_socket_pub fn_socket_pub = NULL; |
| | | static tfn_net_mod_socket_pub_timeout fn_socket_pub_timeout = NULL; |
| | | static tfn_net_mod_socket_pub_nowait fn_socket_pub_nowait = NULL; |
| | | static tfn_net_mod_socket_sub fn_socket_sub = NULL; |
| | | static tfn_net_mod_socket_sub_timeout fn_socket_sub_timeout = NULL; |
| | | static tfn_net_mod_socket_sub_nowait fn_socket_sub_nowait = NULL; |
| | | static tfn_net_mod_socket_desub fn_socket_desub = NULL; |
| | | static tfn_net_mod_socket_desub_timeout fn_socket_desub_timeout = NULL; |
| | | static tfn_net_mod_socket_desub_nowait fn_socket_desub_nowait = NULL; |
| | | static tfn_net_mod_socket_get_key fn_socket_get_key = NULL; |
| | | static tfn_net_mod_socket_free_recv_msg_arr fn_socket_free_recv_msg_arr = NULL; |
| | | static tfn_net_mod_socket_free fn_socket_free = NULL; |
| | | static tfn_shm_mod_socket_remove_key fn_socket_remove_key = NULL; |
| | | static tfn_shm_mod_socket_remove_keys fn_socket_remove_keys = NULL; |
| | | |
| | | ////////////////////////////////////////////////////////////////////// |
| | | |
| | | // labels |
| | | // shm |
| | | const static char l_shm_init[] = "shm_mm_wrapper_init"; |
| | | const static char l_shm_destroy[] = "shm_mm_wrapper_destroy"; |
| | | const static char l_shm_alloc_key[] = "shm_mm_wrapper_alloc_key"; |
| | | |
| | | // net mode socket |
| | | const static char l_socket_open[] = "net_mod_socket_open"; |
| | | const static char l_net_mod_socket_open[] = "net_mod_socket_open"; |
| | | const static char l_net_mod_socket_close[] = "net_mod_socket_close"; |
| | | const static char l_net_mod_socket_bind[] = "net_mod_socket_bind"; |
| | | const static char l_net_mod_socket_force_bind[] = "net_mod_socket_force_bind"; |
| | | const static char l_net_mod_socket_sendto[] = "net_mod_socket_sendto"; |
| | | const static char l_net_mod_socket_sendto_timeout[] = "net_mod_socket_sendto_timeout"; |
| | | const static char l_net_mod_socket_sendto_nowait[] = "net_mod_socket_sendto_nowait"; |
| | | const static char l_net_mod_socket_recvfrom[] = "net_mod_socket_recvfrom"; |
| | | const static char l_net_mod_socket_recvfrom_timeout[] = "net_mod_socket_recvfrom_timeout"; |
| | | const static char l_net_mod_socket_recvfrom_nowait[] = "net_mod_socket_recvfrom_nowait"; |
| | | const static char l_net_mod_socket_sendandrecv[] = "net_mod_socket_sendandrecv"; |
| | | const static char l_net_mod_socket_sendandrecv_timeout[] = "net_mod_socket_sendandrecv_timeout"; |
| | | const static char l_net_mod_socket_sendandrecv_nowait[] = "net_mod_socket_sendandrecv_nowait"; |
| | | const static char l_net_mod_socket_start_bus[] = "net_mod_socket_start_bus"; |
| | | const static char l_net_mod_socket_pub[] = "net_mod_socket_pub"; |
| | | const static char l_net_mod_socket_pub_timeout[] = "net_mod_socket_pub_timeout"; |
| | | const static char l_net_mod_socket_pub_nowait[] = "net_mod_socket_pub_nowait"; |
| | | const static char l_net_mod_socket_sub[] = "net_mod_socket_sub"; |
| | | const static char l_net_mod_socket_sub_timeout[] = "net_mod_socket_sub_timeout"; |
| | | const static char l_net_mod_socket_sub_nowait[] = "net_mod_socket_sub_nowait"; |
| | | const static char l_net_mod_socket_desub[] = "net_mod_socket_desub"; |
| | | const static char l_net_mod_socket_desub_timeout[] = "net_mod_socket_desub_timeout"; |
| | | const static char l_net_mod_socket_desub_nowait[] = "net_mod_socket_desub_nowait"; |
| | | const static char l_net_mod_socket_get_key[] = "net_mod_socket_get_key"; |
| | | const static char l_net_mod_socket_free_recv_msg_arr[] = "net_mod_socket_free_recv_msg_arr"; |
| | | const static char l_net_mod_socket_free[] = "net_mod_socket_free"; |
| | | const static char l_shm_mod_socket_remove_key[] = "shm_mod_socket_remove_key"; |
| | | const static char l_shm_mod_socket_remove_keys[] = "shm_mod_socket_remove_keys"; |
| | | ////////////////////////////////////////////////////////////////////// |
| | | |
| | | // dlopen dynamic library |
| | | typedef void* hbhomebus; |
| | | hbhomebus c_bhomebus_handle(const char *so); |
| | | void c_bhomebus_release(hbhomebus lib); |
| | | |
| | | // shm manipulate |
| | | void wrap_fn_shm_init(hbhomebus lib, int size); |
| | | int wrap_fn_shm_alloc_key(hbhomebus lib); |
| | | void wrap_fn_shm_destroy(hbhomebus lib); |
| | | |
| | | ///////////////////////////////////////////////////////// |
| | | // net mode socket |
| | | void * wrap_fn_socket_open(hbhomebus lib); |
| | | void wrap_fn_socket_close(hbhomebus lib, void *_sockt); |
| | | int wrap_fn_socket_bind(hbhomebus lib, void * _socket, int key); |
| | | int wrap_fn_socket_force_bind(hbhomebus lib, void * _socket, int key); |
| | | int wrap_fn_socket_sendto(hbhomebus lib, void *_socket, const void *buf, const int size, const int key); |
| | | int wrap_fn_socket_sendto_timeout(hbhomebus lib, void *_socket, const void *buf, const int size, const int key, int sec, int nsec); |
| | | int wrap_fn_socket_sendto_nowait(hbhomebus lib, void *_socket, const void *buf, const int size, const int key); |
| | | int wrap_fn_socket_recvfrom(hbhomebus lib, void *_socket, void **buf, int *size, int *key); |
| | | int wrap_fn_socket_recvfrom_timeout(hbhomebus lib, void *_socket, void **buf, int *size, int *key, int sec, int nsec); |
| | | int wrap_fn_socket_recvfrom_nowait(hbhomebus lib, void *_socket, void **buf, int *size, int *key); |
| | | int wrap_fn_socket_sendandrecv(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size) ; |
| | | int wrap_fn_socket_sendandrecv_timeout(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size, int timeout); |
| | | int wrap_fn_socket_sendandrecv_nowait(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size, |
| | | void ** recv_arr, int *recv_arr_size) ; |
| | | int wrap_fn_socket_start_bus(hbhomebus lib, void * _socket); |
| | | int wrap_fn_socket_pub(hbhomebus lib, void *_sockt, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | int wrap_fn_socket_pub_timeout(hbhomebus lib, void *_sockt, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout); |
| | | int wrap_fn_socket_pub_nowait(hbhomebus lib, void *_sockt, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | int wrap_fn_socket_sub(hbhomebus lib, void * _socket, void *topic, int size, int key); |
| | | int wrap_fn_socket_sub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | int wrap_fn_socket_sub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key); |
| | | int wrap_fn_socket_desub(hbhomebus lib, void * _socket, void *topic, int size, int key); |
| | | int wrap_fn_socket_desub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | int wrap_fn_socket_desub_nowait(hbhomebus lib, void * _socket, void *topic, int size, int key); |
| | | int wrap_fn_socket_get_key(hbhomebus lib, void * _socket) ; |
| | | void wrap_fn_socket_free_recv_msg_arr(hbhomebus lib, void * arr, int size); |
| | | void wrap_fn_socket_free(hbhomebus lib, void *buf) ; |
| | | |
| | | int wrap_fn_socket_remove_key(hbhomebus lib, int key); |
| | | int wrap_fn_socket_remove_keys(hbhomebus lib, void *keys, int length); |
| | | |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | | |
| | | #endif |
New file |
| | |
| | | #ifndef _c_libshm_queue_so_func_h_ |
| | | #define _c_libshm_queue_so_func_h_ |
| | | |
| | | |
| | | #ifdef __cplusplus |
| | | extern "C"{ |
| | | #endif |
| | | |
| | | #ifndef net_node_t |
| | | typedef struct _net_node_t{ |
| | | const char *host; |
| | | int port; |
| | | int key; |
| | | } net_node_t; |
| | | #endif |
| | | |
| | | #ifndef net_mod_recv_msg_t |
| | | #ifndef NI_MAXHOST |
| | | #define _BSD_SOURCE /* To get definitions of NI_MAXHOST and |
| | | NI_MAXSERV from <netdb.h> */ |
| | | #include <netdb.h> |
| | | // #define NI_MAXHOST 1025 |
| | | #endif |
| | | typedef struct _net_mod_recv_msg_t{ |
| | | char host[NI_MAXHOST]; |
| | | int port; |
| | | int key; |
| | | void *content; |
| | | int content_length; |
| | | } net_mod_recv_msg_t; |
| | | #endif |
| | | |
| | | /** |
| | | * 初始化共享内存 |
| | | * @size 共享内存大小 |
| | | * |
| | | */ |
| | | typedef void(*tfn_shm_init)(int); |
| | | /** |
| | | * 销毁共享内存 |
| | | * 整个进程退出时需要执行这个方法,该方法首先会检查是否还有其他进程在使用该共享内存,如果还有其他进程在使用就只是detach,如果没有其他进程在使用则销毁整块内存。 |
| | | */ |
| | | typedef void(*tfn_shm_destroy)(); |
| | | /** |
| | | * 获取key |
| | | */ |
| | | typedef int(*tfn_shm_alloc_key) (); |
| | | |
| | | |
| | | ///////////////////////////////////////////// |
| | | // net_mod_socket |
| | | /** |
| | | * 创建 |
| | | */ |
| | | typedef void * (*tfn_net_mod_socket_open)(); |
| | | |
| | | /** |
| | | * 关闭 |
| | | */ |
| | | typedef void (*tfn_net_mod_socket_close)(void *_sockt); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_bind)(void * _socket, int key); |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_force_bind)(void * _socket, int key); |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_sendto)(void *_socket, const void *buf, const int size, const int key); |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | typedef int (*tfn_net_mod_socket_sendto_timeout)(void *_socket, const void *buf, const int size, const int key, int sec, int nsec); |
| | | // 发送信息立刻返回。 |
| | | typedef int (*tfn_net_mod_socket_sendto_nowait)(void *_socket, const void *buf, const int size, const int key); |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @key 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_recvfrom)(void *_socket, void **buf, int *size, int *key); |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | typedef int (*tfn_net_mod_socket_recvfrom_timeout)(void *_socket, void **buf, int *size, int *key, int sec, int nsec); |
| | | typedef int (*tfn_net_mod_socket_recvfrom_nowait)(void *_socket, void **buf, int *size, int *key); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息会一直等待 |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2性能好 |
| | | * 缺点:不是线程安全的, 即不能有两个以上的线程同时使用这个对象的方法 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_sendandrecv)(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息等待timeout的时间后返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_sendandrecv_timeout)(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout); |
| | | |
| | | /** |
| | | * 不等待立即返回 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_sendandrecv_nowait)(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_start_bus)(void * _socket); |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @topic 主题,@topic_size 该主题的长度 |
| | | * @content 内容,@content_size 内容长度 |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_pub)(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | typedef int (*tfn_net_mod_socket_pub_timeout)(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout); |
| | | typedef int (*tfn_net_mod_socket_pub_nowait)(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_sub)(void * _socket, void *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | typedef int (*tfn_net_mod_socket_sub_timeout)(void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | typedef int (*tfn_net_mod_socket_sub_nowait)(void * _socket, void *topic, int size, int key); |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @key 总线端口 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_desub)(void * _socket, void *topic, int size, int key); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | typedef int (*tfn_net_mod_socket_desub_timeout)(void * _socket, void *topic, int size, int key, int sec, int nsec); |
| | | typedef int (*tfn_net_mod_socket_desub_nowait)(void * _socket, void *topic, int size, int key); |
| | | |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | */ |
| | | typedef int (*tfn_net_mod_socket_get_key)(void * _socket) ; |
| | | |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | | * @arr 消息组 |
| | | * @size 消息组的长度 |
| | | */ |
| | | typedef void (*tfn_net_mod_socket_free_recv_msg_arr)(net_mod_recv_msg_t * arr, int size); |
| | | |
| | | |
| | | /** |
| | | * 释放存储接收信息的buf |
| | | */ |
| | | typedef void (*tfn_net_mod_socket_free)(void *buf) ; |
| | | |
| | | typedef int(*tfn_shm_mod_socket_remove_key) (int); |
| | | /** |
| | | * 批量删除key对应的共享队列,并在bus里删除该key的订阅 |
| | | */ |
| | | typedef int(*tfn_shm_mod_socket_remove_keys) (int*, int); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | | |
| | | #endif |