mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Use user id instead of ice username
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "x.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
// std::cout << "Please input which transmisson want to join: ";
|
||||
// std::cin >> transmission_id;
|
||||
|
||||
PeerPtr* peer = CreatePeer(¶ms);
|
||||
JoinConnection(peer, transmission_id.c_str());
|
||||
|
||||
std::string msg = "Offer peer";
|
||||
|
||||
int i = 100;
|
||||
while (i--) {
|
||||
getchar();
|
||||
std::cout << "Send data: [" << msg << "]" << std::endl;
|
||||
SendData(peer, msg.data(), msg.size());
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
#include <iostream>
|
||||
|
||||
#include "x.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
std::string user_id = argv[1];
|
||||
// std::cout << "Please input which transmisson want to join: ";
|
||||
// std::cin >> transmission_id;
|
||||
|
||||
PeerPtr* peer = CreatePeer(¶ms);
|
||||
JoinConnection(peer, transmission_id.c_str(), user_id.c_str());
|
||||
|
||||
std::string msg = "Offer peer";
|
||||
|
||||
int i = 100;
|
||||
while (i--) {
|
||||
getchar();
|
||||
std::cout << "Send data: [" << msg << "]" << std::endl;
|
||||
SendData(peer, msg.data(), msg.size());
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "x.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
|
||||
PeerPtr* peer = CreatePeer(¶ms);
|
||||
CreateConnection(peer);
|
||||
|
||||
std::string msg = "Answer peer";
|
||||
|
||||
int i = 100;
|
||||
while (i--) {
|
||||
getchar();
|
||||
std::cout << "Send data: [" << msg << "]" << std::endl;
|
||||
SendData(peer, msg.data(), msg.size());
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
#include <iostream>
|
||||
|
||||
#include "x.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
|
||||
PeerPtr* peer = CreatePeer(¶ms);
|
||||
CreateConnection(peer);
|
||||
|
||||
std::string msg = "Answer peer";
|
||||
|
||||
int i = 100;
|
||||
while (i--) {
|
||||
getchar();
|
||||
std::cout << "Send data: [" << msg << "]" << std::endl;
|
||||
SendData(peer, msg.data(), msg.size());
|
||||
}
|
||||
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "signal_server.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
SignalServer s;
|
||||
std::string port = "";
|
||||
if (argc > 1) {
|
||||
port = argv[1];
|
||||
} else {
|
||||
port = "9090";
|
||||
}
|
||||
std::cout << "Port: " << port << std::endl;
|
||||
s.run(std::stoi(port));
|
||||
return 0;
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "signal_server.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
SignalServer s;
|
||||
std::string port = "";
|
||||
if (argc > 1) {
|
||||
port = argv[1];
|
||||
} else {
|
||||
port = "9090";
|
||||
}
|
||||
std::cout << "Port: " << port << std::endl;
|
||||
s.run(std::stoi(port));
|
||||
return 0;
|
||||
}
|
||||
@@ -72,6 +72,7 @@ bool SignalServer::on_pong(websocketpp::connection_hdl hdl, std::string s) {
|
||||
}
|
||||
|
||||
void SignalServer::run(uint16_t port) {
|
||||
server_.set_reuse_addr(true);
|
||||
server_.listen(port);
|
||||
|
||||
// Queues a connection accept operation
|
||||
@@ -95,6 +96,7 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "create_transmission"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string user_id = j["user_id"].get<std::string>();
|
||||
LOG_INFO("Receive create transmission request with id [{}]",
|
||||
transmission_id);
|
||||
if (transmission_list_.find(transmission_id) ==
|
||||
@@ -111,7 +113,16 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
transmission_id);
|
||||
}
|
||||
transmission_list_.insert(transmission_id);
|
||||
transmission_manager_.BindHostToTransmission(hdl, 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"},
|
||||
@@ -129,37 +140,6 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
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);
|
||||
std::string guest_username = GetIceUsername(sdp);
|
||||
transmission_manager_.BindGuestUsernameToWsHandle(guest_username, hdl);
|
||||
|
||||
websocketpp::connection_hdl host_hdl =
|
||||
transmission_manager_.GetHostOfTransmission(transmission_id);
|
||||
std::vector<websocketpp::connection_hdl> guest_hdl_list =
|
||||
transmission_manager_.GetAllGuestsOfTransmission(transmission_id);
|
||||
|
||||
// LOG_INFO("send offer sdp [{}]", sdp);
|
||||
json message = {
|
||||
{"type", "offer"}, {"sdp", sdp}, {"guest", guest_username}};
|
||||
|
||||
LOG_INFO("[{}] send offer sdp to host", guest_username);
|
||||
send_msg(host_hdl, message);
|
||||
|
||||
LOG_INFO("Size of guest_hdl_list: {}", guest_hdl_list.size());
|
||||
for (auto guest_hdl : guest_hdl_list) {
|
||||
if (guest_hdl.lock().get() != hdl.lock().get()) {
|
||||
LOG_INFO("[{}] send offer sdp to [{}]", guest_username,
|
||||
transmission_manager_.GetGuestUsername(guest_hdl));
|
||||
send_msg(guest_hdl, message);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "query_members"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::vector<std::string> member_list =
|
||||
@@ -169,23 +149,44 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
{"transmission_id", transmission_id},
|
||||
{"transmission_members", member_list},
|
||||
{"status", "success"}};
|
||||
|
||||
LOG_INFO("Send member_list: [{}]", message.dump());
|
||||
send_msg(hdl, message);
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_peer = j["remote_peer"].get<std::string>();
|
||||
// LOG_INFO("Receive transmission id [{}] with offer sdp [{}]",
|
||||
// transmission_id, sdp);
|
||||
transmission_manager_.BindWsHandleToTransmission(hdl, transmission_id);
|
||||
std::string offer_peer = GetIceUsername(sdp);
|
||||
transmission_manager_.BindUsernameToWsHandle(offer_peer, hdl);
|
||||
|
||||
websocketpp::connection_hdl destination_hdl =
|
||||
transmission_manager_.GetWsHandle(remote_peer);
|
||||
|
||||
json message = {{"type", "offer"}, {"sdp", sdp}};
|
||||
|
||||
LOG_INFO("[{}] send offer sdp to [{}]", offer_peer, remote_peer);
|
||||
send_msg(destination_hdl, message);
|
||||
|
||||
break;
|
||||
}
|
||||
case "answer"_H: {
|
||||
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>();
|
||||
std::string host_ice_username = GetIceUsername(sdp);
|
||||
if (transmission_manager_.GetHostUsername(hdl).empty()) {
|
||||
transmission_manager_.BindHostUsernameToWsHandle(host_ice_username,
|
||||
hdl);
|
||||
if (transmission_manager_.GetUsername(hdl) == "host") {
|
||||
LOG_INFO("Update transmission [{}] [host] to [{}]", transmission_id,
|
||||
host_ice_username);
|
||||
transmission_manager_.UpdateUsernameToWsHandle(host_ice_username, hdl);
|
||||
}
|
||||
// LOG_INFO("Receive transmission id [{}] with answer sdp [{}]",
|
||||
// transmission_id, sdp);
|
||||
|
||||
websocketpp::connection_hdl guest_hdl =
|
||||
transmission_manager_.GetGuestWsHandle(guest_ice_username);
|
||||
transmission_manager_.GetWsHandle(guest_ice_username);
|
||||
|
||||
// LOG_INFO("send answer sdp [{}]", sdp);
|
||||
LOG_INFO("[{}] send answer sdp to [{}]", host_ice_username,
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
#ifndef _SIGNAL_SERVER_H_
|
||||
#define _SIGNAL_SERVER_H_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include "transmission_manager.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> server;
|
||||
typedef unsigned int connection_id;
|
||||
typedef std::string room_id;
|
||||
|
||||
class SignalServer {
|
||||
public:
|
||||
SignalServer();
|
||||
~SignalServer();
|
||||
|
||||
bool on_open(websocketpp::connection_hdl hdl);
|
||||
|
||||
bool on_close(websocketpp::connection_hdl hdl);
|
||||
|
||||
bool on_ping(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
bool on_pong(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
void run(uint16_t port);
|
||||
|
||||
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
|
||||
|
||||
void send_msg(websocketpp::connection_hdl hdl, json message);
|
||||
|
||||
private:
|
||||
server server_;
|
||||
std::map<websocketpp::connection_hdl, connection_id,
|
||||
std::owner_less<websocketpp::connection_hdl>>
|
||||
ws_connections_;
|
||||
std::map<room_id, connection_id> rooms_;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
|
||||
std::set<std::string> transmission_list_;
|
||||
TransmissionManager transmission_manager_;
|
||||
};
|
||||
|
||||
#ifndef _SIGNAL_SERVER_H_
|
||||
#define _SIGNAL_SERVER_H_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include "transmission_manager.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> server;
|
||||
typedef unsigned int connection_id;
|
||||
typedef std::string room_id;
|
||||
|
||||
class SignalServer {
|
||||
public:
|
||||
SignalServer();
|
||||
~SignalServer();
|
||||
|
||||
bool on_open(websocketpp::connection_hdl hdl);
|
||||
|
||||
bool on_close(websocketpp::connection_hdl hdl);
|
||||
|
||||
bool on_ping(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
bool on_pong(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
void run(uint16_t port);
|
||||
|
||||
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
|
||||
|
||||
void send_msg(websocketpp::connection_hdl hdl, json message);
|
||||
|
||||
private:
|
||||
server server_;
|
||||
std::map<websocketpp::connection_hdl, connection_id,
|
||||
std::owner_less<websocketpp::connection_hdl>>
|
||||
ws_connections_;
|
||||
std::map<room_id, connection_id> rooms_;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
|
||||
std::set<std::string> transmission_list_;
|
||||
TransmissionManager transmission_manager_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -6,101 +6,114 @@ 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::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::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::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::ReleaseGuestFromTransmission(
|
||||
// websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::BindHostUsernameToWsHandle(
|
||||
const std::string& host_username, websocketpp::connection_hdl hdl) {
|
||||
if (transmission_host_username_list_.find(host_username) !=
|
||||
transmission_host_username_list_.end()) {
|
||||
LOG_ERROR("Guest already bind to username [{}]", host_username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
transmission_host_username_list_[host_username] = hdl;
|
||||
}
|
||||
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::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;
|
||||
}
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
// return true;
|
||||
// }
|
||||
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
// }
|
||||
|
||||
return "";
|
||||
}
|
||||
// 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::GetGuestUsername(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto guest : transmission_guest_username_list_) {
|
||||
if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
}
|
||||
// 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 "";
|
||||
}
|
||||
// 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(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_guest_list_.find(transmission_id) !=
|
||||
transmission_guest_list_.end()) {
|
||||
return transmission_guest_list_[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>();
|
||||
}
|
||||
@@ -121,12 +134,127 @@ std::vector<std::string> TransmissionManager::GetAllMembersOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
std::vector<std::string> member_list;
|
||||
|
||||
member_list.push_back(
|
||||
GetHostUsername(GetHostOfTransmission(transmission_id)));
|
||||
|
||||
for (auto guest_hdl : GetAllGuestsOfTransmission(transmission_id)) {
|
||||
member_list.push_back(GetGuestUsername(guest_hdl));
|
||||
member_list.push_back(GetUsername(guest_hdl));
|
||||
}
|
||||
|
||||
return member_list;
|
||||
}
|
||||
|
||||
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()) {
|
||||
transmission_user_id_list_[transmission_id].push_back(user_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);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transmission_user_id_list_[transmission_id].push_back(user_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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());
|
||||
return false;
|
||||
} else {
|
||||
user_id_ws_hdl_list_[user_id] = hdl;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
user_name_user_id_list_[user_id].push_back(user_name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -11,22 +11,23 @@ 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 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(const std::string& host_username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool BindGuestUsernameToWsHandle(const std::string& guest_username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
// 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);
|
||||
// std::string GetHostUsername(websocketpp::connection_hdl hdl);
|
||||
// std::string GetGuestUsername(websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetHostOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
@@ -38,6 +39,25 @@ class TransmissionManager {
|
||||
std::vector<std::string> GetAllMembersOfTransmission(
|
||||
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);
|
||||
|
||||
private:
|
||||
std::map<std::string, websocketpp::connection_hdl> transmission_host_list_;
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
@@ -47,6 +67,15 @@ class TransmissionManager {
|
||||
transmission_host_username_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl>
|
||||
transmission_guest_username_list_;
|
||||
|
||||
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, std::vector<std::string>> user_name_user_id_list_;
|
||||
|
||||
std::map<std::string, websocketpp::connection_hdl> username_ws_hdl_list_;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user