]> git.meshlink.io Git - meshlink/commitdiff
Changed C++ channel API to use member functions.
authorAaron Krebs <aaron.krebs@everbase.net>
Wed, 29 Oct 2014 12:55:53 +0000 (13:55 +0100)
committerGuus Sliepen <guus@meshlink.io>
Sat, 27 Dec 2014 17:20:02 +0000 (18:20 +0100)
src/meshlink++.h

index 7be3de51768d424be132e44644481cb74ee2723b..49d7f602bdc88acdd1e502b81fa4ccea00dfa9fc 100644 (file)
@@ -59,7 +59,6 @@ namespace meshlink {
        /// A callback for accepting incoming channels.
        /** @param mesh         A handle which represents an instance of MeshLink.
         *  @param channel      A handle for the incoming channel.
-        *  @param node         The node from which this channel is being initiated.
         *  @param port         The port number the peer wishes to connect to.
         *  @param data         A pointer to a buffer containing data already received. (Not yet used.)
         *  @param len          The length of the data. (Not yet used.)
@@ -67,7 +66,7 @@ namespace meshlink {
         *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
         *                      If returning false, the channel is invalid and may not be used anymore.
         */
-       typedef bool (*channel_accept_cb_t)(mesh *mesh, channel *channel, node *node, uint16_t port, const void *data, size_t len);
+       typedef bool (*channel_accept_cb_t)(mesh *mesh, channel *channel, uint16_t port, const void *data, size_t len);
 
        /// A callback for receiving data from a channel.
        /** @param mesh         A handle which represents an instance of MeshLink.
@@ -170,6 +169,52 @@ namespace meshlink {
                
                /// This functions is called whenever MeshLink has some information to log.
                virtual void log(log_level_t level, const char* message)            { /* do nothing */ }
+
+               /// This functions is called whenever another node attemps to open a channel to the local node.
+               /** 
+                *  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.
+                *  The callback should also not block itself and return as quickly as possible.
+                *
+                *  @param channel      A handle for the incoming channel.
+                *  @param port         The port number the peer wishes to connect to.
+                *  @param data         A pointer to a buffer containing data already received. (Not yet used.)
+                *  @param len          The length of the data. (Not yet used.)
+                *
+                *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
+                *                      If returning false, the channel is invalid and may not be used anymore.
+                */
+               virtual bool channel_accept(channel *channel, uint16_t port, const void *data, size_t len)
+               {
+                       /* by default reject all channels */
+                       return false;
+               }
+
+               /// This function is called by Meshlink for receiving data from a channel.
+               /** 
+                *  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.
+                *  The callback should also not block itself and return as quickly as possible.
+                *
+                *  @param channel      A handle for the channel.
+                *  @param data         A pointer to a buffer containing data sent by the source.
+                *  @param len          The length of the data.
+                */
+               virtual void channel_receive(channel *channel, const void *data, size_t len) { /* do nothing */ }
+
+               /// This function is called by Meshlink when data can be send on a channel.
+               /**
+                *  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.
+                *
+                *  The callback should also not block itself and return as quickly as possible.
+                *  @param channel      A handle for the channel.
+                *  @param len          The maximum length of data that is guaranteed to be accepted by a call to channel_send().
+                */
+               virtual void channel_poll(channel *channel, size_t len) { /* do nothing */ }
                
                /// Start MeshLink.
                /** This function causes MeshLink to open network sockets, make outgoing connections, and
@@ -178,10 +223,11 @@ namespace meshlink {
                 *  @return         This function will return true if MeshLink has succesfully started its thread, false otherwise.
                 */
                bool start() {
-                       meshlink_set_receive_cb    (handle, &receive_trampoline);
-                       meshlink_set_node_status_cb(handle, &node_status_trampoline);
-                       meshlink_set_log_cb        (handle, MESHLINK_DEBUG, &log_trampoline);
-                       return meshlink_start      (handle);
+                       meshlink_set_receive_cb       (handle, &receive_trampoline);
+                       meshlink_set_node_status_cb   (handle, &node_status_trampoline);
+                       meshlink_set_log_cb           (handle, MESHLINK_DEBUG, &log_trampoline);
+                       meshlink_set_channel_accept_cb(handle, &channel_accept_trampoline);
+                       return meshlink_start         (handle);
                }
 
                /// Stop MeshLink.
@@ -338,20 +384,6 @@ namespace meshlink {
                        return meshlink_blacklist(handle, node);
                }
 
-               /// Set the accept callback.
-               /** This functions sets the callback that is called whenever another node sends data to the local node.
-                *  The callback is run in MeshLink's own thread.
-                *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
-                *  to hand the data over to the application's thread.
-                *  The callback should also not block itself and return as quickly as possible.
-                *
-                *  @param channel   A handle for the channel.
-                *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
-                */
-               void set_channel_accept_cb(channel *channel, channel_accept_cb_t cb) {
-                       return meshlink_set_channel_accept_cb(handle, (meshlink_channel_accept_cb_t)cb);
-               }
-
                /// Set the poll callback.
                /** This functions sets the callback that is called whenever data can be sent to another node.
                 *  The callback is run in MeshLink's own thread.
@@ -364,7 +396,7 @@ namespace meshlink {
                 *                   If a NULL pointer is given, the callback will be disabled.
                 */
                void set_channel_poll_cb(channel *channel, channel_poll_cb_t cb) {
-                       return meshlink_set_channel_poll_cb(handle, channel, (meshlink_channel_poll_cb_t)cb);
+                       meshlink_set_channel_poll_cb(handle, channel, (meshlink_channel_poll_cb_t)cb);
                }
 
                /// Open a reliable stream channel to another node.
@@ -383,6 +415,14 @@ namespace meshlink {
                        return (channel *)meshlink_channel_open(handle, node, port, (meshlink_channel_receive_cb_t)cb, data, len);
                }
 
+               /**
+                * @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) {
+                       return (channel *)meshlink_channel_open(handle, node, port, &channel_receive_trampoline, data, len);
+               }
+
                /// Partially close a reliable stream channel.
                /** This shuts down the read or write side of a channel, or both, without closing the handle.
                 *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
@@ -427,23 +467,46 @@ namespace meshlink {
                /// static callback trampolines:
                static void receive_trampoline(meshlink_handle_t* handle, meshlink_node_t* source, const void* data, size_t length)
                {
-                       mesh* that = static_cast<mesh*>(handle->priv);
+                       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)
                {
-                       mesh* that = static_cast<mesh*>(handle->priv);
+                       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)
                {
-                       mesh* that = static_cast<mesh*>(handle->priv);
+                       meshlink::mesh* that = static_cast<mesh*>(handle->priv);
                        that->log(level, message);
                }
-               
-               meshlink_handle_t*  handle;
+
+               static bool channel_accept_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, uint16_t port, const void *data, size_t len)
+               {
+                       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_poll_cb(handle, channel, &channel_poll_trampoline);
+                       }
+                       return accepted;
+               }
+
+               static void channel_receive_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, const void* data, size_t len)
+               {
+                       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)
+               {
+                       meshlink::mesh* that = static_cast<mesh*>(handle->priv);
+                       that->channel_poll(static_cast<meshlink::channel*>(channel), len);
+               }
+
+               meshlink_handle_t* handle;
        };
 
        static const char *strerror(errno_t err = meshlink_errno) {