#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/time.h>
#include "../src/meshlink.h"
volatile bool bar_responded = false;
void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
+ static struct timeval tv0;
+ struct timeval tv;
+
+ if(tv0.tv_sec == 0)
+ gettimeofday(&tv0, NULL);
+ gettimeofday(&tv, NULL);
+ fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec-tv0.tv_sec), (unsigned int)tv.tv_usec/1000);
+
if(mesh)
fprintf(stderr, "(%s) ", mesh->name);
fprintf(stderr, "[%d] %s\n", level, text);
}
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
+ printf("status_cb: %s %sreachable\n", node->name, reachable?"":"un");
if(!strcmp(node->name, "bar"))
bar_reachable = reachable;
}
void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
+ printf("foo_receive_cb %zu: ", len); fwrite(data, 1, len, stdout); printf("\n");
if(len == 5 && !memcmp(data, "Hello", 5))
bar_responded = true;
}
void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
+ printf("bar_receive_cb %zu: ", len); fwrite(data, 1, len, stdout);
// Echo the data back.
meshlink_channel_send(mesh, channel, data, len);
}
}
bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
+ printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port);
+ if(data) { fwrite(data, 1, len, stdout); printf("\n"); }
+
if(port != 7)
return false;
meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
return 1;
}
+ meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
+ meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
+
// Import and export both side's data
meshlink_add_address(mesh1, "localhost");