SimNetSocketManager.cpp 3.1 KB

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