Skip to content

Instantly share code, notes, and snippets.

@nbaksalyar
Last active August 14, 2017 11:42
Show Gist options
  • Save nbaksalyar/e2036ad841f59936f1615ca70fb476d2 to your computer and use it in GitHub Desktop.
Save nbaksalyar/e2036ad841f59936f1615ca70fb476d2 to your computer and use it in GitHub Desktop.
// To compile this:
// g++ -lsafe_app test.cc
// Don't forget to put libsafe_app.so (or libsafe_app.dll, depending on your OS) with the source code.
#include <stdint.h>
#include <stdlib.h>
#include <iostream>
// https://docs.rs/ffi_utils/0.2.0/ffi_utils/struct.FfiResult.html
typedef struct {
int err_code;
char *description;
} FfiResult;
typedef void *AppPtr;
// https://docs.rs/safe_app/0.2.1/safe_app/ffi/fn.app_unregistered.html
extern "C" void app_unregistered(char *bootstrap_config_ptr,
size_t bootstrap_config_len,
void *network_cb_user_data,
void *user_data,
void (*o_network_observer_cb)(void *, FfiResult, int32_t),
void (*o_cb)(void *, FfiResult, AppPtr));
int main() {
// Create an unregistered app and provide callbacks as C++ anonymous functions
app_unregistered(nullptr, 0, 0L, 0L, [](void *network_ud, FfiResult res, int32_t res_type) {
std::cout << "Network event: " << res_type << std::endl;
}, [](void *user_data, FfiResult res, AppPtr app_ptr) {
if (res.err_code == 0) {
std::cout << "Successfully created App handle" << std::endl;
} else {
std::cout << "Error: " << res.err_code << std::endl;
}
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment