]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink++.h
Add the MESHLINK_CHANNEL_NO_PARTIAL flag.
[meshlink] / src / meshlink++.h
index ac72f3cc96f46a0438a24d567fa25688257a8ade..2dcac00201f8f658903cdaef2c0b604aa45d2131 100644 (file)
@@ -43,6 +43,13 @@ typedef meshlink_errno_t errno_t;
  */
 typedef void (*receive_cb_t)(mesh *mesh, node *source, const void *data, size_t len);
 
+/// A callback reporting the meta-connection attempt made by the host node to an another node.
+/** @param mesh      A handle which represents an instance of MeshLink.
+ *  @param node      A pointer to a meshlink_node_t describing the node to whom meta-connection is being tried.
+ *                   This pointer is valid until meshlink_close() is called.
+ */
+typedef void (*connection_try_cb_t)(mesh *mesh, node *node);
+
 /// A callback reporting node status changes.
 /** @param mesh       A handle which represents an instance of MeshLink.
  *  @param node       A pointer to a meshlink::node describing the node whose status changed.
@@ -106,6 +113,7 @@ public:
        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 NO_PARTIAL = MESHLINK_CHANNEL_NO_PARTIAL;
        static const uint32_t TCP = MESHLINK_CHANNEL_TCP;
        static const uint32_t UDP = MESHLINK_CHANNEL_UDP;
 };
@@ -213,6 +221,12 @@ public:
                (void)message;
        }
 
+       /// This functions is called whenever MeshLink a meta-connection attempt is made.
+       virtual void connection_try(node *peer) {
+               /* do nothing */
+               (void)peer;
+       }
+
        /// This functions is called whenever another node attempts 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
@@ -287,6 +301,7 @@ public:
                meshlink_set_node_duplicate_cb(handle, &node_duplicate_trampoline);
                meshlink_set_log_cb(handle, MESHLINK_DEBUG, &log_trampoline);
                meshlink_set_channel_accept_cb(handle, &channel_accept_trampoline);
+               meshlink_set_connection_try_cb(handle, &connection_try_trampoline);
                return meshlink_start(handle);
        }
 
@@ -327,6 +342,18 @@ public:
                return (node *)meshlink_get_node(handle, name);
        }
 
+       /// Get a handle for a specific submesh.
+       /** This function returns a handle for the submesh with the given name.
+        *
+        *  @param name         The name of the submesh for which a handle is requested.
+        *
+        *  @return             A pointer to a meshlink::submesh which represents the requested submesh,
+        *                      or NULL if the requested submesh does not exist.
+        */
+       submesh *get_submesh(const char *name) {
+               return (submesh *)meshlink_get_submesh(handle, name);
+       }
+
        /// Get a handle for our own node.
        /** This function returns a handle for the local node.
         *
@@ -507,7 +534,9 @@ public:
         *                       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 successfully changed, false otherwise.
+        *  @return              This function returns true if the port was successfully changed
+        *                       to the desired port, false otherwise. If it returns false, there
+        *                       is no guarantee that MeshLink is listening on the old port.
         */
        bool set_port(int port) {
                return meshlink_set_port(handle, port);
@@ -686,6 +715,8 @@ public:
         *  @param len          The length of the data.
         *
         *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
+        *                      If MESHLINK_CHANNEL_NO_PARTIAL is set, then the result will either be len,
+        *                      0 if the buffer is currently too full, or -1 if len is too big even for an empty buffer.
         */
        ssize_t channel_send(channel *channel, void *data, size_t len) {
                return meshlink_channel_send(handle, channel, data, len);
@@ -767,6 +798,15 @@ private:
                that->log(level, message);
        }
 
+       static void connection_try_trampoline(meshlink_handle_t *handle, meshlink_node_t *peer) {
+               if(!(handle->priv)) {
+                       return;
+               }
+
+               meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+               that->connection_try(static_cast<node *>(peer));
+       }
+
        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;