DeferredPackets.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_DEFERREDPACKETS_HPP
  28. #define ZT_DEFERREDPACKETS_HPP
  29. #include "Constants.hpp"
  30. #include "SharedPtr.hpp"
  31. #include "Mutex.hpp"
  32. #include "DeferredPackets.hpp"
  33. #include "BinarySemaphore.hpp"
  34. /**
  35. * Maximum number of deferred packets
  36. */
  37. #define ZT_DEFFEREDPACKETS_MAX 1024
  38. namespace ZeroTier {
  39. class IncomingPacket;
  40. class RuntimeEnvironment;
  41. /**
  42. * Deferred packets
  43. *
  44. * IncomingPacket can defer its decoding this way by enqueueing itself here.
  45. * When this is done, deferredDecode() is called later. This is done for
  46. * operations that may be expensive to allow them to potentially be handled
  47. * in the background or rate limited to maintain quality of service for more
  48. * routine operations.
  49. */
  50. class DeferredPackets
  51. {
  52. public:
  53. DeferredPackets(const RuntimeEnvironment *renv);
  54. ~DeferredPackets();
  55. /**
  56. * Enqueue a packet
  57. *
  58. * Since packets enqueue themselves, they call it with 'this' and we wrap
  59. * them in a SharedPtr<>. This is safe as SharedPtr<> is introspective and
  60. * supports this. This should not be called from any other code outside
  61. * IncomingPacket.
  62. *
  63. * @param pkt Packet to process later (possibly in the background)
  64. * @return False if queue is full
  65. */
  66. bool enqueue(IncomingPacket *pkt);
  67. /**
  68. * Wait for and then process a deferred packet
  69. *
  70. * If we are shutting down (in destructor), this returns -1 and should
  71. * not be called again. Otherwise it returns the number of packets
  72. * processed.
  73. *
  74. * @return Number processed or -1 if shutting down
  75. */
  76. int process();
  77. private:
  78. SharedPtr<IncomingPacket> _q[ZT_DEFFEREDPACKETS_MAX];
  79. const RuntimeEnvironment *const RR;
  80. unsigned long _readPtr;
  81. unsigned long _writePtr;
  82. bool _die;
  83. Mutex _q_m;
  84. BinarySemaphore _q_s;
  85. };
  86. } // namespace ZeroTier
  87. #endif