X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink%2B%2B.h;h=4f35e8a97f7c95def970cfbab06a376751818f19;hb=c6cc7da56d108f05ea6f9a2f765f699e6e2353db;hp=dc32855a212a869e52b77eaf90f2bb016a0bb396;hpb=287899bd4015afe0faff84545f95227846a01b65;p=meshlink diff --git a/src/meshlink++.h b/src/meshlink++.h index dc32855a..4f35e8a9 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -21,6 +21,7 @@ #define MESHLINKPP_H #include +#include // for 'placement new' namespace meshlink { class mesh; @@ -66,7 +67,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, void *data, size_t len); + typedef bool (*channel_accept_cb_t)(mesh *mesh, channel *channel, node *node, 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. @@ -74,7 +75,14 @@ namespace meshlink { * @param data A pointer to a buffer containing data sent by the source. * @param len The length of the data. */ - typedef void (*channel_receive_cb_t)(mesh *mesh, channel *channel, void *data, size_t len); + typedef void (*channel_receive_cb_t)(mesh *mesh, channel *channel, const void *data, size_t len); + + /// A callback that is called when data can be send on a channel. + /** @param mesh A handle which represents an instance of MeshLink. + * @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(). + */ + typedef void (*channel_poll_cb_t)(mesh *mesh, channel *channel, size_t len); /// A class describing a MeshLink node. class node: public meshlink_node_t { @@ -86,9 +94,30 @@ namespace meshlink { /// A class describing a MeshLink mesh. class mesh: public meshlink_handle_t { - public: - // TODO: delete constructor, add a destructor. - + public: + mesh() : meshlink_handle_t() {} + + virtual ~mesh() { + meshlink_close(this); + } + + /** instead of registerin callbacks you derive your own class and overwrite the following abstract member functions. + * These functions are run in MeshLink's own thread. + * It is therefore important that these functions use apprioriate methods (queues, pipes, locking, etc.) + * to hand the data over to the application's thread. + * These functions should also not block itself and return as quickly as possible. + * The default member functions are no-ops, so you are not required to overwrite all these member functions + */ + + /// This function is called whenever another node sends data to the local node. + virtual void receive(node* source, const void* data, size_t length) { /* do nothing */ } + + /// This functions is called whenever another node's status changed. + virtual void node_status(node* peer, bool reachable) { /* do nothing */ } + + /// This functions is called whenever MeshLink has some information to log. + virtual void log(log_level_t level, const char* message) { /* do nothing */ } + /// Start MeshLink. /** This function causes MeshLink to open network sockets, make outgoing connections, and * create a new thread, which will handle all network I/O. @@ -96,6 +125,9 @@ namespace meshlink { * @return This function will return true if MeshLink has succesfully started its thread, false otherwise. */ bool start() { + meshlink_set_receive_cb (this, &receive_trampoline); + meshlink_set_node_status_cb(this, &node_status_trampoline); + meshlink_set_log_cb (this, MESHLINK_DEBUG, &log_trampoline); return meshlink_start(this); } @@ -107,46 +139,6 @@ namespace meshlink { meshlink_stop(this); } - /// Set the receive 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 cb A pointer to the function which will be called when another node sends data to the local node. - */ - void set_receive_cb(receive_cb_t cb) { - meshlink_set_receive_cb(this, (meshlink_receive_cb_t)cb); - } - - /// Set the node status callback. - /** This functions sets the callback that is called whenever another node's status changed. - * 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 cb A pointer to the function which will be called when another node's status changes. - */ - void set_node_status_cb(node_status_cb_t cb) { - meshlink_set_node_status_cb(this, (meshlink_node_status_cb_t)cb); - } - - /// Set the log callback. - /** This functions sets the callback that is called whenever MeshLink has some information to log. - * The callback is run in MeshLink's own thread. - * It is 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 level An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback. - * @param cb A pointer to the function which will be called when another node sends data to the local node. - */ - void set_log_cb(meshlink_log_level_t level, log_cb_t cb) { - meshlink_set_log_cb(this, level, (meshlink_log_cb_t)cb); - } - /// Send data to another node. /** This functions sends one packet of data to another node in the mesh. * The packet is sent using UDP semantics, which means that @@ -182,10 +174,11 @@ namespace meshlink { * @param nodes A pointer to an array of pointers to meshlink::node, which should be allocated by the application. * @param nmemb The maximum number of pointers that can be stored in the nodes array. * - * @return The number of known nodes. This can be larger than nmemb, in which case not all nodes were stored in the nodes array. + * @return The number of known nodes, or -1 in case of an error. + * This can be larger than nmemb, in which case not all nodes were stored in the nodes array. */ - size_t get_all_nodes(node **nodes, size_t nmemb) { - return meshlink_get_all_nodes(this, (meshlink_node_t **)nodes, nmemb); + node **get_all_nodes(node **nodes, size_t *nmemb) { + return (node **)meshlink_get_all_nodes(this, (meshlink_node_t **)nodes, nmemb); } /// Sign data using the local node's MeshLink key. @@ -299,12 +292,28 @@ namespace meshlink { * 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(this, (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. + * 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 cb A pointer to the function which will be called when data can be sent to another node. + * 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(this, (meshlink_channel_poll_cb_t)cb); + } + /// 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. @@ -317,8 +326,8 @@ namespace meshlink { * * @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 recv, void *data, size_t len) { - return (channel *)meshlink_channel_open(this, node, port, (meshlink_channel_receive_cb_t)recv, data, len); + channel *channel_open(node *node, uint16_t port, channel_receive_cb_t cb, const void *data, size_t len) { + return (channel *)meshlink_channel_open(this, node, port, (meshlink_channel_receive_cb_t)cb, data, len); } /// Partially close a reliable stream channel. @@ -357,6 +366,29 @@ namespace meshlink { return meshlink_channel_send(this, channel, data, len); } + private: + // non-copyable: + mesh(const mesh&) /* TODO: C++11: = delete */; + void operator=(const mesh&) /* TODO: C++11: = delete */ ; + + /// 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(handle); + that->receive(static_cast(source), data, length); + } + + static void node_status_trampoline(meshlink_handle_t* handle, meshlink_node_t* peer, bool reachable) + { + mesh* that = static_cast(handle); + that->node_status(static_cast(peer), reachable); + } + + static void log_trampoline(meshlink_handle_t* handle, log_level_t level, const char* message) + { + mesh* that = static_cast(handle); + that->log(level, message); + } }; /// Initialize MeshLink's configuration directory. @@ -371,11 +403,15 @@ namespace meshlink { * * @param confbase The directory in which MeshLink will store its configuration files. * @param name The name which this instance of the application will use in the mesh. + * @param appname The application name which will be used in the mesh. + * @param dclass The device class which will be used in the mesh. * * @return This function will return a pointer to a meshlink::mesh if MeshLink has succesfully set up its configuration files, NULL otherwise. */ - static mesh *open(const char *confbase, const char *name) { - return (mesh *)meshlink_open(confbase, name); + template + static MESH* open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) { + void* mp = (void *)meshlink_open_with_size(confbase, name, appname, devclass, sizeof(MESH)); + return new (mp) MESH; } /// Close the MeshLink handle. @@ -386,6 +422,11 @@ namespace meshlink { static void close(mesh *mesh) { meshlink_close(mesh); } -}; + + static const char *strerror(errno_t err = meshlink_errno) { + return meshlink_strerror(err); + } + +} #endif // MESHLINKPP_H