(void)peer;
}
+ /// This functions is called whenever the MeshLink thread status changes.
+ virtual void thread_status(bool status) {
+ /* do nothing */
+ (void)status;
+ }
+
/// This functions is called whenever MeshLink a meta-connection attempt is made.
virtual void connection_try(node *peer) {
/* do nothing */
meshlink_set_log_cb(handle, MESHLINK_DEBUG, &log_trampoline);
meshlink_set_error_cb(handle, &error_trampoline);
meshlink_set_blacklisted_cb(handle, &blacklisted_trampoline);
+ meshlink_set_thread_status_cb(handle, &thread_status_trampoline);
meshlink_set_channel_listen_cb(handle, &channel_listen_trampoline);
meshlink_set_channel_accept_cb(handle, &channel_accept_trampoline);
meshlink_set_connection_try_cb(handle, &connection_try_trampoline);
that->blacklisted(static_cast<node *>(peer));
}
+ static void thread_status_trampoline(meshlink_handle_t *handle, bool status) {
+ if(!(handle->priv)) {
+ return;
+ }
+
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->thread_status(status);
+ }
+
static void connection_try_trampoline(meshlink_handle_t *handle, meshlink_node_t *peer) {
if(!(handle->priv)) {
return;
abort();
}
+ if(mesh->thread_status_cb) {
+ mesh->thread_status_cb(mesh, true);
+ }
+
logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
pthread_cond_broadcast(&mesh->cond);
main_loop(mesh);
logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
+ if(mesh->thread_status_cb) {
+ mesh->thread_status_cb(mesh, false);
+ }
+
pthread_mutex_unlock(&mesh->mutex);
// Stop discovery
pthread_mutex_unlock(&mesh->mutex);
}
+void meshlink_set_thread_status_cb(struct meshlink_handle *mesh, meshlink_thread_status_cb_t cb) {
+ logger(mesh, MESHLINK_DEBUG, "meshlink_set_thread_status_cb(%p)", (void *)(intptr_t)cb);
+
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
+
+ mesh->thread_status_cb = cb;
+ pthread_mutex_unlock(&mesh->mutex);
+}
+
static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) {
meshlink_packethdr_t *hdr;
*/
void meshlink_set_blacklisted_cb(struct meshlink_handle *mesh, meshlink_blacklisted_cb_t cb);
+/// A callback notifying when the MeshLink thread starts and stops.
+/* @param mesh A handle which represents an instance of MeshLink, or NULL.
+ * @param started True if the MeshLink thread has started, false if it is about to stop.
+ */
+typedef void (*meshlink_thread_status_cb_t)(struct meshlink_handle *mesh, bool started);
+
+/// Set the thread status callback.
+/** This functions sets the callback that is called whenever the MeshLink thread has started or is about to stop.
+ *
+ * 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.
+ *
+ * \memberof meshlink_handle
+ * @param mesh A handle which represents an instance of MeshLink, or NULL.
+ * @param cb A pointer to the function which will be called when a serious error is encountered.
+ * If a NULL pointer is given, the callback will be disabled.
+ */
+void meshlink_set_thread_status_cb(struct meshlink_handle *mesh, meshlink_thread_status_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
meshlink_set_port
meshlink_set_receive_cb
meshlink_set_scheduling_granularity
+meshlink_set_thread_status_cb
meshlink_sign
meshlink_start
meshlink_stop
meshlink_connection_try_cb_t connection_try_cb;
meshlink_error_cb_t error_cb;
meshlink_blacklisted_cb_t blacklisted_cb;
+ meshlink_thread_status_cb_t thread_status_cb;
// Mesh parameters
char *appname;