From 6c927588f78c663fded354fd696997d09fb5d697 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 14 Jun 2019 16:01:29 -0400 Subject: handle errors in C bindings --- include/salticidae/network.h | 8 ++++---- include/salticidae/stream.h | 44 ++++++++++++++++++++++---------------------- include/salticidae/util.h | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 35 deletions(-) (limited to 'include') diff --git a/include/salticidae/network.h b/include/salticidae/network.h index fc33414..de7c32e 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -964,10 +964,10 @@ void msgnetwork_config_queue_capacity(msgnetwork_config_t *self, size_t cap); msgnetwork_t *msgnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config); void msgnetwork_free(const msgnetwork_t *self); void msgnetwork_send_msg_by_move(msgnetwork_t *self, msg_t *_moved_msg, const msgnetwork_conn_t *conn); -msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr); +msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr, SalticidaeCError *err); msgnetwork_conn_t *msgnetwork_conn_copy(const msgnetwork_conn_t *self); void msgnetwork_conn_free(const msgnetwork_conn_t *self); -void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr); +void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *err); void msgnetwork_start(msgnetwork_t *self); void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn); @@ -996,14 +996,14 @@ void peernetwork_free(const peernetwork_t *self); void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr); void peernetwork_del_peer(peernetwork_t *self, const netaddr_t *paddr); bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr); -const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, const netaddr_t *paddr); +const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, const netaddr_t *paddr, SalticidaeCError *cerror); msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self); msgnetwork_conn_t *msgnetwork_conn_new_from_peernetwork_conn(const peernetwork_conn_t *conn); peernetwork_conn_t *peernetwork_conn_copy(const peernetwork_conn_t *self); void peernetwork_conn_free(const peernetwork_conn_t *self); void peernetwork_send_msg_by_move(peernetwork_t *self, msg_t * _moved_msg, const netaddr_t *paddr); void peernetwork_multicast_msg_by_move(peernetwork_t *self, msg_t *_moved_msg, const netaddr_array_t *paddrs); -void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr); +void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *err); #ifdef __cplusplus } diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h index cf007ac..37786b2 100644 --- a/include/salticidae/stream.h +++ b/include/salticidae/stream.h @@ -505,32 +505,32 @@ datastream_t *datastream_new(); datastream_t *datastream_new_from_bytes(const uint8_t *base, size_t size); void datastream_free(const datastream_t *self); -void datastream_assign_by_copy(datastream_t *dst, const datastream_t *src); -void datastream_assign_by_move(datastream_t *dst, datastream_t *_moved_src); +bool datastream_assign_by_copy(datastream_t *dst, const datastream_t *src); +bool datastream_assign_by_move(datastream_t *dst, datastream_t *_moved_src); uint8_t *datastream_data(datastream_t *self); void datastream_clear(datastream_t *self); size_t datastream_size(const datastream_t *self); -void datastream_put_u8(datastream_t *self, uint8_t val); -void datastream_put_u16(datastream_t *self, uint16_t val); -void datastream_put_u32(datastream_t *self, uint32_t val); -void datastream_put_u64(datastream_t *self, uint64_t val); - -void datastream_put_i8(datastream_t *self, int8_t val); -void datastream_put_i16(datastream_t *self, int16_t val); -void datastream_put_i32(datastream_t *self, int32_t val); -void datastream_put_i64(datastream_t *self, int64_t val); -void datastream_put_data(datastream_t *self, const uint8_t *base, size_t size); - -uint8_t datastream_get_u8(datastream_t *self); -uint16_t datastream_get_u16(datastream_t *self); -uint32_t datastream_get_u32(datastream_t *self); -uint64_t datastream_get_u64(datastream_t *self); - -int8_t datastream_get_i8(datastream_t *self); -int16_t datastream_get_i16(datastream_t *self); -int32_t datastream_get_i32(datastream_t *self); -int64_t datastream_get_i64(datastream_t *self); +bool datastream_put_u8(datastream_t *self, uint8_t val); +bool datastream_put_u16(datastream_t *self, uint16_t val); +bool datastream_put_u32(datastream_t *self, uint32_t val); +bool datastream_put_u64(datastream_t *self, uint64_t val); + +bool datastream_put_i8(datastream_t *self, int8_t val); +bool datastream_put_i16(datastream_t *self, int16_t val); +bool datastream_put_i32(datastream_t *self, int32_t val); +bool datastream_put_i64(datastream_t *self, int64_t val); +bool datastream_put_data(datastream_t *self, const uint8_t *base, size_t size); + +uint8_t datastream_get_u8(datastream_t *self, bool *succ); +uint16_t datastream_get_u16(datastream_t *self, bool *succ); +uint32_t datastream_get_u32(datastream_t *self, bool *succ); +uint64_t datastream_get_u64(datastream_t *self, bool *succ); + +int8_t datastream_get_i8(datastream_t *self, bool *succ); +int16_t datastream_get_i16(datastream_t *self, bool *succ); +int32_t datastream_get_i32(datastream_t *self, bool *succ); +int64_t datastream_get_i64(datastream_t *self, bool *succ); const uint8_t *datastream_get_data_inplace(datastream_t *self, size_t len); uint256_t *datastream_get_hash(const datastream_t *self); diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 41f681c..007fcc4 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -22,11 +22,32 @@ * SOFTWARE. */ -#ifndef _SALTICIDAE_COMMON_H -#define _SALTICIDAE_COMMON_H +#ifndef _SALTICIDAE_UTIL_H +#define _SALTICIDAE_UTIL_H + +#include +#include +#include +#include "salticidae/config.h" + +typedef struct SalticidaeCError { + int code; + int oscode; +} SalticidaeCError; + +#ifdef __cplusplus +extern "C" { +#endif + +SalticidaeCError salticidae_cerror_normal(); +SalticidaeCError salticidae_cerror_unknown(); +const char *salticidae_strerror(int code); #ifdef __cplusplus +} +#endif +#ifdef __cplusplus #include #include #include @@ -34,11 +55,6 @@ #include #include #include -#include -#include -#include - -#include "salticidae/config.h" #include "salticidae/ref.h" namespace salticidae { @@ -66,7 +82,8 @@ enum SalticidaeErrorCode { SALTI_ERROR_OPTNAME_ALREADY_EXISTS, SALTI_ERROR_OPT_UNKNOWN_ACTION, SALTI_ERROR_CONFIG_LINE_TOO_LONG, - SALTI_ERROR_OPT_INVALID + SALTI_ERROR_OPT_INVALID, + SALTI_ERROR_UNKNOWN }; extern const char *SALTICIDAE_ERROR_STRINGS[]; @@ -95,6 +112,12 @@ class SalticidaeError: public std::exception { const char *what() const throw() override { return msg.c_str(); } int get_code() const { return code; } int get_oscode() const { return oscode; } + SalticidaeCError get_cerr() const { + SalticidaeCError res; + res.code = code; + res.oscode = oscode; + return res; + } }; @@ -366,7 +389,6 @@ class Config { }; } - #endif #endif -- cgit v1.2.3-70-g09d2