mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
Remove user from transmission when websocket closed
This commit is contained in:
@@ -19,8 +19,6 @@ int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
juice_config_t config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
|
||||
LOG_INFO("stun server ip[{}] port[{}]", ip_, port_);
|
||||
|
||||
// STUN server example
|
||||
config.stun_server_host = ip_.c_str();
|
||||
config.stun_server_port = port_;
|
||||
@@ -38,7 +36,6 @@ int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
|
||||
int IceAgent::DestoryIceAgent() {
|
||||
juice_destroy(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -50,25 +47,15 @@ char *IceAgent::GenerateLocalSdp() {
|
||||
|
||||
juice_get_local_description(agent_, local_sdp_, JUICE_MAX_SDP_STRING_LEN);
|
||||
// LOG_INFO("Generate local sdp:[\n{}]", local_sdp_);
|
||||
LOG_INFO("Generate local sdp");
|
||||
|
||||
return local_sdp_;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
|
||||
LOG_INFO("[{}] Set remote sdp", (void *)this);
|
||||
juice_set_remote_description(agent_, remote_sdp);
|
||||
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
|
||||
|
||||
return 0;
|
||||
return juice_set_remote_description(agent_, remote_sdp);
|
||||
}
|
||||
|
||||
int IceAgent::GatherCandidates() {
|
||||
LOG_INFO("[{}] Gather candidates", (void *)this);
|
||||
juice_gather_candidates(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
int IceAgent::GatherCandidates() { return juice_gather_candidates(agent_); }
|
||||
|
||||
juice_state_t IceAgent::GetIceState() {
|
||||
state_ = juice_get_state(agent_);
|
||||
@@ -110,18 +97,13 @@ bool IceAgent::GetSelectedAddresses() {
|
||||
}
|
||||
|
||||
int IceAgent::AddRemoteCandidates(const char *remote_candidates) {
|
||||
juice_add_remote_candidate(agent_, remote_candidates);
|
||||
|
||||
return 0;
|
||||
return juice_add_remote_candidate(agent_, remote_candidates);
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteGatheringDone() {
|
||||
juice_set_remote_gathering_done(agent_);
|
||||
|
||||
return 0;
|
||||
return juice_set_remote_gathering_done(agent_);
|
||||
}
|
||||
|
||||
int IceAgent::Send(const char *data, size_t size) {
|
||||
juice_send(agent_, data, size);
|
||||
return 0;
|
||||
return juice_send(agent_, data, size);
|
||||
}
|
||||
@@ -32,7 +32,14 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
|
||||
ice_agent_->CreateIceAgent(
|
||||
[](juice_agent_t *agent, juice_state_t state, void *user_ptr) {
|
||||
LOG_INFO("state_change: {}", ice_status[state]);
|
||||
if (user_ptr) {
|
||||
IceTransmission *ice_transmission_obj =
|
||||
static_cast<IceTransmission *>(user_ptr);
|
||||
LOG_INFO("[{}->{}] state_change: {}", ice_transmission_obj->user_id_,
|
||||
ice_transmission_obj->remote_user_id_, ice_status[state]);
|
||||
} else {
|
||||
LOG_INFO("state_change: {}", ice_status[state]);
|
||||
}
|
||||
},
|
||||
[](juice_agent_t *agent, const char *sdp, void *user_ptr) {
|
||||
// LOG_INFO("candadite: {}", sdp);
|
||||
@@ -41,15 +48,15 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
// *>(user_ptr)->SendOfferLocalCandidate(sdp);
|
||||
},
|
||||
[](juice_agent_t *agent, void *user_ptr) {
|
||||
LOG_INFO("gather_done");
|
||||
// non-trickle
|
||||
if (user_ptr) {
|
||||
IceTransmission *ice_transmission_obj =
|
||||
static_cast<IceTransmission *>(user_ptr);
|
||||
LOG_INFO("[{}] gather_done", ice_transmission_obj->user_id_);
|
||||
|
||||
if (ice_transmission_obj->offer_peer_) {
|
||||
ice_transmission_obj->GetLocalSdp();
|
||||
ice_transmission_obj->SendOffer();
|
||||
LOG_INFO("[{}] SendOffer", (void *)ice_transmission_obj)
|
||||
} else {
|
||||
ice_transmission_obj->CreateAnswer();
|
||||
ice_transmission_obj->SendAnswer();
|
||||
@@ -104,29 +111,25 @@ int IceTransmission::SetTransmissionId(const std::string &transmission_id) {
|
||||
int IceTransmission::JoinTransmission() {
|
||||
LOG_INFO("Join transport");
|
||||
|
||||
// if (SignalStatus::Connected != signal_status_) {
|
||||
// LOG_ERROR("Not connect to signalserver");
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
// QueryRemoteSdp(transmission_id);
|
||||
CreateOffer();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::GatherCandidates() {
|
||||
ice_agent_->GatherCandidates();
|
||||
LOG_INFO("[{}] Gather candidates", user_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::GetLocalSdp() {
|
||||
local_sdp_ = ice_agent_->GenerateLocalSdp();
|
||||
LOG_INFO("Local ice username: [{}]", GetIceUsername(local_sdp_));
|
||||
LOG_INFO("[{}] generate local sdp", user_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::SetRemoteSdp(const std::string &remote_sdp) {
|
||||
ice_agent_->SetRemoteSdp(remote_sdp.c_str());
|
||||
LOG_INFO("[{}] set remote sdp", user_id_);
|
||||
remote_ice_username_ = GetIceUsername(remote_sdp);
|
||||
return 0;
|
||||
}
|
||||
@@ -137,7 +140,7 @@ int IceTransmission::AddRemoteCandidate(const std::string &remote_candidate) {
|
||||
}
|
||||
|
||||
int IceTransmission::CreateOffer() {
|
||||
LOG_INFO("[{}] Create offer", (void *)this);
|
||||
LOG_INFO("[{}] create offer", user_id_);
|
||||
GatherCandidates();
|
||||
return 0;
|
||||
}
|
||||
@@ -148,11 +151,11 @@ int IceTransmission::SendOffer() {
|
||||
{"user_id", user_id_},
|
||||
{"remote_user_id", remote_user_id_},
|
||||
{"sdp", local_sdp_}};
|
||||
LOG_INFO("Send offer:\n{}", message.dump());
|
||||
// LOG_INFO("Send offer");
|
||||
// LOG_INFO("Send offer:\n{}", message.dump());
|
||||
|
||||
if (ice_ws_transport_) {
|
||||
ice_ws_transport_->Send(message.dump());
|
||||
LOG_INFO("[{}->{}] send offer", user_id_, remote_user_id_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -160,7 +163,7 @@ int IceTransmission::SendOffer() {
|
||||
int IceTransmission::QueryRemoteSdp(std::string transmission_id) {
|
||||
json message = {{"type", "query_remote_sdp"},
|
||||
{"transmission_id", transmission_id_}};
|
||||
LOG_INFO("Query remote sdp");
|
||||
LOG_INFO("[{}] query remote sdp", user_id_);
|
||||
|
||||
if (ice_ws_transport_) {
|
||||
ice_ws_transport_->Send(message.dump());
|
||||
@@ -180,11 +183,9 @@ int IceTransmission::SendAnswer() {
|
||||
{"user_id", user_id_},
|
||||
{"remote_user_id", remote_user_id_}};
|
||||
|
||||
LOG_INFO("[{}] Send answer to [{}]", GetIceUsername(local_sdp_),
|
||||
remote_ice_username_);
|
||||
|
||||
if (ice_ws_transport_) {
|
||||
ice_ws_transport_->Send(message.dump());
|
||||
LOG_INFO("[{}->{}] send answer", user_id_, remote_user_id_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -195,7 +196,7 @@ int IceTransmission::SendOfferLocalCandidate(
|
||||
{"transmission_id", transmission_id_},
|
||||
{"sdp", remote_candidate}};
|
||||
// LOG_INFO("Send candidate:\n{}", message.dump().c_str());
|
||||
LOG_INFO("Send candidate");
|
||||
LOG_INFO("[{}] send candidate", user_id_);
|
||||
|
||||
if (ice_ws_transport_) {
|
||||
ice_ws_transport_->Send(message.dump());
|
||||
@@ -209,7 +210,7 @@ int IceTransmission::SendAnswerLocalCandidate(
|
||||
{"transmission_id", transmission_id_},
|
||||
{"sdp", remote_candidate}};
|
||||
// LOG_INFO("Send candidate:\n{}", message.dump().c_str());
|
||||
LOG_INFO("Send candidate");
|
||||
LOG_INFO("[{}] send candidate", user_id_);
|
||||
|
||||
if (ice_ws_transport_) {
|
||||
ice_ws_transport_->Send(message.dump());
|
||||
@@ -223,63 +224,17 @@ int IceTransmission::SendData(const char *data, size_t size) {
|
||||
}
|
||||
|
||||
void IceTransmission::OnReceiveMessage(const std::string &msg) {
|
||||
auto j = json::parse(msg);
|
||||
// auto j = json::parse(msg);
|
||||
// LOG_INFO("msg: {}", msg.c_str());
|
||||
|
||||
std::string type = j["type"];
|
||||
// std::string type = j["type"];
|
||||
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "offer"_H: {
|
||||
remote_sdp_ = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp_.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
// LOG_INFO("Receive remote sdp [{}]", remote_sdp_);
|
||||
LOG_INFO("Receive remote sdp");
|
||||
SetRemoteSdp(remote_sdp_);
|
||||
|
||||
GatherCandidates();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "transmission_id"_H: {
|
||||
if (j["status"].get<std::string>() == "success") {
|
||||
transmission_id_ = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Create transmission success with id [{}]", transmission_id_);
|
||||
// SendOffer();
|
||||
} else if (j["status"].get<std::string>() == "fail") {
|
||||
LOG_WARN("Create transmission failed with id [{}], due to [{}]",
|
||||
transmission_id_, j["reason"].get<std::string>().c_str());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "remote_sdp"_H: {
|
||||
remote_sdp_ = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp_.empty()) {
|
||||
LOG_INFO("Offer peer not ready, wait 1 second and requery remote sdp");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
QueryRemoteSdp(transmission_id_);
|
||||
} else {
|
||||
// LOG_INFO("Receive remote sdp [{}]", remote_sdp_);
|
||||
LOG_INFO("Receive remote sdp");
|
||||
SetRemoteSdp(remote_sdp_);
|
||||
|
||||
if (!offer_peer_) {
|
||||
GatherCandidates();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "candidate"_H: {
|
||||
std::string candidate = j["sdp"].get<std::string>();
|
||||
// LOG_INFO("Receive candidate [{}]", candidate);
|
||||
LOG_INFO("Receive candidate");
|
||||
AddRemoteCandidate(candidate);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
// case "offer"_H: {
|
||||
// remote_sdp_ = j["sdp"].get<std::string>();
|
||||
// break;
|
||||
// }
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -37,10 +37,13 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
signal_server_port_ = stoi(cfg_signal_server_port_);
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
LOG_INFO("stun server ip [{}] port [{}]", cfg_stun_server_ip_,
|
||||
stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type.c_str());
|
||||
LOG_INFO("msg type: {}", type.c_str());
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
@@ -107,7 +110,7 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
}
|
||||
|
||||
do {
|
||||
LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
// LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
json message = {{"type", "create_transmission"},
|
||||
@@ -115,7 +118,9 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
{"transmission_id", transmission_id}};
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
LOG_INFO("Send create transmission request: {}", message.dump().c_str());
|
||||
// LOG_INFO("Send create transmission request: {}", message.dump());
|
||||
LOG_INFO("Send create transmission request, transmission_id [{}]",
|
||||
transmission_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -139,42 +144,21 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
// do {
|
||||
// } while (ice_transmission_list_.empty());
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type);
|
||||
LOG_INFO("msg type: {}", type);
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "transmission_members"_H: {
|
||||
transmission_member_list_ = j["transmission_members"];
|
||||
case "user_id_list"_H: {
|
||||
user_id_list_ = j["user_id_list"];
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
|
||||
LOG_INFO("Transmission [{}] members: [", transmission_id);
|
||||
for (auto member : transmission_member_list_) {
|
||||
LOG_INFO("{}", member);
|
||||
for (auto user_id : user_id_list_) {
|
||||
LOG_INFO("{}", user_id);
|
||||
}
|
||||
LOG_INFO("]");
|
||||
|
||||
// if (transmission_member_list_.size() == 1 &&
|
||||
// transmission_member_list_[0] == "host") {
|
||||
// ice_transmission_list_["host"] = new IceTransmission(
|
||||
// true, "host", ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_["host"]->InitIceTransmission(
|
||||
// cfg_stun_server_ip_, stun_server_port_);
|
||||
// ice_transmission_list_["host"]->JoinTransmission(transmission_id,
|
||||
// user_id_);
|
||||
// } else {
|
||||
// for (auto &member : transmission_member_list_) {
|
||||
// ice_transmission_list_[member] = new IceTransmission(
|
||||
// true, member, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_[member]->InitIceTransmission(
|
||||
// cfg_stun_server_ip_, stun_server_port_);
|
||||
// ice_transmission_list_[member]->JoinTransmission(transmission_id,
|
||||
// user_id_);
|
||||
// }
|
||||
// }
|
||||
|
||||
for (auto &remote_user_id : transmission_member_list_) {
|
||||
for (auto &remote_user_id : user_id_list_) {
|
||||
ice_transmission_list_[remote_user_id] = new IceTransmission(
|
||||
true, transmission_id, user_id_, remote_user_id, ws_transport_,
|
||||
on_receive_ice_msg_);
|
||||
@@ -235,25 +219,11 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
ice_transmission_list_.end()) {
|
||||
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
||||
}
|
||||
|
||||
// if (!offer_peer_) {
|
||||
// GatherCandidates();
|
||||
// }
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "candidate"_H: {
|
||||
std::string remote_sdp_with_candidates = j["sdp"].get<std::string>();
|
||||
std::string ice_username = GetIceUsername(remote_sdp_with_candidates);
|
||||
LOG_INFO("Receive remote candidates from [{}]", ice_username);
|
||||
// LOG_INFO("Receive candidate [{}]", candidate);
|
||||
|
||||
ice_transmission_list_[ice_username]->AddRemoteCandidate(
|
||||
remote_sdp_with_candidates);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ice_transmission_->OnReceiveMessage(msg);
|
||||
// ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -272,22 +242,11 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
// ice_transmission_list_["self"] =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_["self"]->InitIceTransmission(cfg_stun_server_ip_,
|
||||
// stun_server_port_);
|
||||
// ice_transmission_ =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_->InitIceTransmission(cfg_stun_server_ip,
|
||||
// stun_server_port);
|
||||
|
||||
do {
|
||||
// LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
RequestTransmissionMemberList(transmission_id_);
|
||||
// ice_transmission_->JoinTransmission(transmission_id_);
|
||||
// ice_transmission_list_["self"]->JoinTransmission(transmission_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -295,7 +254,7 @@ int PeerConnection::RequestTransmissionMemberList(
|
||||
const std::string &transmission_id) {
|
||||
LOG_INFO("Request member list");
|
||||
|
||||
json message = {{"type", "query_members"},
|
||||
json message = {{"type", "query_user_id_list"},
|
||||
{"transmission_id", transmission_id_}};
|
||||
|
||||
if (ws_transport_) {
|
||||
|
||||
@@ -49,13 +49,13 @@ class PeerConnection {
|
||||
int stun_server_port_ = 0;
|
||||
WsTransmission *ws_transport_ = nullptr;
|
||||
IceTransmission *ice_transmission_ = nullptr;
|
||||
std::vector<std::string> transmission_member_list_;
|
||||
std::map<std::string, IceTransmission *> ice_transmission_list_;
|
||||
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
std::string user_id_ = "";
|
||||
std::string transmission_id_ = "";
|
||||
std::vector<std::string> user_id_list_;
|
||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||
};
|
||||
|
||||
|
||||
@@ -55,6 +55,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_);
|
||||
bool ret = transmission_manager_.ReleaseUserIdFromTransmission(hdl);
|
||||
ws_connections_.erase(hdl);
|
||||
return true;
|
||||
}
|
||||
@@ -114,16 +115,9 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
}
|
||||
transmission_list_.insert(transmission_id);
|
||||
|
||||
// transmission_manager_.BindWsHandleToTransmission(hdl,
|
||||
// transmission_id);
|
||||
transmission_manager_.BindUserIdToTransmission(user_id,
|
||||
transmission_id);
|
||||
transmission_manager_.BindUserIdToWsHandle(user_id, hdl);
|
||||
// transmission_manager_.BindUserNameToUserId("host", user_id);
|
||||
|
||||
// if (transmission_manager_.GetUsername(hdl).empty()) {
|
||||
// transmission_manager_.BindUsernameToWsHandle("host", hdl);
|
||||
// }
|
||||
|
||||
LOG_INFO("Create transmission id [{}]", transmission_id);
|
||||
json message = {{"type", "transmission_id"},
|
||||
@@ -141,17 +135,17 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
|
||||
break;
|
||||
}
|
||||
case "query_members"_H: {
|
||||
case "query_user_id_list"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::vector<std::string> member_list =
|
||||
transmission_manager_.GetAllMembersOfTransmission(transmission_id);
|
||||
std::vector<std::string> user_id_list =
|
||||
transmission_manager_.GetAllUserIdOfTransmission(transmission_id);
|
||||
|
||||
json message = {{"type", "transmission_members"},
|
||||
json message = {{"type", "user_id_list"},
|
||||
{"transmission_id", transmission_id},
|
||||
{"transmission_members", member_list},
|
||||
{"user_id_list", user_id_list},
|
||||
{"status", "success"}};
|
||||
|
||||
LOG_INFO("Send member_list: [{}]", message.dump());
|
||||
// LOG_INFO("Send member_list: [{}]", message.dump());
|
||||
send_msg(hdl, message);
|
||||
break;
|
||||
}
|
||||
@@ -161,10 +155,6 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
std::string user_id = j["user_id"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
|
||||
// transmission_manager_.BindWsHandleToTransmission(hdl, transmission_id);
|
||||
// std::string offer_peer = GetIceUsername(sdp);
|
||||
// transmission_manager_.BindUsernameToWsHandle(offer_peer, hdl);
|
||||
|
||||
transmission_manager_.BindUserIdToTransmission(user_id, transmission_id);
|
||||
transmission_manager_.BindUserIdToWsHandle(user_id, hdl);
|
||||
|
||||
@@ -176,7 +166,7 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
{"remote_user_id", user_id},
|
||||
{"transmission_id", transmission_id}};
|
||||
|
||||
LOG_INFO("[{}] send offer sdp to [{}]", user_id, remote_user_id);
|
||||
LOG_INFO("[{}] send offer to [{}]", user_id, remote_user_id);
|
||||
send_msg(destination_hdl, message);
|
||||
|
||||
break;
|
||||
@@ -187,25 +177,11 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
std::string user_id = j["user_id"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
|
||||
// transmission_manager_.BindUserIdToTransmission(user_id,
|
||||
// transmission_id); transmission_manager_.BindUserIdToWsHandle(user_id,
|
||||
// hdl);
|
||||
|
||||
websocketpp::connection_hdl destination_hdl =
|
||||
transmission_manager_.GetWsHandle(remote_user_id);
|
||||
|
||||
// if (transmission_manager_.GetUsername(hdl) == "host") {
|
||||
// LOG_INFO("Update transmission [{}] [host] to [{}]", transmission_id,
|
||||
// host_ice_username);
|
||||
// transmission_manager_.UpdateUsernameToWsHandle(host_ice_username,
|
||||
// hdl);
|
||||
// }
|
||||
|
||||
// websocketpp::connection_hdl guest_hdl =
|
||||
// transmission_manager_.GetWsHandle(guest_ice_username);
|
||||
|
||||
// LOG_INFO("send answer sdp [{}]", sdp);
|
||||
LOG_INFO("[{}] send answer sdp to [{}]", user_id, remote_user_id);
|
||||
LOG_INFO("[{}] send answer to [{}]", user_id, remote_user_id);
|
||||
json message = {{"type", "remote_sdp"},
|
||||
{"sdp", sdp},
|
||||
{"remote_user_id", user_id},
|
||||
|
||||
@@ -6,135 +6,8 @@ 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) {
|
||||
// if (transmission_guest_list_.find(transmission_id) !=
|
||||
// transmission_guest_list_.end()) {
|
||||
// transmission_guest_list_[transmission_id].push_back(hdl);
|
||||
// } else {
|
||||
// std::vector<websocketpp::connection_hdl> guest_hdl_list;
|
||||
// guest_hdl_list.push_back(hdl);
|
||||
// transmission_guest_list_[transmission_id] = guest_hdl_list;
|
||||
// }
|
||||
// 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::BindHostUsernameToWsHandle(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// if (transmission_host_username_list_.find("host") !=
|
||||
// transmission_host_username_list_.end()) {
|
||||
// LOG_ERROR("Host already exist");
|
||||
// return false;
|
||||
// } else {
|
||||
// transmission_host_username_list_["host"] = hdl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// bool TransmissionManager::UpdateHostUsernameToWsHandle(
|
||||
// const std::string& host_username, websocketpp::connection_hdl hdl) {
|
||||
// if (transmission_host_username_list_.find("host") ==
|
||||
// transmission_host_username_list_.end()) {
|
||||
// LOG_ERROR("Host not exist");
|
||||
// return false;
|
||||
// }
|
||||
// transmission_host_username_list_.erase("host");
|
||||
// transmission_host_username_list_[host_username] = hdl;
|
||||
|
||||
// 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_ERROR("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::string TransmissionManager::GetHostUsername(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// for (auto host : transmission_host_username_list_) {
|
||||
// if (host.second.lock().get() == hdl.lock().get()) return host.first;
|
||||
// }
|
||||
|
||||
// return "";
|
||||
// }
|
||||
|
||||
// std::string TransmissionManager::GetGuestUsername(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// for (auto guest : transmission_guest_username_list_) {
|
||||
// if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
// }
|
||||
|
||||
// return "";
|
||||
// }
|
||||
|
||||
std::vector<websocketpp::connection_hdl>
|
||||
TransmissionManager::GetAllGuestsOfTransmission(
|
||||
std::vector<std::string> TransmissionManager::GetAllUserIdOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_user_ws_hdl_list_.find(transmission_id) !=
|
||||
transmission_user_ws_hdl_list_.end()) {
|
||||
return transmission_user_ws_hdl_list_[transmission_id];
|
||||
} else {
|
||||
return std::vector<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;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> TransmissionManager::GetAllMembersOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
// for (auto guest_hdl : GetAllGuestsOfTransmission(transmission_id)) {
|
||||
// member_list.push_back(GetUsername(guest_hdl));
|
||||
// }
|
||||
if (transmission_user_id_list_.find(transmission_id) !=
|
||||
transmission_user_id_list_.end()) {
|
||||
return transmission_user_id_list_[transmission_id];
|
||||
@@ -143,44 +16,26 @@ std::vector<std::string> TransmissionManager::GetAllMembersOfTransmission(
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindWsHandleToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
if (transmission_user_ws_hdl_list_.find(transmission_id) ==
|
||||
transmission_user_ws_hdl_list_.end()) {
|
||||
transmission_user_ws_hdl_list_[transmission_id].push_back(hdl);
|
||||
return true;
|
||||
} else {
|
||||
auto hdl_list = transmission_user_ws_hdl_list_[transmission_id];
|
||||
for (auto h : hdl_list) {
|
||||
if (h.lock().get() == hdl.lock().get()) {
|
||||
LOG_ERROR("Ws handle [{}] already bind to transmission [{}]",
|
||||
hdl.lock().get(), transmission_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transmission_user_ws_hdl_list_[transmission_id].push_back(hdl);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUserIdToTransmission(
|
||||
const std::string& user_id, const std::string& transmission_id) {
|
||||
if (transmission_user_id_list_.find(transmission_id) ==
|
||||
transmission_user_id_list_.end()) {
|
||||
LOG_INFO("Add user id [{}] to transmission [{}]", user_id, transmission_id);
|
||||
transmission_user_id_list_[transmission_id].push_back(user_id);
|
||||
LOG_INFO("Bind user id [{}] to transmission [{}]", user_id,
|
||||
transmission_id);
|
||||
return true;
|
||||
} else {
|
||||
auto user_id_list = transmission_user_id_list_[transmission_id];
|
||||
for (auto id : user_id_list) {
|
||||
if (id == user_id) {
|
||||
LOG_ERROR("User id [{}] already bind to transmission [{}]", user_id,
|
||||
transmission_id);
|
||||
LOG_WARN("User id [{}] already bind to transmission [{}]", user_id,
|
||||
transmission_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transmission_user_id_list_[transmission_id].push_back(user_id);
|
||||
LOG_INFO("Add user id [{}] to transmission [{}]", user_id, transmission_id);
|
||||
LOG_INFO("Bind user id [{}] to transmission [{}]", user_id,
|
||||
transmission_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -188,8 +43,8 @@ bool TransmissionManager::BindUserIdToTransmission(
|
||||
bool TransmissionManager::BindUserIdToWsHandle(
|
||||
const std::string& user_id, websocketpp::connection_hdl hdl) {
|
||||
if (user_id_ws_hdl_list_.find(user_id) != user_id_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("User id already bind to websocket handle [{}]", user_id,
|
||||
hdl.lock().get());
|
||||
LOG_WARN("User id already bind to websocket handle [{}]", user_id,
|
||||
hdl.lock().get());
|
||||
return false;
|
||||
} else {
|
||||
user_id_ws_hdl_list_[user_id] = hdl;
|
||||
@@ -197,72 +52,46 @@ bool TransmissionManager::BindUserIdToWsHandle(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUserNameToUserId(const std::string& user_name,
|
||||
const std::string& user_id) {
|
||||
if (user_name_user_id_list_.find(user_id) == user_name_user_id_list_.end()) {
|
||||
user_name_user_id_list_[user_id].push_back(user_name);
|
||||
return true;
|
||||
} else {
|
||||
auto user_name_list = user_name_user_id_list_[user_id];
|
||||
for (auto name : user_name_list) {
|
||||
if (name == user_name) {
|
||||
LOG_ERROR("User name [{}] already bind to user id [{}]", user_name,
|
||||
user_id);
|
||||
return false;
|
||||
bool TransmissionManager::ReleaseWsHandleFromTransmission(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto trans_it = transmission_user_ws_hdl_list_.begin();
|
||||
trans_it != transmission_user_ws_hdl_list_.end(); ++trans_it) {
|
||||
auto hdl_list = trans_it->second;
|
||||
for (auto it = hdl_list.begin(); it != hdl_list.end(); ++it) {
|
||||
if (it->lock().get() == hdl.lock().get()) {
|
||||
hdl_list.erase(it);
|
||||
LOG_INFO("Remove ws handle [{}] from transmission [{}]",
|
||||
hdl.lock().get(), trans_it->first);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
user_name_user_id_list_[user_id].push_back(user_name);
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseWsHandleFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUsernameToWsHandle(
|
||||
const std::string& username, websocketpp::connection_hdl hdl) {
|
||||
if (username_ws_hdl_list_.find(username) != username_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("Guest already bind to username [{}]", username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
username_ws_hdl_list_[username] = hdl;
|
||||
bool TransmissionManager::ReleaseUserIdFromTransmission(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto it = user_id_ws_hdl_list_.begin(); it != user_id_ws_hdl_list_.end();
|
||||
++it) {
|
||||
if (it->second.lock().get() == hdl.lock().get()) {
|
||||
for (auto trans_it = transmission_user_id_list_.begin();
|
||||
trans_it != transmission_user_id_list_.end(); ++trans_it) {
|
||||
auto& user_id_list = trans_it->second;
|
||||
auto user_id_it =
|
||||
std::find(user_id_list.begin(), user_id_list.end(), it->first);
|
||||
if (user_id_it != user_id_list.end()) {
|
||||
user_id_list.erase(user_id_it);
|
||||
LOG_INFO("Remove user id [{}] from transmission [{}]", it->first,
|
||||
trans_it->first);
|
||||
user_id_ws_hdl_list_.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TransmissionManager::UpdateUsernameToWsHandle(
|
||||
const std::string& username, websocketpp::connection_hdl hdl) {
|
||||
if (username_ws_hdl_list_.find("host") == username_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("Host not exist");
|
||||
return false;
|
||||
}
|
||||
username_ws_hdl_list_.erase("host");
|
||||
username_ws_hdl_list_[username] = hdl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string TransmissionManager::GetUsername(websocketpp::connection_hdl hdl) {
|
||||
for (auto guest : username_ws_hdl_list_) {
|
||||
if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
}
|
||||
|
||||
LOG_ERROR("No user with websocket handle [{}]", hdl.lock().get());
|
||||
return "";
|
||||
}
|
||||
|
||||
// websocketpp::connection_hdl TransmissionManager::GetWsHandle(
|
||||
// const std::string& username) {
|
||||
// if (username_ws_hdl_list_.find(username) != username_ws_hdl_list_.end()) {
|
||||
// return username_ws_hdl_list_[username];
|
||||
// } else {
|
||||
// websocketpp::connection_hdl hdl;
|
||||
// return hdl;
|
||||
// }
|
||||
// }
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetWsHandle(
|
||||
const std::string& user_id) {
|
||||
if (user_id_ws_hdl_list_.find(user_id) != user_id_ws_hdl_list_.end()) {
|
||||
|
||||
@@ -11,52 +11,18 @@ class 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 BindHostUsernameToWsHandle(websocketpp::connection_hdl hdl);
|
||||
// bool UpdateHostUsernameToWsHandle(const std::string& host_username,
|
||||
// websocketpp::connection_hdl hdl);
|
||||
// bool BindGuestUsernameToWsHandle(const std::string& guest_username,
|
||||
// websocketpp::connection_hdl hdl);
|
||||
|
||||
// std::string GetHostUsername(websocketpp::connection_hdl hdl);
|
||||
// std::string GetGuestUsername(websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetHostOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
std::vector<websocketpp::connection_hdl> GetAllGuestsOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
websocketpp::connection_hdl GetGuestWsHandle(
|
||||
const std::string& guest_username);
|
||||
|
||||
std::vector<std::string> GetAllMembersOfTransmission(
|
||||
std::vector<std::string> GetAllUserIdOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
|
||||
public:
|
||||
bool BindWsHandleToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindUserIdToTransmission(const std::string& user_id,
|
||||
const std::string& transmission_id);
|
||||
bool BindUserIdToWsHandle(const std::string& user_id,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool BindUserNameToUserId(const std::string& user_name,
|
||||
const std::string& user_id);
|
||||
|
||||
bool ReleaseWsHandleFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindUsernameToWsHandle(const std::string& username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool UpdateUsernameToWsHandle(const std::string& username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
std::string GetUsername(websocketpp::connection_hdl hdl);
|
||||
// websocketpp::connection_hdl GetWsHandle(const std::string& username);
|
||||
bool ReleaseWsHandleFromTransmission(websocketpp::connection_hdl hdl);
|
||||
bool ReleaseUserIdFromTransmission(websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetWsHandle(const std::string& user_id);
|
||||
|
||||
private:
|
||||
@@ -72,11 +38,12 @@ class TransmissionManager {
|
||||
private:
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
transmission_user_ws_hdl_list_;
|
||||
std::map<std::string, std::vector<std::string>> transmission_user_id_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> user_id_ws_hdl_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> username_ws_hdl_list_;
|
||||
|
||||
std::map<std::string, std::vector<std::string>> user_name_user_id_list_;
|
||||
|
||||
std::map<std::string, websocketpp::connection_hdl> username_ws_hdl_list_;
|
||||
std::map<std::string, std::vector<std::string>> transmission_user_id_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> user_id_ws_hdl_list_;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user