modchannels.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Minetest
  3. Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <unordered_map>
  18. #include <string>
  19. #include <vector>
  20. #include <memory>
  21. #include "network/networkprotocol.h"
  22. #include "irrlichttypes.h"
  23. enum ModChannelState : u8
  24. {
  25. MODCHANNEL_STATE_INIT,
  26. MODCHANNEL_STATE_READ_WRITE,
  27. MODCHANNEL_STATE_READ_ONLY,
  28. MODCHANNEL_STATE_MAX,
  29. };
  30. class ModChannel
  31. {
  32. public:
  33. ModChannel(const std::string &name) : m_name(name) {}
  34. ~ModChannel() = default;
  35. const std::string &getName() const { return m_name; }
  36. bool registerConsumer(session_t peer_id);
  37. bool removeConsumer(session_t peer_id);
  38. const std::vector<u16> &getChannelPeers() const { return m_client_consumers; }
  39. bool canWrite() const;
  40. void setState(ModChannelState state);
  41. private:
  42. std::string m_name;
  43. ModChannelState m_state = MODCHANNEL_STATE_INIT;
  44. std::vector<u16> m_client_consumers;
  45. };
  46. enum ModChannelSignal : u8
  47. {
  48. MODCHANNEL_SIGNAL_JOIN_OK,
  49. MODCHANNEL_SIGNAL_JOIN_FAILURE,
  50. MODCHANNEL_SIGNAL_LEAVE_OK,
  51. MODCHANNEL_SIGNAL_LEAVE_FAILURE,
  52. MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED,
  53. MODCHANNEL_SIGNAL_SET_STATE,
  54. };
  55. class ModChannelMgr
  56. {
  57. public:
  58. ModChannelMgr() = default;
  59. ~ModChannelMgr() = default;
  60. void registerChannel(const std::string &channel);
  61. bool setChannelState(const std::string &channel, ModChannelState state);
  62. bool joinChannel(const std::string &channel, session_t peer_id);
  63. bool leaveChannel(const std::string &channel, session_t peer_id);
  64. bool channelRegistered(const std::string &channel) const;
  65. ModChannel *getModChannel(const std::string &channel);
  66. /**
  67. * This function check if a local mod can write on the channel
  68. *
  69. * @param channel
  70. * @return true if write is allowed
  71. */
  72. bool canWriteOnChannel(const std::string &channel) const;
  73. void leaveAllChannels(session_t peer_id);
  74. const std::vector<u16> &getChannelPeers(const std::string &channel) const;
  75. private:
  76. bool removeChannel(const std::string &channel);
  77. std::unordered_map<std::string, std::unique_ptr<ModChannel>>
  78. m_registered_channels;
  79. };