SocketManager.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_SOCKETMANAGER_HPP
  28. #define ZT_SOCKETMANAGER_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. #include <stdexcept>
  33. #include "Constants.hpp"
  34. #include "InetAddress.hpp"
  35. #include "Buffer.hpp"
  36. #include "NonCopyable.hpp"
  37. #include "Socket.hpp"
  38. namespace ZeroTier {
  39. /**
  40. * Socket I/O implementation
  41. */
  42. class SocketManager : NonCopyable
  43. {
  44. public:
  45. SocketManager(void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg) :
  46. _packetHandler(packetHandler),
  47. _arg(arg) {}
  48. virtual ~SocketManager() {}
  49. /**
  50. * Send a message to a remote peer
  51. *
  52. * @param to Destination address
  53. * @param tcp Use TCP?
  54. * @param autoConnectTcp If true, automatically initiate TCP connection if there is none
  55. * @param msg Message to send
  56. * @param msglen Length of message
  57. */
  58. virtual bool send(
  59. const InetAddress &to,
  60. bool tcp,
  61. bool autoConnectTcp,
  62. const void *msg,
  63. unsigned int msglen) = 0;
  64. /**
  65. * Send a message to a remote peer via UDP (shortcut for setting both TCP params to false in send)
  66. *
  67. * @param to Destination address
  68. * @param msg Message to send
  69. * @param msglen Length of message
  70. */
  71. inline bool sendUdp(
  72. const InetAddress &to,
  73. const void *msg,
  74. unsigned int msglen) { return send(to,false,false,msg,msglen); }
  75. /**
  76. * Perform I/O polling operation (e.g. select())
  77. *
  78. * If called concurrently, one will block until the other completes.
  79. *
  80. * @param timeout Timeout in milliseconds, may return sooner if whack() is called
  81. */
  82. virtual void poll(unsigned long timeout) = 0;
  83. /**
  84. * Cause current or next blocking poll() operation to timeout immediately
  85. */
  86. virtual void whack() = 0;
  87. /**
  88. * Close TCP sockets
  89. */
  90. virtual void closeTcpSockets() = 0;
  91. protected:
  92. void (*_packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &);
  93. void *_arg;
  94. };
  95. } // namespace ZeroTier
  96. #endif