瀏覽代碼

Updated rtc::Cleanup() to return a future resolved on completion

Paul-Louis Ageneau 3 年之前
父節點
當前提交
8302d8ef17
共有 6 個文件被更改,包括 53 次插入10 次删除
  1. 2 1
      include/rtc/global.hpp
  2. 1 1
      src/global.cpp
  3. 27 6
      src/impl/init.cpp
  4. 3 1
      src/impl/init.hpp
  5. 9 0
      test/capi_websocketserver.cpp
  6. 11 1
      test/main.cpp

+ 2 - 1
include/rtc/global.hpp

@@ -23,6 +23,7 @@
 
 #include <chrono>
 #include <iostream>
+#include <future>
 
 namespace rtc {
 
@@ -47,7 +48,7 @@ RTC_CPP_EXPORT void InitLogger(plog::Severity severity, plog::IAppender *appende
 #endif
 
 RTC_CPP_EXPORT void Preload();
-RTC_CPP_EXPORT void Cleanup();
+RTC_CPP_EXPORT std::shared_future<void> Cleanup();
 
 struct SctpSettings {
 	// For the following settings, not set means optimized default

+ 1 - 1
src/global.cpp

@@ -103,7 +103,7 @@ void InitLogger(plog::Severity severity, plog::IAppender *appender) {
 }
 
 void Preload() { Init::Instance().preload(); }
-void Cleanup() { Init::Instance().cleanup(); }
+std::shared_future<void> Cleanup() { return Init::Instance().cleanup(); }
 
 void SetSctpSettings(SctpSettings s) { Init::Instance().setSctpSettings(std::move(s)); }
 

+ 27 - 6
src/impl/init.cpp

@@ -40,12 +40,28 @@
 namespace rtc {
 
 struct Init::TokenPayload {
-	TokenPayload() { Init::Instance().doInit(); }
+	TokenPayload(std::shared_future<void> *cleanupFuture) {
+		Init::Instance().doInit();
+		if(cleanupFuture)
+			*cleanupFuture = cleanupPromise.get_future().share();
+	}
 
 	~TokenPayload() {
-		std::thread t([]() { Init::Instance().doCleanup(); });
+		std::thread t(
+		    [](std::promise<void> promise) {
+			    try {
+				    Init::Instance().doCleanup();
+				    promise.set_value();
+			    } catch (const std::exception &e) {
+				    PLOG_WARNING << e.what();
+				    promise.set_exception(std::make_exception_ptr(e));
+			    }
+		    },
+		    std::move(cleanupPromise));
 		t.detach();
 	}
+
+	std::promise<void> cleanupPromise;
 };
 
 Init &Init::Instance() {
@@ -53,7 +69,11 @@ Init &Init::Instance() {
 	return *instance;
 }
 
-Init::Init() {}
+Init::Init() {
+	std::promise<void> p;
+    p.set_value();
+    mCleanupFuture = p.get_future(); // make it ready
+}
 
 Init::~Init() {}
 
@@ -62,7 +82,7 @@ init_token Init::token() {
 	if (auto locked = mWeak.lock())
 		return locked;
 
-	mGlobal = std::make_shared<TokenPayload>();
+	mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
 	mWeak = *mGlobal;
 	return *mGlobal;
 }
@@ -70,14 +90,15 @@ init_token Init::token() {
 void Init::preload() {
 	std::lock_guard lock(mMutex);
 	if (!mGlobal) {
-		mGlobal = std::make_shared<TokenPayload>();
+		mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
 		mWeak = *mGlobal;
 	}
 }
 
-void Init::cleanup() {
+std::shared_future<void> Init::cleanup() {
 	std::lock_guard lock(mMutex);
 	mGlobal.reset();
+	return mCleanupFuture;
 }
 
 void Init::setSctpSettings(SctpSettings s) {

+ 3 - 1
src/impl/init.hpp

@@ -24,6 +24,7 @@
 
 #include <chrono>
 #include <mutex>
+#include <future>
 
 namespace rtc {
 
@@ -40,7 +41,7 @@ public:
 
 	init_token token();
 	void preload();
-	void cleanup();
+	std::shared_future<void> cleanup();
 	void setSctpSettings(SctpSettings s);
 
 private:
@@ -55,6 +56,7 @@ private:
 	bool mInitialized = false;
 	SctpSettings mCurrentSctpSettings = {};
 	std::mutex mMutex;
+	std::shared_future<void> mCleanupFuture;
 
 	struct TokenPayload;
 };

+ 9 - 0
test/capi_websocketserver.cpp

@@ -35,6 +35,7 @@ static const char *MESSAGE = "Hello, this is a C API WebSocket test!";
 
 static bool success = false;
 static bool failed = false;
+static int wsclient = -1;
 
 static void RTC_API openCallback(int ws, void *ptr) {
 	printf("WebSocket: Connection open\n");
@@ -86,6 +87,8 @@ static void RTC_API serverMessageCallback(int ws, const char *message, int size,
 }
 
 static void RTC_API serverClientCallback(int wsserver, int ws, void *ptr) {
+	wsclient = ws;
+
 	char address[256];
 	if (rtcGetWebSocketRemoteAddress(ws, address, 256) < 0) {
 		fprintf(stderr, "rtcGetWebSocketRemoteAddress failed\n");
@@ -144,6 +147,9 @@ int test_capi_websocketserver_main() {
 	if (failed)
 		goto error;
 
+	rtcDeleteWebSocket(wsclient);
+	sleep(1);
+
 	rtcDeleteWebSocket(ws);
 	sleep(1);
 
@@ -154,6 +160,9 @@ int test_capi_websocketserver_main() {
 	return 0;
 
 error:
+	if (wsclient >= 0)
+		rtcDeleteWebSocket(wsclient);
+
 	if (ws >= 0)
 		rtcDeleteWebSocket(ws);
 

+ 11 - 1
test/main.cpp

@@ -20,6 +20,8 @@
 #include <iostream>
 #include <thread>
 
+#include <rtc/rtc.hpp>
+
 using namespace std;
 using namespace chrono_literals;
 
@@ -132,6 +134,14 @@ int main(int argc, char **argv) {
 		return -1;
 	}
 */
-	std::this_thread::sleep_for(1s);
+	try {
+		cout << endl << "*** Running cleanup..." << endl;
+		if(rtc::Cleanup().wait_for(10s) == future_status::timeout)
+			throw std::runtime_error("Timeout");
+
+	} catch (const exception &e) {
+		cerr << "Cleanup failed: " << e.what() << endl;
+		return -1;
+	}
 	return 0;
 }