]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink++.h
Add meshlink_get_self().
[meshlink] / src / meshlink++.h
index c7dbd4a308248e8d20b68bbce9bb3013670066c2..346549013e010aa3bcb28292395a2dd45b1258ca 100644 (file)
@@ -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.
@@ -267,6 +274,15 @@ namespace meshlink {
                        return (node *)meshlink_get_node(handle, name);
                }
 
+               /// Get a handle for our own node.
+               /** This function returns a handle for the local node.
+                *
+                *  @return             A pointer to a meshlink::node which represents the local node.
+                */
+               node *get_self() {
+                       return (node *)meshlink_get_self(handle);
+               }
+
                /// Get a list of all nodes.
                /** This function returns a list with handles for all known nodes.
                 *
@@ -402,9 +418,9 @@ namespace meshlink {
                /// Open a reliable stream channel to another node.
                /** 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.
+                *
+                *  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.
@@ -416,8 +432,8 @@ namespace meshlink {
                 */
                channel *channel_open(node *node, uint16_t port, channel_receive_cb_t cb, const void *data, size_t 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;
+                       meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline);
+                       return ch;
                }
 
                /**
@@ -426,8 +442,8 @@ namespace meshlink {
                 */
                channel *channel_open(node *node, uint16_t port, const void *data, size_t 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;
+                       meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline);
+                       return ch;
                }
 
                /// Partially close a reliable stream channel.
@@ -474,28 +490,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<mesh*>(handle->priv);
                        that->receive(static_cast<node*>(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<mesh*>(handle->priv);
                        that->node_status(static_cast<node*>(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<mesh*>(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 false;
                        meshlink::mesh* that = static_cast<mesh*>(handle->priv);
                        bool accepted = that->channel_accept(static_cast<meshlink::channel*>(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;
@@ -503,12 +528,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<mesh*>(handle->priv);
                        that->channel_receive(static_cast<meshlink::channel*>(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<mesh*>(handle->priv);
                        that->channel_poll(static_cast<meshlink::channel*>(channel), len);
                }
@@ -520,6 +549,19 @@ namespace meshlink {
                return meshlink_strerror(err);
        }
 
+       /// Destroy a MeshLink instance.
+       /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
+        *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
+        *  new instance.
+        *
+        *  @param confbase The directory in which MeshLink stores its configuration files.
+        *                  After the function returns, the application is free to overwrite or free @a confbase @a.
+        *
+        *  @return         This function will return true if the MeshLink instance was succesfully destroyed, false otherwise.
+        */
+       static bool destroy(const char *confbase) {
+               return meshlink_destroy(confbase);
+       }
 }
 
 #endif // MESHLINKPP_H