From 5346d31cc19c3d4c9570059ab783f37843ac01a1 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Wed, 14 Apr 2021 21:51:36 +0200 Subject: [PATCH] Introduced onOpen() callback on receiving side in tests --- test/capi_connectivity.cpp | 2 +- test/capi_track.cpp | 2 +- test/connectivity.cpp | 36 ++++++++++++++++++++---------------- test/turn_connectivity.cpp | 14 ++++++++++---- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/test/capi_connectivity.cpp b/test/capi_connectivity.cpp index c1fb849..e990074 100644 --- a/test/capi_connectivity.cpp +++ b/test/capi_connectivity.cpp @@ -137,11 +137,11 @@ static void RTC_API dataChannelCallback(int pc, int dc, void *ptr) { return; } + rtcSetOpenCallback(dc, openCallback); rtcSetClosedCallback(dc, closedCallback); rtcSetMessageCallback(dc, messageCallback); peer->dc = dc; - peer->connected = true; const char *message = peer == peer1 ? "Hello from 1" : "Hello from 2"; rtcSendMessage(peer->dc, message, -1); // negative size indicates a null-terminated string diff --git a/test/capi_track.cpp b/test/capi_track.cpp index df11839..9c0f64f 100644 --- a/test/capi_track.cpp +++ b/test/capi_track.cpp @@ -83,7 +83,7 @@ static void RTC_API closedCallback(int id, void *ptr) { static void RTC_API trackCallback(int pc, int tr, void *ptr) { Peer *peer = (Peer *)ptr; peer->tr = tr; - peer->connected = true; + rtcSetOpenCallback(tr, openCallback); rtcSetClosedCallback(tr, closedCallback); char buffer[1024]; diff --git a/test/connectivity.cpp b/test/connectivity.cpp index bd0f989..ff47d49 100644 --- a/test/connectivity.cpp +++ b/test/connectivity.cpp @@ -107,26 +107,29 @@ void test_connectivity() { return; } + dc->onOpen([wdc = make_weak_ptr(dc)]() { + if (auto dc = wdc.lock()) + dc->send("Hello from 2"); + }); + dc->onMessage([](variant message) { if (holds_alternative(message)) { cout << "Message 2: " << get(message) << endl; } }); - dc->send("Hello from 2"); - std::atomic_store(&dc2, dc); }); auto dc1 = pc1.createDataChannel("test"); - dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { - auto dc1 = wdc1.lock(); - if (!dc1) - return; - cout << "DataChannel 1: Open" << endl; - dc1->send("Hello from 1"); + dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { + if (auto dc1 = wdc1.lock()) { + cout << "DataChannel 1: Open" << endl; + dc1->send("Hello from 1"); + } }); + dc1->onMessage([](const variant &message) { if (holds_alternative(message)) { cout << "Message 1: " << get(message) << endl; @@ -177,25 +180,26 @@ void test_connectivity() { return; } + dc->onOpen([wdc = make_weak_ptr(dc)]() { + if (auto dc = wdc.lock()) + dc->send("Second hello from 2"); + }); + dc->onMessage([](variant message) { if (holds_alternative(message)) { cout << "Second Message 2: " << get(message) << endl; } }); - dc->send("Send hello from 2"); - std::atomic_store(&second2, dc); }); auto second1 = pc1.createDataChannel("second"); second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() { - auto second1 = wsecond1.lock(); - if (!second1) - return; - - cout << "Second DataChannel 1: Open" << endl; - second1->send("Second hello from 1"); + if (auto second1 = wsecond1.lock()) { + cout << "Second DataChannel 1: Open" << endl; + second1->send("Second hello from 1"); + } }); dc1->onMessage([](const variant &message) { if (holds_alternative(message)) { diff --git a/test/turn_connectivity.cpp b/test/turn_connectivity.cpp index 6ceb3cc..7c2fb9b 100644 --- a/test/turn_connectivity.cpp +++ b/test/turn_connectivity.cpp @@ -108,14 +108,17 @@ void test_turn_connectivity() { return; } + dc->onOpen([wdc = make_weak_ptr(dc)]() { + if (auto dc = wdc.lock()) + dc->send("Hello from 2"); + }); + dc->onMessage([](variant message) { if (holds_alternative(message)) { cout << "Message 2: " << get(message) << endl; } }); - dc->send("Hello from 2"); - std::atomic_store(&dc2, dc); }); @@ -175,14 +178,17 @@ void test_turn_connectivity() { return; } + dc->onOpen([wdc = make_weak_ptr(dc)]() { + if (auto dc = wdc.lock()) + dc->send("Second hello from 2"); + }); + dc->onMessage([](variant message) { if (holds_alternative(message)) { cout << "Second Message 2: " << get(message) << endl; } }); - dc->send("Send hello from 2"); - std::atomic_store(&second2, dc); });