mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Support multiple ice connections
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "signal_server.h"
|
||||
|
||||
int main() {
|
||||
SignalServer s;
|
||||
// connect ws://localhost:9002
|
||||
s.run();
|
||||
return 0;
|
||||
int main(int argc, char* argv[]) {
|
||||
SignalServer s;
|
||||
std::string port = argv[1];
|
||||
std::cout << "Port: " << port << std::endl;
|
||||
s.run(std::stoi(port));
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
|
||||
#include "sdp_manager.h"
|
||||
|
||||
SdpManager ::SdpManager() {}
|
||||
|
||||
SdpManager ::~SdpManager() {}
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef _SDP_MANAGER_H_
|
||||
#define _SDP_MANAGER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
class SdpManager {
|
||||
public:
|
||||
SdpManager();
|
||||
~SdpManager();
|
||||
|
||||
public:
|
||||
int AddOfferSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||
int AddAnswerSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||
int UpdateOfferSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||
int UpdateAnswerSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||
|
||||
const std::string &GetOfferSdpFromConnection(std::string &connection_id);
|
||||
const std::string &GetAnswerSdpFromConnection(std::string &connection_id);
|
||||
|
||||
int RemoveConnetion(std::string &connection_id);
|
||||
|
||||
private:
|
||||
// <connection_id, <offer_sdp, answer_sdp>.
|
||||
std::map<std::string, std::pair<std::string, std::string>> answer_hdl_map_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,20 +1,9 @@
|
||||
#include "signal_server.h"
|
||||
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
|
||||
constexpr size_t HASH_STRING_PIECE(const char* string_piece) {
|
||||
std::size_t result = 0;
|
||||
while (*string_piece) {
|
||||
result = (result * 131) + *string_piece++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr size_t operator"" _H(const char* string_piece, size_t) {
|
||||
return HASH_STRING_PIECE(string_piece);
|
||||
}
|
||||
|
||||
const std::string gen_random_6() {
|
||||
const std::string GenerateTransmissionId() {
|
||||
static const char alphanum[] = "0123456789";
|
||||
std::string random_id;
|
||||
random_id.reserve(6);
|
||||
@@ -65,7 +54,7 @@ bool SignalServer::on_open(websocketpp::connection_hdl hdl) {
|
||||
}
|
||||
|
||||
bool SignalServer::on_close(websocketpp::connection_hdl hdl) {
|
||||
LOG_INFO("Websocket onnection [{}] closed", ws_connection_id_++);
|
||||
LOG_INFO("Websocket onnection [{}] closed", ws_connection_id_);
|
||||
ws_connections_.erase(hdl);
|
||||
return true;
|
||||
}
|
||||
@@ -82,9 +71,9 @@ bool SignalServer::on_pong(websocketpp::connection_hdl hdl, std::string s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void SignalServer::run() {
|
||||
// Listen on port 9002
|
||||
server_.listen(9002);
|
||||
void SignalServer::run(uint16_t port) {
|
||||
// Listen on port 9093
|
||||
server_.listen(port);
|
||||
|
||||
// Queues a connection accept operation
|
||||
server_.start_accept();
|
||||
@@ -102,36 +91,38 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
std::string payload = msg->get_payload();
|
||||
|
||||
auto j = json::parse(payload);
|
||||
std::string type = j["type"];
|
||||
std::string type = j["type"].get<std::string>();
|
||||
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "create_transmission"_H: {
|
||||
transmission_id_ = j["transmission_id"];
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Receive create transmission request with id [{}]",
|
||||
transmission_id_);
|
||||
if (transmission_list_.find(transmission_id_) ==
|
||||
transmission_id);
|
||||
if (transmission_list_.find(transmission_id) ==
|
||||
transmission_list_.end()) {
|
||||
if (transmission_id_.empty()) {
|
||||
transmission_id_ = gen_random_6();
|
||||
while (transmission_list_.find(transmission_id_) !=
|
||||
if (transmission_id.empty()) {
|
||||
transmission_id = GenerateTransmissionId();
|
||||
while (transmission_list_.find(transmission_id) !=
|
||||
transmission_list_.end()) {
|
||||
transmission_id_ = gen_random_6();
|
||||
transmission_id = GenerateTransmissionId();
|
||||
}
|
||||
LOG_INFO(
|
||||
"Transmission id is empty, generate a new one for this request "
|
||||
"[{}]",
|
||||
transmission_id_);
|
||||
transmission_id);
|
||||
}
|
||||
transmission_list_.insert(transmission_id_);
|
||||
LOG_INFO("Create transmission id [{}]", transmission_id_);
|
||||
transmission_list_.insert(transmission_id);
|
||||
transmission_manager_.BindHostToTransmission(hdl, transmission_id);
|
||||
|
||||
LOG_INFO("Create transmission id [{}]", transmission_id);
|
||||
json message = {{"type", "transmission_id"},
|
||||
{"transmission_id", transmission_id_},
|
||||
{"transmission_id", transmission_id},
|
||||
{"status", "success"}};
|
||||
send_msg(hdl, message);
|
||||
} else {
|
||||
LOG_INFO("Transmission id [{}] already exist", transmission_id_);
|
||||
LOG_INFO("Transmission id [{}] already exist", transmission_id);
|
||||
json message = {{"type", "transmission_id"},
|
||||
{"transmission_id", transmission_id_},
|
||||
{"transmission_id", transmission_id},
|
||||
{"status", "fail"},
|
||||
{"reason", "Transmission id exist"}};
|
||||
send_msg(hdl, message);
|
||||
@@ -140,49 +131,52 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
std::string sdp = j["sdp"];
|
||||
LOG_INFO("Save transmission_id[{}] with offer sdp[{}]", transmission_id,
|
||||
sdp);
|
||||
// ws_handle_manager_.BindHandleToConnection(hdl, );
|
||||
offer_sdp_map_[transmission_id] = sdp;
|
||||
offer_hdl_map_[transmission_id] = hdl;
|
||||
break;
|
||||
}
|
||||
case "query_remote_sdp"_H: {
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
std::string sdp = offer_sdp_map_[transmission_id];
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
LOG_INFO("Receive transmission id [{}] with offer sdp [{}]",
|
||||
transmission_id, sdp);
|
||||
transmission_manager_.BindGuestToTransmission(hdl, transmission_id);
|
||||
|
||||
websocketpp::connection_hdl host_hdl =
|
||||
transmission_manager_.GetHostOfTransmission(transmission_id);
|
||||
|
||||
std::string ice_username = GetIceUsername(sdp);
|
||||
transmission_manager_.BindGuestUsernameToWsHandle(ice_username, hdl);
|
||||
|
||||
LOG_INFO("send offer sdp [{}]", sdp.c_str());
|
||||
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
||||
send_msg(hdl, message);
|
||||
json message = {{"type", "offer"}, {"sdp", sdp}, {"guest", ice_username}};
|
||||
send_msg(host_hdl, message);
|
||||
break;
|
||||
}
|
||||
case "answer"_H: {
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
std::string sdp = j["sdp"];
|
||||
LOG_INFO("Save transmission_id[{}] with answer sdp[{}]", transmission_id,
|
||||
sdp);
|
||||
answer_sdp_map_[transmission_id] = sdp;
|
||||
answer_hdl_map_[transmission_id] = hdl;
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string guest_ice_username = j["guest"].get<std::string>();
|
||||
LOG_INFO("Receive transmission id [{}] with answer sdp [{}]",
|
||||
transmission_id, sdp);
|
||||
|
||||
websocketpp::connection_hdl guest_hdl =
|
||||
transmission_manager_.GetGuestWsHandle(guest_ice_username);
|
||||
|
||||
LOG_INFO("send answer sdp [{}]", sdp.c_str());
|
||||
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
||||
send_msg(offer_hdl_map_[transmission_id], message);
|
||||
send_msg(guest_hdl, message);
|
||||
break;
|
||||
}
|
||||
case "offer_candidate"_H: {
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
std::string candidate = j["sdp"];
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string candidate = j["sdp"].get<std::string>();
|
||||
LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
||||
send_msg(answer_hdl_map_[transmission_id], message);
|
||||
// send_msg(answer_hdl_map_[transmission_id], message);
|
||||
break;
|
||||
}
|
||||
case "answer_candidate"_H: {
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
std::string candidate = j["sdp"];
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string candidate = j["sdp"].get<std::string>();
|
||||
LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
||||
send_msg(offer_hdl_map_[transmission_id], message);
|
||||
// send_msg(offer_hdl_map_[transmission_id], message);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include "sdp_manager.h"
|
||||
#include "ws_handle_manager.h"
|
||||
#include "transmission_manager.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
@@ -31,7 +30,7 @@ class SignalServer {
|
||||
|
||||
bool on_pong(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
void run();
|
||||
void run(uint16_t port);
|
||||
|
||||
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
|
||||
|
||||
@@ -44,17 +43,9 @@ class SignalServer {
|
||||
ws_connections_;
|
||||
std::map<room_id, connection_id> rooms_;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
std::string transmission_id_ = "000000";
|
||||
|
||||
std::set<std::string> transmission_list_;
|
||||
|
||||
std::map<std::string, std::string> offer_sdp_map_;
|
||||
std::map<std::string, std::string> answer_sdp_map_;
|
||||
std::map<std::string, websocketpp::connection_hdl> offer_hdl_map_;
|
||||
std::map<std::string, websocketpp::connection_hdl> answer_hdl_map_;
|
||||
|
||||
WsHandleManager ws_handle_manager_;
|
||||
SdpManager sdp_manager_;
|
||||
TransmissionManager transmission_manager_;
|
||||
};
|
||||
|
||||
#endif
|
||||
80
tests/signal_server/transmission_manager.cpp
Normal file
80
tests/signal_server/transmission_manager.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "transmission_manager.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
TransmissionManager::TransmissionManager() {}
|
||||
|
||||
TransmissionManager::~TransmissionManager() {}
|
||||
|
||||
bool TransmissionManager::BindHostToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
if (transmission_host_list_.find(transmission_id) !=
|
||||
transmission_host_list_.end()) {
|
||||
LOG_WARN("Transmission already has a host [{}]",
|
||||
transmission_host_list_[transmission_id].lock().get());
|
||||
return false;
|
||||
} else {
|
||||
transmission_host_list_[transmission_id] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindGuestToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseHostFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseGuestFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindGuestUsernameToWsHandle(
|
||||
const std::string& guest_username, websocketpp::connection_hdl hdl) {
|
||||
if (transmission_guest_username_list_.find(guest_username) !=
|
||||
transmission_guest_username_list_.end()) {
|
||||
LOG_WARN("Guest already bind to username [{}]", guest_username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
transmission_guest_username_list_[guest_username] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetHostOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_host_list_.find(transmission_id) !=
|
||||
transmission_host_list_.end()) {
|
||||
return transmission_host_list_[transmission_id];
|
||||
} else {
|
||||
websocketpp::connection_hdl hdl;
|
||||
return hdl;
|
||||
}
|
||||
}
|
||||
|
||||
std::set<websocketpp::connection_hdl>
|
||||
TransmissionManager::GetGuestOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_guest_list_.find(transmission_id) !=
|
||||
transmission_guest_list_.end()) {
|
||||
return transmission_guest_list_[transmission_id];
|
||||
} else {
|
||||
return std::set<websocketpp::connection_hdl>();
|
||||
}
|
||||
}
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetGuestWsHandle(
|
||||
const std::string& guest_username) {
|
||||
if (transmission_guest_username_list_.find(guest_username) !=
|
||||
transmission_guest_username_list_.end()) {
|
||||
return transmission_guest_username_list_[guest_username];
|
||||
} else {
|
||||
websocketpp::connection_hdl hdl;
|
||||
return hdl;
|
||||
}
|
||||
}
|
||||
42
tests/signal_server/transmission_manager.h
Normal file
42
tests/signal_server/transmission_manager.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef _TRANSIMISSION_MANAGER_H_
|
||||
#define _TRANSIMISSION_MANAGER_H_
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
class TransmissionManager {
|
||||
public:
|
||||
TransmissionManager();
|
||||
~TransmissionManager();
|
||||
|
||||
public:
|
||||
bool BindHostToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindGuestToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool ReleaseHostFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool ReleaseGuestFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
|
||||
bool BindGuestUsernameToWsHandle(const std::string& guest_username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetHostOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
std::set<websocketpp::connection_hdl> GetGuestOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
websocketpp::connection_hdl GetGuestWsHandle(
|
||||
const std::string& guest_username);
|
||||
|
||||
private:
|
||||
std::map<std::string, websocketpp::connection_hdl> transmission_host_list_;
|
||||
std::map<std::string, std::set<websocketpp::connection_hdl>>
|
||||
transmission_guest_list_;
|
||||
|
||||
std::map<std::string, websocketpp::connection_hdl>
|
||||
transmission_guest_username_list_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,5 +0,0 @@
|
||||
#include "ws_handle_manager.h"
|
||||
|
||||
WsHandleManager::WsHandleManager() {}
|
||||
|
||||
WsHandleManager::~WsHandleManager() {}
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef _WS_HANDLE_MANAGER_H_
|
||||
#define _WS_HANDLE_MANAGER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
class WsHandleManager {
|
||||
public:
|
||||
WsHandleManager();
|
||||
~WsHandleManager();
|
||||
|
||||
public:
|
||||
bool BindHandleToConnection(websocketpp::connection_hdl hdl,
|
||||
std::string& connection_id);
|
||||
bool ReleaseHandleFromConnection(websocketpp::connection_hdl hdl,
|
||||
std::string& connection_id);
|
||||
|
||||
const std::string& GetConnectionId(websocketpp::connection_hdl hdl);
|
||||
websocketpp::connection_hdl GetWsHandle(std::string& connection_id);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user