]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink++.h
Call srand() at startup.
[meshlink] / src / meshlink++.h
index f1a3b96270e44fb084c1a7648df4bef50a45832e..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.
@@ -274,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.
                 *
@@ -328,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.
@@ -348,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.
@@ -418,11 +504,12 @@ namespace meshlink {
                 *  @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);
+               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;
                }
@@ -431,8 +518,8 @@ namespace meshlink {
                 * @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);
+               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;
                }
@@ -540,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