Some code refactoring

This commit is contained in:
WolverinDEV
2021-04-14 14:57:04 +02:00
parent bf643b4797
commit 6b774d3a80
29 changed files with 610 additions and 468 deletions
+24 -26
View File
@@ -175,7 +175,7 @@ void VirtualServer::unregisterInternalClient(std::shared_ptr<ConnectedClient> cl
}
bool VirtualServer::assignDefaultChannel(const shared_ptr<ConnectedClient>& client, bool join) {
std::shared_lock server_channel_lock{this->channel_tree_lock};
std::shared_lock server_channel_lock{this->channel_tree_mutex};
std::shared_ptr<BasicChannel> channel{};
auto requested_channel_path = client->properties()[property::CLIENT_DEFAULT_CHANNEL].value();
@@ -206,7 +206,7 @@ bool VirtualServer::assignDefaultChannel(const shared_ptr<ConnectedClient>& clie
debugMessage(this->getServerId(), "{} Allowing client to join channel {} because the token he used explicitly allowed it.", client->getLoggingPrefix(), channel->channelId());
if(whitelist_entry->second != "ignore") {
if (!channel->passwordMatch(client_channel_password, true)) {
if (!channel->verify_password(std::make_optional(client_channel_password), true)) {
if (!permission::v2::permission_granted(1, client->calculate_permission(permission::b_channel_join_ignore_password, channel->channelId()))) {
channel = nullptr;
goto skip_permissions;
@@ -222,7 +222,7 @@ bool VirtualServer::assignDefaultChannel(const shared_ptr<ConnectedClient>& clie
goto skip_permissions;
}
if (!channel->passwordMatch(client->properties()[property::CLIENT_DEFAULT_CHANNEL_PASSWORD], true)) {
if (!channel->verify_password(std::make_optional(client->properties()[property::CLIENT_DEFAULT_CHANNEL_PASSWORD].value()), true)) {
if(!permission::v2::permission_granted(1, client->calculate_permission(permission::b_channel_join_ignore_password, channel->channelId()))) {
debugMessage(this->getServerId(), "{} Tried to join channel {} but hasn't given the right channel password.", client->getLoggingPrefix(), channel->channelId());
channel = nullptr;
@@ -245,7 +245,7 @@ bool VirtualServer::assignDefaultChannel(const shared_ptr<ConnectedClient>& clie
debugMessage(this->getServerId(), "{} Using channel {} as default client channel.", client->getLoggingPrefix(), channel->channelId());
if(join) {
server_channel_lock.unlock();
unique_lock server_channel_w_lock(this->channel_tree_lock);
unique_lock server_channel_w_lock(this->channel_tree_mutex);
this->client_move(client, channel, nullptr, "", ViewReasonId::VREASON_USER_ACTION, false, server_channel_w_lock);
} else {
client->currentChannel = channel;
@@ -268,14 +268,14 @@ void VirtualServer::testBanStateChange(const std::shared_ptr<ConnectedClient>& i
void VirtualServer::notify_client_ban(const shared_ptr<ConnectedClient> &target, const std::shared_ptr<ts::server::ConnectedClient> &invoker, const std::string &reason, size_t time) {
/* the target is not allowed to execute anything; Must before channel tree lock because the target may waits for us to finish the channel stuff */
lock_guard command_lock(target->command_lock);
unique_lock server_channel_lock(this->channel_tree_lock); /* we're "moving" a client! */
unique_lock server_channel_lock(this->channel_tree_mutex); /* we're "moving" a client! */
if(target->currentChannel) {
for(const auto& client : this->getClients()) {
if(!client || client == target)
continue;
unique_lock client_channel_lock(client->channel_lock);
unique_lock client_channel_lock(client->channel_tree_mutex);
if(client->isClientVisible(target, false))
client->notifyClientLeftViewBanned(target, reason, invoker, time, false);
}
@@ -285,7 +285,7 @@ void VirtualServer::notify_client_ban(const shared_ptr<ConnectedClient> &target,
}
/* now disconnect the target itself */
unique_lock client_channel_lock(target->channel_lock);
unique_lock client_channel_lock(target->channel_tree_mutex);
target->notifyClientLeftViewBanned(target, reason, invoker, time, false);
target->currentChannel = nullptr;
}
@@ -298,19 +298,19 @@ void VirtualServer::notify_client_kick(
if(target_channel) {
/* use the move! */
unique_lock server_channel_lock(this->channel_tree_lock, defer_lock);
unique_lock server_channel_lock(this->channel_tree_mutex, defer_lock);
this->client_move(target, target_channel, invoker, reason, ViewReasonId::VREASON_CHANNEL_KICK, true, server_channel_lock);
} else {
/* the target is not allowed to execute anything; Must before channel tree lock because the target may waits for us to finish the channel stuff */
lock_guard command_lock(target->command_lock);
unique_lock server_channel_lock(this->channel_tree_lock); /* we're "moving" a client! */
unique_lock server_channel_lock(this->channel_tree_mutex); /* we're "moving" a client! */
if(target->currentChannel) {
for(const auto& client : this->getClients()) {
if(!client || client == target)
continue;
unique_lock client_channel_lock(client->channel_lock);
unique_lock client_channel_lock(client->channel_tree_mutex);
if(client->isClientVisible(target, false))
client->notifyClientLeftViewKicked(target, nullptr, reason, invoker, false);
}
@@ -323,7 +323,7 @@ void VirtualServer::notify_client_kick(
}
/* now disconnect the target itself */
unique_lock client_channel_lock(target->channel_lock);
unique_lock client_channel_lock(target->channel_tree_mutex);
target->notifyClientLeftViewKicked(target, nullptr, reason, invoker, false);
target->currentChannel = nullptr;
}
@@ -361,7 +361,6 @@ void VirtualServer::delete_channel(shared_ptr<ts::ServerChannel> channel, const
channel->deleted = true;
}
auto default_channel = this->channelTree->getDefaultChannel();
tree_lock.unlock();
deque<unique_lock<threads::Mutex>> command_locks;
for(const auto& client : clients) {
@@ -373,7 +372,8 @@ void VirtualServer::delete_channel(shared_ptr<ts::ServerChannel> channel, const
}
if(!tree_lock.owns_lock()) {
tree_lock.lock(); /* no clients left within that tree */
/* This case should never happen. client_move should never unlock the tree lock! */
tree_lock.lock();
}
command_locks.clear();
@@ -384,8 +384,8 @@ void VirtualServer::delete_channel(shared_ptr<ts::ServerChannel> channel, const
}
this->forEachClient([&](const shared_ptr<ConnectedClient>& client) {
unique_lock client_channel_lock(client->channel_lock);
client->notifyChannelDeleted(client->channels->delete_channel_root(channel), invoker);
unique_lock client_channel_lock(client->channel_tree_mutex);
client->notifyChannelDeleted(client->channel_tree->delete_channel_root(channel), invoker);
});
{
@@ -451,11 +451,11 @@ void VirtualServer::client_move(
/* second step: show the target channel to the client if its not shown and let him subscibe to the channel */
if(target_channel && notify_client) {
unique_lock client_channel_lock(target->channel_lock);
unique_lock client_channel_lock(target->channel_tree_mutex);
bool success = false;
/* TODO: Use a bunk here and not a notify for every single */
for(const auto& channel : target->channels->show_channel(l_target_channel, success))
for(const auto& channel : target->channel_tree->show_channel(l_target_channel, success))
target->notifyChannelShow(channel->channel(), channel->previous_channel);
sassert(success);
if(!success)
@@ -469,11 +469,11 @@ void VirtualServer::client_move(
this->forEachClient([&](const shared_ptr<ConnectedClient>& client) {
if (!notify_client && client == target) return;
unique_lock client_channel_lock(client->channel_lock);
auto chan_target = client->channels->find_channel(target_channel);
unique_lock client_channel_lock(client->channel_tree_mutex);
auto chan_target = client->channel_tree->find_channel(target_channel);
if(chan_target) {
auto chan_source = client->channels->find_channel(s_source_channel);
auto chan_source = client->channel_tree->find_channel(s_source_channel);
if(chan_source) {
if (chan_target->subscribed || client == target) {
if (client == target || client->isClientVisible(target, false)) {
@@ -527,7 +527,7 @@ void VirtualServer::client_move(
if(!client || client == target)
continue;
unique_lock client_channel_lock(client->channel_lock);
unique_lock client_channel_lock(client->channel_tree_mutex);
if(client->isClientVisible(target, false))
client->notifyClientLeftView(target, nullptr, reason_id, reason_message, invoker, false);
}
@@ -541,11 +541,9 @@ void VirtualServer::client_move(
TIMING_STEP(timings, "notify view");
target->currentChannel = target_channel;
server_channel_write_lock.unlock();
/* third step: update stuff for the client (remember: the client cant execute anything at the moment!) */
shared_lock server_channel_read_lock(this->channel_tree_lock);
unique_lock client_channel_lock(target->channel_lock);
unique_lock client_channel_lock{target->channel_tree_mutex};
TIMING_STEP(timings, "lock own tr");
if (s_source_channel) {
@@ -573,7 +571,7 @@ void VirtualServer::client_move(
if(s_source_channel) {
deque<ChannelId> deleted;
for(const auto& channel : target->channels->test_channel(l_source_channel, l_target_channel)) {
for(const auto& channel : target->channel_tree->test_channel(l_source_channel, l_target_channel)) {
deleted.push_back(channel->channelId());
}
@@ -586,7 +584,7 @@ void VirtualServer::client_move(
auto source_channel_sub_power = target->calculate_permission(permission::i_channel_subscribe_power, i_source_channel);
if(!s_source_channel->permission_granted(permission::i_channel_needed_subscribe_power, source_channel_sub_power, false)) {
auto source_channel_sub_power_ignore = target->calculate_permission(permission::b_channel_ignore_subscribe_power, i_source_channel);
if(!permission::v2::permission_granted(1, source_channel_sub_power_ignore, true)) {
if(!permission::v2::permission_granted(1, source_channel_sub_power_ignore)) {
logTrace(this->serverId, "Force unsubscribing of client {} for channel {}/{}. (Channel switch and no permissions)",
CLIENT_STR_LOG_PREFIX_(target), s_source_channel->name(),
i_source_channel