DeferredPackets.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <list>
  21. #include "Constants.hpp"
  22. #include "SharedPtr.hpp"
  23. #include "Mutex.hpp"
  24. #include "DeferredPackets.hpp"
  25. #include "BinarySemaphore.hpp"
  26. /**
  27. * Maximum number of deferred packets
  28. */
  29. #define ZT_DEFFEREDPACKETS_MAX 256
  30. namespace ZeroTier {
  31. class IncomingPacket;
  32. class RuntimeEnvironment;
  33. /**
  34. * Deferred packets
  35. *
  36. * IncomingPacket can defer its decoding this way by enqueueing itself here.
  37. * When this is done, deferredDecode() is called later. This is done for
  38. * operations that may be expensive to allow them to potentially be handled
  39. * in the background or rate limited to maintain quality of service for more
  40. * routine operations.
  41. */
  42. class DeferredPackets
  43. {
  44. public:
  45. DeferredPackets(const RuntimeEnvironment *renv);
  46. ~DeferredPackets();
  47. /**
  48. * Enqueue a packet
  49. *
  50. * @param pkt Packet to process later (possibly in the background)
  51. * @return False if queue is full
  52. */
  53. bool enqueue(IncomingPacket *pkt);
  54. /**
  55. * Wait for and then process a deferred packet
  56. *
  57. * If we are shutting down (in destructor), this returns -1 and should
  58. * not be called again. Otherwise it returns the number of packets
  59. * processed.
  60. *
  61. * @return Number processed or -1 if shutting down
  62. */
  63. int process();
  64. private:
  65. std::list<IncomingPacket> _q;
  66. const RuntimeEnvironment *const RR;
  67. volatile int _waiting;
  68. volatile bool _die;
  69. Mutex _q_m;
  70. BinarySemaphore _q_s;
  71. };
  72. } // namespace ZeroTier
  73. #endif