From 04fa5ca0174f7aef1ccedc148b6cacf3cf87b14a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 27 Nov 2019 19:47:51 +0900 Subject: [PATCH] Add is_valid_context(), fixes/refactorings --- src/UDPConnection.cpp | 19 +++++++++++++------ src/UDPConnection.h | 8 +++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index d57cdac..b8ddc83 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1809,10 +1809,10 @@ UDPC_HContext UDPC_init_threaded_update_ms( return (UDPC_HContext) ctx; } -void UDPC_enable_threaded_update(UDPC_HContext ctx) { +int UDPC_enable_threaded_update(UDPC_HContext ctx) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c || c->flags.test(0) || c->thread.joinable()) { - return; + return 0; } c->flags.set(0); @@ -1821,12 +1821,13 @@ void UDPC_enable_threaded_update(UDPC_HContext ctx) { c->thread = std::thread(UDPC::threadedUpdate, c); UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_INFO, "Started threaded update"); + return 1; } -void UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) { +int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c || c->flags.test(0) || c->thread.joinable()) { - return; + return 0; } c->flags.set(0); @@ -1841,12 +1842,13 @@ void UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) { c->thread = std::thread(UDPC::threadedUpdate, c); UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_INFO, "Started threaded update"); + return 1; } -void UDPC_disable_threaded_update(UDPC_HContext ctx) { +int UDPC_disable_threaded_update(UDPC_HContext ctx) { UDPC::Context *c = UDPC::verifyContext(ctx); if(!c || !c->flags.test(0) || !c->thread.joinable()) { - return; + return 0; } c->threadRunning.store(false); @@ -1854,6 +1856,11 @@ void UDPC_disable_threaded_update(UDPC_HContext ctx) { c->flags.reset(0); UDPC_CHECK_LOG(c, UDPC_LoggingType::UDPC_INFO, "Stopped threaded update"); + return 1; +} + +int UDPC_is_valid_context(UDPC_HContext ctx) { + return UDPC::verifyContext(ctx) != nullptr ? 1 : 0; } void UDPC_destroy(UDPC_HContext ctx) { diff --git a/src/UDPConnection.h b/src/UDPConnection.h index eb95ef6..b96873e 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -135,9 +135,11 @@ UDPC_HContext UDPC_init_threaded_update_ms( int updateMS, int isUsingLibsodium); -void UDPC_enable_threaded_update(UDPC_HContext ctx); -void UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS); -void UDPC_disable_threaded_update(UDPC_HContext ctx); +int UDPC_enable_threaded_update(UDPC_HContext ctx); +int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS); +int UDPC_disable_threaded_update(UDPC_HContext ctx); + +int UDPC_is_valid_context(UDPC_HContext ctx); void UDPC_destroy(UDPC_HContext ctx);