SimNetSocketManager.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <vector>
  32. #include "../node/Constants.hpp"
  33. #include "../node/SocketManager.hpp"
  34. #include "../node/Mutex.hpp"
  35. #include "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. std::map< InetAddress,TransferStats >::const_iterator s(_stats.find(peer));
  69. if (s == _stats.end())
  70. return TransferStats();
  71. return s->second;
  72. }
  73. /**
  74. * @return Network to which this endpoint belongs
  75. */
  76. inline SimNet *net() const { return _sn; }
  77. /**
  78. * Enqueue data from another endpoint to be picked up on next poll()
  79. *
  80. * @param from Originating endpoint address
  81. * @param data Data
  82. * @param len Length of data in bytes
  83. */
  84. inline void enqueue(const InetAddress &from,const void *data,unsigned int len)
  85. {
  86. Mutex::Lock _l(_inbox_m);
  87. _inbox.push_back(std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> >(from,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN>(data,len)));
  88. _waitCond.signal();
  89. }
  90. virtual bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
  91. virtual void poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg);
  92. virtual void whack();
  93. virtual void closeTcpSockets();
  94. private:
  95. // These are set by SimNet after object creation
  96. SimNet *_sn;
  97. InetAddress _address;
  98. SharedPtr<Socket> _mySocket;
  99. TransferStats _totals;
  100. std::vector< std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> > > _inbox;
  101. Mutex _inbox_m;
  102. std::map< InetAddress,TransferStats > _stats;
  103. Mutex _stats_m;
  104. Condition _waitCond;
  105. };
  106. } // namespace ZeroTier
  107. #endif