|
| 1 | +/* |
| 2 | + * libdatachannel echo client |
| 3 | + * Copyright (c) 2021 Paul-Louis Ageneau |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <httplib.h> |
| 20 | +#include <nlohmann/json.hpp> |
| 21 | +#include <rtc/rtc.hpp> |
| 22 | + |
| 23 | +#include <chrono> |
| 24 | +#include <future> |
| 25 | +#include <iostream> |
| 26 | +#include <memory> |
| 27 | + |
| 28 | +namespace http = httplib; |
| 29 | +using json = nlohmann::json; |
| 30 | + |
| 31 | +using namespace std::chrono_literals; |
| 32 | + |
| 33 | +int main(int argc, char **argv) try { |
| 34 | + const std::string server = argc > 1 ? argv[1] : "http://localhost:8080"; |
| 35 | + const std::string message = "Hello world!"; |
| 36 | + |
| 37 | + rtc::InitLogger(rtc::LogLevel::Warning); |
| 38 | + |
| 39 | + http::Client cl(server.c_str()); |
| 40 | + rtc::PeerConnection pc{rtc::Configuration{}}; |
| 41 | + |
| 42 | + std::promise<void> promise; |
| 43 | + auto future = promise.get_future(); |
| 44 | + |
| 45 | + pc.onGatheringStateChange([&pc, &cl, &promise](rtc::PeerConnection::GatheringState state) { |
| 46 | + if (state == rtc::PeerConnection::GatheringState::Complete) { |
| 47 | + try { |
| 48 | + auto local = pc.localDescription().value(); |
| 49 | + json msg; |
| 50 | + msg["sdp"] = std::string(local); |
| 51 | + msg["type"] = local.typeString(); |
| 52 | + |
| 53 | + auto res = cl.Post("/offer", msg.dump().c_str(), "application/json"); |
| 54 | + if (!res) |
| 55 | + throw std::runtime_error("HTTP request failed"); |
| 56 | + |
| 57 | + if (res->status != 200) |
| 58 | + throw std::runtime_error("HTTP request failed with status " + |
| 59 | + std::to_string(res->status)); |
| 60 | + |
| 61 | + auto parsed = json::parse(res->body); |
| 62 | + rtc::Description remote(parsed["sdp"].get<std::string>(), |
| 63 | + parsed["type"].get<std::string>()); |
| 64 | + pc.setRemoteDescription(std::move(remote)); |
| 65 | + |
| 66 | + } catch (...) { |
| 67 | + promise.set_exception(std::current_exception()); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + pc.onStateChange([&pc, &promise](rtc::PeerConnection::State state) { |
| 73 | + if (state == rtc::PeerConnection::State::Disconnected) { |
| 74 | + pc.close(); |
| 75 | + promise.set_exception( |
| 76 | + std::make_exception_ptr(std::runtime_error("Peer Connection failed"))); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + auto dc = pc.createDataChannel("echo"); |
| 81 | + |
| 82 | + dc->onOpen([dc, message]() { dc->send(message); }); |
| 83 | + |
| 84 | + dc->onMessage([message, &promise](auto msg) { |
| 85 | + if (std::holds_alternative<std::string>(msg)) { |
| 86 | + auto str = std::get<std::string>(msg); |
| 87 | + if (str == message) |
| 88 | + promise.set_value(); |
| 89 | + else |
| 90 | + promise.set_exception( |
| 91 | + std::make_exception_ptr(std::runtime_error("Echo test failed"))); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + if (future.wait_for(10s) != std::future_status::ready) { |
| 96 | + if (dc->isOpen()) |
| 97 | + throw std::runtime_error("Timeout waiting on echo message"); |
| 98 | + else if (pc.state() == rtc::PeerConnection::State::Connected) |
| 99 | + throw std::runtime_error("Timeout waiting on Data Channel opening"); |
| 100 | + else |
| 101 | + throw std::runtime_error("Timeout waiting on Peer Connection"); |
| 102 | + } |
| 103 | + return 0; |
| 104 | + |
| 105 | +} catch (const std::exception &e) { |
| 106 | + std::cerr << "Error: " << e.what() << std::endl; |
| 107 | + return -1; |
| 108 | +} |
0 commit comments