| | |
| | | #include <stdio.h> |
| | | #include "libcbhomebus.h" |
| | | |
| | | void* create_net_node_t(const int size){ |
| | | static 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){ |
| | | static 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){ |
| | | static 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, |
| | | static 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; |
| | |
| | | *content_length = msg[index].content_length; |
| | | return 0; |
| | | } |
| | | void* create_int_array(const int size){ |
| | | static void* create_int_array(const int size){ |
| | | int* arr = (int*)malloc(sizeof(int) * size); |
| | | return arr; |
| | | } |
| | | void release_int_array(void* arr){ |
| | | static void release_int_array(void* arr){ |
| | | int* carr = (int*)arr; |
| | | free(carr); |
| | | } |
| | | int set_1_int(void* arr, const int index, const int key){ |
| | | static 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; |
| | | } |
| | | |
| | | typedef void(*recvandsend_callback_fn)(void*, int, int, void**, int *, void *); |
| | | |
| | | extern void cCallback(void *recvbuf, int recvsize, int recvkey, |
| | | void **sendbuf, int *sendsize, void *user_data); |
| | | */ |
| | | import "C" |
| | | import ( |
| | | "errors" |
| | | "unsafe" |
| | | ) |
| | | |
| | | // add recvandsend funcs |
| | | //export cCallback |
| | | func cCallback(rbuf unsafe.Pointer, rsize C.int, rkey C.int, |
| | | sbuf *unsafe.Pointer, ssize *C.int, userData unsafe.Pointer) { |
| | | |
| | | v := Restore(userData).(*goCallback) |
| | | rdata := C.GoBytes(rbuf, rsize) |
| | | |
| | | var sdata []byte |
| | | |
| | | if v.cb(rdata, int(rkey), &sdata) { |
| | | *sbuf = unsafe.Pointer(&sdata[0]) |
| | | *ssize = C.int(len(sdata)) |
| | | } else { |
| | | *sbuf = nil |
| | | *ssize = 0 |
| | | } |
| | | } |
| | | |
| | | type goCallback struct { |
| | | cb func(rdata []byte, rkey int, sdata *[]byte) bool |
| | | } |
| | | |
| | | // Recvandsend socket |
| | | func (s *Socket) Recvandsend(fn func(rdata []byte, rkey int, sdata *[]byte) bool) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | gcb := goCallback{ |
| | | cb: fn, |
| | | } |
| | | p := Save(&gcb) |
| | | defer Unref(p) |
| | | |
| | | cbC := (C.recvandsend_callback_fn)(unsafe.Pointer(C.cCallback)) |
| | | |
| | | ret := C.wrap_fn_socket_recvandsend(libbhomebus, s.socket, cbC, p) |
| | | return int(ret) |
| | | } |
| | | |
| | | // RecvandsendTimeout socket |
| | | func (s *Socket) RecvandsendTimeout(milliseconds int, fn func(rdata []byte, rkey int, sdata *[]byte) bool) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | gcb := goCallback{ |
| | | cb: fn, |
| | | } |
| | | p := Save(&gcb) |
| | | defer Unref(p) |
| | | |
| | | cbC := (C.recvandsend_callback_fn)(unsafe.Pointer(C.cCallback)) |
| | | |
| | | ret := C.wrap_fn_socket_recvandsend_timeout(libbhomebus, s.socket, cbC, 0, C.int(milliseconds*1000000), p) |
| | | return int(ret) |
| | | } |
| | | |
| | | // RecvandsendNowait socket |
| | | func (s *Socket) RecvandsendNowait(fn func(rdata []byte, rkey int, sdata *[]byte) bool) int { |
| | | if libbhomebus == nil || s.socket == nil { |
| | | return -1 |
| | | } |
| | | |
| | | gcb := goCallback{ |
| | | cb: fn, |
| | | } |
| | | p := Save(&gcb) |
| | | defer Unref(p) |
| | | |
| | | cbC := (C.recvandsend_callback_fn)(unsafe.Pointer(C.cCallback)) |
| | | |
| | | ret := C.wrap_fn_socket_recvandsend_nowait(libbhomebus, s.socket, cbC, p) |
| | | return int(ret) |
| | | } |
| | | |
| | | var libbhomebus C.hbhomebus |
| | | |
| | |
| | | return int(C.wrap_fn_server_socket_start(libbhomebus, s.socket)) |
| | | } |
| | | |
| | | // ServerOpen bus server |
| | | // BusServerOpen bus server |
| | | func BusServerOpen() *Socket { |
| | | if libbhomebus == nil { |
| | | return nil |
| | |
| | | return &Socket{sock} |
| | | } |
| | | |
| | | // Close close |
| | | // BusClose close |
| | | func (s *Socket) BusClose() { |
| | | if libbhomebus == nil { |
| | | return |
| | |
| | | C.wrap_fn_bus_server_socket_close(libbhomebus, s.socket) |
| | | } |
| | | |
| | | // Start start |
| | | // BusStart start |
| | | func (s *Socket) BusStart() int { |
| | | if libbhomebus == nil { |
| | | return -1 |