mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Remove user from transmission when websocket closed
This commit is contained in:
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user