X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink%2B%2B.h;h=176dd1b6d95d3c1c08bb25750990407a58d28caf;hb=8d213e55f77461b182e5276823f9ba1ae56ee638;hp=49d7f602bdc88acdd1e502b81fa4ccea00dfa9fc;hpb=ad6a0bf4c6b0fce2cf29e0a7089909e23a577155;p=meshlink diff --git a/src/meshlink++.h b/src/meshlink++.h index 49d7f602..176dd1b6 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -149,7 +149,10 @@ namespace meshlink { */ void close() { if(handle) + { + handle->priv = 0; meshlink_close(handle); + } handle=0; } @@ -172,6 +175,10 @@ namespace meshlink { /// This functions is called whenever another node attemps to open a channel to the local node. /** + * If the channel is accepted, the poll_callback will be set to channel_poll and can be + * changed using set_channel_poll_cb(). Likewise, the receive callback is set to + * channel_receive(). + * * The function is run in MeshLink's own thread. * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.) * to pass data to or from the application's thread. @@ -187,8 +194,8 @@ namespace meshlink { */ virtual bool channel_accept(channel *channel, uint16_t port, const void *data, size_t len) { - /* by default reject all channels */ - return false; + /* by default reject all channels */ + return false; } /// This function is called by Meshlink for receiving data from a channel. @@ -403,6 +410,9 @@ namespace meshlink { /** This function is called whenever a remote node wants to open a channel to the local node. * The application then has to decide whether to accept or reject this channel. * + * This function sets the channel poll callback to channel_poll_trampoline, which in turn + * calls channel_poll. To set a differnt, channel-specific poll callback, use set_channel_poll_cb. + * * @param node The node to which this channel is being initiated. * @param port The port number the peer wishes to connect to. * @param cb A pointer to the function which will be called when the remote node sends data to the local node. @@ -412,7 +422,9 @@ namespace meshlink { * @return A handle for the channel, or NULL in case of an error. */ channel *channel_open(node *node, uint16_t port, channel_receive_cb_t cb, const void *data, size_t len) { - return (channel *)meshlink_channel_open(handle, node, port, (meshlink_channel_receive_cb_t)cb, data, len); + channel *ch = (channel *)meshlink_channel_open(handle, node, port, (meshlink_channel_receive_cb_t)cb, data, len); + meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline); + return ch; } /** @@ -420,7 +432,9 @@ namespace meshlink { * Sets channel_receive_trampoline as cb, which in turn calls this->channel_receive( ... ). */ channel *channel_open(node *node, uint16_t port, const void *data, size_t len) { - return (channel *)meshlink_channel_open(handle, node, port, &channel_receive_trampoline, data, len); + channel *ch = (channel *)meshlink_channel_open(handle, node, port, &channel_receive_trampoline, data, len); + meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline); + return ch; } /// Partially close a reliable stream channel. @@ -467,28 +481,37 @@ namespace meshlink { /// static callback trampolines: static void receive_trampoline(meshlink_handle_t* handle, meshlink_node_t* source, const void* data, size_t length) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); that->receive(static_cast(source), data, length); } static void node_status_trampoline(meshlink_handle_t* handle, meshlink_node_t* peer, bool reachable) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); that->node_status(static_cast(peer), reachable); } static void log_trampoline(meshlink_handle_t* handle, log_level_t level, const char* message) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); that->log(level, message); } static bool channel_accept_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, uint16_t port, const void *data, size_t len) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); bool accepted = that->channel_accept(static_cast(channel), port, data, len); if (accepted) { + meshlink_set_channel_receive_cb(handle, channel, &channel_receive_trampoline); meshlink_set_channel_poll_cb(handle, channel, &channel_poll_trampoline); } return accepted; @@ -496,12 +519,16 @@ namespace meshlink { static void channel_receive_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, const void* data, size_t len) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); that->channel_receive(static_cast(channel), data, len); } static void channel_poll_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, size_t len) { + if (!(handle->priv)) + return; meshlink::mesh* that = static_cast(handle->priv); that->channel_poll(static_cast(channel), len); }