From 18ca002ef64121630ead99612e0e0fe90ae073f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 13:01:13 +0200 Subject: [PATCH] Catch errors. --- include/neocomm.h | 17 +++++++++++++++-- src/node.cpp | 23 ++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index c69ed15..1eee86a 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -44,7 +44,6 @@ struct user { struct message { const char *msg; ///< The text message. time_t sent; ///< The time it was sent. - time_t received; ///< The time it was received. struct user from; ///< Info on the peer who sent the message. }; @@ -53,8 +52,11 @@ struct message { * number will be used. * * @param port The port for the node to listen on. + * + * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a + * text description of the error. */ -void NeoComm_init(const unsigned short port); +int NeoComm_init(const unsigned short port); /** * @brief Deinitialize the local node. @@ -129,6 +131,17 @@ struct message *NeoComm_get_next_message(const char *channel_name); */ void NeoComm_free_message(struct message *msg); +/** + * @brief Send a message to a channel. + * + * @param channel_name Name of the channel to send the message to. + * @param message A message to send to the channel. + * + * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a + * text description of the error. + */ +int NeoComm_send_message(const char *channel_name, const char *message); + /** * @brief Get the last error that occurred in text. * diff --git a/src/node.cpp b/src/node.cpp index 9bb33f5..9149cf5 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -24,18 +24,35 @@ #include #include #include +#include + +extern "C" { +#include +} #define DEFAULT_PORT 1335 dht::DhtRunner node; -void NeoComm_init(const unsigned short port) { - node.run((port == 0 ? DEFAULT_PORT : port), - dht::crypto::generateIdentity(), true); +int NeoComm_init(const unsigned short port) { +#ifdef WOE32 + gnutls_global_init(); +#endif //WOE32 + try { + node.run((port == 0 ? DEFAULT_PORT : port), + dht::crypto::generateIdentity(), true); + } catch(const std::exception &e) { + add_error(e.what()); + return 0; + } + return 1; } void NeoComm_deinit() { node.join(); +#ifdef WOE32 + gnutls_global_deinit(); +#endif //WOE32 } int NeoComm_connect(const char *address, const unsigned short port) {