diff options
author | Loïc Blot <nerzhul@users.noreply.github.com> | 2017-09-26 00:11:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-26 00:11:20 +0200 |
commit | 6f1c90720402415b62fb4d5e809ec7dbc1cd7f96 (patch) | |
tree | 6f94c2bbc2d343be50945a0074bc16da282a4bc1 /src/modchannels.h | |
parent | 6df312a608912b3cb21d04532151e29e8b0c7301 (diff) | |
download | minetest-6f1c90720402415b62fb4d5e809ec7dbc1cd7f96.tar.gz minetest-6f1c90720402415b62fb4d5e809ec7dbc1cd7f96.tar.bz2 minetest-6f1c90720402415b62fb4d5e809ec7dbc1cd7f96.zip |
Implement mod communication channels (#6351)
Implement network communication for channels
* Implement ModChannel manager server side to route incoming messages from clients to other clients
* Add signal handler switch on client & ModChannelMgr on client to handle channels
* Add Lua API bindings + client packet sending + unittests
* Implement server message sending
* Add callback from received message handler to Lua API using registration method
Diffstat (limited to 'src/modchannels.h')
-rw-r--r-- | src/modchannels.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/modchannels.h b/src/modchannels.h new file mode 100644 index 000000000..5f1aa884e --- /dev/null +++ b/src/modchannels.h @@ -0,0 +1,92 @@ +/* +Minetest +Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#pragma once + +#include <unordered_map> +#include <string> +#include <vector> +#include <memory> +#include "irrlichttypes.h" + +enum ModChannelState : u8 +{ + MODCHANNEL_STATE_INIT, + MODCHANNEL_STATE_READ_WRITE, + MODCHANNEL_STATE_READ_ONLY, + MODCHANNEL_STATE_MAX, +}; + +class ModChannel +{ +public: + ModChannel(const std::string &name) : m_name(name) {} + ~ModChannel() = default; + + const std::string &getName() const { return m_name; } + bool registerConsumer(u16 peer_id); + bool removeConsumer(u16 peer_id); + const std::vector<u16> &getChannelPeers() const { return m_client_consumers; } + bool canWrite() const; + void setState(ModChannelState state); + +private: + std::string m_name; + ModChannelState m_state = MODCHANNEL_STATE_INIT; + std::vector<u16> m_client_consumers; +}; + +enum ModChannelSignal : u8 +{ + MODCHANNEL_SIGNAL_JOIN_OK, + MODCHANNEL_SIGNAL_JOIN_FAILURE, + MODCHANNEL_SIGNAL_LEAVE_OK, + MODCHANNEL_SIGNAL_LEAVE_FAILURE, + MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED, + MODCHANNEL_SIGNAL_SET_STATE, +}; + +class ModChannelMgr +{ +public: + ModChannelMgr() = default; + ~ModChannelMgr() = default; + + void registerChannel(const std::string &channel); + bool setChannelState(const std::string &channel, ModChannelState state); + bool joinChannel(const std::string &channel, u16 peer_id); + bool leaveChannel(const std::string &channel, u16 peer_id); + bool channelRegistered(const std::string &channel) const; + ModChannel *getModChannel(const std::string &channel); + /** + * This function check if a local mod can write on the channel + * + * @param channel + * @return true if write is allowed + */ + bool canWriteOnChannel(const std::string &channel) const; + void leaveAllChannels(u16 peer_id); + const std::vector<u16> &getChannelPeers(const std::string &channel) const; + +private: + bool removeChannel(const std::string &channel); + + std::unordered_map<std::string, std::unique_ptr<ModChannel>> + m_registered_channels; +}; |