messagehandlerelement.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef RTC_MESSAGE_HANDLER_ELEMENT_H
  18. #define RTC_MESSAGE_HANDLER_ELEMENT_H
  19. #if RTC_ENABLE_MEDIA
  20. #include "include.hpp"
  21. #include "message.hpp"
  22. #include "rtp.hpp"
  23. namespace rtc {
  24. using ChainedMessagesProduct = std::shared_ptr<std::vector<binary_ptr>>;
  25. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product();
  26. RTC_CPP_EXPORT ChainedMessagesProduct make_chained_messages_product(message_ptr msg);
  27. /// Ougoing messages
  28. struct RTC_CPP_EXPORT ChainedOutgoingProduct {
  29. ChainedOutgoingProduct(ChainedMessagesProduct messages, std::optional<message_ptr> control = nullopt);
  30. const ChainedMessagesProduct messages;
  31. const std::optional<message_ptr> control;
  32. };
  33. /// Ougoing response to incoming messages
  34. struct RTC_CPP_EXPORT ChainedOutgoingResponseProduct {
  35. ChainedOutgoingResponseProduct(std::optional<ChainedMessagesProduct> messages = nullopt, std::optional<message_ptr> control = nullopt);
  36. const std::optional<ChainedMessagesProduct> messages;
  37. const std::optional<message_ptr> control;
  38. };
  39. /// Incoming messages with response
  40. struct RTC_CPP_EXPORT ChainedIncomingProduct {
  41. ChainedIncomingProduct(std::optional<ChainedMessagesProduct> incoming = nullopt, std::optional<ChainedMessagesProduct> outgoing = nullopt);
  42. const std::optional<ChainedMessagesProduct> incoming;
  43. const std::optional<ChainedOutgoingResponseProduct> outgoing;
  44. };
  45. /// Incoming control messages with response
  46. struct RTC_CPP_EXPORT ChainedIncomingControlProduct {
  47. ChainedIncomingControlProduct(message_ptr incoming, std::optional<ChainedOutgoingResponseProduct> outgoing = nullopt);
  48. const std::optional<message_ptr> incoming;
  49. const std::optional<ChainedOutgoingResponseProduct> outgoing;
  50. };
  51. /// Chainable handler
  52. class RTC_CPP_EXPORT MessageHandlerElement: public std::enable_shared_from_this<MessageHandlerElement> {
  53. std::optional<std::shared_ptr<MessageHandlerElement>> upstream = nullopt;
  54. std::optional<std::shared_ptr<MessageHandlerElement>> downstream = nullopt;
  55. void prepareAndSendResponse(std::optional<ChainedOutgoingResponseProduct> outgoing, std::function<bool (ChainedOutgoingResponseProduct)> send);
  56. void removeFromChain();
  57. public:
  58. MessageHandlerElement();
  59. /// Creates response to incoming message
  60. /// @param messages Current repsonse
  61. /// @returns New response
  62. std::optional<ChainedOutgoingResponseProduct> processOutgoingResponse(ChainedOutgoingResponseProduct messages);
  63. // Process incoming and ougoing messages
  64. std::optional<message_ptr> formIncomingControlMessage(message_ptr message, std::function<bool (ChainedOutgoingResponseProduct)> send);
  65. std::optional<ChainedMessagesProduct> formIncomingBinaryMessage(ChainedMessagesProduct messages, std::function<bool (ChainedOutgoingResponseProduct)> send);
  66. std::optional<message_ptr> formOutgoingControlMessage(message_ptr message);
  67. std::optional<ChainedOutgoingProduct> formOutgoingBinaryMessage(ChainedOutgoingProduct product);
  68. /// Process current control message
  69. /// @param messages current message
  70. /// @returns Modified message and response
  71. virtual ChainedIncomingControlProduct processIncomingControlMessage(message_ptr messages);
  72. /// Process current control message
  73. /// @param messages current message
  74. /// @returns Modified message
  75. virtual message_ptr processOutgoingControlMessage(message_ptr messages);
  76. /// Process current binary message
  77. /// @param messages current message
  78. /// @returns Modified message and response
  79. virtual ChainedIncomingProduct processIncomingBinaryMessage(ChainedMessagesProduct messages);
  80. /// Process current binary message
  81. /// @param messages current message
  82. /// @param control current control message
  83. /// @returns Modified binary message and control message
  84. virtual ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages, std::optional<message_ptr> control);
  85. /// Set given element as upstream to this
  86. /// @param upstream Upstream element
  87. /// @returns Upstream element
  88. std::shared_ptr<MessageHandlerElement> chainWith(std::shared_ptr<MessageHandlerElement> upstream);
  89. /// Remove all downstream elements from chain
  90. void recursiveRemoveChain();
  91. };
  92. } // namespace rtc
  93. #endif // RTC_ENABLE_MEDIA
  94. #endif // RTC_MESSAGE_HANDLER_ELEMENT_H