DeferredPackets.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  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 General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_DEFERREDPACKETS_HPP
  19. #define ZT_DEFERREDPACKETS_HPP
  20. #include "Constants.hpp"
  21. #include "SharedPtr.hpp"
  22. #include "Mutex.hpp"
  23. #include "DeferredPackets.hpp"
  24. #include "BinarySemaphore.hpp"
  25. /**
  26. * Maximum number of deferred packets
  27. */
  28. #define ZT_DEFFEREDPACKETS_MAX 1024
  29. namespace ZeroTier {
  30. class IncomingPacket;
  31. class RuntimeEnvironment;
  32. /**
  33. * Deferred packets
  34. *
  35. * IncomingPacket can defer its decoding this way by enqueueing itself here.
  36. * When this is done, deferredDecode() is called later. This is done for
  37. * operations that may be expensive to allow them to potentially be handled
  38. * in the background or rate limited to maintain quality of service for more
  39. * routine operations.
  40. */
  41. class DeferredPackets
  42. {
  43. public:
  44. DeferredPackets(const RuntimeEnvironment *renv);
  45. ~DeferredPackets();
  46. /**
  47. * Enqueue a packet
  48. *
  49. * Since packets enqueue themselves, they call it with 'this' and we wrap
  50. * them in a SharedPtr<>. This is safe as SharedPtr<> is introspective and
  51. * supports this. This should not be called from any other code outside
  52. * IncomingPacket.
  53. *
  54. * @param pkt Packet to process later (possibly in the background)
  55. * @return False if queue is full
  56. */
  57. bool enqueue(IncomingPacket *pkt);
  58. /**
  59. * Wait for and then process a deferred packet
  60. *
  61. * If we are shutting down (in destructor), this returns -1 and should
  62. * not be called again. Otherwise it returns the number of packets
  63. * processed.
  64. *
  65. * @return Number processed or -1 if shutting down
  66. */
  67. int process();
  68. private:
  69. SharedPtr<IncomingPacket> _q[ZT_DEFFEREDPACKETS_MAX];
  70. const RuntimeEnvironment *const RR;
  71. unsigned long _readPtr;
  72. unsigned long _writePtr;
  73. volatile int _waiting;
  74. volatile bool _die;
  75. Mutex _q_m;
  76. BinarySemaphore _q_s;
  77. };
  78. } // namespace ZeroTier
  79. #endif