2
0

SimNetSocketManager.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "SimNetSocketManager.hpp"
  28. #include "../node/Constants.hpp"
  29. #include "../node/Socket.hpp"
  30. namespace ZeroTier {
  31. class SimNetSocket : public Socket
  32. {
  33. public:
  34. SimNetSocket(SimNetSocketManager *sm) :
  35. Socket(ZT_SOCKET_TYPE_UDP_V4),
  36. _parent(sm) {}
  37. virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen)
  38. {
  39. SimNetSocketManager *dest = _parent->net()->get(to);
  40. if (dest)
  41. dest->enqueue(_parent->address(),msg,msglen);
  42. return true; // we emulate UDP, which has no delivery guarantee semantics
  43. }
  44. SimNetSocketManager *_parent;
  45. };
  46. SimNetSocketManager::SimNetSocketManager() :
  47. _sn((SimNet *)0), // initialized by SimNet
  48. _mySocket(new SimNetSocket(this))
  49. {
  50. }
  51. SimNetSocketManager::~SimNetSocketManager()
  52. {
  53. }
  54. bool SimNetSocketManager::send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen)
  55. {
  56. if (tcp)
  57. return false; // we emulate UDP
  58. SimNetSocketManager *dest = _sn->get(to);
  59. if (dest)
  60. dest->enqueue(_address,msg,msglen);
  61. return true; // we emulate UDP, which has no delivery guarantee semantics
  62. }
  63. void SimNetSocketManager::poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg)
  64. {
  65. {
  66. Mutex::Lock _l(_lock);
  67. while (!_queue.empty()) {
  68. handler(_mySocket,arg,_queue.front().first,_queue.front().second);
  69. _queue.pop();
  70. }
  71. }
  72. if (timeout)
  73. _waitCond.wait(timeout);
  74. else _waitCond.wait();
  75. {
  76. Mutex::Lock _l(_lock);
  77. while (!_queue.empty()) {
  78. handler(_mySocket,arg,_queue.front().first,_queue.front().second);
  79. _queue.pop();
  80. }
  81. }
  82. }
  83. void SimNetSocketManager::whack()
  84. {
  85. _waitCond.signal();
  86. }
  87. void SimNetSocketManager::closeTcpSockets()
  88. {
  89. }
  90. } // namespace ZeroTier