clientobject: add null checks
[waspsaliva.git] / src / modchannels.cpp
blob301dcb0923c6bd47a6c454bd030426873c18471a
1 /*
2 Minetest
3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "modchannels.h"
21 #include <algorithm>
22 #include <cassert>
23 #include "util/basic_macros.h"
25 bool ModChannel::registerConsumer(session_t peer_id)
28 // ignore if peer_id already joined
29 if (CONTAINS(m_client_consumers, peer_id))
30 return false;
32 m_client_consumers.push_back(peer_id);
33 return true;
36 bool ModChannel::removeConsumer(session_t peer_id)
38 bool found = false;
39 auto peer_removal_fct = [peer_id, &found](u16 p) {
40 if (p == peer_id)
41 found = true;
43 return p == peer_id;
46 m_client_consumers.erase(
47 std::remove_if(m_client_consumers.begin(),
48 m_client_consumers.end(), peer_removal_fct),
49 m_client_consumers.end());
51 return found;
54 bool ModChannel::canWrite() const
56 return m_state == MODCHANNEL_STATE_READ_WRITE;
59 void ModChannel::setState(ModChannelState state)
61 assert(state != MODCHANNEL_STATE_INIT);
63 m_state = state;
66 bool ModChannelMgr::channelRegistered(const std::string &channel) const
68 return m_registered_channels.find(channel) != m_registered_channels.end();
71 ModChannel *ModChannelMgr::getModChannel(const std::string &channel)
73 if (!channelRegistered(channel))
74 return nullptr;
76 return m_registered_channels[channel].get();
79 bool ModChannelMgr::canWriteOnChannel(const std::string &channel) const
81 const auto channel_it = m_registered_channels.find(channel);
82 if (channel_it == m_registered_channels.end()) {
83 return false;
86 return channel_it->second->canWrite();
89 void ModChannelMgr::registerChannel(const std::string &channel)
91 m_registered_channels[channel] =
92 std::unique_ptr<ModChannel>(new ModChannel(channel));
95 bool ModChannelMgr::setChannelState(const std::string &channel, ModChannelState state)
97 if (!channelRegistered(channel))
98 return false;
100 auto channel_it = m_registered_channels.find(channel);
101 channel_it->second->setState(state);
103 return true;
106 bool ModChannelMgr::removeChannel(const std::string &channel)
108 if (!channelRegistered(channel))
109 return false;
111 m_registered_channels.erase(channel);
112 return true;
115 bool ModChannelMgr::joinChannel(const std::string &channel, session_t peer_id)
117 if (!channelRegistered(channel))
118 registerChannel(channel);
120 return m_registered_channels[channel]->registerConsumer(peer_id);
123 bool ModChannelMgr::leaveChannel(const std::string &channel, session_t peer_id)
125 if (!channelRegistered(channel))
126 return false;
128 // Remove consumer from channel
129 bool consumerRemoved = m_registered_channels[channel]->removeConsumer(peer_id);
131 // If channel is empty, remove it
132 if (m_registered_channels[channel]->getChannelPeers().empty()) {
133 removeChannel(channel);
135 return consumerRemoved;
138 void ModChannelMgr::leaveAllChannels(session_t peer_id)
140 for (auto &channel_it : m_registered_channels)
141 channel_it.second->removeConsumer(peer_id);
144 static std::vector<u16> empty_channel_list;
145 const std::vector<u16> &ModChannelMgr::getChannelPeers(const std::string &channel) const
147 const auto &channel_it = m_registered_channels.find(channel);
148 if (channel_it == m_registered_channels.end())
149 return empty_channel_list;
151 return channel_it->second->getChannelPeers();