}
static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
- const char *msg = data;
-
if(!len) {
if(meshlink_errno)
fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
}
// TODO: we now have TCP semantics, don't expect exactly one message per receive call.
- if(msg[len - 1]) {
- fprintf(stderr, "Received invalid data from %s\n", channel->node->name);
- return;
- }
- printf("%s says: %s\n", channel->node->name, msg);
+ printf("%s says: ", channel->node->name);
+ fwrite(data, len, 1, stdout);
+ fputc('\n', stdout);
}
static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
destination->priv = channel;
}
- if(!meshlink_channel_send(mesh, channel, msg, len + 1)) {
+ if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
return;
}
static bool channel_pre_accept(struct utcp *utcp, uint16_t port) {
//TODO: implement
- return false;
-}
-
-static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) {
- //TODO: implement
+ return true;
}
-static int channel_recv(struct utcp_connection *connection, const void *data, size_t len) {
+static ssize_t channel_recv(struct utcp_connection *connection, const void *data, size_t len) {
meshlink_channel_t *channel = connection->priv;
+ if(!channel)
+ abort();
node_t *n = channel->node;
meshlink_handle_t *mesh = n->mesh;
if(!channel->receive_cb)
return -1;
else {
channel->receive_cb(mesh, channel, data, len);
- return 0;
+ return len;
}
}
-static int channel_send(struct utcp *utcp, const void *data, size_t len) {
+static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) {
+ node_t *n = utcp_connection->utcp->priv;
+ if(!n)
+ abort();
+ meshlink_handle_t *mesh = n->mesh;
+ if(!mesh->channel_accept_cb)
+ return;
+ meshlink_channel_t *channel = xzalloc(sizeof *channel);
+ channel->node = n;
+ channel->c = utcp_connection;
+ if(mesh->channel_accept_cb(mesh, channel, port, NULL, 0))
+ utcp_accept(utcp_connection, channel_recv, channel);
+ else
+ free(channel);
+}
+
+static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) {
node_t *n = utcp->priv;
meshlink_handle_t *mesh = n->mesh;
+ char hex[len * 2 + 1];
+ bin2hex(data, hex, len);
+ logger(mesh, MESHLINK_WARNING, "channel_send(%p, %p, %zu): %s\n", utcp, data, len, hex);
return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1;
}
channel->receive_cb = cb;
}
+static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
+ node_t *n = (node_t *)source;
+ if(!n->utcp)
+ abort();
+ char hex[len * 2 + 1];
+ bin2hex(data, hex, len);
+ logger(mesh, MESHLINK_WARNING, "channel_receive(%p, %p, %zu): %s\n", n->utcp, data, len, hex);
+ utcp_recv(n->utcp, data, len);
+}
+
+void meshlink_channel_init(meshlink_handle_t *mesh) {
+}
+
meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len) {
+ logger(mesh, MESHLINK_WARNING, "meshlink_channel_open(%p, %s, %u, %p, %p, %zu)\n", mesh, node->name, port, cb, data, len);
node_t *n = (node_t *)node;
if(!n->utcp) {
n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
- if(!n->utcp)
+ mesh->receive_cb = channel_receive;
+ if(!n->utcp) {
+ meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
return NULL;
+ }
}
meshlink_channel_t *channel = xzalloc(sizeof *channel);
channel->node = n;
channel->receive_cb = cb;
channel->c = utcp_connect(n->utcp, port, channel_recv, channel);
if(!channel->c) {
+ meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
free(channel);
return NULL;
}