]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink++.h
Call srand() at startup.
[meshlink] / src / meshlink++.h
index c7dbd4a308248e8d20b68bbce9bb3013670066c2..258d6cf49b796b75c42bb3194509223af6d2497d 100644 (file)
@@ -89,6 +89,13 @@ namespace meshlink {
 
        /// A class describing a MeshLink channel.
        class channel: public meshlink_channel_t {
+               public:
+               static const uint32_t RELIABLE = MESHLINK_CHANNEL_RELIABLE;
+               static const uint32_t ORDERED = MESHLINK_CHANNEL_ORDERED;
+               static const uint32_t FRAMED = MESHLINK_CHANNEL_FRAMED;
+               static const uint32_t DROP_LATE = MESHLINK_CHANNEL_DROP_LATE;
+               static const uint32_t TCP = MESHLINK_CHANNEL_TCP;
+               static const uint32_t UDP = MESHLINK_CHANNEL_UDP;
        };
 
        /// A class describing a MeshLink mesh.
@@ -149,7 +156,10 @@ namespace meshlink {
                 */
                void close() {
                        if(handle)
+                       {
+                               handle->priv = 0;
                                meshlink_close(handle);
+                       }
                        handle=0;
                }
        
@@ -172,6 +182,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 +201,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 +281,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.
                 *
@@ -321,6 +344,70 @@ namespace meshlink {
                        return meshlink_add_address(handle, address);
                }
 
+               /** This function performs tries to discover the local node's external address
+                *  by contacting the meshlink.io server. If a reverse lookup of the address works,
+                *  the FQDN associated with the address will be returned.
+                *
+                *  Please note that this is function only returns a single address,
+                *  even if the local node might have more than one external address.
+                *  In that case, there is no control over which address will be selected.
+                *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
+                *  there is no guarantee that the external address will be valid for an extended period of time.
+                *
+                *  This function is blocking. It can take several seconds before it returns.
+                *  There is no guarantee it will be able to resolve the external address.
+                *  Failures might be because by temporary network outages.
+                *
+                *  @return             This function returns a pointer to a C string containing the discovered external address,
+                *                      or NULL if there was an error looking up the address.
+                *                      After get_external_address() returns, the application is free to overwrite or free this string.
+                */
+               bool get_external_address() {
+                       return meshlink_get_external_address(handle);
+               }
+
+               /// Try to discover the external address for the local node, and add it to its list of addresses.
+               /** This function is equivalent to:
+                *
+                *    mesh->add_address(mesh->get_external_address());
+                *
+                *  Read the description of get_external_address() for the limitations of this function.
+                *
+                *  @return             This function returns true if the address was added, false otherwise.
+                */
+               bool add_external_address() {
+                       return meshlink_add_external_address(handle);
+               }
+
+               /// Get the network port used by the local node.
+               /** This function returns the network port that the local node is listening on.
+                *
+                *  @param mesh          A handle which represents an instance of MeshLink.
+                *
+                *  @return              This function returns the port number, or -1 in case of an error.
+                */
+               int get_port() {
+                       return meshlink_get_port(handle);
+               }
+
+               /// Set the network port used by the local node.
+               /** This function sets the network port that the local node is listening on.
+                *  It may only be called when the mesh is not running.
+                *  If unsure, call stop() before calling this function.
+                *  Also note that if your node is already part of a mesh with other nodes,
+                *  that the other nodes may no longer be able to initiate connections to the local node,
+                *  since they will try to connect to the previously configured port.
+                *
+                *  @param port          The port number to listen on. This must be between 0 and 65535.
+                *                       If the port is set to 0, then MeshLink will listen on a port
+                *                       that is randomly assigned by the operating system every time open() is called.
+                *
+                *  @return              This function returns true if the port was succesfully changed, false otherwise.
+                */
+               bool set_port(int port) {
+                       return meshlink_set_port(handle, port);
+               }
+
                /// Invite another node into the mesh.
                /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
                 *  The generated invitation is a string containing a URL.
@@ -341,6 +428,12 @@ namespace meshlink {
                 *  An invitation can only be used if the local node has never connected to other nodes before.
                 *  After a succesfully accepted invitation, the name of the local node may have changed.
                 *
+                *  This function may only be called on a mesh that has not been started yet and which is not already part of an existing mesh.
+                *
+                *  This function is blocking. It can take several seconds before it returns.
+                *  There is no guarantee it will perform a successful join.
+                *  Failures might be caused by temporary network outages, or by the invitation having expired.
+                *
                 *  @param invitation   A string containing the invitation URL.
                 *
                 *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
@@ -402,32 +495,33 @@ 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.
                 *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
                 *  @param data         A pointer to a buffer containing data to already queue for sending.
                 *  @param len          The length of the data.
+                *  @param flags        A bitwise-or'd combination of flags that set the semantics for this channel.
                 *
                 *  @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) {
-                       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;
+               channel *channel_open(node *node, uint16_t port, channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags = channel::TCP) {
+                       channel *ch = (channel *)meshlink_channel_open_ex(handle, node, port, (meshlink_channel_receive_cb_t)cb, data, len, flags);
+                       meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline);
+                       return ch;
                }
 
                /**
                 * @override
                 * 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) {
-                       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;
+               channel *channel_open(node *node, uint16_t port, const void *data, size_t len, uint32_t flags = channel::TCP) {
+                       channel *ch = (channel *)meshlink_channel_open_ex(handle, node, port, &channel_receive_trampoline, data, len, flags);
+                       meshlink_set_channel_poll_cb(handle, ch, &channel_poll_trampoline);
+                       return ch;
                }
 
                /// Partially close a reliable stream channel.
@@ -474,28 +568,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 +606,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 +627,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