SimNetSocketManager.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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_SIMNETSOCKETMANAGER_HPP
  28. #define ZT_SIMNETSOCKETMANAGER_HPP
  29. #include <map>
  30. #include <utility>
  31. #include <queue>
  32. #include "Constants.hpp"
  33. #include "../node/SocketManager.hpp"
  34. #include "../node/Mutex.hpp"
  35. #include "../node/Condition.hpp"
  36. namespace ZeroTier {
  37. class SimNet;
  38. /**
  39. * Socket manager for an IP endpoint in a simulated network
  40. */
  41. class SimNetSocketManager : public SocketManager
  42. {
  43. friend class SimNet;
  44. public:
  45. struct TransferStats
  46. {
  47. TransferStats() : received(0),sent(0) {}
  48. unsigned long long received;
  49. unsigned long long sent;
  50. };
  51. SimNetSocketManager();
  52. virtual ~SimNetSocketManager();
  53. /**
  54. * @return IP address of this simulated endpoint
  55. */
  56. inline const InetAddress &address() const { return _address; }
  57. /**
  58. * @return Local endpoint stats
  59. */
  60. inline const TransferStats &totals() const { return _totals; }
  61. /**
  62. * @param peer Peer IP address
  63. * @return Transfer stats for this peer
  64. */
  65. inline TransferStats stats(const InetAddress &peer) const
  66. {
  67. Mutex::Lock _l(_stats_m);
  68. return _stats[peer];
  69. }
  70. /**
  71. * @return Network to which this endpoint belongs
  72. */
  73. inline SimNet *net() const { return _sn; }
  74. /**
  75. * Enqueue data from another endpoint to be picked up on next poll()
  76. *
  77. * @param from Originating endpoint address
  78. * @param data Data
  79. * @param len Length of data in bytes
  80. */
  81. inline void enqueue(const InetAddress &from,const void *data,unsigned int len)
  82. {
  83. {
  84. Mutex::Lock _l(_inbox_m);
  85. _inbox.push(std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> >(from,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN>(data,len)));
  86. }
  87. _waitCond.signal();
  88. }
  89. virtual bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
  90. virtual void poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg);
  91. virtual void whack();
  92. virtual void closeTcpSockets();
  93. private:
  94. // These are set by SimNet after object creation
  95. SimNet *_sn;
  96. InetAddress _address;
  97. SharedPtr<Socket> _mySocket;
  98. TransferStats _totals;
  99. std::queue< std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> > > _inbox;
  100. Mutex _inbox_m;
  101. std::map< InetAddress,TransferStats > _stats;
  102. Mutex _stats_m;
  103. Condition _waitCond;
  104. };
  105. } // namespace ZeroTier
  106. #endif