From: Guus Sliepen Date: Wed, 30 Jul 2014 13:09:50 +0000 (+0200) Subject: Fix the C++ wrapper and the examples. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=9ff28ad77a6deee4db709c127c843f9fdd68e03b;p=meshlink Fix the C++ wrapper and the examples. --- diff --git a/examples/chat.c b/examples/chat.c index 1c07e422..3dbbb860 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -27,6 +27,9 @@ static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool rea printf("%s left.\n", node->name); } +static meshlink_node_t **nodes; +static size_t nnodes; + static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); if(arg) @@ -75,16 +78,13 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { printf("Node '%s' blacklisted.\n", arg); } else if(!strcasecmp(buf, "who")) { if(!arg) { - meshlink_node_t *nodes[100]; - size_t n = meshlink_get_all_nodes(mesh, nodes, 100); - if(!n) { + nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); + if(!nnodes) { fprintf(stderr, "No nodes known!\n"); } else { printf("Known nodes:"); - for(int i = 0; i < n && i < 100; i++) + for(int i = 0; i < nnodes; i++) printf(" %s", nodes[i]->name); - if(n > 100) - printf(" (and %zu more)", n - 100); printf("\n"); } } else { diff --git a/examples/chatpp.cc b/examples/chatpp.cc index 73ced3c6..00863d50 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -27,6 +27,9 @@ static void node_status(meshlink::mesh *mesh, meshlink::node *node, bool reachab printf("%s left.\n", node->name); } +static meshlink::node **nodes; +static size_t nnodes; + static void parse_command(meshlink::mesh *mesh, char *buf) { char *arg = strchr(buf, ' '); if(arg) @@ -75,16 +78,13 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { printf("Node '%s' blacklisted.\n", arg); } else if(!strcasecmp(buf, "who")) { if(!arg) { - meshlink::node *nodes[100]; - size_t n = mesh->get_all_nodes(nodes, 100); - if(!n) { + nodes = mesh->get_all_nodes(nodes, &nnodes); + if(!nnodes) { fprintf(stderr, "No nodes known!\n"); } else { printf("Known nodes:"); - for(size_t i = 0; i < n && i < 100; i++) + for(size_t i = 0; i < nnodes; i++) printf(" %s", nodes[i]->name); - if(n > 100) - printf(" (and %zu more)", n - 100); printf("\n"); } } else { diff --git a/src/meshlink++.h b/src/meshlink++.h index 2729130c..1d6c9861 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -185,8 +185,8 @@ namespace meshlink { * @return The number of known nodes, or -1 in case of an error. * This can be larger than nmemb, in which case not all nodes were stored in the nodes array. */ - ssize_t get_all_nodes(node **nodes, size_t nmemb) { - return meshlink_get_all_nodes(this, (meshlink_node_t **)nodes, nmemb); + node **get_all_nodes(node **nodes, size_t *nmemb) { + return (node **)meshlink_get_all_nodes(this, (meshlink_node_t **)nodes, nmemb); } /// Sign data using the local node's MeshLink key.