connectivity.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. using namespace rtc;
  25. using namespace std;
  26. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  27. void test_connectivity() {
  28. InitLogger(LogLevel::Debug);
  29. Configuration config1;
  30. // STUN server example (not necessary to connect locally)
  31. // Please do not use outside of libdatachannel tests
  32. config1.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  33. // Custom MTU example
  34. config1.mtu = 1500;
  35. // Custom max message size
  36. config1.maxMessageSize = 1048576;
  37. PeerConnection pc1(config1);
  38. Configuration config2;
  39. // STUN server example (not necessary to connect locally)
  40. // Please do not use outside of libdatachannel tests
  41. config2.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  42. // Custom MTU example
  43. config2.mtu = 1500;
  44. // Custom max message size
  45. config2.maxMessageSize = 1048576;
  46. // Port range example
  47. config2.portRangeBegin = 5000;
  48. config2.portRangeEnd = 6000;
  49. PeerConnection pc2(config2);
  50. pc1.onLocalDescription([&pc2](Description sdp) {
  51. cout << "Description 1: " << sdp << endl;
  52. pc2.setRemoteDescription(string(sdp));
  53. });
  54. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  55. cout << "Candidate 1: " << candidate << endl;
  56. pc2.addRemoteCandidate(string(candidate));
  57. });
  58. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  59. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  60. cout << "Gathering state 1: " << state << endl;
  61. });
  62. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  63. cout << "Signaling state 1: " << state << endl;
  64. });
  65. pc2.onLocalDescription([&pc1](Description sdp) {
  66. cout << "Description 2: " << sdp << endl;
  67. pc1.setRemoteDescription(string(sdp));
  68. });
  69. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  70. cout << "Candidate 2: " << candidate << endl;
  71. pc1.addRemoteCandidate(string(candidate));
  72. });
  73. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  74. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  75. cout << "Gathering state 2: " << state << endl;
  76. });
  77. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  78. cout << "Signaling state 2: " << state << endl;
  79. });
  80. shared_ptr<DataChannel> dc2;
  81. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  82. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  83. if (dc->label() != "test") {
  84. cerr << "Wrong DataChannel label" << endl;
  85. return;
  86. }
  87. dc->onMessage([](variant<binary, string> message) {
  88. if (holds_alternative<string>(message)) {
  89. cout << "Message 2: " << get<string>(message) << endl;
  90. }
  91. });
  92. dc->send("Hello from 2");
  93. std::atomic_store(&dc2, dc);
  94. });
  95. auto dc1 = pc1.createDataChannel("test");
  96. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  97. auto dc1 = wdc1.lock();
  98. if (!dc1)
  99. return;
  100. cout << "DataChannel 1: Open" << endl;
  101. dc1->send("Hello from 1");
  102. });
  103. dc1->onMessage([](const variant<binary, string> &message) {
  104. if (holds_alternative<string>(message)) {
  105. cout << "Message 1: " << get<string>(message) << endl;
  106. }
  107. });
  108. // Wait a bit
  109. int attempts = 10;
  110. shared_ptr<DataChannel> adc2;
  111. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  112. this_thread::sleep_for(1s);
  113. if (pc1.state() != PeerConnection::State::Connected &&
  114. pc2.state() != PeerConnection::State::Connected)
  115. throw runtime_error("PeerConnection is not connected");
  116. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  117. throw runtime_error("DataChannel is not open");
  118. if (dc1->maxMessageSize() != 1048576 || dc2->maxMessageSize() != 1048576)
  119. throw runtime_error("DataChannel max message size is incorrect");
  120. if (auto addr = pc1.localAddress())
  121. cout << "Local address 1: " << *addr << endl;
  122. if (auto addr = pc1.remoteAddress())
  123. cout << "Remote address 1: " << *addr << endl;
  124. if (auto addr = pc2.localAddress())
  125. cout << "Local address 2: " << *addr << endl;
  126. if (auto addr = pc2.remoteAddress())
  127. cout << "Remote address 2: " << *addr << endl;
  128. Candidate local, remote;
  129. if (pc1.getSelectedCandidatePair(&local, &remote)) {
  130. cout << "Local candidate 1: " << local << endl;
  131. cout << "Remote candidate 1: " << remote << endl;
  132. }
  133. if (pc2.getSelectedCandidatePair(&local, &remote)) {
  134. cout << "Local candidate 2: " << local << endl;
  135. cout << "Remote candidate 2: " << remote << endl;
  136. }
  137. // Try to open a second data channel with another label
  138. shared_ptr<DataChannel> second2;
  139. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  140. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  141. if (dc->label() != "second") {
  142. cerr << "Wrong second DataChannel label" << endl;
  143. return;
  144. }
  145. dc->onMessage([](variant<binary, string> message) {
  146. if (holds_alternative<string>(message)) {
  147. cout << "Second Message 2: " << get<string>(message) << endl;
  148. }
  149. });
  150. dc->send("Send hello from 2");
  151. std::atomic_store(&second2, dc);
  152. });
  153. auto second1 = pc1.createDataChannel("second");
  154. second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
  155. auto second1 = wsecond1.lock();
  156. if (!second1)
  157. return;
  158. cout << "Second DataChannel 1: Open" << endl;
  159. second1->send("Second hello from 1");
  160. });
  161. dc1->onMessage([](const variant<binary, string> &message) {
  162. if (holds_alternative<string>(message)) {
  163. cout << "Second Message 1: " << get<string>(message) << endl;
  164. }
  165. });
  166. // Wait a bit
  167. attempts = 10;
  168. shared_ptr<DataChannel> asecond2;
  169. while (
  170. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  171. attempts--)
  172. this_thread::sleep_for(1s);
  173. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  174. throw runtime_error("Second DataChannel is not open");
  175. // Try to open a negotiated channel
  176. DataChannelInit init;
  177. init.negotiated = true;
  178. init.id = 42;
  179. auto negotiated1 = pc1.createDataChannel("negotiated", init);
  180. auto negotiated2 = pc2.createDataChannel("negoctated", init);
  181. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  182. throw runtime_error("Negotiated DataChannel is not open");
  183. std::atomic<bool> received = false;
  184. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  185. if (holds_alternative<string>(message)) {
  186. cout << "Second Message 2: " << get<string>(message) << endl;
  187. received = true;
  188. }
  189. });
  190. negotiated1->send("Hello from negotiated channel");
  191. // Wait a bit
  192. attempts = 5;
  193. while (!received && attempts--)
  194. this_thread::sleep_for(1s);
  195. if (!received)
  196. throw runtime_error("Negotiated DataChannel failed");
  197. // Delay close of peer 2 to check closing works properly
  198. pc1.close();
  199. this_thread::sleep_for(1s);
  200. pc2.close();
  201. this_thread::sleep_for(1s);
  202. cout << "Success" << endl;
  203. }