From 7c2fed579ddd0a06d13c13c75b89eb595a7a859b Mon Sep 17 00:00:00 2001 From: Xavrax Date: Mon, 20 Jan 2025 10:00:54 +0100 Subject: [PATCH 01/24] compiling api enforcement --- CMakeLists.txt | 65 +++++++-- core/pbpal_ntf_callback_admin.c | 41 +++++- core/pubnub_callback_subscribe_loop.c | 5 + core/pubnub_callback_subscribe_loop.h | 5 + core/pubnub_internal_common.h | 13 ++ core/pubnub_ntf_dynamic.c | 203 ++++++++++++++++++++++++++ core/pubnub_ntf_dynamic.h | 97 ++++++++++++ core/pubnub_ntf_sync.c | 126 ++++++++++++++-- core/pubnub_pubsubapi.c | 6 + core/pubnub_sync_subscribe_loop.c | 5 + core/pubnub_sync_subscribe_loop.h | 5 + posix/pubnub_ntf_callback_posix.c | 99 +++++++++++-- 12 files changed, 629 insertions(+), 41 deletions(-) create mode 100644 core/pubnub_ntf_dynamic.c create mode 100644 core/pubnub_ntf_dynamic.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 50f9881a..a37eb9e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,8 +58,10 @@ log_option(ASAN "Use Address sanitizer" OFF) if (WITH_CPP) set(DEFAULT_USE_CALLBACK_API ON) + set(DEFAULT_USE_SYNC_API OFF) else () set(DEFAULT_USE_CALLBACK_API OFF) + set(DEFAULT_USE_SYNC_API ON) endif () log_option(USE_CPP11 "Use C++11 [WITH_CPP=ON needed]" OFF) @@ -79,7 +81,8 @@ num_option(USE_REVOKE_TOKEN_API "Use revoke token API [OPENSSL ONLY]" OFF) num_option(USE_GRANT_TOKEN_API "Use grant token API [OPENSSL ONLY]" OFF) num_option(USE_FETCH_HISTORY "Use fetch history" ON) num_option(USE_CRYPTO_API "Use crypto API [OPENSSL ONLY]" OFF) -num_option(USE_CALLBACK_API "Use callback API [CALLBACK=ON SYNC=OFF]" ${DEFAULT_USE_CALLBACK_API}) +num_option(USE_SYNC_API "Use sync API" ${DEFAULT_USE_SYNC_API}) +num_option(USE_CALLBACK_API "Use callback API" ${DEFAULT_USE_CALLBACK_API}) num_option(USE_IPV6 "Use IPv6" ON) num_option(USE_SET_DNS_SERVERS "Use set DNS servers [CALLBACK=ON]" ${DEFAULT_USE_CALLBACK_API}) num_option(USE_EXTERN_API "Use extern C API [WITH_CPP=ON]" ON) @@ -97,6 +100,10 @@ if (${OPENSSL} AND ${MBEDTLS}) message(FATAL_ERROR "You can't use both OpenSSL and mbedTLS at the same time!") endif () +if (NOT ${USE_CALLBACK_API} AND NOT ${USE_SYNC_API}) + message(FATAL_ERROR "You must select at least one API type!") +endif () + # Flags configuration set(FLAGS "\ @@ -143,7 +150,13 @@ if (${USE_CALLBACK_API}) ${FLAGS} \ -D PUBNUB_SET_DNS_SERVERS=${USE_SET_DNS_SERVERS} \ -D PUBNUB_USE_IPV6=${USE_IPV6} \ - -D PUBNUB_CALLBACK_API") + -D PUBNUB_CALLBACK_API=${USE_CALLBACK_API}") +endif () + +if (${USE_SYNC_API}) + set(FLAGS "\ + ${FLAGS} \ + -D PUBNUB_USE_SYNC_API=${USE_SYNC_API}") endif () if (${ASAN}) @@ -276,8 +289,6 @@ endif () set(INTF_SOURCEFILES) if (${USE_CALLBACK_API}) - message(STATUS "Using callback API") - set(CORE_SOURCEFILES ${CORE_SOURCEFILES} ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_memory_block.c @@ -308,9 +319,9 @@ if (${USE_CALLBACK_API}) ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_dns_system_servers.c ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_get_native_socket.c) endif () -else () - message(STATUS "Using sync API") +endif () +if (${USE_SYNC_API}) set(INTF_SOURCEFILES ${INTF_SOURCEFILES} ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_sync.c @@ -318,6 +329,23 @@ else () ${CMAKE_CURRENT_LIST_DIR}/core/srand_from_pubnub_time.c) endif () +if (${USE_SYNC_API} AND ${USE_CALLBACK_API}) + message(STATUS "Using runtime API selection") + + set(FLAGS "\ + ${FLAGS} \ + -D PUBNUB_NTF_DYNAMIC=1") + + set(INTF_SOURCEFILES + ${INTF_SOURCEFILES} + ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_dynamic.c) + +elseif (${USE_SYNC_API}) + message(STATUS "Using sync API") +elseif (${USE_CALLBACK_API}) + message(STATUS "Using callback API") +endif () + set(FEATURE_SOURCEFILES) if (${USE_PROXY}) @@ -732,11 +760,17 @@ if (${EXAMPLES}) message(STATUS "Building callback examples") set(EXAMPLE_LIST pubnub_callback_sample - pubnub_callback_subloop_sample subscribe_publish_callback_sample - subscribe_publish_from_callback - publish_callback_subloop_sample - publish_queue_callback_subloop) + subscribe_publish_from_callback) + + if (NOT ${USE_SYNC_API}) + set(EXAMPLE_LIST + pubnub_callback_subloop_sample + publish_callback_subloop_sample + publish_queue_callback_subloop + ${EXAMPLE_LIST}) + endif () + if (${USE_SUBSCRIBE_EVENT_ENGINE}) set(EXAMPLE_LIST subscribe_event_engine_sample @@ -747,16 +781,23 @@ if (${EXAMPLES}) subscribe_publish_callback_sample # Only supports callback! ${CPP_EXAMPLE_LIST}) endif () - else () + endif () + if (${USE_SYNC_API}) message(STATUS "Building sync examples") set(EXAMPLE_LIST metadata - pubnub_sync_subloop_sample pubnub_sync_publish_retry pubnub_publish_via_post_sample pubnub_advanced_history_sample pubnub_fetch_history_sample cancel_subscribe_sync_sample) + + if (NOT ${USE_CALLBACK_API}) + set(EXAMPLE_LIST + pubnub_sync_subloop_sample + ${EXAMPLE_LIST}) + endif () + if (OPENSSL) set(EXAMPLE_LIST pubnub_crypto_module_sample diff --git a/core/pbpal_ntf_callback_admin.c b/core/pbpal_ntf_callback_admin.c index 46a67e42..6bdadebc 100644 --- a/core/pbpal_ntf_callback_admin.c +++ b/core/pbpal_ntf_callback_admin.c @@ -1,4 +1,5 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "core/pubnub_ntf_dynamic.h" #include "pubnub_internal.h" #if PUBNUB_USE_RETRY_CONFIGURATION @@ -12,6 +13,18 @@ #include "pubnub_assert.h" +// TODO: decide if it is worth to keep that here +// 1 - till the flag is fixed +#if 1 +#define MAYBE_INLINE +#else +#if __STDC_VERSION__ >= 199901L +#define MAYBE_INLINE static inline +#else +#define MAYBE_INLINE static +#endif +#endif // 1 + void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state) { @@ -48,7 +61,7 @@ void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state) } } -void pbnc_tr_cxt_state_reset(pubnub_t* pb) +MAYBE_INLINE void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb) { if (pb->trans == PBTT_SET_STATE) { @@ -60,7 +73,7 @@ void pbnc_tr_cxt_state_reset(pubnub_t* pb) } } -enum pubnub_res pubnub_last_result(pubnub_t* pb) +MAYBE_INLINE enum pubnub_res pubnub_last_result(pubnub_t* pb) { enum pubnub_res rslt; @@ -69,7 +82,7 @@ enum pubnub_res pubnub_last_result(pubnub_t* pb) pubnub_mutex_lock(pb->monitor); rslt = pb->core.last_result; if (rslt != PNR_OK){ - pbnc_tr_cxt_state_reset(pb); + pbnc_tr_cxt_state_reset_callback(pb); } pubnub_mutex_unlock(pb->monitor); @@ -77,12 +90,34 @@ enum pubnub_res pubnub_last_result(pubnub_t* pb) } +// TODO: DYNAMIC API +#if 0 +void pbnc_tr_cxt_state_reset(pubnub_t* pb) +{ + pbnc_tr_cxt_state_reset_callback(pb); +} + + +enum pubnub_res pubnub_last_result(pubnub_t* pb) +{ + return pubnub_last_result_callback(pb); +} +#endif + enum pubnub_res pubnub_register_callback(pubnub_t* pb, pubnub_callback_t cb, void* user_data) { PUBNUB_ASSERT(pb_valid_ctx_ptr(pb)); pubnub_mutex_lock(pb->monitor); + +// TODO: DYNAMIC API +#if 1 + if (PNA_DYNAMIC == pb->api_policy && NULL == pb->cb) { + pbntf_init_callback(); + } +#endif + pb->cb = cb; pb->user_data = user_data; pubnub_mutex_unlock(pb->monitor); diff --git a/core/pubnub_callback_subscribe_loop.c b/core/pubnub_callback_subscribe_loop.c index d1c7f77a..c0e99f20 100644 --- a/core/pubnub_callback_subscribe_loop.c +++ b/core/pubnub_callback_subscribe_loop.c @@ -61,7 +61,12 @@ static void sublup_context_callback(pubnub_t* pb, } +// TODO: DYNAMIC API +#if 0 pubnub_subloop_t* pubnub_subloop_define(pubnub_t* p, +#else +pubnub_subloop_t* pubnub_callback_subloop_define(pubnub_t* p, +#endif char const* channel, struct pubnub_subscribe_options options, pubnub_subloop_callback_t cb) diff --git a/core/pubnub_callback_subscribe_loop.h b/core/pubnub_callback_subscribe_loop.h index e9a1f725..e916115f 100644 --- a/core/pubnub_callback_subscribe_loop.h +++ b/core/pubnub_callback_subscribe_loop.h @@ -43,7 +43,12 @@ typedef void (*pubnub_subloop_callback_t)(pubnub_t *pbp, char const* message, en @retval NULL Failed to create a descriptor @result The subscribe loop descriptor created */ +// TODO: DYNAMIC API +#if 1 +PUBNUB_EXTERN pubnub_subloop_t* pubnub_callback_subloop_define(pubnub_t *p, char const *channel, struct pubnub_subscribe_options options, pubnub_subloop_callback_t cb); +#else PUBNUB_EXTERN pubnub_subloop_t* pubnub_subloop_define(pubnub_t *p, char const *channel, struct pubnub_subscribe_options options, pubnub_subloop_callback_t cb); +#endif /** Starts a subscribe loop. diff --git a/core/pubnub_internal_common.h b/core/pubnub_internal_common.h index 6d5ae3b5..63f559ff 100644 --- a/core/pubnub_internal_common.h +++ b/core/pubnub_internal_common.h @@ -109,6 +109,13 @@ #include #endif +// TODO: FIX FLAG MANAGEMENT +//#if !defined PUBNUB_NTF_DYNAMIC +//#define PUBNUB_NTF_DYNAMIC 0 +//#else +#include "core/pubnub_ntf_dynamic.h" +//#endif + #include "core/pubnub_crypto.h" /* Maximum object length that will be sent via PATCH, or POST methods */ @@ -535,6 +542,12 @@ struct pubnub_ { #endif /* PUBNUB_PROXY_API */ /** Crypto module for encryption and decryption */ struct pubnub_crypto_provider_t *crypto_module; + +// TODO: FIX FLAG MANAGEMENT +//#if PUBNUB_NTF_DYNAMIC + /** The PubNub API enforcement policy. */ + enum pubnub_api_enforcement api_policy; +//#endif // PUBNUB_NTF_DYNAMIC }; diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_dynamic.c new file mode 100644 index 00000000..1d164372 --- /dev/null +++ b/core/pubnub_ntf_dynamic.c @@ -0,0 +1,203 @@ +#include "core/pubnub_ntf_callback.h" +#include "pubnub_internal.h" +#include "core/pubnub_ntf_dynamic.h" +#include "core/pubnub_internal_common.h" +#include "pubnub_config.h" + + +enum determined_api_policy { + API_SYNC, + API_CALLBACK +}; + + +static enum determined_api_policy determine_api_policy(pubnub_t* pb) +{ + switch (pb->api_policy) { + case PNA_DYNAMIC: + return (pb->cb != NULL) ? API_CALLBACK : API_SYNC; + case PNA_SYNC: + return API_SYNC; + case PNA_CALLBACK: + return API_CALLBACK; + }; +} + + +enum pubnub_res pubnub_enforce_api(pubnub_t* p, enum pubnub_api_enforcement policy) { + enum pubnub_res rslt = PNR_OK; + + pubnub_mutex_lock(p->monitor); + + if (p->api_policy == policy) { + pubnub_mutex_unlock(p->monitor); + return PNR_OK; + } + + // We shouldn't change the API policy if the transaction is already in progress + if (!pbnc_can_start_transaction(p)) { + pubnub_mutex_unlock(p->monitor); + return PNR_IN_PROGRESS; + } + + enum determined_api_policy previous_policy = determine_api_policy(p); + + p->api_policy = policy; + + enum determined_api_policy current_policy = determine_api_policy(p); + + if (previous_policy != current_policy) { + switch (previous_policy) { + case API_SYNC: + if (0 != pbntf_init_callback()) { + rslt = PNR_INTERNAL_ERROR; + } + break; + case API_CALLBACK: + pubnub_stop(); + if (0 != pbntf_init_sync()) { + rslt = PNR_INTERNAL_ERROR; + } + break; + } + } + + pubnub_mutex_unlock(p->monitor); + return rslt; +} + + +int pbntf_watch_in_events(pubnub_t* pbp) +{ + enum determined_api_policy api_policy = determine_api_policy(pbp); + + switch (api_policy) { + case API_SYNC: + return pbntf_watch_in_events_sync(pbp); + case API_CALLBACK: + return pbntf_watch_in_events_callback(pbp); + } +} + + +int pbntf_watch_out_events(pubnub_t* pbp) +{ + enum determined_api_policy api_policy = determine_api_policy(pbp); + + switch (api_policy) { + case API_SYNC: + return pbntf_watch_out_events_sync(pbp); + case API_CALLBACK: + return pbntf_watch_out_events_callback(pbp); + } +} + + +int pbntf_init(void) +{ + // Calling the sync version by default because in most of cases the sync version + // would be selected as there won't be any callback set yet. + // During the first callback addition, function pbntf_init_callback() will be called. + pbntf_init_sync(); +} + + +int pbntf_enqueue_for_processing(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + return pbntf_enqueue_for_processing_sync(pb); + case API_CALLBACK: + return pbntf_enqueue_for_processing_callback(pb); + } +} + + +int pbntf_requeue_for_processing(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + return pbntf_requeue_for_processing_sync(pb); + case API_CALLBACK: + return pbntf_requeue_for_processing_callback(pb); + } +} + + +int pbntf_got_socket(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + return pbntf_got_socket_sync(pb); + case API_CALLBACK: + return pbntf_got_socket_callback(pb); + } +} + + +void pbntf_lost_socket(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + pbntf_lost_socket_sync(pb); + break; + case API_CALLBACK: + pbntf_lost_socket_callback(pb); + break; + } +} + + +void pbntf_start_wait_connect_timer(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + pbntf_start_wait_connect_timer_sync(pb); + break; + case API_CALLBACK: + pbntf_start_wait_connect_timer_callback(pb); + break; + } +} + + +void pbntf_start_transaction_timer(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + pbntf_start_transaction_timer_sync(pb); + break; + case API_CALLBACK: + pbntf_start_transaction_timer_callback(pb); + break; + } +} + + +void pbntf_update_socket(pubnub_t* pb) +{ + enum determined_api_policy api_policy = determine_api_policy(pb); + + switch (api_policy) { + case API_SYNC: + pbntf_update_socket_sync(pb); + break; + case API_CALLBACK: + pbntf_update_socket_callback(pb); + break; + } +} + + diff --git a/core/pubnub_ntf_dynamic.h b/core/pubnub_ntf_dynamic.h new file mode 100644 index 00000000..6f0db886 --- /dev/null +++ b/core/pubnub_ntf_dynamic.h @@ -0,0 +1,97 @@ +#ifndef PUBNUB_NTF_DYNAMIC_H +#define PUBNUB_NTF_DYNAMIC_H + +// TODO: FIX THIS FLAG MANAGMENT BEFORE MERGING +//#if PUBNUB_NTF_DYNAMIC + +#include "core/pubnub_api_types.h" + +/** The PubNub API enforcement policy. + * + * This is used to select the PubNub API to be used by the client at runtime. + * The client can use the sync API, the callback API, or it can + * dynamically select the API depending on the context. + * + * By default API will be selected depending on the context. + * + * Policy is required only if the client is built with both sync and callback + * APIs. If the client is built with only one API, the policy is not even compiled. + */ +enum pubnub_api_enforcement { + /** The PubNub API will be selected depending on the context. + * By default, The client will use the sync API, unless any + * callback is set, in which case the client will use the callback API. + */ + PNA_DYNAMIC, + + /** The PubNub client will enforce the sync API. */ + PNA_SYNC, + + /** The PubNub client will be the callback API. */ + PNA_CALLBACK +}; + + +/** Enforce the PubNub API policy. + * + * This function is used to enforce the PubNub API policy. + * The policy is used to select the PubNub API to be used by the client. + * + * Policy is required only if the client is built with both sync and callback + * APIs. If the client is built with only one API, the policy is not even compiled. + * + * @param pb The PubNub context. + * @param policy The PubNub API enforcement policy. + * + * @return PNR_OK if the policy is enforced successfully or one of matching + * `pubnub_res` error codes. + * + * @see pubnub_api_enforcement + * @see pubnub_res + */ +enum pubnub_res pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); + + +// TODO: maybe move it to pbcc file + +void pbntf_trans_outcome_sync(pubnub_t* pb, enum pubnub_state state); +void pbntf_trans_outcome_callback(pubnub_t* pb, enum pubnub_state state); + +int pbntf_init_sync(void); +int pbntf_init_callback(void); + +int pbntf_got_socket_sync(pubnub_t* pb); +int pbntf_got_socket_callback(pubnub_t* pb); + +void pbntf_update_socket_sync(pubnub_t* pb); +void pbntf_update_socket_callback(pubnub_t* pb); + +void pbntf_start_wait_connect_timer_sync(pubnub_t* pb); +void pbntf_start_wait_connect_timer_callback(pubnub_t* pb); + +void pbntf_start_transaction_timer_sync(pubnub_t* pb); +void pbntf_start_transaction_timer_callback(pubnub_t* pb); + +void pbntf_lost_socket_sync(pubnub_t* pb); +void pbntf_lost_socket_callback(pubnub_t* pb); + +int pbntf_enqueue_for_processing_sync(pubnub_t* pb); +int pbntf_enqueue_for_processing_callback(pubnub_t* pb); + +int pbntf_requeue_for_processing_sync(pubnub_t* pb); +int pbntf_requeue_for_processing_callback(pubnub_t* pb); + +int pbntf_watch_in_events_sync(pubnub_t* pb); +int pbntf_watch_in_events_callback(pubnub_t* pb); + +int pbntf_watch_out_events_sync(pubnub_t* pb); +int pbntf_watch_out_events_callback(pubnub_t* pb); + +void pbnc_tr_cxt_state_reset_sync(pubnub_t* pb); +void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb); + +enum pubnub_res pubnub_last_result_sync(pubnub_t* pb); +enum pubnub_res pubnub_last_result_callback(pubnub_t* pb); + +//#endif // PUBNUB_NTF_DYNAMIC +#endif // PUBNUB_NTF_DYNAMIC_H diff --git a/core/pubnub_ntf_sync.c b/core/pubnub_ntf_sync.c index 20caac8e..69a0e15b 100644 --- a/core/pubnub_ntf_sync.c +++ b/core/pubnub_ntf_sync.c @@ -16,13 +16,26 @@ #include "lib/msstopwatch/msstopwatch.h" -int pbntf_init(void) +// TODO: decide if it is worth to keep that here +// 1 - till the flag is fixed +#if 1 +#define MAYBE_INLINE +#else +#if __STDC_VERSION__ >= 199901L +#define MAYBE_INLINE static inline +#else +#define MAYBE_INLINE static +#endif +#endif // 1 + + +MAYBE_INLINE int pbntf_init_sync(void) { return 0; } -int pbntf_got_socket(pubnub_t* pb) +MAYBE_INLINE int pbntf_got_socket_sync(pubnub_t* pb) { #if PUBNUB_BLOCKING_IO_SETTABLE pbpal_set_blocking_io(pb); @@ -31,31 +44,31 @@ int pbntf_got_socket(pubnub_t* pb) } -void pbntf_update_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_update_socket_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); } -void pbntf_start_transaction_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_transaction_timer_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); } -void pbntf_start_wait_connect_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_wait_connect_timer_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); } -void pbntf_lost_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_lost_socket_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); } -void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state) +MAYBE_INLINE void pbntf_trans_outcome_sync(pubnub_t* pb, enum pubnub_state state) { PBNTF_TRANS_OUTCOME_COMMON(pb, state); PUBNUB_ASSERT(pbnc_can_start_transaction(pb)); @@ -83,14 +96,14 @@ void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state) } -int pbntf_enqueue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_enqueue_for_processing_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); return 0; } -int pbntf_requeue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_requeue_for_processing_sync(pubnub_t* pb) { PUBNUB_UNUSED(pb); @@ -98,20 +111,21 @@ int pbntf_requeue_for_processing(pubnub_t* pb) } -int pbntf_watch_in_events(pubnub_t* pbp) +MAYBE_INLINE int pbntf_watch_out_events_sync(pubnub_t* pbp) { PUBNUB_UNUSED(pbp); return 0; } -int pbntf_watch_out_events(pubnub_t* pbp) +MAYBE_INLINE int pbntf_watch_in_events_sync(pubnub_t* pbp) { PUBNUB_UNUSED(pbp); return 0; } -void pbnc_tr_cxt_state_reset(pubnub_t* pb) + +MAYBE_INLINE void pbnc_tr_cxt_state_reset_sync(pubnub_t* pb) { if (pb->trans == PBTT_SET_STATE) { @@ -123,7 +137,8 @@ void pbnc_tr_cxt_state_reset(pubnub_t* pb) } } -enum pubnub_res pubnub_last_result(pubnub_t* pb) + +MAYBE_INLINE enum pubnub_res pubnub_last_result_sync(pubnub_t* pb) { enum pubnub_res result; PUBNUB_ASSERT(pb_valid_ctx_ptr(pb)); @@ -134,13 +149,92 @@ enum pubnub_res pubnub_last_result(pubnub_t* pb) } result = pb->core.last_result; if (result != PNR_OK){ - pbnc_tr_cxt_state_reset(pb); + pbnc_tr_cxt_state_reset_sync(pb); } pubnub_mutex_unlock(pb->monitor); return result; } + +#if 0 + +int pbntf_init(void) +{ + return pbntf_init_sync(); +} + + +int pbntf_got_socket(pubnub_t* pb) +{ + return pbntf_got_socket_sync(pb); +} + + +void pbntf_update_socket(pubnub_t* pb) +{ + pbntf_update_socket_sync(pb); +} + + +void pbntf_start_transaction_timer(pubnub_t* pb) +{ + pbntf_start_transaction_timer_sync(pb); +} + + +void pbntf_start_wait_connect_timer(pubnub_t* pb) +{ + pbntf_start_wait_connect_timer_sync(pb); +} + + +void pbntf_lost_socket(pubnub_t* pb) +{ + pbntf_lost_socket_sync(pb); +} + + +void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state) +{ + pbntf_trans_outcome_sync(pb, state); +} + + +int pbntf_enqueue_for_processing(pubnub_t* pb) { + return pbntf_enqueue_for_processing_sync(pb); +} + + +int pbntf_requeue_for_processing(pubnub_t* pb) +{ + return pbntf_requeue_for_processing_sync(pb); +} + + +int pbntf_watch_in_events(pubnub_t* pbp) +{ + return pbntf_watch_in_events_sync(pbp); +} + + +int pbntf_watch_out_events(pubnub_t* pbp) +{ + return pbntf_watch_out_events_sync(pbp); +} + + +void pbnc_tr_cxt_state_reset(pubnub_t* pb) { + return pbnc_tr_cxt_state_reset_sync(pb); +} + + +enum pubnub_res pubnub_last_result(pubnub_t* pb) { + return pubnub_last_result_sync(pb); +} +#endif + + enum pubnub_res pubnub_await(pubnub_t* pb) { pbmsref_t t0; @@ -170,9 +264,11 @@ enum pubnub_res pubnub_await(pubnub_t* pb) } result = pb->core.last_result; if (result != PNR_OK){ - pbnc_tr_cxt_state_reset(pb); + pbnc_tr_cxt_state_reset_sync(pb); } pubnub_mutex_unlock(pb->monitor); return result; } + + diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index a8953a97..dbca55a0 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -62,6 +62,12 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->keep_alive.max = 1000; p->keep_alive.timeout = 50; #endif + +// TODO: DYNAMIC API +#if 1 + p->api_policy = PNA_DYNAMIC; +#endif + pbpal_init(p); #if PUBNUB_PROXY_API p->proxy_type = pbproxyNONE; diff --git a/core/pubnub_sync_subscribe_loop.c b/core/pubnub_sync_subscribe_loop.c index 84b7fdf2..0933c535 100644 --- a/core/pubnub_sync_subscribe_loop.c +++ b/core/pubnub_sync_subscribe_loop.c @@ -9,7 +9,12 @@ +// TODO: DYNAMIC API +#if 0 struct pubnub_subloop_descriptor pubnub_subloop_define(pubnub_t *p, char const *channel) +#else +struct pubnub_subloop_descriptor pubnub_sync_subloop_define(pubnub_t *p, char const *channel) +#endif { struct pubnub_subloop_descriptor rslt = { p, channel }; rslt.options = pubnub_subscribe_defopts(); diff --git a/core/pubnub_sync_subscribe_loop.h b/core/pubnub_sync_subscribe_loop.h index a8132c59..b0970f26 100644 --- a/core/pubnub_sync_subscribe_loop.h +++ b/core/pubnub_sync_subscribe_loop.h @@ -39,7 +39,12 @@ struct pubnub_subloop_descriptor { @result The subscribe loop descriptor made */ +// TODO: DYNAMIC API +#if 1 +PUBNUB_EXTERN struct pubnub_subloop_descriptor pubnub_sync_subloop_define(pubnub_t *p, char const *channel); +#else PUBNUB_EXTERN struct pubnub_subloop_descriptor pubnub_subloop_define(pubnub_t *p, char const *channel); +#endif /** Designed to be called once in every iteration of a subscribe loop. Fetches the next message on the given @p channel and/or @p diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index 61145663..733b48e9 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -37,17 +37,30 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -int pbntf_watch_in_events(pubnub_t* pbp) +// TODO: decide if it is worth to keep that here +// 1 - till the flag is fixed +#if 1 +#define MAYBE_INLINE +#else +#if __STDC_VERSION__ >= 199901L +#define MAYBE_INLINE static inline +#else +#define MAYBE_INLINE static +#endif +#endif // 1 + + +MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_in_events(m_watcher.poll, pbp); } -int pbntf_watch_out_events(pubnub_t* pbp) +MAYBE_INLINE int pbntf_watch_out_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_out_events(m_watcher.poll, pbp); } - + void* socket_watcher_thread(void* arg) { @@ -108,7 +121,7 @@ void pubnub_stop(void) } -int pbntf_init(void) +MAYBE_INLINE int pbntf_init_callback(void) { int rslt; pthread_mutexattr_t attr; @@ -227,19 +240,19 @@ int pbntf_init(void) } -int pbntf_enqueue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_enqueue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_enqueue_for_processing(&m_watcher.queue, pb); } -int pbntf_requeue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_requeue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_requeue_for_processing(&m_watcher.queue, pb); } -int pbntf_got_socket(pubnub_t* pb) +MAYBE_INLINE int pbntf_got_socket_callback(pubnub_t* pb) { pthread_mutex_lock(&m_watcher.mutw); pbpal_ntf_callback_save_socket(m_watcher.poll, pb); @@ -257,7 +270,7 @@ int pbntf_got_socket(pubnub_t* pb) } -void pbntf_lost_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_lost_socket_callback(pubnub_t* pb) { pthread_mutex_lock(&m_watcher.mutw); pbpal_ntf_callback_remove_socket(m_watcher.poll, pb); @@ -271,7 +284,7 @@ void pbntf_lost_socket(pubnub_t* pb) } -void pbntf_start_wait_connect_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_wait_connect_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { pthread_mutex_lock(&m_watcher.timerlock); @@ -284,7 +297,7 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) } -void pbntf_start_transaction_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_transaction_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { pthread_mutex_lock(&m_watcher.timerlock); @@ -297,9 +310,73 @@ void pbntf_start_transaction_timer(pubnub_t* pb) } -void pbntf_update_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_update_socket_callback(pubnub_t* pb) { pthread_mutex_lock(&m_watcher.mutw); pbpal_ntf_callback_update_socket(m_watcher.poll, pb); pthread_mutex_unlock(&m_watcher.mutw); } + +#if 0 + +int pbntf_watch_in_events(pubnub_t* pbp) +{ + return pbntf_watch_in_events_callback(pbp); +} + + +int pbntf_watch_out_events(pubnub_t* pbp) +{ + return pbntf_watch_out_events_callback(pbp); +} + + +int pbntf_init(void) +{ + return pbntf_init_callback(); +} + + +int pbntf_enqueue_for_processing(pubnub_t* pb) +{ + return pbntf_enqueue_for_processing_callback(pb); +} + + +int pbntf_requeue_for_processing(pubnub_t* pb) +{ + return pbntf_requeue_for_processing_callback(pb); +} + + +int pbntf_got_socket(pubnub_t* pb) +{ + return pbntf_got_socket_callback(pb); +} + + +void pbntf_lost_socket(pubnub_t* pb) +{ + pbntf_lost_socket_callback(pb); +} + + +void pbntf_start_wait_connect_timer(pubnub_t* pb) +{ + pbntf_start_wait_connect_timer_callback(pb); +} + + +void pbntf_start_transaction_timer(pubnub_t* pb) +{ + pbntf_start_transaction_timer_callback(pb); +} + + +void pbntf_update_socket(pubnub_t* pb) +{ + pbntf_update_socket_callback(pb); +} + +#endif + From f8fc91fc2d6150fa4656ce99cad6f80e83bd99aa Mon Sep 17 00:00:00 2001 From: Xavrax Date: Tue, 21 Jan 2025 11:03:10 +0100 Subject: [PATCH 02/24] some cmake changes --- CMakeLists.txt | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a37eb9e3..babf8fca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,7 @@ if (WITH_CPP) set(DEFAULT_USE_CALLBACK_API ON) set(DEFAULT_USE_SYNC_API OFF) else () - set(DEFAULT_USE_CALLBACK_API OFF) + set(DEFAULT_USE_CALLBACK_API ON) set(DEFAULT_USE_SYNC_API ON) endif () @@ -332,9 +332,10 @@ endif () if (${USE_SYNC_API} AND ${USE_CALLBACK_API}) message(STATUS "Using runtime API selection") - set(FLAGS "\ - ${FLAGS} \ - -D PUBNUB_NTF_DYNAMIC=1") + # TODO: something is wrong with flags :c + set(CMAKE_C_FLAGS "\ + ${CMAKE_C_FLAGS} \ + -D PUBNUB_NTF_RUNTIME_SELECTION") set(INTF_SOURCEFILES ${INTF_SOURCEFILES} @@ -761,7 +762,8 @@ if (${EXAMPLES}) set(EXAMPLE_LIST pubnub_callback_sample subscribe_publish_callback_sample - subscribe_publish_from_callback) + subscribe_publish_from_callback + ${EXAMPLE_LIST}) if (NOT ${USE_SYNC_API}) set(EXAMPLE_LIST @@ -790,7 +792,8 @@ if (${EXAMPLES}) pubnub_publish_via_post_sample pubnub_advanced_history_sample pubnub_fetch_history_sample - cancel_subscribe_sync_sample) + cancel_subscribe_sync_sample + ${EXAMPLE_LIST}) if (NOT ${USE_CALLBACK_API}) set(EXAMPLE_LIST @@ -814,6 +817,13 @@ if (${EXAMPLES}) ${EXAMPLE_LIST}) endif () endif () + + if (${USE_SYNC_API} AND ${USE_CALLBACK_API}) + set(EXAMPLE_LIST + pubnub_api_enforcement_sample + ${EXAMPLE_LIST}) + endif () + else () message(STATUS "Building example ${EXAMPLE}") set(EXAMPLE_LIST ${EXAMPLE}) From 90ac3d3855e050b5ac128d92da4b4d6acc8800c8 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Tue, 21 Jan 2025 14:02:59 +0100 Subject: [PATCH 03/24] more works --- CMakeLists.txt | 8 +- core/pbpal_ntf_callback_admin.c | 11 +- core/pubnub_callback_subscribe_loop.c | 3 +- core/pubnub_callback_subscribe_loop.h | 3 +- core/pubnub_internal_common.h | 9 +- core/pubnub_ntf_dynamic.c | 149 +++++----------- core/pubnub_ntf_dynamic.h | 21 +-- core/pubnub_ntf_sync.c | 3 +- core/pubnub_pubsubapi.c | 6 - core/pubnub_sync_subscribe_loop.c | 5 +- core/pubnub_sync_subscribe_loop.h | 3 +- core/samples/pubnub_api_enforcement_sample.c | 159 ++++++++++++++++++ .../pbpal_resolv_and_connect_sockets.c | 33 +++- lib/sockets/pbpal_sockets.c | 9 +- mbedtls/pbpal_mbedtls.c | 6 +- microchip_harmony/pbpal_harmony.c | 6 +- microchip_harmony/pubnub_ntf_harmony.c | 3 +- openssl/pbpal_openssl.c | 6 +- posix/pubnub_ntf_callback_posix.c | 2 +- windows/pubnub_ntf_callback_windows.c | 3 +- 20 files changed, 270 insertions(+), 178 deletions(-) create mode 100644 core/samples/pubnub_api_enforcement_sample.c diff --git a/CMakeLists.txt b/CMakeLists.txt index babf8fca..85db5766 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ if (WITH_CPP) set(DEFAULT_USE_SYNC_API OFF) else () set(DEFAULT_USE_CALLBACK_API ON) - set(DEFAULT_USE_SYNC_API ON) + set(DEFAULT_USE_SYNC_API OFF) endif () log_option(USE_CPP11 "Use C++11 [WITH_CPP=ON needed]" OFF) @@ -88,6 +88,7 @@ num_option(USE_SET_DNS_SERVERS "Use set DNS servers [CALLBACK=ON]" ${DEFAULT_USE num_option(USE_EXTERN_API "Use extern C API [WITH_CPP=ON]" ON) num_option(USE_LEGACY_CRYPTO_RANDOM_IV "Use random IV for legacy crypto module [OpenSSL only]" ON) num_option(USE_LOG_CALLBACK "Use possibility to replace default logging function with user provided callback" OFF) +log_option(COMPILE_COMMANDS "Generate compile_commands.json" OFF) log_set(OPENSSL_ROOT_DIR "" "OpenSSL root directory (leave empty for find_package() defaults)[OPENSSL=ON needed]") log_set(CUSTOM_OPENSSL_LIB_DIR "lib" "OpenSSL lib directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]") log_set(CUSTOM_OPENSSL_INCLUDE_DIR "include" "OpenSSL include directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]") @@ -104,6 +105,11 @@ if (NOT ${USE_CALLBACK_API} AND NOT ${USE_SYNC_API}) message(FATAL_ERROR "You must select at least one API type!") endif () +if(${COMPILE_COMMANDS}) + message(STATUS "Generating compile_commands.json") + set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") +endif() + # Flags configuration set(FLAGS "\ diff --git a/core/pbpal_ntf_callback_admin.c b/core/pbpal_ntf_callback_admin.c index 6bdadebc..dce8dd64 100644 --- a/core/pbpal_ntf_callback_admin.c +++ b/core/pbpal_ntf_callback_admin.c @@ -90,8 +90,7 @@ MAYBE_INLINE enum pubnub_res pubnub_last_result(pubnub_t* pb) } -// TODO: DYNAMIC API -#if 0 +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) void pbnc_tr_cxt_state_reset(pubnub_t* pb) { pbnc_tr_cxt_state_reset_callback(pb); @@ -110,14 +109,6 @@ enum pubnub_res pubnub_register_callback(pubnub_t* pb, { PUBNUB_ASSERT(pb_valid_ctx_ptr(pb)); pubnub_mutex_lock(pb->monitor); - -// TODO: DYNAMIC API -#if 1 - if (PNA_DYNAMIC == pb->api_policy && NULL == pb->cb) { - pbntf_init_callback(); - } -#endif - pb->cb = cb; pb->user_data = user_data; pubnub_mutex_unlock(pb->monitor); diff --git a/core/pubnub_callback_subscribe_loop.c b/core/pubnub_callback_subscribe_loop.c index c0e99f20..01c44ba7 100644 --- a/core/pubnub_callback_subscribe_loop.c +++ b/core/pubnub_callback_subscribe_loop.c @@ -61,8 +61,7 @@ static void sublup_context_callback(pubnub_t* pb, } -// TODO: DYNAMIC API -#if 0 +#if !defined PUBNUB_NTF_RUNTIME_SELECTION pubnub_subloop_t* pubnub_subloop_define(pubnub_t* p, #else pubnub_subloop_t* pubnub_callback_subloop_define(pubnub_t* p, diff --git a/core/pubnub_callback_subscribe_loop.h b/core/pubnub_callback_subscribe_loop.h index e916115f..f2e5a357 100644 --- a/core/pubnub_callback_subscribe_loop.h +++ b/core/pubnub_callback_subscribe_loop.h @@ -43,8 +43,7 @@ typedef void (*pubnub_subloop_callback_t)(pubnub_t *pbp, char const* message, en @retval NULL Failed to create a descriptor @result The subscribe loop descriptor created */ -// TODO: DYNAMIC API -#if 1 +#if defined PUBNUB_NTF_RUNTIME_SELECTION PUBNUB_EXTERN pubnub_subloop_t* pubnub_callback_subloop_define(pubnub_t *p, char const *channel, struct pubnub_subscribe_options options, pubnub_subloop_callback_t cb); #else PUBNUB_EXTERN pubnub_subloop_t* pubnub_subloop_define(pubnub_t *p, char const *channel, struct pubnub_subscribe_options options, pubnub_subloop_callback_t cb); diff --git a/core/pubnub_internal_common.h b/core/pubnub_internal_common.h index 63f559ff..6402573b 100644 --- a/core/pubnub_internal_common.h +++ b/core/pubnub_internal_common.h @@ -109,12 +109,9 @@ #include #endif -// TODO: FIX FLAG MANAGEMENT -//#if !defined PUBNUB_NTF_DYNAMIC -//#define PUBNUB_NTF_DYNAMIC 0 -//#else +#if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_ntf_dynamic.h" -//#endif +#endif #include "core/pubnub_crypto.h" @@ -558,7 +555,7 @@ struct pubnub_ { */ void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state); -int pbntf_init(void); +int pbntf_init(pubnub_t* pb); int pbntf_got_socket(pubnub_t* pb); diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_dynamic.c index 1d164372..412f9ca3 100644 --- a/core/pubnub_ntf_dynamic.c +++ b/core/pubnub_ntf_dynamic.c @@ -1,80 +1,23 @@ +#if defined PUBNUB_NTF_RUNTIME_SELECTION + +#include "core/pubnub_assert.h" #include "core/pubnub_ntf_callback.h" #include "pubnub_internal.h" #include "core/pubnub_ntf_dynamic.h" #include "core/pubnub_internal_common.h" #include "pubnub_config.h" - -enum determined_api_policy { - API_SYNC, - API_CALLBACK -}; - - -static enum determined_api_policy determine_api_policy(pubnub_t* pb) -{ - switch (pb->api_policy) { - case PNA_DYNAMIC: - return (pb->cb != NULL) ? API_CALLBACK : API_SYNC; - case PNA_SYNC: - return API_SYNC; - case PNA_CALLBACK: - return API_CALLBACK; - }; -} - - -enum pubnub_res pubnub_enforce_api(pubnub_t* p, enum pubnub_api_enforcement policy) { - enum pubnub_res rslt = PNR_OK; - - pubnub_mutex_lock(p->monitor); - - if (p->api_policy == policy) { - pubnub_mutex_unlock(p->monitor); - return PNR_OK; - } - - // We shouldn't change the API policy if the transaction is already in progress - if (!pbnc_can_start_transaction(p)) { - pubnub_mutex_unlock(p->monitor); - return PNR_IN_PROGRESS; - } - - enum determined_api_policy previous_policy = determine_api_policy(p); - +void pubnub_enforce_api(pubnub_t* p, enum pubnub_api_enforcement policy) { p->api_policy = policy; - - enum determined_api_policy current_policy = determine_api_policy(p); - - if (previous_policy != current_policy) { - switch (previous_policy) { - case API_SYNC: - if (0 != pbntf_init_callback()) { - rslt = PNR_INTERNAL_ERROR; - } - break; - case API_CALLBACK: - pubnub_stop(); - if (0 != pbntf_init_sync()) { - rslt = PNR_INTERNAL_ERROR; - } - break; - } - } - - pubnub_mutex_unlock(p->monitor); - return rslt; } int pbntf_watch_in_events(pubnub_t* pbp) { - enum determined_api_policy api_policy = determine_api_policy(pbp); - - switch (api_policy) { - case API_SYNC: + switch (pbp->api_policy) { + case PNA_SYNC: return pbntf_watch_in_events_sync(pbp); - case API_CALLBACK: + case PNA_CALLBACK: return pbntf_watch_in_events_callback(pbp); } } @@ -82,34 +25,32 @@ int pbntf_watch_in_events(pubnub_t* pbp) int pbntf_watch_out_events(pubnub_t* pbp) { - enum determined_api_policy api_policy = determine_api_policy(pbp); - - switch (api_policy) { - case API_SYNC: + switch (pbp->api_policy) { + case PNA_SYNC: return pbntf_watch_out_events_sync(pbp); - case API_CALLBACK: + case PNA_CALLBACK: return pbntf_watch_out_events_callback(pbp); } } -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { - // Calling the sync version by default because in most of cases the sync version - // would be selected as there won't be any callback set yet. - // During the first callback addition, function pbntf_init_callback() will be called. - pbntf_init_sync(); + switch (pb->api_policy) { + case PNA_SYNC: + return pbntf_init_sync(); + case PNA_CALLBACK: + return pbntf_init_callback(); + } } int pbntf_enqueue_for_processing(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: return pbntf_enqueue_for_processing_sync(pb); - case API_CALLBACK: + case PNA_CALLBACK: return pbntf_enqueue_for_processing_callback(pb); } } @@ -117,12 +58,10 @@ int pbntf_enqueue_for_processing(pubnub_t* pb) int pbntf_requeue_for_processing(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: return pbntf_requeue_for_processing_sync(pb); - case API_CALLBACK: + case PNA_CALLBACK: return pbntf_requeue_for_processing_callback(pb); } } @@ -130,12 +69,10 @@ int pbntf_requeue_for_processing(pubnub_t* pb) int pbntf_got_socket(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: return pbntf_got_socket_sync(pb); - case API_CALLBACK: + case PNA_CALLBACK: return pbntf_got_socket_callback(pb); } } @@ -143,13 +80,11 @@ int pbntf_got_socket(pubnub_t* pb) void pbntf_lost_socket(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: pbntf_lost_socket_sync(pb); break; - case API_CALLBACK: + case PNA_CALLBACK: pbntf_lost_socket_callback(pb); break; } @@ -158,13 +93,11 @@ void pbntf_lost_socket(pubnub_t* pb) void pbntf_start_wait_connect_timer(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: pbntf_start_wait_connect_timer_sync(pb); break; - case API_CALLBACK: + case PNA_CALLBACK: pbntf_start_wait_connect_timer_callback(pb); break; } @@ -173,13 +106,11 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) void pbntf_start_transaction_timer(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: pbntf_start_transaction_timer_sync(pb); break; - case API_CALLBACK: + case PNA_CALLBACK: pbntf_start_transaction_timer_callback(pb); break; } @@ -188,16 +119,14 @@ void pbntf_start_transaction_timer(pubnub_t* pb) void pbntf_update_socket(pubnub_t* pb) { - enum determined_api_policy api_policy = determine_api_policy(pb); - - switch (api_policy) { - case API_SYNC: + switch (pb->api_policy) { + case PNA_SYNC: pbntf_update_socket_sync(pb); break; - case API_CALLBACK: + case PNA_CALLBACK: pbntf_update_socket_callback(pb); break; } } - +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ diff --git a/core/pubnub_ntf_dynamic.h b/core/pubnub_ntf_dynamic.h index 6f0db886..eee1d353 100644 --- a/core/pubnub_ntf_dynamic.h +++ b/core/pubnub_ntf_dynamic.h @@ -1,29 +1,21 @@ #ifndef PUBNUB_NTF_DYNAMIC_H #define PUBNUB_NTF_DYNAMIC_H -// TODO: FIX THIS FLAG MANAGMENT BEFORE MERGING -//#if PUBNUB_NTF_DYNAMIC +#if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_api_types.h" /** The PubNub API enforcement policy. * * This is used to select the PubNub API to be used by the client at runtime. - * The client can use the sync API, the callback API, or it can - * dynamically select the API depending on the context. + * The client can use the sync API, the callback API. * - * By default API will be selected depending on the context. + * By default it selects the sync API. * * Policy is required only if the client is built with both sync and callback * APIs. If the client is built with only one API, the policy is not even compiled. */ enum pubnub_api_enforcement { - /** The PubNub API will be selected depending on the context. - * By default, The client will use the sync API, unless any - * callback is set, in which case the client will use the callback API. - */ - PNA_DYNAMIC, - /** The PubNub client will enforce the sync API. */ PNA_SYNC, @@ -37,6 +29,9 @@ enum pubnub_api_enforcement { * This function is used to enforce the PubNub API policy. * The policy is used to select the PubNub API to be used by the client. * + * It is crucial to select the API right after the allocation of the context + * but before the initialization to let the client know which API to use. + * * Policy is required only if the client is built with both sync and callback * APIs. If the client is built with only one API, the policy is not even compiled. * @@ -49,7 +44,7 @@ enum pubnub_api_enforcement { * @see pubnub_api_enforcement * @see pubnub_res */ -enum pubnub_res pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); +void pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); // TODO: maybe move it to pbcc file @@ -93,5 +88,5 @@ void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb); enum pubnub_res pubnub_last_result_sync(pubnub_t* pb); enum pubnub_res pubnub_last_result_callback(pubnub_t* pb); -//#endif // PUBNUB_NTF_DYNAMIC +#endif // PUBNUB_NTF_RUNTIME_SELECTION #endif // PUBNUB_NTF_DYNAMIC_H diff --git a/core/pubnub_ntf_sync.c b/core/pubnub_ntf_sync.c index 69a0e15b..a4af3054 100644 --- a/core/pubnub_ntf_sync.c +++ b/core/pubnub_ntf_sync.c @@ -159,8 +159,9 @@ MAYBE_INLINE enum pubnub_res pubnub_last_result_sync(pubnub_t* pb) #if 0 -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { + PUBNUB_UNUSED(pb); return pbntf_init_sync(); } diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index dbca55a0..a8953a97 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -62,12 +62,6 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->keep_alive.max = 1000; p->keep_alive.timeout = 50; #endif - -// TODO: DYNAMIC API -#if 1 - p->api_policy = PNA_DYNAMIC; -#endif - pbpal_init(p); #if PUBNUB_PROXY_API p->proxy_type = pbproxyNONE; diff --git a/core/pubnub_sync_subscribe_loop.c b/core/pubnub_sync_subscribe_loop.c index 0933c535..5f6b2778 100644 --- a/core/pubnub_sync_subscribe_loop.c +++ b/core/pubnub_sync_subscribe_loop.c @@ -7,10 +7,7 @@ #include "pubnub_ntf_sync.h" #include "pubnub_assert.h" - - -// TODO: DYNAMIC API -#if 0 +#if !defined PUBNUB_NTF_RUNTIME_SELECTION struct pubnub_subloop_descriptor pubnub_subloop_define(pubnub_t *p, char const *channel) #else struct pubnub_subloop_descriptor pubnub_sync_subloop_define(pubnub_t *p, char const *channel) diff --git a/core/pubnub_sync_subscribe_loop.h b/core/pubnub_sync_subscribe_loop.h index b0970f26..efa2e905 100644 --- a/core/pubnub_sync_subscribe_loop.h +++ b/core/pubnub_sync_subscribe_loop.h @@ -39,8 +39,7 @@ struct pubnub_subloop_descriptor { @result The subscribe loop descriptor made */ -// TODO: DYNAMIC API -#if 1 +#if defined PUBNUB_NTF_RUNTIME_SELECTION PUBNUB_EXTERN struct pubnub_subloop_descriptor pubnub_sync_subloop_define(pubnub_t *p, char const *channel); #else PUBNUB_EXTERN struct pubnub_subloop_descriptor pubnub_subloop_define(pubnub_t *p, char const *channel); diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c new file mode 100644 index 00000000..3eb6e605 --- /dev/null +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -0,0 +1,159 @@ +/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "core/pubnub_ntf_dynamic.h" +#include "pubnub_sync.h" + +#include "core/pubnub_coreapi_ex.h" +#include "core/pubnub_coreapi.h" +#include "core/pubnub_helper.h" +#include "core/pubnub_timers.h" +#include "core/pubnub_subscribe_event_listener.h" +#include "core/pubnub_subscribe_event_engine.h" +#include "core/pubnub_entities.h" +#include "core/pubnub_free_with_timeout.h" + +#include +#include +#include + + +static void wait_seconds(double time_in_seconds); +static void sync_sample_free(pubnub_t* p); +static void callback_sample_free(pubnub_t* p); +static void subscribe_message_listener( + const pubnub_t* pb, + const struct pubnub_v2_message message); + + +int main() +{ + time_t t0; + enum pubnub_res res; + char const* user_id = "my_user_id"; + char const* channel_id = "my_channel"; + + pubnub_t* pbp_sync = pubnub_alloc(); + pubnub_t* pbp_callback = pubnub_alloc(); + + if (NULL == pbp_sync || NULL == pbp_callback) { + printf("Failed to allocate Pubnub context!\n"); + return -1; + } + + pubnub_enforce_api(pbp_sync, PNA_SYNC); + pubnub_enforce_api(pbp_callback, PNA_CALLBACK); + + char* my_env_publish_key = getenv("PUBNUB_PUBLISH_KEY"); + char* my_env_subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY"); + + if (NULL == my_env_publish_key) { my_env_publish_key = "demo"; } + if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "demo"; } + + pubnub_init(pbp_sync, my_env_publish_key, my_env_subscribe_key); + pubnub_init(pbp_callback, my_env_publish_key, my_env_subscribe_key); + + pubnub_set_user_id(pbp_sync, user_id); + pubnub_set_user_id(pbp_callback, user_id); + + pubnub_set_blocking_io(pbp_sync); + pubnub_set_blocking_io(pbp_callback); + + /** Callback context */ + + pubnub_channel_t* channel = pubnub_channel_alloc(pbp_callback, channel_id); + pubnub_subscription_t* subscription = + pubnub_subscription_alloc((pubnub_entity_t*)channel, NULL); + pubnub_entity_free((void**)&channel); + pubnub_subscribe_add_subscription_listener(subscription, + PBSL_LISTENER_ON_MESSAGE, + subscribe_message_listener); + printf("Subscribing with subscription...\n"); + enum pubnub_res rslt = pubnub_subscribe_with_subscription( + subscription, + NULL); + printf("Subscribe with subscription result: %s\n", + pubnub_res_2_string(rslt)); + + /** Sync context */ + + enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); + if (PNR_OK != publish_result && PNR_STARTED != publish_result) { + printf("Failed to publish message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + + if (PNR_OK != pubnub_await(pbp_sync)) { + printf("Failed to await message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + + wait_seconds(5); + + /** Cleanup */ + + printf("Unsubscribing from all...\n"); + rslt = pubnub_unsubscribe_all(pbp_callback); + printf("Unsubscribe from all result: %s\n", + pubnub_res_2_string(rslt)); + + sync_sample_free(pbp_sync); + + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + + puts("PubNub api enforcement demo over."); + + return 0; +} + +static void sync_sample_free(pubnub_t* p) +{ + if (PN_CANCEL_STARTED == pubnub_cancel(p)) { + enum pubnub_res pnru = pubnub_await(p); + if (pnru != PNR_OK) { + printf("Awaiting cancel failed: %d('%s')\n", + pnru, + pubnub_res_2_string(pnru)); + } + } + if (pubnub_free(p) != 0) { + printf("Failed to free the Pubnub context\n"); + } +} + +static void wait_seconds(double time_in_seconds) +{ + time_t start = time(NULL); + double time_passed_in_seconds; + do { + time_passed_in_seconds = difftime(time(NULL), start); + } while (time_passed_in_seconds < time_in_seconds); +} + + +static void callback_sample_free(pubnub_t* p) +{ + if (pubnub_free_with_timeout(p, 1000) != 0) { + printf("Failed to free the Pubnub context\n"); + } + else { + /* Waits until the context is released from the processing queue */ + wait_seconds(2); + } +} + +static void subscribe_message_listener( + const pubnub_t* pb, + const struct pubnub_v2_message message) { + printf("CALLBACK | Received message: %.*s\n", (int) message.payload.size, message.payload.ptr); +} + + + diff --git a/lib/sockets/pbpal_resolv_and_connect_sockets.c b/lib/sockets/pbpal_resolv_and_connect_sockets.c index ad4b0517..e73d1626 100644 --- a/lib/sockets/pbpal_resolv_and_connect_sockets.c +++ b/lib/sockets/pbpal_resolv_and_connect_sockets.c @@ -6,6 +6,9 @@ #include "core/pubnub_log.h" #include "lib/sockets/pbpal_adns_sockets.h" #include "lib/sockets/pbpal_socket_blocking_io.h" +#ifdef PUBNUB_NTF_RUNTIME_SELECTION +#include "pubnub_ntf_dynamic.h" +#endif #include #include @@ -412,6 +415,9 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t* pb) uint16_t port = HTTP_PORT; char const* origin; +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + if (PNA_CALLBACK == pb->api_policy) { // if policy +#endif #ifdef PUBNUB_CALLBACK_API sockaddr_inX_t dest = { 0 }; #if PUBNUB_USE_IPV6 @@ -489,7 +495,11 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t* pb) return pbpal_resolv_sent; -#else +#endif /* PUBNUB_CALLBACK_API */ +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + } else { // if policy +#endif +#if !defined PUBNUB_CALLBACK_API || defined PUBNUB_NTF_RUNTIME_SELECTION char port_string[20]; struct addrinfo* result; struct addrinfo* it; @@ -553,7 +563,10 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t* pb) socket_disable_SIGPIPE(pb->pal.socket); return error ? pbpal_connect_wouldblock : pbpal_connect_success; -#endif /* PUBNUB_CALLBACK_API */ +#endif /* !defined PUBNUB_CALLBACK_API || defined PUBNUB_NTF_RUNTIME_SELECTION */ +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + } // if policy +#endif } #if PUBNUB_USE_MULTIPLE_ADDRESSES @@ -564,6 +577,9 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t* pb) enum pbpal_resolv_n_connect_result pbpal_check_resolv_and_connect(pubnub_t* pb) { +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + if (PNA_CALLBACK == pb->api_policy) { // if policy +#endif #ifdef PUBNUB_CALLBACK_API sockaddr_inX_t dns_server = { 0 }; @@ -627,7 +643,11 @@ enum pbpal_resolv_n_connect_result pbpal_check_resolv_and_connect(pubnub_t* pb) } #endif /* PUBNUB_USE_MULTIPLE_ADDRESSES */ return rslt; -#else /* PUBNUB_CALLBACK_API */ +#endif /* PUBNUB_CALLBACK_API */ +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + } else { // if policy +#endif +#if !defined PUBNUB_CALLBACK_API || defined PUBNUB_NTF_RUNTIME_SELECTION PUBNUB_UNUSED(pb); @@ -635,7 +655,10 @@ enum pbpal_resolv_n_connect_result pbpal_check_resolv_and_connect(pubnub_t* pb) called unless using async DNS, so this is an error */ return pbpal_connect_failed; -#endif /* PUBNUB_CALLBACK_API */ +#endif /* !defined PUBNUB_CALLBACK_API || defined PUBNUB_NTF_RUNTIME_SELECTION */ +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + } // if policy +#endif } @@ -708,4 +731,4 @@ enum pbpal_resolv_n_connect_result pbpal_check_connect(pubnub_t* pb) } PUBNUB_LOG_TRACE("pbpal_connected(): no select() events\n"); return pbpal_connect_wouldblock; -} \ No newline at end of file +} diff --git a/lib/sockets/pbpal_sockets.c b/lib/sockets/pbpal_sockets.c index 719de89d..8b592b23 100644 --- a/lib/sockets/pbpal_sockets.c +++ b/lib/sockets/pbpal_sockets.c @@ -1,4 +1,5 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "core/pubnub_ntf_dynamic.h" #include "pubnub_internal.h" #include "core/pbpal.h" @@ -19,15 +20,15 @@ static void buf_setup(pubnub_t* pb) pb->left = sizeof pb->core.http_buf / sizeof pb->core.http_buf[0]; } - -static int pal_init(void) +static int pal_init(pubnub_t* pb) { static bool s_init = false; if (!s_init) { if (0 != socket_platform_init()) { return -1; } - pbntf_init(); + + pbntf_init(pb); s_init = true; } return 0; @@ -36,7 +37,7 @@ static int pal_init(void) void pbpal_init(pubnub_t* pb) { - pal_init(); + pal_init(pb); pb->pal.socket = SOCKET_INVALID; pb->sock_state = STATE_NONE; buf_setup(pb); diff --git a/mbedtls/pbpal_mbedtls.c b/mbedtls/pbpal_mbedtls.c index 3abac889..9f1ede3a 100644 --- a/mbedtls/pbpal_mbedtls.c +++ b/mbedtls/pbpal_mbedtls.c @@ -36,7 +36,7 @@ void pbpal_init(pubnub_t* pb) PUBNUB_LOG_DEBUG("pbpal_init()\n"); memset(&pb->pal, 0, sizeof pb->pal); - pbntf_setup(); + pbntf_setup(pb); options_setup(pb); buffer_setup(pb); } @@ -409,7 +409,7 @@ void pbpal_free(pubnub_t* pb) pb->sock_state = STATE_NONE; } -static void pbntf_setup(void) +static void pbntf_setup(pubnub_t* pb) { static bool init_done = false; PUBNUB_LOG_TRACE("pbntf_setup()\n"); @@ -419,7 +419,7 @@ static void pbntf_setup(void) return; } - pbntf_init(); + pbntf_init(pb); init_done = true; } diff --git a/microchip_harmony/pbpal_harmony.c b/microchip_harmony/pbpal_harmony.c index aaf46b7c..419030a2 100644 --- a/microchip_harmony/pbpal_harmony.c +++ b/microchip_harmony/pbpal_harmony.c @@ -17,11 +17,11 @@ static void buf_setup(pubnub_t *pb) } -static int pal_init(void) +static int pal_init(pubnub_t* pb) { static bool s_init = false; if (!s_init) { - pbntf_init(); + pbntf_init(pb); s_init = true; } return 0; @@ -30,7 +30,7 @@ static int pal_init(void) void pbpal_init(pubnub_t *pb) { - pal_init(); + pal_init(pb); pb->options.use_blocking_io = false; pb->pal.socket = SOCKET_INVALID; #if PUBNUB_USE_SSL diff --git a/microchip_harmony/pubnub_ntf_harmony.c b/microchip_harmony/pubnub_ntf_harmony.c index 4623dd06..8485e75b 100644 --- a/microchip_harmony/pubnub_ntf_harmony.c +++ b/microchip_harmony/pubnub_ntf_harmony.c @@ -108,8 +108,9 @@ void pubnub_task(void) } -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { + PUBNUB_UNUSED(pb); return 0; } diff --git a/openssl/pbpal_openssl.c b/openssl/pbpal_openssl.c index 6bf8aa8a..7cafc0a6 100644 --- a/openssl/pbpal_openssl.c +++ b/openssl/pbpal_openssl.c @@ -90,7 +90,7 @@ static void buf_setup(pubnub_t* pb) } -static int pal_init(void) +static int pal_init(pubnub_t* pb) { static bool s_init = false; if (!s_init) { @@ -115,7 +115,7 @@ static int pal_init(void) if (0 != socket_platform_init()) { return -1; } - pbntf_init(); + pbntf_init(pb); s_init = true; } return 0; @@ -124,7 +124,7 @@ static int pal_init(void) void pbpal_init(pubnub_t* pb) { - pal_init(); + pal_init(pb); memset(&pb->pal, 0, sizeof pb->pal); pb->pal.socket = SOCKET_INVALID; pb->options.useSSL = pb->flags.trySSL = pb->options.fallbackSSL = true; diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index 733b48e9..206a8239 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -331,7 +331,7 @@ int pbntf_watch_out_events(pubnub_t* pbp) } -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { return pbntf_init_callback(); } diff --git a/windows/pubnub_ntf_callback_windows.c b/windows/pubnub_ntf_callback_windows.c index 26e6e0ff..828a636d 100644 --- a/windows/pubnub_ntf_callback_windows.c +++ b/windows/pubnub_ntf_callback_windows.c @@ -94,8 +94,9 @@ void socket_watcher_thread(void* arg) } -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { + PUBNUB_UNUSED(pb); InitializeCriticalSection(&m_watcher.stoplock); InitializeCriticalSection(&m_watcher.mutw); InitializeCriticalSection(&m_watcher.timerlock); From 5d17ba87011a51f4a384ef251518b71d08e572f4 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Wed, 22 Jan 2025 17:58:37 +0100 Subject: [PATCH 04/24] almost all callbacks defines are runtime now --- core/pbauto_heartbeat.c | 16 +++++++-- core/pbauto_heartbeat.h | 2 +- core/pbcc_request_retry_timer.c | 10 +++++- core/pbcc_subscribe_event_listener.c | 13 +++++-- core/pbpal_ntf_callback_admin.c | 2 +- core/pubnub_alloc_static.c | 11 +++++- core/pubnub_alloc_std.c | 11 +++++- core/pubnub_internal_common.h | 5 ++- core/pubnub_netcore.c | 13 ++++++- core/pubnub_ntf_dynamic.c | 10 ++++++ core/pubnub_ntf_sync.c | 2 +- core/pubnub_proxy.c | 13 +++++++ core/pubnub_pubsubapi.c | 37 +++++++++++++++++++- core/samples/pubnub_api_enforcement_sample.c | 36 +++++++++---------- posix/pubnub_ntf_callback_posix.c | 5 +-- 15 files changed, 150 insertions(+), 36 deletions(-) diff --git a/core/pbauto_heartbeat.c b/core/pbauto_heartbeat.c index 2f860991..e78fd750 100644 --- a/core/pbauto_heartbeat.c +++ b/core/pbauto_heartbeat.c @@ -104,7 +104,7 @@ static bool pubsub_keys_changed(pubnub_t const* pb_clone, pubnub_t const* pb) || (pb_clone->core.subscribe_key != pb->core.subscribe_key); } -#if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_CALLBACK_API) && !defined(PUBNUB_NTF_RUNTIME_SELECTION) #define add_heartbeat_in_progress(thumper_index) #else static void add_heartbeat_in_progress(unsigned thumper_index) @@ -131,7 +131,13 @@ static void heartbeat_thump(pubnub_t* pb, pubnub_t* heartbeat_pb) if (keys_changed) { /** Used in sync environment while for callback it's an empty macro */ +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_SYNC == pb->api_policy) { + add_heartbeat_in_progress(pb->thumperIndex); + } +#else add_heartbeat_in_progress(pb->thumperIndex); +#endif pubnub_mutex_unlock(pb->monitor); pubnub_cancel(heartbeat_pb); return; @@ -156,7 +162,13 @@ static void heartbeat_thump(pubnub_t* pb, pubnub_t* heartbeat_pb) pubnub_res_2_string(res)); } /** Used in sync environment while for callback it's an empty macro */ +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_SYNC == pb->api_policy) { + add_heartbeat_in_progress(pb->thumperIndex); + } +#else add_heartbeat_in_progress(pb->thumperIndex); +#endif } pubnub_mutex_unlock(pb->monitor); } @@ -240,7 +252,7 @@ void pbauto_take_the_node_out(unsigned* indexes, unsigned i, unsigned* dimension memmove(node_out, node_out + 1, (*dimension - i) * sizeof(unsigned)); } -#if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_CALLBACK_API) && !defined(PUBNUB_NTF_RUNTIME_SELECTION) #define handle_heartbeats_in_progress() #else static void handle_heartbeats_in_progress(void) diff --git a/core/pbauto_heartbeat.h b/core/pbauto_heartbeat.h index 5d99461e..8f822efd 100644 --- a/core/pbauto_heartbeat.h +++ b/core/pbauto_heartbeat.h @@ -29,7 +29,7 @@ struct pubnub_heartbeat_data { struct HeartbeatWatcherData { struct pubnub_heartbeat_data heartbeat_data[PUBNUB_MAX_HEARTBEAT_THUMPERS] pubnub_guarded_by(mutw); -#if !defined(PUBNUB_CALLBACK_API) +#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION) /** Array of thumper indices for which auto heartbeat transactions are currently in progress. Used in sync environment. */ diff --git a/core/pbcc_request_retry_timer.c b/core/pbcc_request_retry_timer.c index c59e3c34..9dc6e3ee 100644 --- a/core/pbcc_request_retry_timer.c +++ b/core/pbcc_request_retry_timer.c @@ -122,6 +122,11 @@ void pbcc_request_retry_timer_start( timer->pb->core.http_retry_count++; pubnub_mutex_unlock(timer->pb->monitor); +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_SYNC == timer->pb->api_policy) { + pbcc_request_retry_timer_run_(timer); + } else { +#endif #if defined(PUBNUB_CALLBACK_API) if (pthread_create(&timer->timer_thread, NULL, @@ -144,6 +149,9 @@ void pbcc_request_retry_timer_start( } pubnub_mutex_unlock(timer->pb->monitor); } +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + } /* if (PNA_SYNC == timer->pb->api_policy) */ +#endif #else pbcc_request_retry_timer_run_(timer); #endif // #if defined(PUBNUB_CALLBACK_API) @@ -195,4 +203,4 @@ void* pbcc_request_retry_timer_run_(pbcc_request_retry_timer_t* timer) pbcc_request_retry_timer_stop(timer); return NULL; -} \ No newline at end of file +} diff --git a/core/pbcc_subscribe_event_listener.c b/core/pbcc_subscribe_event_listener.c index 30d23e34..2b34ee0f 100644 --- a/core/pbcc_subscribe_event_listener.c +++ b/core/pbcc_subscribe_event_listener.c @@ -503,8 +503,15 @@ void pbcc_event_listener_free(pbcc_event_listener_t** event_listener) if (NULL == event_listener || NULL == *event_listener) { return; } pubnub_mutex_lock((*event_listener)->mutw); - pbarray_free(&(*event_listener)->global_status); - pbarray_free(&(*event_listener)->global_events); + + if (NULL != (*event_listener)->global_events) { + pbarray_free(&(*event_listener)->global_events); + } + + if (NULL != (*event_listener)->listeners) { + pbhash_set_free(&(*event_listener)->listeners); + } + pbhash_set_free(&(*event_listener)->listeners); pubnub_mutex_unlock((*event_listener)->mutw); pubnub_mutex_destroy((*event_listener)->mutw); @@ -678,4 +685,4 @@ pbarray_t* pbcc_initialize_array_( PBARRAY_RESIZE_BALANCED, PBARRAY_GENERIC_CONTENT_TYPE, free_fn); -} \ No newline at end of file +} diff --git a/core/pbpal_ntf_callback_admin.c b/core/pbpal_ntf_callback_admin.c index dce8dd64..2af82638 100644 --- a/core/pbpal_ntf_callback_admin.c +++ b/core/pbpal_ntf_callback_admin.c @@ -73,7 +73,7 @@ MAYBE_INLINE void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb) } } -MAYBE_INLINE enum pubnub_res pubnub_last_result(pubnub_t* pb) +MAYBE_INLINE enum pubnub_res pubnub_last_result_callback(pubnub_t* pb) { enum pubnub_res rslt; diff --git a/core/pubnub_alloc_static.c b/core/pubnub_alloc_static.c index 20445e59..f6922d71 100644 --- a/core/pubnub_alloc_static.c +++ b/core/pubnub_alloc_static.c @@ -5,6 +5,7 @@ #include "pubnub_log.h" #include "pbpal.h" +#include "pubnub_ntf_dynamic.h" static struct pubnub_ m_aCtx[PUBNUB_CTX_MAX]; @@ -78,7 +79,15 @@ int pubnub_free(pubnub_t *pb) pubnub_disable_auto_heartbeat(pb); pbauto_heartbeat_free_channelInfo(pb); pb->state = PBS_NULL; -#if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == pb->api_policy) { + pbntf_requeue_for_processing(pb); + pubnub_mutex_unlock(pb->monitor); + } else { + pubnub_mutex_unlock(pb->monitor); + pballoc_free_at_last(pb); + } +#elif defined(PUBNUB_CALLBACK_API) pbntf_requeue_for_processing(pb); pubnub_mutex_unlock(pb->monitor); #else diff --git a/core/pubnub_alloc_std.c b/core/pubnub_alloc_std.c index 3e6e58ee..96f67763 100644 --- a/core/pubnub_alloc_std.c +++ b/core/pubnub_alloc_std.c @@ -5,6 +5,7 @@ #include "pubnub_log.h" #include "pbpal.h" +#include "pubnub_ntf_dynamic.h" #include #include @@ -154,7 +155,15 @@ int pubnub_free(pubnub_t* pb) pubnub_disable_auto_heartbeat(pb); pbauto_heartbeat_free_channelInfo(pb); pb->state = PBS_NULL; -#if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == pb->api_policy) { + pbntf_requeue_for_processing(pb); + pubnub_mutex_unlock(pb->monitor); + } else { + pubnub_mutex_unlock(pb->monitor); + pballoc_free_at_last(pb); + } +#elif defined(PUBNUB_CALLBACK_API) pbntf_requeue_for_processing(pb); pubnub_mutex_unlock(pb->monitor); #else diff --git a/core/pubnub_internal_common.h b/core/pubnub_internal_common.h index 6402573b..78cc09b8 100644 --- a/core/pubnub_internal_common.h +++ b/core/pubnub_internal_common.h @@ -540,11 +540,10 @@ struct pubnub_ { /** Crypto module for encryption and decryption */ struct pubnub_crypto_provider_t *crypto_module; -// TODO: FIX FLAG MANAGEMENT -//#if PUBNUB_NTF_DYNAMIC +#ifdef PUBNUB_NTF_RUNTIME_SELECTION /** The PubNub API enforcement policy. */ enum pubnub_api_enforcement api_policy; -//#endif // PUBNUB_NTF_DYNAMIC +#endif }; diff --git a/core/pubnub_netcore.c b/core/pubnub_netcore.c index 227faabb..4f1e6584 100644 --- a/core/pubnub_netcore.c +++ b/core/pubnub_netcore.c @@ -2,6 +2,7 @@ #include "pubnub_internal.h" #include "lib/pb_strncasecmp.h" +#include "pubnub_ntf_dynamic.h" #include "core/pubnub_assert.h" #include "core/pubnub_log.h" @@ -1425,6 +1426,9 @@ void pbnc_stop(struct pubnub_* pbp, enum pubnub_res outcome_to_report) case PBS_WAIT_DNS_RCV: case PBS_WAIT_CONNECT: #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == pbp->api_policy) { +#endif if (PNR_TIMEOUT == outcome_to_report) { if ((pbp->state != PBS_WAIT_CONNECT) && #if PUBNUB_CHANGE_DNS_SERVERS @@ -1446,6 +1450,9 @@ void pbnc_stop(struct pubnub_* pbp, enum pubnub_res outcome_to_report) pbp->state = PBS_WAIT_CANCEL; } pbntf_requeue_for_processing(pbp); +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + } /* if api_policy */ +#endif #else pbp->state = PBS_WAIT_CANCEL; pbnc_fsm(pbp); @@ -1472,7 +1479,11 @@ void pbnc_stop(struct pubnub_* pbp, enum pubnub_res outcome_to_report) /*FALLTHRU*/ default: pbp->state = PBS_WAIT_CANCEL; -#if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == pbp->api_policy) { + pbntf_requeue_for_processing(pbp); + } +#elif defined(PUBNUB_CALLBACK_API) pbntf_requeue_for_processing(pbp); #else pbnc_fsm(pbp); diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_dynamic.c index 412f9ca3..9e2a2130 100644 --- a/core/pubnub_ntf_dynamic.c +++ b/core/pubnub_ntf_dynamic.c @@ -129,4 +129,14 @@ void pbntf_update_socket(pubnub_t* pb) } } +enum pubnub_res pubnub_last_result(pubnub_t* pb) { + switch (pb->api_policy) { + case PNA_SYNC: + return pubnub_last_result_sync(pb); + case PNA_CALLBACK: + return pubnub_last_result_callback(pb); + } +} + + #endif /* PUBNUB_NTF_RUNTIME_SELECTION */ diff --git a/core/pubnub_ntf_sync.c b/core/pubnub_ntf_sync.c index a4af3054..f5e3ee8a 100644 --- a/core/pubnub_ntf_sync.c +++ b/core/pubnub_ntf_sync.c @@ -157,7 +157,7 @@ MAYBE_INLINE enum pubnub_res pubnub_last_result_sync(pubnub_t* pb) } -#if 0 +#ifndef PUBNUB_NTF_RUNTIME_SELECTION int pbntf_init(pubnub_t* pb) { diff --git a/core/pubnub_proxy.c b/core/pubnub_proxy.c index 1d7c4447..d0c6d865 100644 --- a/core/pubnub_proxy.c +++ b/core/pubnub_proxy.c @@ -1,5 +1,6 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "pubnub_ntf_dynamic.h" #if PUBNUB_PROXY_API #include "pubnub_internal.h" @@ -48,6 +49,9 @@ int pubnub_set_proxy_manual(pubnub_t* p, p->proxy_type = protocol; p->proxy_port = port; #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == p->api_policy) { +#endif /* If we haven't got numerical address for proxy we'll have to do DNS resolution(from proxy host name) later on, but in order to do that we have to have all proxy addresses(on the given context) set to zeros. @@ -63,6 +67,9 @@ int pubnub_set_proxy_manual(pubnub_t* p, #if PUBNUB_USE_MULTIPLE_ADDRESSES pbpal_multiple_addresses_reset_counters(&p->spare_addresses); #endif +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + } /* if (PNA_CALLBACK == p->api_policy) */ +#endif #endif /* defined(PUBNUB_CALLBACK_API) */ memcpy(p->proxy_hostname, ip_address_or_url, ip_or_url_len + 1); pubnub_mutex_unlock(p->monitor); @@ -77,6 +84,9 @@ void pubnub_set_proxy_none(pubnub_t* p) pubnub_mutex_lock(p->monitor); #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == p->api_policy) { +#endif memset(&(p->proxy_ipv4_address), 0, sizeof p->proxy_ipv4_address); #if PUBNUB_USE_IPV6 memset(&(p->proxy_ipv6_address), 0, sizeof p->proxy_ipv6_address); @@ -84,6 +94,9 @@ void pubnub_set_proxy_none(pubnub_t* p) #if PUBNUB_USE_MULTIPLE_ADDRESSES pbpal_multiple_addresses_reset_counters(&p->spare_addresses); #endif +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + } /* if (PNA_CALLBACK == p->api_policy) */ +#endif #endif /* defined(PUBNUB_CALLBACK_API) */ p->proxy_type = pbproxyNONE; p->proxy_port = 0; diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index a8953a97..bc8ab7df 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -9,6 +9,7 @@ #include "core/pubnub_timers.h" #include "core/pbpal.h" +#include "pubnub_ntf_dynamic.h" #include #include @@ -24,13 +25,27 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->transaction_timeout_ms = PUBNUB_DEFAULT_TRANSACTION_TIMER; p->wait_connect_timeout_ms = PUBNUB_DEFAULT_WAIT_CONNECT_TIMER; #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (p->api_policy == PNA_CALLBACK) { + p->previous = p->next = NULL; + } +#else p->previous = p->next = NULL; -#endif +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ +#endif /* defined(PUBNUB_CALLBACK_API) */ } #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (p->api_policy == PNA_CALLBACK) { + p->cb = NULL; + p->user_data = NULL; + p->flags.sent_queries = 0; + } +#else p->cb = NULL; p->user_data = NULL; p->flags.sent_queries = 0; +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ #endif /* defined(PUBNUB_CALLBACK_API) */ if (PUBNUB_ORIGIN_SETTABLE) { p->origin = PUBNUB_ORIGIN; @@ -38,7 +53,18 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib } #if PUBNUB_BLOCKING_IO_SETTABLE #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + switch (p->api_policy) { + case PNA_CALLBACK: + p->options.use_blocking_io = false; + break; + case PNA_SYNC: + p->options.use_blocking_io = true; + break; + } +#else p->options.use_blocking_io = false; +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ #else p->options.use_blocking_io = true; #endif @@ -67,10 +93,19 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->proxy_type = pbproxyNONE; p->proxy_hostname[0] = '\0'; #if defined(PUBNUB_CALLBACK_API) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) + if (PNA_CALLBACK == p->api_policy) { + memset(&(p->proxy_ipv4_address), 0, sizeof p->proxy_ipv4_address); +#if PUBNUB_USE_IPV6 + memset(&(p->proxy_ipv6_address), 0, sizeof p->proxy_ipv6_address); +#endif + } +#else memset(&(p->proxy_ipv4_address), 0, sizeof p->proxy_ipv4_address); #if PUBNUB_USE_IPV6 memset(&(p->proxy_ipv6_address), 0, sizeof p->proxy_ipv6_address); #endif +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ #endif /* defined(PUBNUB_CALLBACK_API) */ p->proxy_tunnel_established = false; p->proxy_port = 80; diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 3eb6e605..1903d53f 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -75,24 +75,24 @@ int main() /** Sync context */ - enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); - if (PNR_OK != publish_result && PNR_STARTED != publish_result) { - printf("Failed to publish message from sync context!\n"); - - sync_sample_free(pbp_sync); - pubnub_subscription_free(&subscription); - callback_sample_free(pbp_callback); - return -1; - } - - if (PNR_OK != pubnub_await(pbp_sync)) { - printf("Failed to await message from sync context!\n"); - - sync_sample_free(pbp_sync); - pubnub_subscription_free(&subscription); - callback_sample_free(pbp_callback); - return -1; - } +// enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); +// if (PNR_OK != publish_result && PNR_STARTED != publish_result) { +// printf("Failed to publish message from sync context!\n"); +// +// sync_sample_free(pbp_sync); +// pubnub_subscription_free(&subscription); +// callback_sample_free(pbp_callback); +// return -1; +// } + +// if (PNR_OK != pubnub_await(pbp_sync)) { +// printf("Failed to await message from sync context!\n"); +// +// sync_sample_free(pbp_sync); +// pubnub_subscription_free(&subscription); +// callback_sample_free(pbp_callback); +// return -1; +// } wait_seconds(5); diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index 206a8239..bcafbec0 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -317,7 +317,7 @@ MAYBE_INLINE void pbntf_update_socket_callback(pubnub_t* pb) pthread_mutex_unlock(&m_watcher.mutw); } -#if 0 +#ifndef PUBNUB_NTF_RUNTIME_SELECTION int pbntf_watch_in_events(pubnub_t* pbp) { @@ -378,5 +378,6 @@ void pbntf_update_socket(pubnub_t* pb) pbntf_update_socket_callback(pb); } -#endif +#endif // !PUBNUB_NTF_RUNTIME_SELECTION + From 296fa922cdef614057948fb74788a5ca2521b734 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Wed, 22 Jan 2025 19:08:42 +0100 Subject: [PATCH 05/24] it (almost) works!!!!! --- core/pubnub_ntf_dynamic.c | 13 ++++++ core/pubnub_pubsubapi.c | 4 +- core/samples/pubnub_api_enforcement_sample.c | 49 +++++++++++--------- lib/sockets/pbpal_sockets.c | 28 +++++++++++ posix/pubnub_ntf_callback_posix.c | 6 +-- 5 files changed, 72 insertions(+), 28 deletions(-) diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_dynamic.c index 9e2a2130..0ae1ea88 100644 --- a/core/pubnub_ntf_dynamic.c +++ b/core/pubnub_ntf_dynamic.c @@ -1,3 +1,4 @@ +#include "pubnub_log.h" #if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_assert.h" @@ -8,12 +9,14 @@ #include "pubnub_config.h" void pubnub_enforce_api(pubnub_t* p, enum pubnub_api_enforcement policy) { + PUBNUB_LOG_INFO("pubnub_enforce_api(pb=%p, policy=%d)\n", p, policy); p->api_policy = policy; } int pbntf_watch_in_events(pubnub_t* pbp) { + PUBNUB_LOG_TRACE("pbntf_watch_in_events(pb=%p, policy=%d)\n", pbp, pbp->api_policy); switch (pbp->api_policy) { case PNA_SYNC: return pbntf_watch_in_events_sync(pbp); @@ -25,6 +28,7 @@ int pbntf_watch_in_events(pubnub_t* pbp) int pbntf_watch_out_events(pubnub_t* pbp) { + PUBNUB_LOG_TRACE("pbntf_watch_out_events(pb=%p, policy=%d)\n", pbp, pbp->api_policy); switch (pbp->api_policy) { case PNA_SYNC: return pbntf_watch_out_events_sync(pbp); @@ -36,6 +40,7 @@ int pbntf_watch_out_events(pubnub_t* pbp) int pbntf_init(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_init(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_init_sync(); @@ -47,6 +52,7 @@ int pbntf_init(pubnub_t* pb) int pbntf_enqueue_for_processing(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_enqueue_for_processing(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_enqueue_for_processing_sync(pb); @@ -58,6 +64,7 @@ int pbntf_enqueue_for_processing(pubnub_t* pb) int pbntf_requeue_for_processing(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_requeue_for_processing(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_requeue_for_processing_sync(pb); @@ -69,6 +76,7 @@ int pbntf_requeue_for_processing(pubnub_t* pb) int pbntf_got_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_got_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_got_socket_sync(pb); @@ -80,6 +88,7 @@ int pbntf_got_socket(pubnub_t* pb) void pbntf_lost_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_lost_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_lost_socket_sync(pb); @@ -93,6 +102,7 @@ void pbntf_lost_socket(pubnub_t* pb) void pbntf_start_wait_connect_timer(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_start_wait_connect_timer(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_start_wait_connect_timer_sync(pb); @@ -106,6 +116,7 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) void pbntf_start_transaction_timer(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_start_transaction_timer(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_start_transaction_timer_sync(pb); @@ -119,6 +130,7 @@ void pbntf_start_transaction_timer(pubnub_t* pb) void pbntf_update_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_update_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_update_socket_sync(pb); @@ -130,6 +142,7 @@ void pbntf_update_socket(pubnub_t* pb) } enum pubnub_res pubnub_last_result(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pubnub_last_result(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pubnub_last_result_sync(pb); diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index bc8ab7df..ca99a38f 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -26,7 +26,7 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->wait_connect_timeout_ms = PUBNUB_DEFAULT_WAIT_CONNECT_TIMER; #if defined(PUBNUB_CALLBACK_API) #if defined(PUBNUB_NTF_RUNTIME_SELECTION) - if (p->api_policy == PNA_CALLBACK) { + if (PNA_CALLBACK == p->api_policy) { p->previous = p->next = NULL; } #else @@ -36,7 +36,7 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib } #if defined(PUBNUB_CALLBACK_API) #if defined(PUBNUB_NTF_RUNTIME_SELECTION) - if (p->api_policy == PNA_CALLBACK) { + if (PNA_CALLBACK == p->api_policy) { p->cb = NULL; p->user_data = NULL; p->flags.sent_queries = 0; diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 1903d53f..28bca278 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -26,14 +26,14 @@ static void subscribe_message_listener( int main() { - time_t t0; - enum pubnub_res res; char const* user_id = "my_user_id"; char const* channel_id = "my_channel"; pubnub_t* pbp_sync = pubnub_alloc(); pubnub_t* pbp_callback = pubnub_alloc(); + printf("PubNub API enforcement demo\n"); + if (NULL == pbp_sync || NULL == pbp_callback) { printf("Failed to allocate Pubnub context!\n"); return -1; @@ -45,8 +45,8 @@ int main() char* my_env_publish_key = getenv("PUBNUB_PUBLISH_KEY"); char* my_env_subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY"); - if (NULL == my_env_publish_key) { my_env_publish_key = "demo"; } - if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "demo"; } + if (NULL == my_env_publish_key) { my_env_publish_key = "pub-c-3c19dc89-6dd9-4ecf-be7e-01ea81d284d2"; } + if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "sub-c-e9c43746-6c7f-44da-b292-8c50c0e4c39e"; } pubnub_init(pbp_sync, my_env_publish_key, my_env_subscribe_key); pubnub_init(pbp_callback, my_env_publish_key, my_env_subscribe_key); @@ -75,24 +75,29 @@ int main() /** Sync context */ -// enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); -// if (PNR_OK != publish_result && PNR_STARTED != publish_result) { -// printf("Failed to publish message from sync context!\n"); -// -// sync_sample_free(pbp_sync); -// pubnub_subscription_free(&subscription); -// callback_sample_free(pbp_callback); -// return -1; -// } - -// if (PNR_OK != pubnub_await(pbp_sync)) { -// printf("Failed to await message from sync context!\n"); -// -// sync_sample_free(pbp_sync); -// pubnub_subscription_free(&subscription); -// callback_sample_free(pbp_callback); -// return -1; -// } + /** Wait for the subscription to be established */ + wait_seconds(2); + + printf("Publishing message from sync context...\n"); + enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); + if (PNR_OK != publish_result && PNR_STARTED != publish_result) { + printf("Failed to publish message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + + if (PNR_OK != pubnub_await(pbp_sync)) { + printf("Failed to await message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + printf("Message published from sync context!\n"); wait_seconds(5); diff --git a/lib/sockets/pbpal_sockets.c b/lib/sockets/pbpal_sockets.c index 8b592b23..8d7fcfe4 100644 --- a/lib/sockets/pbpal_sockets.c +++ b/lib/sockets/pbpal_sockets.c @@ -20,6 +20,7 @@ static void buf_setup(pubnub_t* pb) pb->left = sizeof pb->core.http_buf / sizeof pb->core.http_buf[0]; } +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) static int pal_init(pubnub_t* pb) { static bool s_init = false; @@ -33,6 +34,33 @@ static int pal_init(pubnub_t* pb) } return 0; } +#else +static int pal_init(pubnub_t* pb) +{ + bool* s_init = NULL; + static bool s_init_sync = false; + static bool s_init_callback = false; + + switch(pb->api_policy) { + case PNA_SYNC: + s_init = &s_init_sync; + break; + case PNA_CALLBACK: + s_init = &s_init_callback; + break; + } + + if (!*s_init) { + if (0 != socket_platform_init()) { + return -1; + } + + pbntf_init(pb); + *s_init = true; + } + return 0; +} +#endif void pbpal_init(pubnub_t* pb) diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index bcafbec0..0d0a8d2b 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -37,9 +37,7 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -// TODO: decide if it is worth to keep that here -// 1 - till the flag is fixed -#if 1 +#if PUBNUB_NTF_RUNTIME_SELECTION #define MAYBE_INLINE #else #if __STDC_VERSION__ >= 199901L @@ -47,7 +45,7 @@ static struct SocketWatcherData m_watcher; #else #define MAYBE_INLINE static #endif -#endif // 1 +#endif // PUBNUB_NTF_RUNTIME_SELECTION MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp) From a5f078ac632533982008001ef8da164718a1f74f Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 09:39:45 +0100 Subject: [PATCH 06/24] works --- core/samples/pubnub_api_enforcement_sample.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 28bca278..a56bcd66 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -1,5 +1,6 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ #include "core/pubnub_ntf_dynamic.h" +#include "pubnub_api_types.h" #include "pubnub_sync.h" #include "core/pubnub_coreapi_ex.h" @@ -122,7 +123,7 @@ static void sync_sample_free(pubnub_t* p) { if (PN_CANCEL_STARTED == pubnub_cancel(p)) { enum pubnub_res pnru = pubnub_await(p); - if (pnru != PNR_OK) { + if (pnru != PNR_CANCELLED) { printf("Awaiting cancel failed: %d('%s')\n", pnru, pubnub_res_2_string(pnru)); From d0c474cccedf75202be453106bcaee0567841d0e Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 09:43:11 +0100 Subject: [PATCH 07/24] less waiting time --- core/samples/pubnub_api_enforcement_sample.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index a56bcd66..77cb93d8 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -100,7 +100,7 @@ int main() } printf("Message published from sync context!\n"); - wait_seconds(5); + wait_seconds(2); /** Cleanup */ From 0767e3b591288cabf9cb6b0a43337360e3470b4f Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 12:14:16 +0100 Subject: [PATCH 08/24] windows callbacks --- posix/pubnub_ntf_callback_posix.c | 4 +- windows/pubnub_ntf_callback_windows.c | 97 ++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index 0d0a8d2b..43a7b63a 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -37,7 +37,7 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -#if PUBNUB_NTF_RUNTIME_SELECTION +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) #define MAYBE_INLINE #else #if __STDC_VERSION__ >= 199901L @@ -315,7 +315,7 @@ MAYBE_INLINE void pbntf_update_socket_callback(pubnub_t* pb) pthread_mutex_unlock(&m_watcher.mutw); } -#ifndef PUBNUB_NTF_RUNTIME_SELECTION +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) int pbntf_watch_in_events(pubnub_t* pbp) { diff --git a/windows/pubnub_ntf_callback_windows.c b/windows/pubnub_ntf_callback_windows.c index 828a636d..a4ba9195 100644 --- a/windows/pubnub_ntf_callback_windows.c +++ b/windows/pubnub_ntf_callback_windows.c @@ -41,13 +41,24 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -int pbntf_watch_in_events(pubnub_t* pbp) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) +#define MAYBE_INLINE +#else +#if __STDC_VERSION__ >= 199901L +#define MAYBE_INLINE static inline +#else +#define MAYBE_INLINE static +#endif +#endif // PUBNUB_NTF_RUNTIME_SELECTION + + +MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_in_events(m_watcher.poll, pbp); } -int pbntf_watch_out_events(pubnub_t* pbp) +MAYBE_INLINE int pbntf_watch_out_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_out_events(m_watcher.poll, pbp); } @@ -94,7 +105,7 @@ void socket_watcher_thread(void* arg) } -int pbntf_init(pubnub_t* pb) +MAYBE_INLINE int pbntf_init_callback(pubnub_t* pb) { PUBNUB_UNUSED(pb); InitializeCriticalSection(&m_watcher.stoplock); @@ -133,22 +144,22 @@ void pubnub_stop(void) EnterCriticalSection(&m_watcher.stoplock); m_watcher.stop_socket_watcher_thread = true; LeaveCriticalSection(&m_watcher.stoplock); -} +} -int pbntf_enqueue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_enqueue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_enqueue_for_processing(&m_watcher.queue, pb); } -int pbntf_requeue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_requeue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_requeue_for_processing(&m_watcher.queue, pb); } -int pbntf_got_socket(pubnub_t* pb) +MAYBE_INLINE int pbntf_got_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_save_socket(m_watcher.poll, pb); @@ -166,7 +177,7 @@ int pbntf_got_socket(pubnub_t* pb) } -void pbntf_lost_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_lost_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_remove_socket(m_watcher.poll, pb); @@ -180,7 +191,7 @@ void pbntf_lost_socket(pubnub_t* pb) } -void pbntf_start_wait_connect_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_wait_connect_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { EnterCriticalSection(&m_watcher.timerlock); @@ -193,7 +204,7 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) } -void pbntf_start_transaction_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_transaction_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { EnterCriticalSection(&m_watcher.timerlock); @@ -206,9 +217,73 @@ void pbntf_start_transaction_timer(pubnub_t* pb) } -void pbntf_update_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_update_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_update_socket(m_watcher.poll, pb); LeaveCriticalSection(&m_watcher.mutw); } + + +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) + +int pbntf_watch_in_events(pubnub_t* pbp) +{ + return pbntf_watch_in_events_callback(pbp); +} + + +int pbntf_watch_out_events(pubnub_t* pbp) +{ + return pbntf_watch_out_events_callback(pbp); +} + + +int pbntf_init(pubnub_t* pb) +{ + return pbntf_init_callback(pb); +} + + +int pbntf_got_socket(pubnub_t* pb) +{ + return pbntf_got_socket_callback(pb); +} + + +int pbntf_requeue_for_processing(pubnub_t* pb) +{ + return pbntf_requeue_for_processing_callback(pb); +} + + +int pbntf_enqueue_for_processing(pubnub_t* pb) +{ + return pbntf_enqueue_for_processing_callback(pb); +} + + +void pbntf_lost_socket(pubnub_t* pb) +{ + return pbntf_lost_socket_callback(pb); +} + + +void pbntf_start_wait_connect_timer(pubnub_t* pb) +{ + return pbntf_start_wait_connect_timer_callback(pb); +} + + +void pbntf_start_transaction_timer(pubnub_t* pb) +{ + return pbntf_start_transaction_timer_callback(pb); +} + + +void pbntf_update_socket(pubnub_t* pb) +{ + return pbntf_update_socket_callback(pb); +} + +#endif From dceb9fef156ad5d07f0d45b7cf45b2431297c570 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:22:38 +0100 Subject: [PATCH 09/24] openssl windows --- openssl/pubnub_ntf_callback_windows.c | 94 ++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/openssl/pubnub_ntf_callback_windows.c b/openssl/pubnub_ntf_callback_windows.c index 5c5fd03d..c595fcf2 100644 --- a/openssl/pubnub_ntf_callback_windows.c +++ b/openssl/pubnub_ntf_callback_windows.c @@ -41,13 +41,24 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -int pbntf_watch_in_events(pubnub_t* pbp) +#if defined(PUBNUB_NTF_RUNTIME_SELECTION) +#define MAYBE_INLINE +#else +#if __STDC_VERSION__ >= 199901L +#define MAYBE_INLINE static inline +#else +#define MAYBE_INLINE static +#endif +#endif // PUBNUB_NTF_RUNTIME_SELECTION + + +MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_in_events(m_watcher.poll, pbp); } -int pbntf_watch_out_events(pubnub_t* pbp) +MAYBE_INLINE int pbntf_watch_out_events_callback(pubnub_t* pbp) { return pbpal_ntf_watch_out_events(m_watcher.poll, pbp); } @@ -94,7 +105,7 @@ void socket_watcher_thread(void* arg) } -int pbntf_init(void) +MAYBE_INLINE int pbntf_init_callback(void) { InitializeCriticalSection(&m_watcher.stoplock); InitializeCriticalSection(&m_watcher.mutw); @@ -135,19 +146,19 @@ void pubnub_stop(void) } -int pbntf_enqueue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_enqueue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_enqueue_for_processing(&m_watcher.queue, pb); } -int pbntf_requeue_for_processing(pubnub_t* pb) +MAYBE_INLINE int pbntf_requeue_for_processing_callback(pubnub_t* pb) { return pbpal_ntf_callback_requeue_for_processing(&m_watcher.queue, pb); } -int pbntf_got_socket(pubnub_t* pb) +MAYBE_INLINE int pbntf_got_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_save_socket(m_watcher.poll, pb); @@ -165,7 +176,7 @@ int pbntf_got_socket(pubnub_t* pb) } -void pbntf_lost_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_lost_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_remove_socket(m_watcher.poll, pb); @@ -179,7 +190,7 @@ void pbntf_lost_socket(pubnub_t* pb) } -void pbntf_start_wait_connect_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_wait_connect_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { EnterCriticalSection(&m_watcher.timerlock); @@ -192,7 +203,7 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) } -void pbntf_start_transaction_timer(pubnub_t* pb) +MAYBE_INLINE void pbntf_start_transaction_timer_callback(pubnub_t* pb) { if (PUBNUB_TIMERS_API) { EnterCriticalSection(&m_watcher.timerlock); @@ -205,9 +216,72 @@ void pbntf_start_transaction_timer(pubnub_t* pb) } -void pbntf_update_socket(pubnub_t* pb) +MAYBE_INLINE void pbntf_update_socket_callback(pubnub_t* pb) { EnterCriticalSection(&m_watcher.mutw); pbpal_ntf_callback_update_socket(m_watcher.poll, pb); LeaveCriticalSection(&m_watcher.mutw); } + +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) + +int pbntf_watch_in_events(pubnub_t* pbp) +{ + return pbntf_watch_in_events_callback(pbp); +} + + +int pbntf_watch_out_events(pubnub_t* pbp) +{ + return pbntf_watch_out_events_callback(pbp); +} + + +int pbntf_init(pubnub_t* pb) +{ + return pbntf_init_callback(pb); +} + + +int pbntf_got_socket(pubnub_t* pb) +{ + return pbntf_got_socket_callback(pb); +} + + +int pbntf_requeue_for_processing(pubnub_t* pb) +{ + return pbntf_requeue_for_processing_callback(pb); +} + + +int pbntf_enqueue_for_processing(pubnub_t* pb) +{ + return pbntf_enqueue_for_processing_callback(pb); +} + + +void pbntf_lost_socket(pubnub_t* pb) +{ + return pbntf_lost_socket_callback(pb); +} + + +void pbntf_start_wait_connect_timer(pubnub_t* pb) +{ + return pbntf_start_wait_connect_timer_callback(pb); +} + + +void pbntf_start_transaction_timer(pubnub_t* pb) +{ + return pbntf_start_transaction_timer_callback(pb); +} + + +void pbntf_update_socket(pubnub_t* pb) +{ + return pbntf_update_socket_callback(pb); +} + +#endif From ca7e13ac26d873f2cfd5da3ad4b836d3720bb0e9 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:28:59 +0100 Subject: [PATCH 10/24] better file name --- CMakeLists.txt | 2 +- core/pbpal_ntf_callback_admin.c | 6 ++---- core/pubnub_alloc_std.c | 2 +- core/pubnub_internal_common.h | 2 +- core/pubnub_netcore.c | 2 +- core/{pubnub_ntf_dynamic.c => pubnub_ntf_enforcement.c} | 4 ++-- core/{pubnub_ntf_dynamic.h => pubnub_ntf_enforcement.h} | 0 core/pubnub_proxy.c | 2 +- core/pubnub_pubsubapi.c | 2 +- core/samples/pubnub_api_enforcement_sample.c | 2 +- lib/sockets/pbpal_resolv_and_connect_sockets.c | 2 +- lib/sockets/pbpal_sockets.c | 2 +- 12 files changed, 13 insertions(+), 15 deletions(-) rename core/{pubnub_ntf_dynamic.c => pubnub_ntf_enforcement.c} (99%) rename core/{pubnub_ntf_dynamic.h => pubnub_ntf_enforcement.h} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 85db5766..6c735530 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -345,7 +345,7 @@ if (${USE_SYNC_API} AND ${USE_CALLBACK_API}) set(INTF_SOURCEFILES ${INTF_SOURCEFILES} - ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_dynamic.c) + ${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_enforcement.c) elseif (${USE_SYNC_API}) message(STATUS "Using sync API") diff --git a/core/pbpal_ntf_callback_admin.c b/core/pbpal_ntf_callback_admin.c index 2af82638..5e9d450d 100644 --- a/core/pbpal_ntf_callback_admin.c +++ b/core/pbpal_ntf_callback_admin.c @@ -1,5 +1,4 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -#include "core/pubnub_ntf_dynamic.h" #include "pubnub_internal.h" #if PUBNUB_USE_RETRY_CONFIGURATION @@ -11,11 +10,10 @@ #include "pubnub_log.h" #include "pubnub_assert.h" +#include "core/pubnub_ntf_enforcement.h" -// TODO: decide if it is worth to keep that here -// 1 - till the flag is fixed -#if 1 +#if PUBNUB_NTF_RUNTIME_SELECTION #define MAYBE_INLINE #else #if __STDC_VERSION__ >= 199901L diff --git a/core/pubnub_alloc_std.c b/core/pubnub_alloc_std.c index 96f67763..30f913bf 100644 --- a/core/pubnub_alloc_std.c +++ b/core/pubnub_alloc_std.c @@ -5,7 +5,7 @@ #include "pubnub_log.h" #include "pbpal.h" -#include "pubnub_ntf_dynamic.h" +#include "pubnub_ntf_enforcement.h" #include #include diff --git a/core/pubnub_internal_common.h b/core/pubnub_internal_common.h index 78cc09b8..c0e26794 100644 --- a/core/pubnub_internal_common.h +++ b/core/pubnub_internal_common.h @@ -110,7 +110,7 @@ #endif #if defined PUBNUB_NTF_RUNTIME_SELECTION -#include "core/pubnub_ntf_dynamic.h" +#include "core/pubnub_ntf_enforcement.h" #endif #include "core/pubnub_crypto.h" diff --git a/core/pubnub_netcore.c b/core/pubnub_netcore.c index 4f1e6584..2f01cd45 100644 --- a/core/pubnub_netcore.c +++ b/core/pubnub_netcore.c @@ -2,12 +2,12 @@ #include "pubnub_internal.h" #include "lib/pb_strncasecmp.h" -#include "pubnub_ntf_dynamic.h" #include "core/pubnub_assert.h" #include "core/pubnub_log.h" #include "core/pubnub_ccore.h" #include "core/pubnub_ccore_pubsub.h" +#include "pubnub_ntf_enforcement.h" #include "core/pbpal.h" #include "core/pubnub_version.h" #include "core/pubnub_version_internal.h" diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_enforcement.c similarity index 99% rename from core/pubnub_ntf_dynamic.c rename to core/pubnub_ntf_enforcement.c index 0ae1ea88..983982b9 100644 --- a/core/pubnub_ntf_dynamic.c +++ b/core/pubnub_ntf_enforcement.c @@ -1,10 +1,10 @@ -#include "pubnub_log.h" #if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_assert.h" #include "core/pubnub_ntf_callback.h" +#include "pubnub_log.h" #include "pubnub_internal.h" -#include "core/pubnub_ntf_dynamic.h" +#include "core/pubnub_ntf_enforcement.h" #include "core/pubnub_internal_common.h" #include "pubnub_config.h" diff --git a/core/pubnub_ntf_dynamic.h b/core/pubnub_ntf_enforcement.h similarity index 100% rename from core/pubnub_ntf_dynamic.h rename to core/pubnub_ntf_enforcement.h diff --git a/core/pubnub_proxy.c b/core/pubnub_proxy.c index d0c6d865..ca0d2aae 100644 --- a/core/pubnub_proxy.c +++ b/core/pubnub_proxy.c @@ -1,12 +1,12 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -#include "pubnub_ntf_dynamic.h" #if PUBNUB_PROXY_API #include "pubnub_internal.h" #include "pubnub_proxy.h" #include "pubnub_assert.h" +#include "pubnub_ntf_enforcement.h" #include "pubnub_log.h" #if defined(PUBNUB_CALLBACK_API) #include "lib/pubnub_parse_ipv4_addr.h" diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index ca99a38f..609e1b32 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -9,7 +9,7 @@ #include "core/pubnub_timers.h" #include "core/pbpal.h" -#include "pubnub_ntf_dynamic.h" +#include "pubnub_ntf_enforcement.h" #include #include diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 77cb93d8..5907b922 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -1,5 +1,5 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -#include "core/pubnub_ntf_dynamic.h" +#include "core/pubnub_ntf_enforcement.h" #include "pubnub_api_types.h" #include "pubnub_sync.h" diff --git a/lib/sockets/pbpal_resolv_and_connect_sockets.c b/lib/sockets/pbpal_resolv_and_connect_sockets.c index e73d1626..b0eca1e1 100644 --- a/lib/sockets/pbpal_resolv_and_connect_sockets.c +++ b/lib/sockets/pbpal_resolv_and_connect_sockets.c @@ -7,7 +7,7 @@ #include "lib/sockets/pbpal_adns_sockets.h" #include "lib/sockets/pbpal_socket_blocking_io.h" #ifdef PUBNUB_NTF_RUNTIME_SELECTION -#include "pubnub_ntf_dynamic.h" +#include "pubnub_ntf_enforcement.h" #endif #include diff --git a/lib/sockets/pbpal_sockets.c b/lib/sockets/pbpal_sockets.c index 8d7fcfe4..a49ed269 100644 --- a/lib/sockets/pbpal_sockets.c +++ b/lib/sockets/pbpal_sockets.c @@ -1,5 +1,4 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -#include "core/pubnub_ntf_dynamic.h" #include "pubnub_internal.h" #include "core/pbpal.h" @@ -7,6 +6,7 @@ #include "core/pubnub_netcore.h" #include "core/pubnub_assert.h" #include "core/pubnub_log.h" +#include "core/pubnub_ntf_enforcement.h" #include #include From d43401df47427233c4907452fefe1868bd2af15f Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:33:06 +0100 Subject: [PATCH 11/24] stdbool --- core/pubnub_netcore.h | 2 ++ core/pubnub_ntf_enforcement.h | 1 + 2 files changed, 3 insertions(+) diff --git a/core/pubnub_netcore.h b/core/pubnub_netcore.h index 4bb8cb62..d3cde28f 100644 --- a/core/pubnub_netcore.h +++ b/core/pubnub_netcore.h @@ -2,6 +2,8 @@ #if !defined INC_PUBNUB_NETCORE #define INC_PUBNUB_NETCORE +#include "stdbool.h" + /** @file pubnub_netcore.h This is the interface of the Pubnub C-core network module. diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index eee1d353..03dec7a3 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -4,6 +4,7 @@ #if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_api_types.h" +#include "core/pubnub_netcore.h" /** The PubNub API enforcement policy. * From 04217576be380b8abc4e52cdda62aa088cb07d40 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:36:59 +0100 Subject: [PATCH 12/24] proper defaults in CMAKE --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c735530..d0788744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,8 +60,8 @@ if (WITH_CPP) set(DEFAULT_USE_CALLBACK_API ON) set(DEFAULT_USE_SYNC_API OFF) else () - set(DEFAULT_USE_CALLBACK_API ON) - set(DEFAULT_USE_SYNC_API OFF) + set(DEFAULT_USE_CALLBACK_API OFF) + set(DEFAULT_USE_SYNC_API ON) endif () log_option(USE_CPP11 "Use C++11 [WITH_CPP=ON needed]" OFF) From 0c6798fc78a1864e0a354a30e862e78689f567b5 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:38:07 +0100 Subject: [PATCH 13/24] todos --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0788744..99a0356c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -338,7 +338,6 @@ endif () if (${USE_SYNC_API} AND ${USE_CALLBACK_API}) message(STATUS "Using runtime API selection") - # TODO: something is wrong with flags :c set(CMAKE_C_FLAGS "\ ${CMAKE_C_FLAGS} \ -D PUBNUB_NTF_RUNTIME_SELECTION") From 7e2df3bc9b08a402d6612c56fff22263747bf2ab Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:42:44 +0100 Subject: [PATCH 14/24] fixes --- core/pubnub_alloc_static.c | 2 +- core/pubnub_ntf_enforcement.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/pubnub_alloc_static.c b/core/pubnub_alloc_static.c index f6922d71..008b81c4 100644 --- a/core/pubnub_alloc_static.c +++ b/core/pubnub_alloc_static.c @@ -5,7 +5,7 @@ #include "pubnub_log.h" #include "pbpal.h" -#include "pubnub_ntf_dynamic.h" +#include "pubnub_ntf_enforcement.h" static struct pubnub_ m_aCtx[PUBNUB_CTX_MAX]; diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index 03dec7a3..d0826865 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -1,5 +1,5 @@ -#ifndef PUBNUB_NTF_DYNAMIC_H -#define PUBNUB_NTF_DYNAMIC_H +#ifndef PUBNUB_NTF_ENFORCEMENT_H +#define PUBNUB_NTF_ENFORCEMENT_H #if defined PUBNUB_NTF_RUNTIME_SELECTION @@ -90,4 +90,4 @@ enum pubnub_res pubnub_last_result_sync(pubnub_t* pb); enum pubnub_res pubnub_last_result_callback(pubnub_t* pb); #endif // PUBNUB_NTF_RUNTIME_SELECTION -#endif // PUBNUB_NTF_DYNAMIC_H +#endif // PUBNUB_NTF_ENFORCEMENT_H From ae3f4098eaf9d7ac5e71865411d806605e1056d8 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:49:02 +0100 Subject: [PATCH 15/24] some docks --- core/pubnub_ntf_enforcement.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index d0826865..41864113 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -47,8 +47,9 @@ enum pubnub_api_enforcement { */ void pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); - -// TODO: maybe move it to pbcc file +/* This section declares the functions that are used when the api enforcement + * policy is set to sync. They are bridge between the sync and callback interfaces. + */ void pbntf_trans_outcome_sync(pubnub_t* pb, enum pubnub_state state); void pbntf_trans_outcome_callback(pubnub_t* pb, enum pubnub_state state); @@ -89,5 +90,7 @@ void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb); enum pubnub_res pubnub_last_result_sync(pubnub_t* pb); enum pubnub_res pubnub_last_result_callback(pubnub_t* pb); +/* End of the section */ + #endif // PUBNUB_NTF_RUNTIME_SELECTION #endif // PUBNUB_NTF_ENFORCEMENT_H From 0f50d013836729214a52c9b7f516b729d0ac519b Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 13:49:46 +0100 Subject: [PATCH 16/24] fix docs --- core/pubnub_ntf_enforcement.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index 41864113..62981549 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -48,7 +48,7 @@ enum pubnub_api_enforcement { void pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); /* This section declares the functions that are used when the api enforcement - * policy is set to sync. They are bridge between the sync and callback interfaces. + * policy is set. They are bridge between the sync and callback interfaces. */ void pbntf_trans_outcome_sync(pubnub_t* pb, enum pubnub_state state); From fac6fb6f3dfc58ec68768bc4d91b79b5aa8a6375 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 15:26:11 +0100 Subject: [PATCH 17/24] remove leaked keys --- core/samples/pubnub_api_enforcement_sample.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 5907b922..4c4812b2 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -46,8 +46,8 @@ int main() char* my_env_publish_key = getenv("PUBNUB_PUBLISH_KEY"); char* my_env_subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY"); - if (NULL == my_env_publish_key) { my_env_publish_key = "pub-c-3c19dc89-6dd9-4ecf-be7e-01ea81d284d2"; } - if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "sub-c-e9c43746-6c7f-44da-b292-8c50c0e4c39e"; } + if (NULL == my_env_publish_key) { my_env_publish_key = "demo"; } + if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "demo"; } pubnub_init(pbp_sync, my_env_publish_key, my_env_subscribe_key); pubnub_init(pbp_callback, my_env_publish_key, my_env_subscribe_key); From 6cd81195de7f37f6eb7f8e9adaece2c094c50a14 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 17:10:35 +0100 Subject: [PATCH 18/24] openssl init --- openssl/pbpal_openssl.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/openssl/pbpal_openssl.c b/openssl/pbpal_openssl.c index 7cafc0a6..aaa45ef9 100644 --- a/openssl/pbpal_openssl.c +++ b/openssl/pbpal_openssl.c @@ -92,8 +92,25 @@ static void buf_setup(pubnub_t* pb) static int pal_init(pubnub_t* pb) { +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) static bool s_init = false; if (!s_init) { +#else + bool* s_init = NULL; + static bool s_init_sync = false; + static bool s_init_callback = false; + + switch(pb->api_policy) { + case PNA_SYNC: + s_init = &s_init_sync; + break; + case PNA_CALLBACK: + s_init = &s_init_callback; + break; + } + + if (!*s_init) { +#endif #if !defined(__UWP__) && (OPENSSL_VERSION_MAJOR < 3) ERR_load_BIO_strings(); //Per OpenSSL 3.0 this is deprecated. Allowing this stmt for non-UWP as it exists. #endif @@ -116,7 +133,11 @@ static int pal_init(pubnub_t* pb) return -1; } pbntf_init(pb); +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) s_init = true; +#else + *s_init = true; +#endif } return 0; } From d08c2a8d8b01701a0cbaf09e07d62bc7559756e2 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 17:15:02 +0100 Subject: [PATCH 19/24] mbedtls --- mbedtls/pbpal_mbedtls.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mbedtls/pbpal_mbedtls.c b/mbedtls/pbpal_mbedtls.c index 9f1ede3a..acf73026 100644 --- a/mbedtls/pbpal_mbedtls.c +++ b/mbedtls/pbpal_mbedtls.c @@ -409,6 +409,8 @@ void pbpal_free(pubnub_t* pb) pb->sock_state = STATE_NONE; } + +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) static void pbntf_setup(pubnub_t* pb) { static bool init_done = false; @@ -422,6 +424,33 @@ static void pbntf_setup(pubnub_t* pb) pbntf_init(pb); init_done = true; } +#else +static void pbntf_setup(pubnub_t* pb) +{ + bool* init_done = NULL; + static bool init_sync_done = false; + static bool init_callback_done = false; + + switch(pb->api_policy) { + case PNA_SYNC: + s_init = &s_init_sync; + break; + case PNA_CALLBACK: + s_init = &s_init_callback; + break; + } + + PUBNUB_LOG_TRACE("pbntf_setup()\n"); + + if (*init_done) { + PUBNUB_LOG_TRACE("pbntf_setup() already done\n"); + return; + } + + pbntf_init(pb); + *init_done = true; +} +#endif static void options_setup(pubnub_t* pb) From 4730575cccf5097a6ea6d2b24b5844a79889ad39 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Thu, 23 Jan 2025 17:17:17 +0100 Subject: [PATCH 20/24] freertos --- freertos/pubnub_ntf_callback_freertos.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/freertos/pubnub_ntf_callback_freertos.c b/freertos/pubnub_ntf_callback_freertos.c index 5d7d9e7b..78301fc3 100644 --- a/freertos/pubnub_ntf_callback_freertos.c +++ b/freertos/pubnub_ntf_callback_freertos.c @@ -187,8 +187,10 @@ void socket_watcher_task(void *arg) #define PUBNUB_TASK_PRIORITY 2 -int pbntf_init(void) +int pbntf_init(pubnub_t* pb) { + PUBNUB_UNUSED(pb); + m_watcher.mutw = xSemaphoreCreateRecursiveMutex(); if (NULL == m_watcher.mutw) { return -1; @@ -425,4 +427,4 @@ pubnub_callback_t pubnub_get_callback(pubnub_t *pb) pubnub_mutex_unlock(pb->monitor); return result; -} \ No newline at end of file +} From 270eae99c98de451f904f3c0a99abc99391c03e8 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Tue, 28 Jan 2025 12:27:49 +0100 Subject: [PATCH 21/24] fail on await --- core/pubnub_ntf_sync.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/pubnub_ntf_sync.c b/core/pubnub_ntf_sync.c index f5e3ee8a..5737979f 100644 --- a/core/pubnub_ntf_sync.c +++ b/core/pubnub_ntf_sync.c @@ -1,5 +1,7 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ #include "pubnub_ntf_sync.h" +#include "pubnub_api_types.h" +#include "pubnub_ntf_enforcement.h" #if PUBNUB_USE_RETRY_CONFIGURATION #include "core/pubnub_retry_configuration_internal.h" @@ -245,6 +247,13 @@ enum pubnub_res pubnub_await(pubnub_t* pb) PUBNUB_ASSERT(pb_valid_ctx_ptr(pb)); pubnub_mutex_lock(pb->monitor); + +#ifdef PUBNUB_NTF_RUNTIME_SELECTION + if (PNA_SYNC != pb->api_policy) { + return PNR_INTERNAL_ERROR; + } +#endif /* PUBNUB_NTF_RUNTIME_SELECTION */ + t0 = pbms_start(); while (!pbnc_can_start_transaction(pb)) { pbms_t delta; From 9938039320943516d728689a5f727d8e66d60c9d Mon Sep 17 00:00:00 2001 From: Xavrax Date: Tue, 28 Jan 2025 15:29:22 +0100 Subject: [PATCH 22/24] this is sus --- core/pubnub_ntf_enforcement.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index 62981549..e470f1db 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -3,7 +3,11 @@ #if defined PUBNUB_NTF_RUNTIME_SELECTION -#include "core/pubnub_api_types.h" +struct pubnub_; +typedef struct pubnub_ pubnub_t; + +enum pubnub_res; + #include "core/pubnub_netcore.h" /** The PubNub API enforcement policy. From 6f3c665682d75acca1d9f1612d028bf0e7294205 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Tue, 28 Jan 2025 15:33:35 +0100 Subject: [PATCH 23/24] okay it have to work --- core/pubnub_ntf_enforcement.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/pubnub_ntf_enforcement.h b/core/pubnub_ntf_enforcement.h index e470f1db..03720952 100644 --- a/core/pubnub_ntf_enforcement.h +++ b/core/pubnub_ntf_enforcement.h @@ -9,6 +9,7 @@ typedef struct pubnub_ pubnub_t; enum pubnub_res; #include "core/pubnub_netcore.h" +#include "lib/pb_extern.h" /** The PubNub API enforcement policy. * @@ -49,7 +50,7 @@ enum pubnub_api_enforcement { * @see pubnub_api_enforcement * @see pubnub_res */ -void pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); +PUBNUB_EXTERN void pubnub_enforce_api(pubnub_t* pb, enum pubnub_api_enforcement policy); /* This section declares the functions that are used when the api enforcement * policy is set. They are bridge between the sync and callback interfaces. From 0795fae0aca04d1ffb1d2fe89ab85130197d8957 Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Wed, 12 Feb 2025 13:50:08 +0100 Subject: [PATCH 24/24] Fix callback api not working on Windows when built by cmake. --- CMakeLists.txt | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 99a0356c..05b8736d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -315,15 +315,34 @@ if (${USE_CALLBACK_API}) if (UNIX) set(INTF_SOURCEFILES ${INTF_SOURCEFILES} - ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_dns_system_servers.c - ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_ntf_callback_posix.c - ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_get_native_socket.c) + ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_dns_system_servers.c) + if (${OPENSSL}) + set(INTF_SOURCEFILES + ${INTF_SOURCEFILES} + ${CMAKE_CURRENT_LIST_DIR}/openssl/pubnub_get_native_socket.c + ${CMAKE_CURRENT_LIST_DIR}/openssl/pubnub_ntf_callback_posix.c) + else () + set(INTF_SOURCEFILES + ${INTF_SOURCEFILES} + ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_get_native_socket.c + ${CMAKE_CURRENT_LIST_DIR}/posix/pubnub_ntf_callback_posix.c) + endif () + elseif (WIN32 OR WIN64 OR MSVC) set(INTF_SOURCEFILES ${INTF_SOURCEFILES} - ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_ntf_callback_windows.c - ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_dns_system_servers.c - ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_get_native_socket.c) + ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_dns_system_servers.c) + if (${OPENSSL}) + set(INTF_SOURCEFILES + ${INTF_SOURCEFILES} + ${CMAKE_CURRENT_LIST_DIR}/openssl/pubnub_get_native_socket.c + ${CMAKE_CURRENT_LIST_DIR}/openssl/pubnub_ntf_callback_windows.c) + else () + set(INTF_SOURCEFILES + ${INTF_SOURCEFILES} + ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_get_native_socket.c + ${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_ntf_callback_windows.c) + endif () endif () endif ()